GlobalTrigger.cc
1 #include <vector>
2 #include <array>
3 #include <iostream>
4 #include <fstream>
5 #include "vector.hh"
6 #include "position.hh"
7 #include "TF1.h"
8 #include "TCanvas.h"
9 #include "TGraph.h"
10 #include "TTree.h"
11 #include "TH2F.h"
12 #include "TMath.h"
13 #include "TVector3.h"
14 
15 #include "rx.hpp"
16 #include "Constants.h"
17 #include "anita.hh"
18 #include "balloon.hh"
19 #include <cmath>
20 #include "Tools.h"
21 #include "Settings.h"
22 #include "screen.hh"
23 #include "ChanTrigger.h"
24 #include "GlobalTrigger.h"
25 #include "icemc_random.h"
26 
27 using std::cout;
28 
29 
30 GlobalTrigger::GlobalTrigger(Settings *settings1,Anita *anita1){
31 
32  // 2=top,1=middle,0=bottom
33  WHICHLAYERSLCPRCP[0]=0;
34  WHICHLAYERSLCPRCP[1]=1;
35  WHICHLAYERSLCPRCP[2]=0;
36 
37 
38  // time step between sampling tunnel diode output for the trigger
39  TRIGTIMESTEP=2.E-9;
40 
41 
42  L3_COINCIDENCE=22.5e-9;
43 
44  // L1 coincidence window, in seconds
45  L1_COINCIDENCE_ANITA3[0]=16.E-9; // B->M or T
46  L1_COINCIDENCE_ANITA3[1]=12.E-9; // M->B or T
47  L1_COINCIDENCE_ANITA3[2]=4.E-9; // T->B or M
48 
49  // in this scenario B->M is the same as M->B for example
50  // this needs to be generalized- using this same thing for all scenarios which isn't right
51  LASTTIMETOTESTL1_ANITA3=((double)anita1->NFOUR/2)*anita1->TIMESTEP-Tools::dMax(L1_COINCIDENCE_ANITA3,3); // can't test L1 after this point because the l1_coincidence windows go past the end of the waveform.
52 
53 
54 
55 
56  // layers indexed in reverse order!
57  // B->M
58  // B->T
59  L1_COINCIDENCE_LR_SCA[0]=16.E-9;
60  L1_COINCIDENCE_LR_SCA[1]=16.E-9;
61 
62  LASTTIMETOTESTL1_ANITA4LR_SCA=Tools::dMax(L1_COINCIDENCE_LR_SCA,2);
63  LASTTIMETOTESTL1_ANITA4LR_SCA=((double)anita1->NFOUR/2)*anita1->TIMESTEP-LASTTIMETOTESTL1_ANITA4LR_SCA; // can't test L1 after this point because the l1_coincidence windows go past the end of the waveform.
64 
65  L1_COINCIDENCE_MOREGENERAL[0][0]=16.E-9;
66  L1_COINCIDENCE_MOREGENERAL[0][1]=4.E-9;
67 
68  L1_COINCIDENCE_MOREGENERAL[1][0]=16.E-9;
69  L1_COINCIDENCE_MOREGENERAL[1][1]=4.E-9;
70 
71  L1_COINCIDENCE_MOREGENERAL[2][0]=4.E-9; // L1 coincidence window, in seconds ;
72  L1_COINCIDENCE_MOREGENERAL[2][1]=4.E-9; // L1 coincidence window, in seconds ;
73 
74  LASTTIMETOTESTL1_ANITA4=0.;
75  for (int i=0;i<3;i++) {
76  for (int j=0;j<2;j++) {
77  if (L1_COINCIDENCE_MOREGENERAL[i][j]>LASTTIMETOTESTL1_ANITA4)
78  LASTTIMETOTESTL1_ANITA4=L1_COINCIDENCE_MOREGENERAL[i][j];
79  }
80  }
81 
82  LASTTIMETOTESTL1_ANITA4=((double)anita1->NFOUR/2)*anita1->TIMESTEP-LASTTIMETOTESTL1_ANITA4;
83  // can't test L1 after this point because the l1_coincidence windows go past the end of the waveform.
84 
85  nstepback=(int)(2.E-9/TRIGTIMESTEP);
86 
87  // coincidence between lcp,rcp within an antenna
88  L1_COINCIDENCE_ANITA4LR_SCB=4.e-9;
89  // B->T, B->M, M->T
90  L2_COINCIDENCE_ANITA4LR_SCB[0]=12.e-9;
91  L2_COINCIDENCE_ANITA4LR_SCB[1]=4.e-9;
92  L2_COINCIDENCE_ANITA4LR_SCB[2]=8.e-9;
93  LASTTIMETOTESTL1_ANITA4LR_SCB=((double)anita1->NFOUR/2)*anita1->TIMESTEP-L1_COINCIDENCE_ANITA4LR_SCB; // can't test L1 after this point because the l1_coincidence windows go past the end of the waveform.;
94  LASTTIMETOTESTL2_ANITA4LR_SCB=((double)anita1->NFOUR/2)*anita1->TIMESTEP-Tools::dMax(L2_COINCIDENCE_ANITA4LR_SCB,3); // can't test L1 after this point because the l1_coincidence windows go past the end of the waveform.;
95  L3_COINCIDENCE_ANITA4LR_SCB=12.e-9;
96 
97  DELAYS[0]=0.; // delay top
98  DELAYS[1]=4.e-9; // delay middle
99  DELAYS[2]=4.e-9; // delay bottom
100 
101  Tools::Zero(triggerbits,Anita::NTRIG);
102 
103  phiTrigMask[0]=anita1->phiTrigMask; // set the phi mask to the input value which comes from the balloon class
104  phiTrigMask[1]=anita1->phiTrigMaskH; // set the phi mask to the input value which comes from the balloon class
105  l1TrigMask[0]=anita1->l1TrigMask; // set the phi mask to the input value which comes from the balloon class
106  l1TrigMask[1]=anita1->l1TrigMaskH; // set the phi mask to the input value which comes from the balloon class
107 
108 
109 
110 
111  for (int i=0;i<Anita::NLAYERS_MAX;i++) {
112  for (int j=0;j<Anita::NPHI_MAX;j++) {
113  for (int k=0;k<2;k++) {
114  for (int p=0;p<anita1->NBANDS+1;p++) {
115  channels_passing[i][j][k][p]=0;
116  channels_passing_justNoise[i][j][k][p]=0;
117  // make vchannels_passing the proper length.
118  vchannels_passing[i][j][k].push_back(0);
119  }
120 
121 
122  }
123  }
124  }
125 
126  for (int i=0;i<3;i++) {
127  for (int j=0;j<16;j++) {
128  for (int k=0;k<2;k++) {
129  for (int p=0;p<5;p++) {
130  arrayofhits[i][j][k][p].clear();
131  arrayofhitsnoise[i][j][k][p].clear();
132  }
133  }
134  }
135  }
136 
137  for (int k=0;k<2;k++) {
138  for (int i=0;i<Anita::NLAYERS_MAX;i++) {
139  for (int j=0;j<Anita::NPHI_MAX;j++) {
140  volts[k][i][j]=0.;
141  volts_em[k][i][j]=0.;
142  volts_original[k][i][j]=0.;
143  }
144  }
145  }
146 
147 
148  //Zeroing
149  for (int i=0;i<settings1->NANTENNAS;i++) {
150  nchannels_perrx_triggered[i] = 0;
151  for (int j=0;j<8;j++) {
152  nchannels_perband_triggered[i][j]=0;
153  }
154  } //Zero the trigger array
155 
156 
157 
158 
159 }
160 
161 
162 
164 
211 void GlobalTrigger::PassesTrigger(Settings *settings1,Anita *anita1,int discones_passing,int mode,int *l3trig,int l2trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int l1trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int antennaclump,int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX],int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX],int inu, int *thispasses, bool noiseOnly) {
212 
213  double this_threshold= anita1->powerthreshold[4];
214  return PassesTrigger(settings1,anita1,discones_passing,mode,l3trig,l2trig,l1trig,antennaclump,loctrig,loctrig_nadironly,inu,this_threshold, thispasses, noiseOnly);
215 }
216 
217 
218 
219 
220 void GlobalTrigger::PassesTrigger(Settings *settings1,Anita *anita1,int discones_passing,int mode,int *l3trig,int l2trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int l1trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int antennaclump,int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX],int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX],int inu,double this_threshold, int *thispasses, bool noiseOnly) {
221 
222 
223  //bool ishpol should only be used for anita3, by default do only vpol
224 
225  if (settings1->TRIGGERSCHEME < 3) {
226 
227  // Basic trigger refers to frequency domain voltage (0) frequency domain energy (1) timedomain diode integration (2)
228 
229  PassesTriggerBasic(settings1, anita1, discones_passing, mode, l3trig, l2trig, l1trig, antennaclump, loctrig, loctrig_nadironly, thispasses, inu, noiseOnly);
230 
231  }
232 
233  else if (settings1->TRIGGERSCHEME == 3) {
234 
235  PassesTriggerCoherentSum(settings1, anita1, inu, thispasses);
236 
237  }
238  else if (settings1->TRIGGERSCHEME == 4) {
239 
240  PassesTriggerSummedPower(settings1, anita1);
241 
242  }
243  else if (settings1->TRIGGERSCHEME == 5) {
244 
245  // Don't know the name of this one so I'll call it 5
246  PassesTriggerScheme5(anita1, this_threshold, thispasses);
247 
248  }
249 
250 
251 }//PassesTrigger
252 
253 
254 
255 void GlobalTrigger::PassesTriggerBasic(Settings *settings1,Anita *anita1,int discones_passing,int mode,int *l3trig,int l2trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int l1trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX],int antennaclump,int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX],int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX], int *thispasses, int inu, bool noiseOnly){
256 
257  int ltsum=0;
258  int channsum=0;
259  int ihit=0;
260  // int thispasses[2]={0,0};
261  int required_bands_failed[2]={0,0}; // keep track of whether bands that were required to pass did not, for each polarization
262 
263  if (noiseOnly){
264  arrayofhits = arrayofhitsnoise;
265  }else{
266  arrayofhits = arrayofhitsall;
267  }
268 
269 
270  // this is an array with 1=pass and 0=fail for each channel
271  // the first two layers on the payload are "compacted"
272  // into one trigger layer of 16 antennas.
273  // layer number, antenna, polarization, bandwidth slice
274  int channels_compacted_passing[Anita::NLAYERS_MAX][Anita::NPHI_MAX][2][5] = {{{{0}}}};
275 
276  for (int k=0;k<Anita::NPOL;k++) {
277  l3trig[k]=0;
278  Tools::Zero(l1trig[k],Anita::NTRIGGERLAYERS_MAX);
279  Tools::Zero(l2trig[k],Anita::NTRIGGERLAYERS_MAX); // for all three layers
280  Tools::Zero(loctrig_nadironly[k],Anita::NPHI_MAX);
281  }
282  // which clumps of 3 antennas
283  //within the nadir layer pass
284  // 0th and 1st element= antenna at phi=0,
285  // 2nd and 3rd element=next antenna, etc.
286 
287  for (int ilayer=0;ilayer<settings1->NLAYERS;ilayer++) {
288  for (int k=0;k<Anita::NPOL;k++) {
289  // for (int j=0;j<Anita::NPHI_MAX;j++) {
290  Tools::Zero(loctrig[k][ilayer],Anita::NPHI_MAX);
291  }
292  // which clumps of 3 antennas
293  //within each trigger layer pass L2 trigger
294  // layer, phi location
295  // } //for
296  } // end of initializing to zero
297 
298  int NBAND=5; // number of bandwidth slices
299  int whichlayer=0;
300  int whichphisector=0;
301  for (int ilayer=0;ilayer<settings1->NLAYERS;ilayer++) {
302  for (int iphi=0;iphi<anita1->NRX_PHI[ilayer];iphi++) {
303  for (int ipolar=0;ipolar<2;ipolar++) {
304 
305  if (anita1->pol_allowed[ipolar]==0) continue;// if this polarization is not allowed to contribute then don't do it
306  // if (ishpol && ipolar==0) continue; // Anita3 : only do the polarisation required
307  //else if ((!ishpol) && ipolar==1) continue; // Anita3 : only do the polarisation required
308 
309  for (int iband=0;iband<NBAND;iband++) {
310  if (!anita1->bwslice_allowed[iband]) continue;
311 
312  GetAnitaLayerPhiSector(settings1,ilayer,iphi,whichlayer,whichphisector); // Translate Anita physical layer to Anita trigger layer and phi sector (4 layers with 8,8,16,8 phi sector to 3 layers with 16 phi sectors each. In the nadir layer, 8 of the 16 slots are empty on the trigger layer.)
313 
314 
315 
316  // combining top two layers on the payload into one trigger layer
317  // this means the 0th antenna in the first trigger layer is
318  // physically higher than the 1st antenna in the first trigger layer
319  // which means that the nadirs are aligned with the antennas with indices 1,3,5 etc.
320  // we will still use indices 0-7 for them though
321  if (noiseOnly) channels_compacted_passing[whichlayer][whichphisector][ipolar][iband]+=channels_passing_justNoise[ilayer][iphi][ipolar][iband];
322  else channels_compacted_passing[whichlayer][whichphisector][ipolar][iband]+=channels_passing[ilayer][iphi][ipolar][iband];
323  // if (channels_compacted_passing[whichlayer][whichphisector][ipolar][iband]>0 && ipolar==0) cout << whichlayer << " " << whichphisector << endl;
324  } //for
325  } //for
326  } //for
327  } //for
328 
329  int antsum[2]={0,0}; // counter for how many channels of each polarization on an antenna pass
330  /* antenna triggers */
331 
332  int Nreq_l2[Anita::NLAYERS_MAX];
333  // number of antennas within each clump
334  //that need to pass for that clump to pass L2
335  // 1st layer, 2nd layer,
336  //nadir layer (to be "OR"ed with upper layers),
337  // and nadir layer (for nadir-only) trigger
338  //int Ntrig_chann[4]={6,6,6,8}; // NOT USED: number of channels
339  //per clump that need to pass.
340  for (int i=0;i<Anita::NLAYERS_MAX;i++) {
341  Nreq_l2[i]=anita1->trigRequirements[1];
342  }
343 
344 
345  int ant[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX] = {{{0}}}; // which antennas and which polarizations pass at L1
346  // trigger layer, phi position
347 
348  int antpass[2]={0,0}; // count how many antennas pass
349  int iphitrig=0; // which trigger phi sector
350  int ihittrig=0;
351  // local level 1 trigger at the antenna
352  for(int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++) { // loop over layers
353  // this is nlayers-1 because NLAYERS counts top 16 antennas as 2 layers
354 
355  for(int iphi=0;iphi<anita1->PHITRIG[iloc];iphi++) { // loop over phi position
356  iphitrig=GetPhiSector(settings1,iloc,iphi); // get trigger phi sector
357  // counts from 0
358 
359  for(int ipolar=0;ipolar<2;ipolar++) {
360 
361  antsum[ipolar] = 0; // start sum for this antenna for each polarization
362 
363  for(int iband=0;iband<NBAND;iband++) {
364  if(channels_compacted_passing[iloc][iphitrig][ipolar][iband] == 1
365  && anita1->bwslice_allowed[iband]==1 && anita1->pol_allowed[ipolar]==1) { // only increment if it's one of the allowed bands and allowed polarizations.
366 
367  if (settings1->PHIMASKING && settings1->WHICH==9){ // only applying channel masking like this if it's Anita-3
368  // if ((ipolar==0 && (1<<iphitrig & l1TrigMask[0])) || (ipolar==1 && (1<<iphitrig & l1TrigMask[1])) ){
369  if (1<<iphitrig & l1TrigMask[ipolar]) {
370  continue; // was this channel masked?
371  }
372  }
373  antsum[ipolar] = antsum[ipolar] +1; // sum channels that pass for this antenna, polarization
374  }
375  } // loop over bands
376  } // end loop over polarizations
377 
378 
379  for (int ipolar=0;ipolar<2;ipolar++) {
380  required_bands_failed[ipolar]=0;
381  for (int iband=0;iband<NBAND;iband++) { // notice sum over 5 bands now
382  if(channels_compacted_passing[iloc][iphitrig][ipolar][iband] == 0
383  && anita1->bwslice_required[iband]==1 ) { // if this band was required to pass and it didn't,
384  required_bands_failed[ipolar] = 1; // fatal for this antenna, polarization
385  }
386 
387  } // end loop over bands
388  } // end loop over polarizations
389 
390  // if the required bands didn't pass then set antsum=0 so the antenna doesn't pass
391 
392 
393  for (int ipolar=0;ipolar<2;ipolar++) {
394  ant[ipolar][iloc][iphitrig] = 1; // start with every antenna passing L1.
395  if( (anita1->pol_required[ipolar]==1 &&
396  (antsum[ipolar] < anita1->trigRequirements[0]
397  || required_bands_failed[ipolar]==1 ) )
398  || (anita1->pol_required[ipolar]==0)
399  ) { // if this polarization is required and it doesn't pass then make the antenna fail
400  ant[ipolar][iloc][iphitrig]=0;
401  } //if
402  else{
403  antpass[ipolar]+=1;// increment if it passed
404  }
405  }
406 
407  } //for (phi position)
408  } //for (layers)
409 
410  if (settings1->DISCONES==2) {
411  // fill the slots in between the actual antennas with the "or"
412  // of the neighboring antennas
413  FillInNadir(anita1,ant[0][2]);
414  FillInNadir(anita1,ant[1][2]);
415  }
416 
417  for(int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++){ //This is NLAYERS-1 because NLAYERS counts top 16 antennas as 2 layers
418  for (int iphi=0;iphi<anita1->PHITRIG[iloc];iphi++){
419  iphitrig=GetPhiSector(settings1,iloc,iphi);
420  for (int ipolar=0;ipolar<2;ipolar++) {
421  if (ant[ipolar][iloc][iphitrig]==1)
422  l1trig[ipolar][iloc] += (1<<iphitrig); // this keeps track of which antennas pass the l1 trigger
423  }
424  }//for (phi position)
425  } //for (layers) // Hmm this is not used
426 
427  if (mode == 1) {// TRIGTYPE=2 -> just require ANTtrig channels pass on 2 antennas
428  for (int ipolar=0;ipolar<Anita::NPOL;ipolar++) {
429  if (antpass[ipolar] >= 2)
430  thispasses[ipolar] = 1;
431  }
432 
433  } else if (mode == 2) { // TRIGTYPE=1 ->
434 
435  // ANITA-3
436  if (settings1->WHICH==9) {
437  // for each of these, need to set
438  // loctrig, l2trig
439 
440  std::array<std::array<std::vector<int>,16>,2> vl1trig;
441 
442  L1Anita3_AllPhiSectors(anita1,vl1trig);
443  // just have to modify this function so it's looping over all relevant trigtimesteps
444 
445  for (int ipol=0;ipol<2;ipol++) {
446  for (int iphi=0;iphi<16;iphi++) {
447 
448  for (unsigned int ibin=0;ibin<vl1trig[ipol][iphi].size();ibin++) {
449  anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=vl1trig[ipol][iphi][ibin];
450  // if (anita1->l1trig_anita3_inanita[ipol][iphi][ibin])
451  // cout << "This l1 is 1.\n";
452  }
453  for (int ibin=vl1trig[ipol][iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
454  anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=0;
455  }
456  }
457  }
458  // cout << "vl1trig size is " << vl1trig[0][0].size() << "\n";
459 
460  std::array<std::array<std::vector<int>,16>,2> vl2trig;
461 
462  L2Anita3and4(anita1,vl1trig,
463  vl2trig);
464 
465  if (settings1->PHIMASKING){ // only applying channel masking like this if it's Anita-3
466  for (int ipol=0;ipol<2;ipol++) {
467  for (int iphi=0;iphi<16;iphi++) {
468  if ((1<<iphi & l1TrigMask[ipol])||(1<<iphi & phiTrigMask[ipol])) {
469  // set phi sector to 0
470  std::fill(vl2trig[ipol][iphi].begin(), vl2trig[ipol][iphi].end(), 0);
471  }
472  }
473  }
474  }
475 
476  int vl3trig[2][16];
477  L3Anita3and4(anita1,vl2trig,
478  vl3trig,thispasses);
479 
480  for (int ipol=0;ipol<2;ipol++) {
481  for (int iphi=0;iphi<16;iphi++) {
482  if (vl3trig[ipol][iphi]>0) l3trig[ipol]+=(1<<iphi);
483  }
484  }
485 
486  }
487  // ANITA-4
488  else if (settings1->WHICH==10 && !(settings1->LCPRCP)) {
489 
490  std::array<std::array<std::vector<int>,16>,2> vl1trig;
491 
492  L1Anita4_AllPhiSectors(anita1,vl1trig);
493  // just have to modify this function so it's looping over all relevant trigtimesteps
494 
495  for (int ipol=0;ipol<2;ipol++) {
496  for (int iphi=0;iphi<16;iphi++) {
497 
498  for (unsigned int ibin=0;ibin<vl1trig[ipol][iphi].size();ibin++) {
499  anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=vl1trig[ipol][iphi][ibin];
500 
501  // if (anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin])
502  // cout << "This l1 is " << ipol << "\t" << iphi << "\t" << ibin << "\t" << anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin] << "\n";
503  }
504  for (unsigned int ibin=vl1trig[ipol][iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
505  anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=0;
506  }
507  }
508  }
509  // cout << "vl1trig size is " << vl1trig[0][0].size() << "\n";
510 
511  std::array<std::array<std::vector<int>,16>,2> vl2trig;
512 
513  L2Anita3and4(anita1,vl1trig,
514  vl2trig);
515 
516  int vl3trig[2][16];
517  L3Anita3and4(anita1,vl2trig,
518  vl3trig,thispasses);
519 
520  for (int ipol=0;ipol<2;ipol++) {
521  for (int iphi=0;iphi<16;iphi++) {
522  if (vl3trig[ipol][iphi]>0) l3trig[ipol]+=(1<<iphi);
523  }
524  }
525 
526  }
527  else if (settings1->WHICH==10 && settings1->LCPRCP) {
528 
529 
530  // std::array<std::array<std::vector<int>,16>,2> vl1trig;
531  // //cout << "calling allphisectors.\n";
532  // L1Anita4LR_ScA_AllPhiSectors(anita1,vl1trig);
533 
534  // for (int ipol=0;ipol<2;ipol++) {
535  // for (int iphi=0;iphi<16;iphi++) {
536 
537  // for (int ibin=0;ibin<vl1trig[ipol][iphi].size();ibin++) {
538  // anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=vl1trig[ipol][iphi][ibin];
539 
540  // //if (anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin])
541  // //cout << "This l1 is " << ipol << "\t" << iphi << "\t" << ibin << "\t" << anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin] << "\n";
542  // }
543  // for (int ibin=vl1trig[ipol][iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
544  // anita1->l1trig_anita3and4_inanita[ipol][iphi][ibin]=0;
545  // }
546  // }
547  // }
548  // // cout << "vl1trig size is " << vl1trig[0][0].size() << "\n";
549 
550  // std::array<std::array<std::vector<int>,16>,2> vl2trig;
551 
552  // L2Anita3and4(anita1,vl1trig,
553  // vl2trig);
554  // L3Anita4LR_ScA(anita1,vl2trig,
555  // thispasses);
556 
557  //cout << "about to do delays.\n";
558  delay_AllAntennas(anita1);
559 
560  // cout << "did delays.\n";
561  // l1 triggers for all antennas
562  std::array< std::array< vector<int>,16>,3> vl1trig_anita4lr_scb;
563 
564  // keep track of whether you get a coincidence between 1, 2 or 3 antennas in a phi sector with the right windows.
565  // for each phi sector, whether there is a 1, 2 or 3 coincidence
566  std::array<std::array<vector<int>,3>,16> vl2_realtime_anita4_scb;
567  // cout << "did L2.\n";
568 
569  std::array<vector<int>,16> vl3trig_type0;
570  std::array<vector<int>,16> vl3trig_type1;
571  int thispasses_l3type0=0;
572  int thispasses_l3type1=0;
573 
574 
575  double time_thisbin=(double)nstepback*TRIGTIMESTEP;
576  int itrigbin=nstepback;
577  //cout << "about to loop.\n";
578  //cout << "size of arrayofhits is " << arrayofhits[0][0][0].size() << "\n";
579  // i should really do this in a different step so this function has fewer inputs.
580  while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
581  //cout << "vector size is " << vl1trig_anita4lr_scb[0][0].size() << "\n";
582  int npassesl1=0;
583  L1Anita4LR_ScB_AllAntennas_OneBin(itrigbin,anita1,vl1trig_anita4lr_scb,npassesl1);
584 
585  // if (npassesl1)
586  //cout << "npassesl1 is " << npassesl1 << "\n";
587 
588  itrigbin++;
589  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
590  } // loop over time steps
591 
592  // cout << "got out of first loop.\n";
593  time_thisbin=(double)nstepback*TRIGTIMESTEP;
594  itrigbin=nstepback;
595 
596  while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
597 
598  int npassesl2=0;
599  int npassesl2_type0=0;
600  L2Anita4LR_ScB_AllPhiSectors_OneBin(itrigbin,anita1,vl1trig_anita4lr_scb,
601  vl2_realtime_anita4_scb,npassesl2,npassesl2_type0);
602 
603 
604  // if (npassesl2)
605  // cout << "npassesl2 is " << npassesl2 << "\n";
606  // if (npassesl2_type0)
607  // cout << "npassesl2_type0 is " << npassesl2_type0 << "\n";
608 
609  itrigbin++;
610  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
611  } // loop over time steps
612 
613 
614  time_thisbin=(double)nstepback*TRIGTIMESTEP;
615  itrigbin=nstepback;
616 
617 
618  while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
619 
620 
621  L3Anita4LR_ScB_OneBin(itrigbin,anita1,vl2_realtime_anita4_scb,
622  vl3trig_type0, vl3trig_type1,
623  thispasses_l3type0,thispasses_l3type1);
624 
625  // cout << "did L3.\n";
626  itrigbin++;
627  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
628  } // loop over time steps
629 
630 
631  // if (thispasses_l3type0)
632  // thispasses[0]=1;
633  if (thispasses_l3type1)
634  thispasses[1]=thispasses[0]=1;
635 
636  // if (thispasses[0] || thispasses[1])
637  //cout << "This passes! " << thispasses[0] << "\t" << thispasses[1] << "\n";
638 
639 
640  for (int ilayer=0;ilayer<anita1->NTRIGGERLAYERS;ilayer++) {
641  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
642  for (int ipolar=0;ipolar<anita1->NPOL;ipolar++) {
643  Tools::reverseTimeOrdering(anita1->HALFNFOUR,anita1->arrayofhits_inanita[ilayer][iphi][ipolar],anita1->arrayofhits_forgaryanderic[ilayer][iphi][ipolar]);
644 
645  }
646  }
647  }
648 
649 
650  for (int ilayer=0;ilayer<anita1->NTRIGGERLAYERS;ilayer++) {
651  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
652  for (unsigned int ibin=0;ibin<vl1trig_anita4lr_scb[ilayer][iphi].size();ibin++) {
653  anita1->l1trig_anita4lr_inanita[ilayer][iphi][ibin]=vl1trig_anita4lr_scb[ilayer][iphi][ibin];
654  }
655  for (unsigned int ibin=vl1trig_anita4lr_scb[ilayer][iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
656  anita1->l1trig_anita4lr_inanita[ilayer][iphi][ibin]=0;
657  }
658 
659  Tools::reverseTimeOrdering(anita1->HALFNFOUR,anita1->l1trig_anita4lr_inanita[ilayer][iphi],anita1->l1trig_anita4lr_forgaryanderic[ilayer][iphi]);
660 
661  }
662  }
663 
664 
665 
666  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
667  for (unsigned int ibin=0;ibin<vl2_realtime_anita4_scb[iphi][1].size();ibin++) {
668  anita1->l2trig_anita4lr_inanita[iphi][1][ibin]=vl2_realtime_anita4_scb[iphi][1][ibin];
669 
670  }
671  for (unsigned int ibin=vl2_realtime_anita4_scb[iphi][1].size();ibin<anita1->HALFNFOUR;ibin++) {
672  anita1->l2trig_anita4lr_inanita[iphi][1][ibin]=0;
673  }
674  Tools::reverseTimeOrdering(anita1->HALFNFOUR,anita1->l2trig_anita4lr_inanita[iphi][1],anita1->l2trig_anita4lr_forgaryanderic[iphi]);
675 
676  for (unsigned int ibin=0;ibin<vl3trig_type0[iphi].size();ibin++) {
677  anita1->l3type0trig_anita4lr_inanita[iphi][ibin]=vl3trig_type0[iphi][ibin];
678  }
679  for (unsigned int ibin=vl3trig_type0[iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
680  anita1->l3type0trig_anita4lr_inanita[iphi][ibin]=0;
681  }
682  Tools::reverseTimeOrdering(anita1->HALFNFOUR,anita1->l3type0trig_anita4lr_inanita[iphi],anita1->l3type0trig_anita4lr_forgaryanderic[iphi]);
683  for (unsigned int ibin=0;ibin<vl3trig_type1[iphi].size();ibin++) {
684 
685  anita1->l3trig_anita4lr_inanita[iphi][ibin]=vl3trig_type1[iphi][ibin];
686  }
687  for (unsigned int ibin=vl3trig_type1[iphi].size();ibin<anita1->HALFNFOUR;ibin++) {
688  anita1->l3trig_anita4lr_inanita[iphi][ibin]=0;
689  }
690  Tools::reverseTimeOrdering(anita1->HALFNFOUR,anita1->l3trig_anita4lr_inanita[iphi],anita1->l3type1trig_anita4lr_forgaryanderic[iphi]);
691  }
692 
693 
694  for (int ibin=0;ibin<anita1->HALFNFOUR;ibin++) {
695  anita1->time_trig[ibin]=TRIGTIMESTEP*(double)ibin;
696  }
697 
698  // LC: not sure about this
699  for (int iphi=0;iphi<16;iphi++) {
700  for (unsigned int ibin=0; ibin<anita1->HALFNFOUR; ibin++){
701  if (anita1->l3trig_anita4lr_inanita[iphi][ibin]>0){
702  l3trig[0]+=(1<<iphi);
703  ibin = anita1->HALFNFOUR;
704  }
705  }
706  }
707 
708 
709 
710 
711 
712 
713 
714 
715 
716  }
717  else {
718 
719 
720  // this is the one we're using for Anita 2
721 
722  // int iloc=0; // top array
723  //int inad=0; // phi position for nadir antennas,
724  //only goes to 8, wheras iphi goes to 16
725  int x;//for number of antenna in clump
726  int y;//for number of antenna in clump
727  x = (antennaclump-1)/2;//setting boundries for clump dependent on clump size(antennaclump)
728  y = (antennaclump-1)/2 +1;
729 
730  for (int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++) {
731  for (int ipolar=0;ipolar<Anita::NPOL;ipolar++) {
732  for(int iphi=0;iphi<16;iphi++) {
733  iphitrig=iphi;
734 
735 
736  ltsum = 0; // zero counter for number of antennas that pass per clump
737  channsum=0; // zero counter for number of channels that pass per clump
738 
739  // loop over clump -- with boundary check -- ugly implement
740  // even uglier - we want to require 2/3 antennas per clump
741  // but the middle antenna has to be one of the two
742  // so we multiply by ant[iloc][iphi] so that when the middle one
743  // passes we are multiplying by 1 and when it doesn't we are
744  // multiplying by zero.
745  for(int inbr=iphi-x;inbr<iphi+y;inbr++) {
746  ihit = (inbr + 16) % 16; // make any negative indices positive
747  ihittrig=ihit;
748 
749  if (anita1->REQUIRE_CENTRE) {
750  ltsum+=ant[ipolar][iloc][ihittrig]*ant[ipolar][iloc][iphitrig]; // increment if this antenna passes
751  }
752  // we multipy by ant[iloc][iphi] because this ensures that
753  // one of the triggered antennas is the center one in the clump
754  else
755  ltsum+=ant[ipolar][iloc][ihittrig];
756 
757  for (int ipolar=0;ipolar<2;ipolar++) {
758  for (int iband=0;iband<4;iband++) {
759  channsum+=channels_compacted_passing[iloc][ihittrig][ipolar][iband]; // increment if this channel passes
760  } //for
761  } //for
762  } //for
763 
764  if(ltsum >= Nreq_l2[iloc]){ // if enough antennas in this clump pass
765  loctrig[ipolar][iloc][iphitrig] = 1;
766  l2trig[ipolar][iloc] += (1<<iphi);
767  }//if
768  } //for (phi position)
769 
770  } // end loop over polarizations
771  } // end loop over trigger layers
772 
773  // fill the slots in between the actual antennas with the "or"
774  // of the neighboring antennas
775  if (settings1->DISCONES==2) {
776  FillInNadir(settings1,anita1,l2trig[0][2]);
777  FillInNadir(settings1,anita1,l2trig[1][2]);
778  }
779  // only difference between these is whether they implement masking in both polarizations
780  // can we simplify it
781  if (settings1->PHIMASKING && settings1->WHICH!=9) { // not for Anita3
782  // nadir antennas are aligned with the second physical layer of antennas
783  for (int iphi=0;iphi<anita1->NTRIGPHISECTORS;iphi++) { // loop over phi sectors
784  if ((1<<iphi) & phiTrigMask[0]) { // if this phi sector is masked
785  for (int ipolar=0;ipolar<Anita::NPOL;ipolar++) {
786  for (int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++) {
787  loctrig[ipolar][iloc][iphi] = 0;
788  if(l2trig[ipolar][iloc] & (1<<iphi)) l2trig[ipolar][iloc] -= (1<<iphi);
789  }
790  } // end loop over polarizations
791  } // if this phi sector is masked
792  }
793  } else if (settings1->PHIMASKING && settings1->WHICH==9) { // only for Anita3
794  // nadir antennas are aligned with the second physical layer of antennas
795  for (int iphi=0;iphi<anita1->NTRIGPHISECTORS;iphi++) { // loop over phi sectors
796 
797  for (int ipolar=0;ipolar<Anita::NPOL;ipolar++) {
798  if ( (1<<iphi) & phiTrigMask[ipolar] ) { // if this phi sector is masked
799  for (int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++) {
800  loctrig[ipolar][iloc][iphi] = 0;
801  if(l2trig[ipolar][iloc] & (1<<iphi)) l2trig[ipolar][iloc] -= (1<<iphi);
802  }
803  }
804  }// if this phi sector is masked
805  }
806  }
807  }
808 
809  if (anita1->trigRequirements[2]==0) {
810  for (int iloc=0;iloc<anita1->NTRIGGERLAYERS;iloc++) {
811  for (int ipolar=0;ipolar<Anita::NPOL;ipolar++) {
812  for (int iphi=0;iphi<anita1->NTRIGPHISECTORS;iphi++) {
813  if (l2trig[ipolar][iloc] & (1<<iphi)) { // if we are not making a L3 trigger requirement and there was a l2 trigger then call it a pass
814  thispasses[ipolar]=1;
815  //cout << "This one passes. inu is " << inu << " " << "iloc is " << iloc << "\n";
816  }
817  }
818  } // end loop over polarizations
819  }
820  }
821 
822 
823  L3Trigger(settings1,anita1,loctrig,loctrig_nadironly,discones_passing,l3trig,
824  thispasses);
825 
826  } //else if (mode 2)
827 } // end of PassesTriggerBasic
828 
829 
830 
831 
832 // This is the case for (settings1->TRIGGERSCHEME == 3), that coherent waveform sum is being used to trigger, based on Andres' idea. This is specific to ANITA III's antenna geometry.
833 
834 // Method:
835 // All bands have passed so far, we now perform the following for the first-hit phi sector and its neighbors:
836 // - Convert the relevent waveforms into a useful format
837 // - Calculate all arrival time delays relevent to the 3 phi sectors (9 antennas)
838 // f- For each of the direction hypotheses, do:
839 // - Shift the waveforms according to their delay offsets
840 // - Sum the 9 waveforms, resulting in one coherently-summed waveform
841 // - Square each element of the summed waveform, resulting in the power of the coherently-summed waveform
842 // - For each of the 16 sample windows (with length of 32 time-samples) do:
843 // - Sum the powers of those samples
844 // - Compare the sum to the threshold, if greater, trigger on this event (return 1)
845 // Notes:
846 // There will be several things hardcoded into the following method, feel free to change these to be variables within the Settings class, but for the sake of sanity, PLEASE no more global variables!
847 // This will be made to implement all types of payloads shortly, what exists below is only a temporary specialization for ANITA III.
848 
849 void GlobalTrigger::PassesTriggerCoherentSum(Settings *settings1,Anita *anita1,int inu, int *thispasses) {
850 
851  for (int center_phi_sector_offset = -1; center_phi_sector_offset <= 1; center_phi_sector_offset++){
852  int center_phi_sector_index = first_phi_sector_hit + center_phi_sector_offset;
853  if (center_phi_sector_index > 15){center_phi_sector_index = 0;}
854  if (center_phi_sector_index < 0){center_phi_sector_index = 15;}
855 
856  bool coherent_trigger_passes = false;
857 
858  unsigned N_STEP_PHI = 16; // The number of different phi hypotheses for a given center phi sector
859  unsigned N_STEP_THETA = 81; // The number of different theta hypotheses for a given center phi sector
860  unsigned N_PHI_SECTORS = 16; // The number of phi sectors
861  unsigned N_LAYERS_TRIGGER = 3; // The number of layers as seen by the trigger
862 
863  double highest_power = 0;
864 
865 
866  // Here the 48 antennas are filled according to the waveforms passed to PassesTrigger(...).
867  unsigned fill_index = 0;
868  for (unsigned fill_index_phi_sector = 0; fill_index_phi_sector < 16; ++fill_index_phi_sector) {
869  for (unsigned fill_index_layer = 0; fill_index_layer < 3; ++fill_index_layer) {
870  anita1->cwst_RXs[fill_index].phi_sector = fill_index_phi_sector;
871  anita1->cwst_RXs[fill_index].layer = fill_index_layer;
872 
873  unsigned physical_layer_index = ((fill_index_phi_sector%2) ? (fill_index_layer + 1) : (0)); // Maps the trigger layers {0, 1, 2} to the physical layers {0, 1, 2, 3}.
874  if (fill_index_phi_sector%2 == 0) {
875  physical_layer_index = (fill_index_layer == 0) ? 0 : (fill_index + 1);
876  }
877  // If phi sector is even then trigger layer zero maps to physical layer zero. If phi odd, maps to layer 1.
878  unsigned physical_phi_index (fill_index_phi_sector);
879  if (fill_index_layer == 0) {
880  physical_phi_index = unsigned(fill_index_phi_sector / 2.); // Map {0, ..., 15} to {0, ..., 7}.
881  }
882  anita1->cwst_RXs[fill_index].x = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][0];
883  anita1->cwst_RXs[fill_index].y = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][1];
884  anita1->cwst_RXs[fill_index].z = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][2];
885 
886  anita1->cwst_RXs[fill_index].waveform->assign(volts_rx_rfcm_trigger[fill_index_phi_sector][fill_index_layer].begin(),volts_rx_rfcm_trigger[fill_index_phi_sector][fill_index_layer].end());
887 
888  for (unsigned fill_index_timestep = 0; fill_index_timestep < anita1->HALFNFOUR; ++fill_index_timestep) {
889  anita1->cwst_RXs[fill_index].digitized->at(fill_index_timestep) = three_bit_round(anita1->cwst_RXs[fill_index].waveform->at(fill_index_timestep)/ anita1->rms_rfcm_e_single_event, (getRNG(RNG_SUMMED_TRIGGER)->Rndm() >= 0.5), false);
890  }
891  ++fill_index;
892  }
893  }
894 
895  for (unsigned center_phi_sector_index = 0; center_phi_sector_index < N_PHI_SECTORS; ++center_phi_sector_index) {
896  // Loop over the hypotheses to align waveforms.
897  for (unsigned index_phi = 0; index_phi < N_STEP_PHI; ++index_phi) {
898  for (unsigned index_theta = 0; index_theta < N_STEP_THETA; ++index_theta) {
899  unsigned fill_index = 0;
900  vector <double> summed_wfm(anita1->HALFNFOUR, 0.);
901 
902  for (int fill_index_phi_sector_offset = -1; fill_index_phi_sector_offset <= 1; ++fill_index_phi_sector_offset) {
903  unsigned fill_index_phi_sector = (center_phi_sector_index + fill_index_phi_sector_offset + N_PHI_SECTORS)%N_PHI_SECTORS;
904 
905  for (unsigned fill_index_layer = 0; fill_index_layer < N_LAYERS_TRIGGER; ++fill_index_layer) {
906  unsigned rx_index = fill_index_phi_sector * 3 + fill_index_layer;
907  anita1->cwst_aligned_wfms[fill_index].phi_sector = fill_index_phi_sector;
908  anita1->cwst_aligned_wfms[fill_index].layer = fill_index_layer;
909 
910  unsigned physical_layer_index = ((fill_index_phi_sector%2) ? (fill_index_layer + 1) : (0)); // Maps the trigger layers {0, 1, 2} to the physical layers {0, 1, 2, 3}.
911  // If phi sector is even then trigger layer zero maps to physical layer zero. If phi odd, maps to layer 1.
912  unsigned physical_phi_index (fill_index_phi_sector);
913  if (fill_index_layer == 0) {
914  physical_phi_index = unsigned(fill_index_phi_sector / 2.); // Map {0, ..., 15} to {0, ..., 7}.
915  }
916  anita1->cwst_aligned_wfms[fill_index].x = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][0];
917  anita1->cwst_aligned_wfms[fill_index].y = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][1];
918  anita1->cwst_aligned_wfms[fill_index].z = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][2];
919 
920  unsigned time_offset = anita1->hypothesis_offsets[center_phi_sector_index][index_phi][index_theta][fill_index_phi_sector_offset + 1][fill_index_layer];
921 
922  for (unsigned fill_index_timestep = 0; fill_index_timestep < anita1->HALFNFOUR - time_offset; ++fill_index_timestep) {
923  anita1->cwst_aligned_wfms[fill_index].digitized->at(fill_index_timestep) = anita1->cwst_RXs[rx_index].digitized->at(fill_index_timestep + time_offset);
924  summed_wfm.at(fill_index_timestep) += anita1->cwst_aligned_wfms[fill_index].digitized->at(fill_index_timestep);
925  }
926 
927  for (unsigned fill_index_timestep = anita1->HALFNFOUR - time_offset; fill_index_timestep < anita1->HALFNFOUR; ++fill_index_timestep) {
928  // Fill the ends of the waveforms with zeros. This should not negatively affect the simulation at all.
929  // This is an attempt to fix the power = 648 issue, where the bins were all +0.5 and summing to 9*0.5, summed over 32 bins.
930  anita1->cwst_aligned_wfms[fill_index].digitized->at(fill_index_timestep) = 0.;
931  }
932  ++fill_index;
933 
934  } // for fill layer indices
935  } // for fill phi sector indices
936 
937  vector <double> power_of_summed_wfm;
938  square_waveform_elements(summed_wfm, power_of_summed_wfm);
939 
940  for (unsigned window_index = 0; window_index < power_of_summed_wfm.size() - 32; window_index += 16) {
941  double power = summed_power_window(power_of_summed_wfm, window_index, 32);
942  if (highest_power < power){
943  highest_power = power;
944  // hi_pow_center = center_phi_sector_index;
945  // hi_pow_phi_index = index_phi;
946  // hi_pow_theta_index = index_theta;
947  }
948 
949  if (power >= settings1->COHERENT_THRESHOLD){
950  coherent_trigger_passes = true;
951  thispasses += 2;
952  }
953 
954  if (power >= settings1->COHERENT_THRESHOLD){
955  anita1->fill_coherent_waveform_sum_tree(inu, center_phi_sector_index, settings1, anita1->rms_rfcm_e_single_event, 0., window_index, window_index + 32, index_theta - 45., index_phi, 33., 33.,summed_wfm, power_of_summed_wfm, power);
956  //thispasses = true;
957  //return 1; // Return that this waveform passes.
958  }
959 
960  if (inu == 1808 && center_phi_sector_index == 6) {
961  anita1->fill_coherent_waveform_sum_tree(inu, center_phi_sector_index, settings1, anita1->rms_rfcm_e_single_event, 0., window_index, window_index + 32, index_theta - 45., index_phi, 33., 33.,summed_wfm, power_of_summed_wfm, power);
962  //return 1;
963  }
964 
965  if (coherent_trigger_passes){
966  // used to be return this_passes;
967  return;
968  }
969 
970  } // for window indices
971  } // for hypothesis theta indices
972  } // for hypothesis phi indices
973  } // for hypothesis center phi sector indices
974 
975 
976  }
977 } // end of PassesTriggerCoherentSum
978 
979 
980 
981 
982 void GlobalTrigger::PassesTriggerSummedPower(Settings *settings1,Anita *anita1){
983 
984  // TRIGGERSCHEME == 4 is the Summed Power Trigger.
985  // For every window, all of the phi sectors find the maximum coherently summed power.
986  // These max powers are summed with the neighboring phi sectors (for three phi sectors in total) and compared to a threshold.
987 
988  double SummedPowerThreshold = settings1->COHERENT_THRESHOLD; // The threshold against which all of the events will be compared.
989  double SummedPowerThetaMin = -45.; // DEGREE! The minimum theta (elevation) angle to try in the hypotheses.
990  double SummedPowerThetaMax = +25.; // DEGREE! The maximum theta (elevation) angle to try in the hypotheses.
991  double SummedPowerThetaStep = 1.; // DEGREE! The angle to advance from minimum theta to maximum theta.
992 
993  // unsigned N_STEP_THETA = 81; // The number of different theta hypotheses for a given center phi sector
994  // unsigned N_PHI_SECTORS = 16; // The number of phi sectors
995  // unsigned N_LAYERS_TRIGGER = 3; // The number of layers as seen by the trigger
996 
997  // double highest_power = 0.;
998  // unsigned hi_pow_center = 0;
999  // unsigned hi_pow_phi_index = 0;
1000  // unsigned hi_pow_theta_index = 0;
1001 
1002  // Here the 48 antennas are filled according to the waveforms passed to PassesTrigger(...).
1003  unsigned fill_index = 0;
1004 
1005  for (unsigned fill_index_phi_sector = 0; fill_index_phi_sector < 16; ++fill_index_phi_sector) {
1006  for (unsigned fill_index_layer = 0; fill_index_layer < 3; ++fill_index_layer) {
1007  anita1->cwst_RXs[fill_index].phi_sector = fill_index_phi_sector;
1008  anita1->cwst_RXs[fill_index].layer = fill_index_layer;
1009 
1010 
1011  unsigned physical_phi_index = fill_index_phi_sector;
1012  unsigned physical_layer_index;
1013 
1014 
1015  // If the phi sector is an odd one, start with physical layer 1. If even, start with layer 0
1016  if (fill_index_phi_sector%2) {
1017  physical_layer_index = fill_index_layer + 1;
1018  } else
1019  if (fill_index_layer == 0) {
1020  physical_layer_index = 0;
1021  } else {
1022  physical_layer_index = fill_index_layer + 1;
1023  }
1024 
1025  if (physical_layer_index == 0) {
1026  physical_phi_index = unsigned(fill_index_phi_sector / 2);
1027  }
1028 
1029  if (physical_layer_index == 1) {
1030  physical_phi_index = unsigned(fill_index_phi_sector / 2);
1031  }
1032 
1033  // Set antenna positions
1034  anita1->cwst_RXs[fill_index].x = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][0];
1035  anita1->cwst_RXs[fill_index].y = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][1];
1036  anita1->cwst_RXs[fill_index].z = anita1->ANTENNA_POSITION_START[0][physical_layer_index][physical_phi_index][2];
1037 
1038  // Fill the waveforms
1039  anita1->cwst_RXs[fill_index].waveform->assign(volts_rx_rfcm_trigger[fill_index_phi_sector][fill_index_layer].begin(),volts_rx_rfcm_trigger[fill_index_phi_sector][fill_index_layer].end());
1040 
1041  for (unsigned fill_index_timestep = 0; fill_index_timestep < anita1->HALFNFOUR; ++fill_index_timestep) {
1042  anita1->cwst_RXs[fill_index].digitized->at(fill_index_timestep) = three_bit_round(anita1->cwst_RXs[fill_index].waveform->at(fill_index_timestep)/ anita1->rms_rfcm_e_single_event, false);
1043  }
1044  ++fill_index;
1045  }
1046  }
1047 
1048 
1049  // For whatever reason ANTENNA_POSITION_START[0] is a c-array, with large dimensions (5 by 400).
1050  // Most of these are initialized as a Vector with (0.,0.,1) for (x,y,z). These are non-existant antennas.
1051  //
1052  // Make sure that the antennas positions being copied are actually from a real antenna, and pay attention to the physical
1053  // antenna layers 0 & 1, as they map to the 0th trigger layer.
1054 
1055 
1056  // Calculate the theta hypothesis angles
1057  vector <double> theta_hypotheses_deg;
1058  double theta_deg = SummedPowerThetaMin;
1059 
1060  do {
1061  theta_hypotheses_deg.push_back (theta_deg);
1062  theta_deg += SummedPowerThetaStep;
1063  } while (theta_deg <= SummedPowerThetaMax);
1064 
1065  // Now we begin the trigger.
1066 
1067  // First steps over windows:
1068  for (unsigned index_window = 0; index_window < 31; ++index_window) {
1069  double max_power_each_sector[16] = {0.};
1070  double summed_power_each_sector[16] = {0.};
1071 
1072  for (unsigned index_phi_sector = 0; index_phi_sector < 16; ++index_phi_sector) {
1073  double max_power = 0.;
1074 
1075  // Use the current and opposite phi sectors to find an even horizontal line.
1076  TVector3 theta_zero (anita1->cwst_RXs[index_phi_sector * 3 + 1].x - anita1->cwst_RXs[(index_phi_sector + 8)%16 * 3 + 1].x, anita1->cwst_RXs[index_phi_sector * 3 + 1].y - anita1->cwst_RXs[(index_phi_sector + 8)%16 * 3 + 1].y, anita1->cwst_RXs[index_phi_sector * 3 + 1].z - anita1->cwst_RXs[(index_phi_sector + 8)%16 * 3 + 1].z);
1077  // Normalizes that line.
1078  theta_zero = theta_zero.Unit();
1079 
1080  // Loop over theta hypotheses
1081  for (unsigned index_theta_hypothesis = 0; index_theta_hypothesis < theta_hypotheses_deg.size(); ++index_theta_hypothesis) {
1082  double power = 0.;
1083 
1084  // Calculate the vector for the hypothesis' direction.
1085  double hypoth_theta_rad = (theta_hypotheses_deg[index_theta_hypothesis] + 45.) * RADDEG;
1086  double hypoth_phi_rad = theta_zero.Phi() * RADDEG;
1087  TVector3 hypoth_vector (sin (hypoth_theta_rad) * cos (hypoth_phi_rad), sin (hypoth_theta_rad) * sin (hypoth_phi_rad), cos (hypoth_theta_rad));
1088 
1089  // There will be three antenna time offsets per phi sector:
1090  double dist[3] = {0.};
1091  double offset[3] = {0.};
1092  unsigned offset_steps[3] = {0};
1093  TVector3 middle_rx (anita1->cwst_RXs[index_phi_sector * 3 + 1].x, anita1->cwst_RXs[index_phi_sector * 3 + 1].y, anita1->cwst_RXs[index_phi_sector * 3 + 1].z);
1094 
1095 
1096  // Calculate the relative path length distances
1097  dist[0] = hypoth_vector * (TVector3(anita1->cwst_RXs[index_phi_sector * 3].x, anita1->cwst_RXs[index_phi_sector * 3].y, anita1->cwst_RXs[index_phi_sector * 3].z) - middle_rx);
1098  dist[1] = 0.; // Just let this one be defined as "zero", the other ones are relative.
1099  dist[2] = hypoth_vector * (TVector3(anita1->cwst_RXs[index_phi_sector * 3 + 2].x, anita1->cwst_RXs[index_phi_sector * 3 + 2].y, anita1->cwst_RXs[index_phi_sector * 3 + 2].z) - middle_rx);
1100 
1101 
1102  // Find time offsets from the relative path lengths
1103  for (unsigned index_antenna = 0; index_antenna < 3; ++index_antenna) {
1104  offset[index_antenna] = dist[index_antenna] / CLIGHT;
1105  }
1106 
1107  // Make the lowest time offset be the minoffset
1108  double minoffset = offset[0];
1109  if (offset[1] <= minoffset) {minoffset = offset[1];}
1110  if (offset[2] <= minoffset) {minoffset = offset[2];}
1111 
1112 
1113  // Subtract the minoffset from the others, "normalizing" them so that the lowest value is zero
1114  for (unsigned index_antenna = 0; index_antenna < 3; ++index_antenna) {
1115  offset[index_antenna] -= minoffset;
1116  offset_steps[index_antenna] = int(Tools::round(offset[index_antenna] / anita1->TIMESTEP));
1117  }
1118 
1119  // Check to make sure that all of the values will be in bounds
1120  if (offset_steps[0] + index_window * 16 + 32 > 511) {continue;}
1121  if (offset_steps[1] + index_window * 16 + 32 > 511) {continue;}
1122  if (offset_steps[2] + index_window * 16 + 32 > 511) {continue;}
1123 
1124  // Calculate the powers for every time in the window and then add it to the window power
1125  for (unsigned time_index = index_window * 16; time_index < index_window * 16 + 32; ++time_index) {
1126  double thispower = (anita1->cwst_RXs[index_phi_sector * 3].digitized->at(time_index + offset_steps[0]) + anita1->cwst_RXs[index_phi_sector * 3 + 1].digitized->at(time_index + offset_steps[1]) + anita1->cwst_RXs[index_phi_sector * 3 + 2].digitized->at(time_index + offset_steps[2]));
1127  thispower *= thispower;
1128  power += thispower;
1129  }
1130 
1131  // Make sure that the power is sane for the windows we care about
1132  if (power == 0 && index_window < 21) {
1133  std::cout << "Trigger error: a trigger window which should not have had zero power had zero power. Window was\t" << index_window << "\n";
1134  }
1135 
1136  // If this is the highest-power hypothesis for this phi sector, save it to max_power
1137  if (power > max_power) {max_power = power;}
1138  }
1139 
1140  max_power_each_sector[index_phi_sector] = max_power;
1141  }
1142 
1143  // Calculate the power for the neighboring phi sectors
1144  summed_power_each_sector[0] = max_power_each_sector[15] + max_power_each_sector[0] + max_power_each_sector[1];
1145  for (unsigned index_phi_sector = 1; index_phi_sector < 15; ++index_phi_sector) {
1146  summed_power_each_sector[index_phi_sector] = max_power_each_sector[index_phi_sector - 1] + max_power_each_sector[index_phi_sector] + max_power_each_sector[index_phi_sector + 1];
1147  }
1148  summed_power_each_sector[15] = max_power_each_sector[14] + max_power_each_sector[15] + max_power_each_sector[1];
1149 
1150 
1151  bool temporary_passing_variable = false;
1152  for (unsigned index_phi_sector = 0; index_phi_sector < 16; ++index_phi_sector) {
1153  //std::cout << "Max Power, sector\t" << index_phi_sector << "\t:\t" << summed_power_each_sector[index_phi_sector] << std::endl;
1154  if (summed_power_each_sector[index_phi_sector] >= SummedPowerThreshold) {
1155  std::cout << "Passed with SummedPower =\t" << summed_power_each_sector[index_phi_sector] << "\ton sector\t" << index_phi_sector << "\n";
1156  //return 1;
1157  temporary_passing_variable = true;
1158  }
1159  }
1160  if (temporary_passing_variable) {index_window=31;} //exit
1161  }
1162 } // end of PassesTriggerSummedPower
1163 
1164 
1165 void GlobalTrigger::PassesTriggerScheme5(Anita *anita1,double this_threshold, int *thispasses) {
1166  double threshold=this_threshold;
1167 
1168  // need to find how many in a set of 6 pass
1169 
1170  int maxsample=TMath::MaxElement(5,anita1->imaxbin);
1171  int minsample=TMath::MinElement(5,anita1->iminbin);
1172 
1173  int nstayhigh=(int)(anita1->l1window/anita1->TIMESTEP);
1174  // now make each flag stay high for the required amount of time
1175 
1176 
1177  minsample=(int)(anita1->maxt_diode/anita1->TIMESTEP)+(anita1->NFOUR/4-(int)(anita1->maxt_diode/anita1->TIMESTEP))+(int)(anita1->arrival_times[0][anita1->rx_minarrivaltime]/anita1->TIMESTEP);
1178  maxsample=anita1->NFOUR/2-(int)(anita1->arrival_times[0][anita1->rx_minarrivaltime]/anita1->TIMESTEP);
1179 
1180  for (int i=0;i<5;i++) {
1181  anita1->iminbin[i]=minsample;
1182  anita1->imaxbin[i]=maxsample;
1183  }
1184 
1185  for (unsigned center_phi_sector_index = 0; center_phi_sector_index < 16; ++center_phi_sector_index) {
1186  // for (unsigned center_phi_sector_index = 0; center_phi_sector_index < 16; center_phi_sector_index+=2) {
1187 
1188 
1189  for (unsigned int index_hyp=0;index_hyp<anita1->vdifferent_offsets.size();index_hyp++) {
1190 
1191 
1192  for (unsigned i_layer = 0; i_layer < anita1->N_SUMMED_LAYERS; ++i_layer) {
1193  for (unsigned i_sector = 0; i_sector < anita1->N_SUMMED_PHI_SECTORS; ++i_sector) {
1194 
1195  int rx=anita1->GetRxTriggerNumbering(i_layer,(center_phi_sector_index+i_sector)%16);
1196 
1197 
1198  double timedomain_output_1_corrected[Anita::NFOUR/2]; // these are corrected for their delays so that they should line up in time
1199  double timedomain_output_2_corrected[Anita::NFOUR/2];
1200  // if we're doing an anita 3 trigger, adjust for delays in the diode outputs so that we can do a time coincidence trigger
1201 
1202  for (int i=0;i<Anita::NFOUR/2;i++) {
1203  timedomain_output_1_corrected[i]=anita1->timedomain_output_allantennas[0][rx][i];
1204  timedomain_output_2_corrected[i]=anita1->timedomain_output_allantennas[1][rx][i];
1205  }
1206 
1207  // Tools::ShiftLeft(timedomain_output_1_corrected,anita1->NFOUR/2,anita1->arrival_times[rx]);
1208  //Tools::ShiftLeft(timedomain_output_2_corrected,anita1->NFOUR/2,anita1->arrival_times[rx]);
1209 
1210  Tools::ShiftLeft(timedomain_output_1_corrected,anita1->NFOUR/2,anita1->vdifferent_offsets[index_hyp][anita1->N_SUMMED_PHI_SECTORS*i_layer+i_sector]);
1211  Tools::ShiftLeft(timedomain_output_2_corrected,anita1->NFOUR/2,anita1->vdifferent_offsets[index_hyp][anita1->N_SUMMED_PHI_SECTORS*i_layer+i_sector]);
1212 
1213 
1214  //for (int k=0;k<5;k++) {
1215  //anita1->ston[k]=anita1->peak_v_banding_rfcm_e[k]/anita1->bwslice_vrms[k];
1216  //} // end loop over bands
1217 
1218  if (rx==anita1->rx_minarrivaltime) {
1219 
1220  for (int i=0;i<Anita::NFOUR/2;i++) {
1221  anita1->timedomain_output_corrected_forplotting[0][0][i]=timedomain_output_1_corrected[i];
1222  anita1->timedomain_output_corrected_forplotting[1][0][i]=timedomain_output_2_corrected[i];
1223  }
1224  }
1225 
1226  flag_e_L1[rx].clear();
1227  flag_h_L1[rx].clear();
1228 
1229 
1230  for (int i=minsample;i<maxsample;i++) {
1231  // for (int i=minsample;i<maxsample;i++) {
1232 
1233  //cout << "timedomain_output_1_corrected[i], threshold, bwslice_rmsdiode[4] are " << timedomain_output_1_corrected[i] << "\t" << threshold*anita1->bwslice_rmsdiode[4] << "\n";
1234  if (timedomain_output_1_corrected[i]<threshold*anita1->bwslice_rmsdiode[0][4]) {
1235  flag_e_L1[rx].push_back(1);
1236  //cout << "passes.\n";
1237  }
1238  else {
1239  flag_e_L1[rx].push_back(0);
1240  //cout << "doesn't pass.\n";
1241  }
1242 
1243  if (timedomain_output_2_corrected[i]<threshold*anita1->bwslice_rmsdiode[1][4])
1244  flag_h_L1[rx].push_back(1);
1245  else
1246  flag_h_L1[rx].push_back(0);
1247 
1248  } // end loop over samples in window where we look for single channel trigger firing
1249 
1250  for (int i=minsample;i<maxsample;i++) {
1251  // for (int i=minsample;i<maxsample;i++) { // loop over samples in window where we looked for a single channel trigger
1252 
1253  if (flag_e_L1[rx][i-minsample]==1) // if the flag is high (remember the second index of flag_e counts from the start of the window)
1254  for(int k=nstayhigh-1; k>0 && i<maxsample-1; k--) { // then for nstayhigh-1 samples after than we keep it high
1255  // i<maxsample-1 makes sure that the second index of flag_e is always less than maxsample-minsample-1.
1256  i++;
1257  flag_e_L1[rx][i-minsample]=1;
1258 
1259  }
1260 
1261  // if (flag_e[j][i]==1) {
1262  // for (int k=i;k<i+nstayhigh;k++) {
1263  // if (k<NSAMPLES)
1264  // flag_e[j][k]=1;
1265  // } // end loop over samples where we want it to stay high
1266 
1267  // } // end if flag is high
1268  }
1269 
1270  for (int i=minsample;i<maxsample;i++) {
1271  if (flag_h_L1[rx][i-minsample]==1)
1272  for(int k=nstayhigh-1; k>0 && i<maxsample-1; k--) {
1273  i++;
1274  flag_h_L1[rx][i-minsample]=1;
1275  }
1276 
1277  // if (flag_h[j][i]==1) {
1278  // for (int k=i;k<i+nstayhigh;k++) {
1279  // if (k<NSAMPLES)
1280  // flag_h[j][k]=1;
1281  // } // end loop over samples where we want it to stay high
1282 
1283  // } // end if flag is high
1284 
1285  } // end loop over samples
1286 
1287  } // end loop over phi sectors being considered for this L1
1288  } // end loop over layers being considered for this L1
1289 
1290 
1291  // now find the sample with the highest number of bands that pass
1292  // int maxbands=0;
1293  // int nbands_pass=0;
1294 
1295 
1296  for (int i=minsample;i<maxsample;i++) {
1297 
1298  int nbands_pass[2]={0};
1299  int maxbands[2]={0};
1300 
1301  for (unsigned i_layer = 0; i_layer < anita1->N_SUMMED_LAYERS; ++i_layer) {
1302  for (unsigned i_sector = 0; i_sector < anita1->N_SUMMED_PHI_SECTORS; ++i_sector) {
1303 
1304  int rx=anita1->GetRxTriggerNumbering(i_layer,(center_phi_sector_index+i_sector)%16);
1305 
1306 
1307 
1308  if (flag_e_L1[rx][i-minsample]==1)
1309  nbands_pass[0]++;
1310  if (flag_h_L1[rx][i-minsample]==1)
1311  nbands_pass[1]++;
1312 
1313 
1314  }
1315  }
1316 
1317  if (nbands_pass[0]>maxbands[0])
1318  maxbands[0]=nbands_pass[0];
1319  if (nbands_pass[1]>maxbands[1])
1320  maxbands[1]=nbands_pass[1];
1321 
1322  if (maxbands[0]>=anita1->NCH_PASS)
1323 
1324  thispasses[0]=1.;
1325  if ( maxbands[1]>=anita1->NCH_PASS)
1326  thispasses[1]=1.;
1327 
1328 
1329  } // end loop over layers
1330 
1331  }
1332 
1333  } // end loop over center phi sectors
1334 
1335 } // end of PassesTriggerScheme5
1336 
1337 
1338 
1340 
1352 void GlobalTrigger::FillInNadir(Settings *settings1,Anita *anita1,int ant) { //overloaded function
1353  int antarray[Anita::NPHI_MAX]={0};
1354  //here the ant array is an array of binary numbers
1355  int whichphi;
1356 
1357  for (int iphi=0;iphi<anita1->NRX_PHI[2];iphi++) {
1358 
1359  whichphi = 1 << GetPhiSector(settings1,2,iphi); // get trigger phi sector
1360  if (whichphi & ant) { // if this phi sector is masked
1361  antarray[GetPhiSector(settings1,2,iphi)]=1;
1362 
1363  }
1364  }
1365 
1366 
1367  FillInNadir(anita1,antarray);
1368 
1369 
1370  ant=0;
1371  for (int iphi=0;iphi<anita1->NTRIGPHISECTORS;iphi++) {
1372  if (antarray[iphi])
1373  ant+=(1<<iphi);
1374  }
1375 
1376 
1377 
1378 }
1379 
1380 
1381 
1383 
1395 void GlobalTrigger::FillInNadir(Anita *anita1,int *ant) {
1396  int ileft,iright;
1397  // cout << "ntrigphisectors is " << ntrigphisectors << "\n";
1398  // for (int i=0;i<ntrigphisectors;i++) {
1399  // cout << "before, ant is " << ant[2][i] << "\n";
1400  // }
1401 
1402  for (int i=0;i<anita1->NTRIGPHISECTORS/2;i++) {
1403  ileft=(2*i+anita1->NTRIGPHISECTORS-1)%anita1->NTRIGPHISECTORS;
1404  iright=(2*i+1)%anita1->NTRIGPHISECTORS;
1405  if (ant[ileft] || ant[iright])
1406  ant[2*i]=1;
1407  }
1408 
1409  // for (int i=0;i<anita1->NTRIGPHISECTORS;i++) {
1410  // cout << "after, ant is " << ant[2][i] << "\n";
1411  // }
1412 
1413 
1414 }
1415 
1416 
1417 
1419 
1426 int GlobalTrigger::GetPhiSector(Settings *settings1,int i,int j) { // given trigger layer and index, get phi sector.
1427  // for the upper two layers, the phi sector is just j
1428  // for the nadir layer, the phi sector is 2*j+1
1429  // warning this counts from 0
1430  if (i<2)
1431  return j;
1432  if (i==2) {
1433  if (settings1->DISCONES==2)
1434  return 2*j+1;
1435  else
1436  return j;
1437  }
1438  else
1439  {
1440  cout << "Input non-existent layer!\n";
1441  return -1;
1442 
1443  }
1444  return -1;
1445 }
1446 
1447 
1449 
1457 void GlobalTrigger::GetAnitaLayerPhiSector(Settings *settings1,int i,int j,int &whichlayer,int &whichphisector) {
1458 
1459 
1460  if (settings1->WHICH==6 || settings1->WHICH==2 || settings1->WHICH==8) {// If Anita 1 or Anita 2
1461  if (i==0) {
1462  whichlayer=0;
1463  whichphisector=2*j+1;
1464  }
1465  else if (i==1) {
1466  whichlayer=0;
1467  whichphisector=2*j;
1468  }
1469  else if (i==2) {
1470  whichlayer=1;
1471  whichphisector=j;
1472  }
1473  else if (i==3) {
1474  whichlayer=2;
1475  whichphisector=2*j+1;
1476  }
1477  } // end anita 1 or anita 2
1478  else if (settings1->WHICH==9 || settings1->WHICH==10) { // anita 3 or anita 4
1479  if (i==0) {
1480  whichlayer=0;
1481  whichphisector=2*j;
1482  }
1483  else if (i==1) {
1484  whichlayer=0;
1485  whichphisector=2*j+1;
1486  }
1487  else if (i==2) {
1488  whichlayer=1;
1489  whichphisector=j;
1490  }
1491  else if (i==3) {
1492  whichlayer=2;
1493  whichphisector=j;
1494  }
1495  } // end anita 3 or 4
1496  else {
1497  whichlayer=i;
1498  whichphisector=j;
1499 
1500  }
1501 
1502 }
1503 
1504 
1506 
1540 void GlobalTrigger::L3Trigger(Settings *settings1,Anita *anita1,int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX],int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX],int discones_passing,int *l3trig,
1541  int *thispasses) {
1542 
1543  int whichphipass[Anita::NPOL][Anita::NPHI_MAX]={{0}};
1544 
1545  for (int i=0;i<Anita::NTRIG;i++)
1546  triggerbits[i]=0;
1547 
1548  // shouldn't get here for anita3 or anita4 anymore.
1549  //assumes the number of trigger phi sectors are the same in each layer
1550  for (int i=0;i<anita1->PHITRIG[0];i++) {
1551 
1552 
1553  // if (settings1->WHICH==9) { // anita 3
1554  // for (int ipolar=0;ipolar<2;ipolar++) {
1555  // if (loctrig[ipolar][0][i]>0 && loctrig[ipolar][1][i]>0 && loctrig[ipolar][2][i]>0) {
1556  // thispasses[ipolar]=1;
1557  // whichphipass[ipolar][i]=1;
1558  // if (!(triggerbits[3] & (1<<i)))
1559  // triggerbits[3]+=(1<<i);
1560  // }
1561  // }
1562  // }
1563  // else { // anita 1 or anita 2
1564 
1565  for (int ilayer=0;ilayer<anita1->NTRIGGERLAYERS-1;ilayer++) {
1566  for (int ipolar=0;ipolar<2;ipolar++) {
1567  // check this - does ilayer and ilayer+1 include just 1 and 2, or also 2 and 3, and is that ok?
1568  if (loctrig[ipolar][ilayer][i]>0 && loctrig[ipolar][ilayer+1][i]>0) { // upper two layers
1569 
1570  //l3trig += (1<<i);
1571  thispasses[ipolar]=1;
1572  whichphipass[ipolar][i]=1;
1573 
1574  if(loctrig[ipolar][0][i]>0 && loctrig[ipolar][1][i]>0) {
1575  if (!(triggerbits[0] & (1<<i)))
1576  triggerbits[0] += (1<<i);
1577 
1578  }
1579 
1580 
1581  }
1582  }
1583  }
1584 
1585  // anita 1, anita 1 kurt, anita 2 kurt, anita 3
1586  // but this is inside a set of brackets for !anita3
1587  if (settings1->WHICH==2 || settings1->WHICH==6 || settings1->WHICH==8) { // Anita 1, 2
1588 
1589  for (int ipolar=0;ipolar<2;ipolar++) {
1590  if (((settings1->DISCONES==2) && (loctrig[ipolar][1][i]>0 && loctrig[ipolar][2][i]>0)) || // second layer and nadir?
1591  ((settings1->DISCONES==2) && (loctrig[ipolar][0][i]>0 && loctrig[ipolar][2][i]>0)) || // first layer and nadir?
1592  ((settings1->DISCONES==2) && loctrig_nadironly[ipolar][i]>0) || // just nadir - does this mean having just a nadir pass makes the whole event pass?
1593  (settings1->DISCONES==1 && (loctrig[ipolar][1][i]>0 && discones_passing>=settings1->NDISCONES_PASS)) || // second layer and discones
1594  (settings1->DISCONES==1 && (loctrig[ipolar][0][i]>0 && discones_passing>=settings1->NDISCONES_PASS)) // top layer and discones
1595  ) {
1596 
1597  thispasses[ipolar]=1;
1598  whichphipass[ipolar][i]=1;
1599  // this is just for nadir studies->
1600  //how many of each trigger condition
1601 
1602  if(loctrig[ipolar][1][i]>0 && loctrig[ipolar][2][i]>0) {
1603  triggerbits[1] += (1<<i);
1604 
1605  }
1606  if(loctrig[ipolar][0][i]>0 && loctrig[ipolar][2][i]>0) {
1607  triggerbits[2]+=(1<<i);
1608 
1609  }
1610  // if (loctrig_nadironly[i]>0)
1611  // triggerbits[3]+=weight2;
1612  // if ((loctrig_nadironly[i]>0 ||
1613  // (loctrig[1][i]>0 && loctrig[2][i]>0) ||
1614  // (loctrig[0][i]>0 && loctrig[2][i]>0)) &&
1615  // !(loctrig[0][i]>0 && loctrig[1][i]>0))
1616  // triggerbits[4]+=weight2;
1617 
1618 
1619  thispasses[ipolar]=1;
1620 
1621  }
1622 
1623  } //if it's the anita 1 payload
1624  }
1625 
1626  // } // if it's not anita 3
1627 
1628 
1629  for (int ipolar=0;ipolar<2;ipolar++) {
1630  if (whichphipass[ipolar][i])
1631  l3trig[ipolar]+=(1<<i);
1632  // std::cout << ipolar << " " << whichphipass[ipolar][i] << " " << l3trig[ipolar] << std::endl;
1633  }
1634 
1635  } // end looping over phi
1636 
1637 
1638 }
1639 
1640 
1642 
1666 // void GlobalTrigger::GetArrivalTimes(int inu, Anita *anita1, const Vector& rf_direction) {
1667 
1668 
1669 // for (int antenna_index = 0; antenna_index < (anita1->number_all_antennas); antenna_index++) { //loop over layers on the payload
1670 // arrival_times[antenna_index] = (anita1->antenna_positions[antenna_index] * rf_direction) / CLIGHT;
1671 // } // for: loop over antenna layers
1672 
1673 // double first_trigger_time = Tools::dMin(arrival_times,(anita1->number_all_antennas));
1674 
1675 // for (int i=0;i<(anita1->number_all_antennas);i++){
1676 // arrival_times[i] -= first_trigger_time;
1677 // if (arrival_times[i] == 0){
1678 // first_phi_sector_hit = floor(i / 16);
1679 // }
1680 // }
1681 
1682 // } // GetArrivalTimes
1683 
1684 
1685 
1687 
1713 void GlobalTrigger::delay_align_antenna_waveforms(const vector< vector < vector <double> > >& waveforms, const vector < vector <unsigned int> >& delays, vector < vector <double> >& output){
1714  // The waveforms vector is accessed by waveforms[phi_sector_id][antenna_id][sample_id]
1715  // the delays are accessed by delays[phi_sector_id][antenna_id]
1716 
1717  // the output is in the same format as the input, but will only include samples which have all elements
1718  output.clear();
1719 
1720  unsigned int shortest_waveform = waveforms[0][0].size();
1721  vector <double> temp_antenna_waveform;
1722  for (unsigned int phi_sector_index = 0; phi_sector_index < 3; phi_sector_index++){
1723  for (unsigned int antenna_index = 0; antenna_index < waveforms[phi_sector_index].size(); antenna_index++){
1724  temp_antenna_waveform.clear();
1725  for (unsigned int sample_index = delays[phi_sector_index][antenna_index]; sample_index < waveforms[phi_sector_index][antenna_index].size(); sample_index++){
1726  temp_antenna_waveform.push_back(waveforms[phi_sector_index][antenna_index][sample_index]);
1727  }
1728  if (temp_antenna_waveform.size() < shortest_waveform){
1729  shortest_waveform = temp_antenna_waveform.size();
1730  }
1731  output.push_back(temp_antenna_waveform);
1732  }
1733  }
1734  // Must now make all have the same length
1735  for (unsigned int antenna_index = 0; antenna_index < output.size(); antenna_index++){
1736  output[antenna_index].resize(shortest_waveform);
1737  }
1738 }
1739 
1740 
1742 
1750 void GlobalTrigger::sum_aligned_waveforms(const vector < vector <double> >& waveforms, vector <double>& output){
1751  output.clear();
1752  unsigned waveform_size = waveforms[0].size();
1753  unsigned waveforms_size = waveforms.size();
1754  for (unsigned element_index = 0; element_index < waveform_size; ++element_index) {
1755  double element = 0.;
1756  for (unsigned antenna_index = 0; antenna_index < waveforms_size; ++antenna_index) {
1757  element += waveforms[antenna_index][element_index];
1758  }
1759  output.push_back(element);
1760  }
1761 }
1762 
1763 
1764 
1765 
1767 
1778 void GlobalTrigger::square_waveform_elements(const vector <double>& waveform, vector <double>& output){
1779  output.clear();
1780 
1781  for (unsigned int element_index = 0; element_index < waveform.size(); element_index++){
1782  output.push_back(waveform[element_index] * waveform[element_index]);
1783  }
1784 }
1785 
1786 
1788 
1795 double GlobalTrigger::summed_power_window(const vector <double>& waveform, unsigned int start_index, unsigned int length){
1796  double result = 0;
1797  for (unsigned int index = start_index; index < start_index + length; index++){
1798  result += waveform[index];
1799  }
1800  return result;
1801 }
1802 
1803 
1805 
1812 double GlobalTrigger::three_bit_round(double input, bool round_zero_up, bool allow_zero) {
1813  //
1814  double result = 0.;
1815  if (input < -3.5){
1816  result = -3.5;
1817  }else if (input > 3.5){
1818  result = 3.5;
1819  }else if (floor(input) == input){
1820  if (!allow_zero) {
1821  if (round_zero_up) {
1822  result = input + 0.5;
1823  } else {
1824  result = input + -0.5;
1825  }
1826  } else {
1827  result = input;
1828  }
1829  }else if (floor(input) + 0.5 > input){
1830  result = ceil(input) - 0.5;
1831  }else if (floor(input) + 0.5 < input){
1832  result = floor(input) + 0.5;
1833  }
1834 
1835  return result;
1836 }
1837 
1839 /*
1840  * This function takes a waveform array as a parameter and returns a vector <double> of 3 bit waveforms
1841  */
1842 void GlobalTrigger::convert_wfm_to_3_bit(const vector <double>& wfm, double rms, vector <double>& output){
1843  output.clear();
1844  for (unsigned int index = 0; index < wfm.size(); index++){
1845  output.push_back(three_bit_round(wfm[index]/rms));
1846  }
1847 }
1848 int GlobalTrigger::findahit(vector<int> myvector,int first,int last) {
1849 
1850  int yes=0;
1851  if ((int)myvector.size()<=last)
1852  return yes;
1853 
1854  for (int i=first;i<=last;i++) {
1855  if (myvector[i]==1) {
1856  yes=1;
1857  continue;
1858  }
1859  }
1860  return yes;
1861 }
1862 int GlobalTrigger::findanl3(int *l3,int NPHISECTORS) {
1863 
1864  for (int j=0;j<NPHISECTORS;j++) {
1865  if (l3[j]==1)
1866  return 1;
1867  }
1868  return 0;
1869 }
1870 void GlobalTrigger::L2Anita3and4(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> vl1trig,
1871  std::array<std::array<std::vector<int>,16>,2> &vl2trig) {
1872 
1873 
1874  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
1875  for (int ipolar=0;ipolar<2;ipolar++) {
1876  //cout << "iphi, ipolar, size is " << iphi << "\t" << ipolar << "\t" << vl1trig[ipolar][iphi].size() << "\n";
1877  for (unsigned int ibin=0;ibin<vl1trig[ipolar][iphi].size();ibin++) {
1878  //cout << "ipolar, iphi , ibin are " << ipolar << "\t" << iphi << "\t" << ibin << "\n";
1879  // if (vl1trig[ipolar][iphi][ibin])
1880  // cout << "actually a nonzero l1trig.\n";
1881  vl2trig[ipolar][iphi].push_back(vl1trig[ipolar][iphi][ibin]);
1882  }
1883  }
1884  }
1885 
1886 
1887 
1888 
1889 }
1890 void GlobalTrigger::L3Anita3and4(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> vl2trig,
1891  int vl3trig[2][16],int *thispasses) {
1892 
1893  int iphimod16_neighbor;
1894 
1895  for (int ipolar=0;ipolar<2;ipolar++) {
1896  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
1897 
1898  for (int iphi_neighbor=iphi-1;iphi_neighbor<=iphi+1;iphi_neighbor++) {
1899  if (iphi_neighbor!=iphi) {
1900  iphimod16_neighbor=(iphi_neighbor+16)%16;
1901  //cout << "L3_COINCIDENCE is " << L3_COINCIDENCE << "\n";
1902  for (unsigned int ibin=0;ibin<vl2trig[ipolar][iphi].size();ibin++) {
1903  if (ibin+(int)(L3_COINCIDENCE/TRIGTIMESTEP)<vl2trig[ipolar][iphimod16_neighbor].size()) {
1904  if (vl2trig[ipolar][iphi][ibin] &&
1905  findahit(vl2trig[ipolar][iphimod16_neighbor],ibin,ibin+(int)(L3_COINCIDENCE/TRIGTIMESTEP))) {
1906  //cout << "passes: " << ipolar << "\t" << iphi << "\t" << ibin << "\n";
1907  //cout << "neighbor: " << ipolar << "\t" << iphimod16_neighbor << "\n";
1908  vl3trig[ipolar][iphi]=1;
1909  thispasses[ipolar]=1;
1910  ibin = vl2trig[ipolar][iphi].size(); // ends this loop
1911  iphi_neighbor=iphi+2; // ends outer loop
1912  }
1913  else
1914  vl3trig[ipolar][iphi]=0;
1915 
1916  } // end if so we're not going off the end of vl2trig
1917  else
1918  vl3trig[ipolar][iphi]=0;
1919  }
1920  }
1921  }
1922  }
1923  }
1924 
1925 }
1926 
1927 // L1 trigger is at the antenna level again. Just require coincidence between LCP and RCP
1928 void GlobalTrigger::L1Anita4LR_ScB_OneBin(int IZERO,vector<int> vleft,vector<int> vright,
1929  vector<int> &vl1trig) {
1930 
1931  if ((vleft[IZERO] && findahit(vright,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP))) ||
1932 
1933  (vright[IZERO] && findahit(vleft,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP)))) {
1934  vl1trig.push_back(1);
1935 
1936  }
1937  else
1938  vl1trig.push_back(0);
1939 
1940 
1941 }
1942 // L1 trigger is at the antenna level again. Just require coincidence between LCP and RCP
1943 void GlobalTrigger::L1Anita4LR_ScB_AllAntennas_OneBin(int IZERO,Anita *anita1,std::array< std::array< vector<int>,16>,3> &vl1trig_anita4lr_scb,int &npassesl1) {
1944 
1945 
1946  for (int itriglayer=0;itriglayer<anita1->NTRIGGERLAYERS;itriglayer++) {
1947  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
1948 
1949  //double time_thisbin=(double)nstepback*TRIGTIMESTEP;
1950  //int itrigbin=nstepback;
1951 
1952  // i should really do this in a different step so this function has fewer inputs.
1953  //while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
1954  L1Anita4LR_ScB_OneBin(IZERO,arrayofhits[itriglayer][iphi][0][4],arrayofhits[itriglayer][iphi][1][4],
1955  vl1trig_anita4lr_scb[itriglayer][iphi]);
1956  if (vl1trig_anita4lr_scb[itriglayer][iphi][vl1trig_anita4lr_scb[itriglayer][iphi].size()-1])
1957  npassesl1++;
1958 
1959  //itrigbin++;
1960  //time_thisbin=(double)itrigbin*TRIGTIMESTEP;
1961  // } // loop over time steps
1962  } // loop over phi sectors
1963  } // loop over trigger layers
1964 
1965 
1966 
1967 }
1968 
1969 
1970 // void GlobalTrigger::L3Anita4LR_ScB(Anita *anita1,std::array<std::array<vector<int>,3>,16> vl2_realtime_anita4_scb,
1971 // std::array<vector<int>,16> &vl3trig_type0, std::array<vector<int>,16> &vl3trig_type1,
1972 // int &thispasses_l3type0,int &thispasses_l3type1) {
1973 // thispasses_l3type0=0;
1974 // thispasses_l3type1=0;
1975 
1976 
1977 // for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
1978 
1979 // vl3trig_type0[iphi].clear();
1980 // vl3trig_type1[iphi].clear();
1981 
1982 // int iphi_next=(iphi+1+16)%16;
1983 // int iphi_previous=(iphi-1+16)%16;
1984 
1985 // if (L3or30Anita4LR_ScB_TwoPhiSectors(
1986 // vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
1987 // vl2_realtime_anita4_scb[iphi_previous], // 3 neighbors, whether 1, 2 or 3 pass
1988 
1989 // 2,2,
1990 // vl3trig_type1[iphi] ))
1991 // thispasses_l3type1=1;
1992 
1993 // if (iphi%2==0) {
1994 // if (L3or30Anita4LR_ScB_TwoPhiSectors(
1995 // vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
1996 // vl2_realtime_anita4_scb[iphi_previous], // 3 neighbors, whether 1, 2 or 3 pass
1997 
1998 // 1,3,
1999 // vl3trig_type0[iphi] ))
2000 // thispasses_l3type0=1;
2001 
2002 
2003 
2004 // }
2005 // }
2006 
2007 
2008 // }
2009 // for each phi sector, does 1, 2 or 3 pass
2010 // void GlobalTrigger::L2Anita4LR_ScB_AllPhiSectors(Anita *anita1,std::array< std::array< vector<int>,16>,3> vl1trig_anita4lr_scb,
2011 // std::array<std::array<vector<int>,3>,16> &vl2_realtime_anita4_scb) {
2012 
2013 // double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2014 // int itrigbin=nstepback;
2015 
2016 // while (time_thisbin<LASTTIMETOTESTL2_ANITA4LR_SCB) {
2017 
2018 // for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2019 
2020 // // cout << "itriglayer, iphi are " << itriglayer << "\t" << iphi << "\n";
2021 // int npassesl2=0;
2022 // int npassesl2type0=0;
2023 
2024 // L2Anita4LR_ScB_OnePhiSector_OneBin(itrigbin,vl1trig_anita4lr_scb[2][iphi],
2025 // vl1trig_anita4lr_scb[1][iphi],
2026 // vl1trig_anita4lr_scb[0][iphi],
2027 // vl2_realtime_anita4_scb[iphi],
2028 // npassesl2,npassesl2type0);
2029 
2030 
2031 // }
2032 
2033 
2034 // itrigbin++;
2035 // time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2036 // }
2037 
2038 
2039 // }
2040 
2041 // for each phi sector, does 1, 2 or 3 pass
2042 void GlobalTrigger::L2Anita4LR_ScB_AllPhiSectors_OneBin(int IZERO,Anita *anita1,std::array< std::array< vector<int>,16>,3> vl1trig_anita4lr_scb,
2043  std::array<std::array<vector<int>,3>,16> &vl2_realtime_anita4_scb,int &npassesl2,int &npassesl2_type0) {
2044 
2045  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2046 
2047  // cout << "itriglayer, iphi are " << itriglayer << "\t" << iphi << "\n";
2048 
2049  L2Anita4LR_ScB_OnePhiSector_OneBin(IZERO,vl1trig_anita4lr_scb[2][iphi],
2050  vl1trig_anita4lr_scb[1][iphi],
2051  vl1trig_anita4lr_scb[0][iphi],
2052  vl2_realtime_anita4_scb[iphi],npassesl2,npassesl2_type0);
2053 
2054 
2055  }
2056 
2057 
2058 
2059 
2060 }
2061 
2062 // ask if L3 type 1 (2 and 2) or L3 type 0 (3 and 1 or 1 and 3) passes
2063 // int GlobalTrigger::L3or30Anita4LR_ScB_TwoPhiSectors(
2064 // std::array<vector<int>,3> vl2_realtime_anita4_scb, // 3 neighbors, whether 1, 2 or 3 pass
2065 // std::array<vector<int>,3> vl2_realtime_anita4_scb_other, // 3 neighbors, whether 1, 2 or 3 pass
2066 // int npass1,int npass2,
2067 // vector<int> &vl3trig ) {
2068 
2069 
2070 
2071 // int passaone=0;
2072 
2073 
2074 
2075 // int thispasses=0;
2076 
2077 
2078 // if (L3or30Anita4LR_ScB_TwoPhiSectors_OneBin(itrigbin,
2079 // vl2_realtime_anita4_scb, // 3 neighbors, whether 1, 2 or 3 pass
2080 // vl2_realtime_anita4_scb_other, // 3 neighbors, whether 1, 2 or 3 pass
2081 // npass1,npass2,
2082 // vl3trig )) {
2083 
2084 
2085 // thispasses=1;
2086 // passaone=1;
2087 // //cout << "got an l3 with " << npass1 << " and " << npass2 << "\n";
2088 // }
2089 // vl3trig.push_back(thispasses);
2090 
2091 // return passaone;
2092 
2093 // }
2094 // ask if L3 type 1 (2 and 2) or L3 type 0 (3 and 1 or 1 and 3) passes
2095 // int GlobalTrigger::L3or30Anita4LR_ScB_TwoPhiSectors_OneBin( int IZERO,
2096 // std::array<vector<int>,3> vl2_realtime_anita4_scb, // 3 neighbors, whether 1, 2 or 3 pass
2097 // std::array<vector<int>,3> vl2_realtime_anita4_scb_other, // 3 neighbors, whether 1, 2 or 3 pass
2098 // int npass1,int npass2,
2099 // vector<int> &vl3trig ) {
2100 
2101 
2102 
2103 // int passaone=0;
2104 // //double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2105 // //int itrigbin=nstepback;
2106 // int thispasses=0;
2107 // //while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
2108 // thispasses=0;
2109 
2110 
2111 // if ((vl2_realtime_anita4_scb[npass1-1][IZERO] &&
2112 // findahit(vl2_realtime_anita4_scb_other[npass2-1],IZERO-nstepback,IZERO-nstepback+(int)(L3_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP))) ||
2113 // (vl2_realtime_anita4_scb_other[npass2-1][IZERO] &&
2114 // findahit(vl2_realtime_anita4_scb[npass1-1],IZERO-nstepback,IZERO-nstepback+(int)(L3_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP)))) {
2115 // thispasses=1;
2116 // passaone=1;
2117 // //cout << "got an l3 with " << npass1 << " and " << npass2 << "\n";
2118 // }
2119 // // cout << "adding to vl3trig.\n";
2120 // vl3trig.push_back(thispasses);
2121 
2122 // //itrigbin++;
2123 // //time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2124 // //}
2125 // return passaone;
2126 
2127 // }
2128 
2129 void GlobalTrigger::L3Anita4LR_ScA(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> vl2trig,
2130  int *thispasses) {
2131 
2132 
2133  for (int ipolar=0;ipolar<2;ipolar++) {
2134  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2135 
2136 
2137 
2138  for (unsigned int ibin=0;ibin<vl2trig[ipolar][iphi].size();ibin++) {
2139 
2140  if (vl2trig[ipolar][iphi][ibin]) {
2141 
2142  thispasses[ipolar]=1;
2143  }
2144  }
2145 
2146 
2147  }
2148 
2149  }
2150 
2151 }
2152 void GlobalTrigger::L1Anita3_AllPhiSectors(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> &vl1trig) {
2153  vector<int> vl1_realtime_vbottom;
2154  vector<int> vl1_realtime_vmiddle;
2155  vector<int> vl1_realtime_vtop;
2156  vector<int> vl1_realtime_hbottom;
2157  vector<int> vl1_realtime_hmiddle;
2158  vector<int> vl1_realtime_htop;
2159  double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2160  int itrigbin=nstepback;
2161  // cout << "phitrig is " << anita1->PHITRIG[0] << "\n";
2162  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2163  itrigbin=nstepback;
2164  time_thisbin=(double)nstepback*TRIGTIMESTEP;
2165  while (time_thisbin<LASTTIMETOTESTL1_ANITA3) {
2166 
2167  vl1trig[0][iphi].push_back(L1Anita3_OnePhiSector(itrigbin,arrayofhits[2][iphi][0][4],
2168  arrayofhits[1][iphi][0][4],
2169  arrayofhits[0][iphi][0][4],
2170  vl1_realtime_vbottom, vl1_realtime_vmiddle, vl1_realtime_vtop));
2171  vl1trig[1][iphi].push_back(L1Anita3_OnePhiSector(itrigbin,arrayofhits[2][iphi][1][4],
2172  arrayofhits[1][iphi][1][4],
2173  arrayofhits[0][iphi][1][4],
2174  vl1_realtime_hbottom, vl1_realtime_hmiddle, vl1_realtime_htop));
2175  itrigbin++;
2176  // if (vl1trig[0][iphi][vl1trig[0][iphi].size()-1]==1) {
2177  // cout << "an l1.\n";
2178  // cout << "iphi, bin is " << iphi << "\t" << vl1trig[0][iphi].size()-1 << "\n";
2179  // }
2180  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2181  }
2182  }
2183  // cout << "inside, vl1trig is " << vl1trig[0][0].size() << "\n";
2184 }
2185 void GlobalTrigger::L1Anita4_AllPhiSectors(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> &vl1trig) {
2186  vector<int> vl1_realtime_vbottom;
2187  vector<int> vl1_realtime_vmiddle;
2188  vector<int> vl1_realtime_vtop;
2189  vector<int> vl1_realtime_hbottom;
2190  vector<int> vl1_realtime_hmiddle;
2191  vector<int> vl1_realtime_htop;
2192  double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2193  int itrigbin=nstepback;
2194  // cout << "phitrig is " << anita1->PHITRIG[0] << "\n";
2195  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2196  itrigbin=nstepback;
2197  time_thisbin=(double)nstepback*TRIGTIMESTEP;
2198  while (time_thisbin<LASTTIMETOTESTL1_ANITA4) {
2199  //cout << "iphi is " << iphi << "\n";
2200  vl1trig[0][iphi].push_back(L1Anita4_OnePhiSector(itrigbin,arrayofhits[2][iphi][0][4], arrayofhits[1][iphi][0][4], arrayofhits[0][iphi][0][4],
2201  vl1_realtime_vbottom, vl1_realtime_vmiddle, vl1_realtime_vtop));
2202  vl1trig[1][iphi].push_back(L1Anita4_OnePhiSector(itrigbin,arrayofhits[2][iphi][1][4], arrayofhits[1][iphi][1][4], arrayofhits[0][iphi][1][4],
2203  vl1_realtime_hbottom, vl1_realtime_hmiddle, vl1_realtime_htop));
2204 
2205  // if (vl1trig[0][iphi][vl1trig[0][iphi].size()-1]==1) {
2206  // cout << "an l1.\n";
2207  // cout << "iphi, bin is " << iphi << "\t" << vl1trig[0][iphi].size()-1 << "\n";
2208  // cout << "arrayofhits are \n";
2209  // int istart=itrigbin-nstepback;
2210  // int iend=itrigbin-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[0][1]/TRIGTIMESTEP)-1;
2211  // cout << "istart, iend are " << istart << "\t" << iend << "\n";
2212  // cout << "sizes are " << arrayofhits[0][iphi][0][4].size() << "\t" << arrayofhits[1][iphi][0][4].size() << "\t" << arrayofhits[2][iphi][0][4].size() << "\n";
2213  // cout << "top:\n";
2214  // for (int i=istart;i<=iend;i++) {
2215  // cout << arrayofhits[0][iphi][0][4][i] << "\t";
2216  // }
2217  // cout << "middle:\n";
2218  // for (int i=istart;i<=iend;i++) {
2219  // cout << arrayofhits[1][iphi][0][4][i] << "\t";
2220  // }
2221  // cout << "bottom:\n";
2222  // for (int i=istart;i<=iend;i++) {
2223  // cout << arrayofhits[2][iphi][0][4][i] << "\t";
2224  // }
2225  // cout << "\n";
2226 
2227  // }
2228  itrigbin++;
2229  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2230  }
2231  }
2232  // cout << "inside, vl1trig is " << vl1trig[0][0].size() << "\n";
2233 }
2234 void GlobalTrigger::L1Anita4LR_ScA_AllPhiSectors(Anita *anita1,std::array<std::array<std::vector<int>,16>,2> &vl1trig) {
2235  vector<int> vl1_realtime_1bottom;
2236  vector<int> vl1_realtime_1middle;
2237  vector<int> vl1_realtime_1top;
2238  vector<int> vl1_realtime_2bottom;
2239  vector<int> vl1_realtime_2middle;
2240  vector<int> vl1_realtime_2top;
2241 
2242  double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2243  int itrigbin=nstepback;
2244  int taketheor=0; // Take the "or" of the L1 with a phi sector and its neighbor to the left, and the L1 with a phi sector and its
2245  // neighbor to the right.
2246  // cout << "phitrig is " << anita1->PHITRIG[0] << "\n";
2247  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2248  itrigbin=nstepback;
2249  time_thisbin=(double)nstepback*TRIGTIMESTEP;
2250  taketheor=0;
2251  int iphi_rightneighbor=(iphi+1+16)%16;
2252  int iphi_leftneighbor=(iphi-1+16)%16;
2253  while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCA) {
2254 
2255  for (int ipolar=0;ipolar<2;ipolar++) {
2256 
2257  if (L1Anita4LR_ScA_TwoPhiSectors(itrigbin,ipolar,
2258  arrayofhits[2][iphi][0][4],arrayofhits[2][iphi][1][4],
2259  arrayofhits[2][iphi_rightneighbor][0][4],arrayofhits[2][iphi_rightneighbor][1][4],
2260  arrayofhits[1][iphi][0][4], arrayofhits[1][iphi][1][4],
2261  arrayofhits[1][iphi_rightneighbor][0][4], arrayofhits[1][iphi_rightneighbor][1][4],
2262  arrayofhits[0][iphi][0][4], arrayofhits[0][iphi][1][4],
2263  arrayofhits[0][iphi_rightneighbor][0][4],arrayofhits[0][iphi_rightneighbor][1][4],
2264  vl1_realtime_1bottom, vl1_realtime_1middle, vl1_realtime_1top) ||
2265 
2266  L1Anita4LR_ScA_TwoPhiSectors(itrigbin,ipolar,
2267  arrayofhits[2][iphi][0][4],arrayofhits[2][iphi][1][4],
2268  arrayofhits[2][iphi_leftneighbor][0][4],arrayofhits[2][iphi_leftneighbor][1][4],
2269  arrayofhits[1][iphi][0][4], arrayofhits[1][iphi][1][4],
2270  arrayofhits[1][iphi_leftneighbor][0][4], arrayofhits[1][iphi_leftneighbor][1][4],
2271  arrayofhits[0][iphi][0][4], arrayofhits[0][iphi][1][4],
2272  arrayofhits[0][iphi_leftneighbor][0][4],arrayofhits[0][iphi_leftneighbor][1][4],
2273  vl1_realtime_2bottom, vl1_realtime_2middle, vl1_realtime_2top))
2274  vl1trig[ipolar][iphi].push_back(1);
2275  else
2276  vl1trig[ipolar][iphi].push_back(0);
2277 
2278 
2279  // if (iphi==13) {
2280  //cout << "itrigbin is " << itrigbin << "\n";
2281  // cout << "first one is " << L1Anita4LR_ScA_TwoPhiSectors(iphi,itrigbin,ipolar,
2282  // arrayofhits[2][iphi][0][4],arrayofhits[2][iphi][1][4],
2283  // arrayofhits[2][iphi_rightneighbor][0][4],arrayofhits[2][iphi_rightneighbor][1][4],
2284  // arrayofhits[1][iphi][0][4], arrayofhits[1][iphi][1][4],
2285  // arrayofhits[1][iphi_rightneighbor][0][4], arrayofhits[1][iphi_rightneighbor][1][4],
2286  // arrayofhits[0][iphi][0][4], arrayofhits[0][iphi][1][4],
2287  // arrayofhits[0][iphi_rightneighbor][0][4],arrayofhits[0][iphi_rightneighbor][1][4],
2288  // vl1_realtime_1bottom, vl1_realtime_1middle, vl1_realtime_1top) << "\n";
2289  // cout << "second one is " << L1Anita4LR_ScA_TwoPhiSectors(iphi,itrigbin,ipolar,
2290  // arrayofhits[2][iphi][0][4],arrayofhits[2][iphi][1][4],
2291  // arrayofhits[2][iphi_leftneighbor][0][4],arrayofhits[2][iphi_leftneighbor][1][4],
2292  // arrayofhits[1][iphi][0][4], arrayofhits[1][iphi][1][4],
2293  // arrayofhits[1][iphi_leftneighbor][0][4], arrayofhits[1][iphi_leftneighbor][1][4],
2294  // arrayofhits[0][iphi][0][4], arrayofhits[0][iphi][1][4],
2295  // arrayofhits[0][iphi_leftneighbor][0][4],arrayofhits[0][iphi_leftneighbor][1][4],
2296  // vl1_realtime_2bottom, vl1_realtime_2middle, vl1_realtime_2top) << "\n";
2297 
2298  //cout << "ipolar, iphi, taketheor are " << ipolar << "\t" << iphi << "\t" << vl1trig[ipolar][iphi][itrigbin] << "\n";
2299 
2300  // }
2301 
2302  }
2303 
2304  itrigbin++;
2305  time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2306  }
2307  }
2308  // cout << "inside, vl1trig is " << vl1trig[0][0].size() << "\n";
2309 }
2310 
2311 
2312 int GlobalTrigger::L1Anita3_OnePhiSector(int IZERO,vector<int> &vl0_realtime_bottom, vector<int> &vl0_realtime_middle, vector<int> &vl0_realtime_top,
2313  vector<int> &vl1_realtime_bottom, vector<int> &vl1_realtime_middle, vector<int> &vl1_realtime_top) {
2314 
2315 
2316 
2317  // ask if L1 trigger passes
2318  // Patrick says "check every 2 ns for 4 ns back and start 4 ns gate, or 12 ns, or 16 ns."
2319  if (vl0_realtime_bottom[IZERO]==1) {
2320 
2321  //cout << "findahit from " << IZERO-nstepback << " to " << IZERO-nstepback+(int)(L1_COINCIDENCE[0]/TRIGTIMESTEP)-1 << "\n";
2322  if (findahit(vl0_realtime_top,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[0]/TRIGTIMESTEP)) ||
2323  findahit(vl0_realtime_middle,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[0]/TRIGTIMESTEP))) {
2324 
2325 
2326  vl1_realtime_bottom.push_back(1);
2327  // cout << "got an l1 here. vl1_realtime_bottom.size() is " << vl1_realtime_bottom.size() << "\n";
2328 
2329  }
2330  else
2331  vl1_realtime_bottom.push_back(0);
2332 
2333  }
2334  else {
2335  vl1_realtime_bottom.push_back(0);
2336 
2337  }
2338 
2339  if (vl0_realtime_middle[IZERO]==1) {
2340  if (findahit(vl0_realtime_top,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[1]/TRIGTIMESTEP)) ||
2341  findahit(vl0_realtime_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[1]/TRIGTIMESTEP))) {
2342 
2343  vl1_realtime_middle.push_back(1);
2344 
2345  }
2346  else
2347  vl1_realtime_middle.push_back(0);
2348 
2349  }
2350  else {
2351  vl1_realtime_middle.push_back(0);
2352 
2353  }
2354  // if (vl1_realtime_bottom[vl1_realtime_bottom.size()-1])
2355  //cout << "the bottom bit is " << vl1_realtime_bottom[vl1_realtime_bottom.size()-1] << "\n";
2356  if (vl0_realtime_top[IZERO]==1) {
2357  if (findahit(vl0_realtime_middle,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[2]/TRIGTIMESTEP)) ||
2358  findahit(vl0_realtime_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_ANITA3[2]/TRIGTIMESTEP))) {
2359 
2360  vl1_realtime_top.push_back(1);
2361 
2362  }
2363  else
2364  vl1_realtime_top.push_back(0);
2365 
2366  }
2367  else {
2368  vl1_realtime_top.push_back(0);
2369 
2370  }
2371 
2372  if (vl1_realtime_bottom[vl1_realtime_bottom.size()-1]==1 || vl1_realtime_top[vl1_realtime_top.size()-1]==1 || vl1_realtime_middle[vl1_realtime_middle.size()-1]==1) {
2373  //cout << "actually got an l1.\n";
2374  return 1;
2375 
2376  }
2377  else return 0;
2378 }
2379 int GlobalTrigger::L1Anita4_OnePhiSector(int IZERO,vector<int> &vl0_realtime_bottom, vector<int> &vl0_realtime_middle, vector<int> &vl0_realtime_top,
2380  vector<int> &vl1_realtime_bottom, vector<int> &vl1_realtime_middle, vector<int> &vl1_realtime_top) {
2381 
2382 
2383 
2384  // ask if L1 trigger passes
2385  // Patrick says "check every 2 ns for 4 ns back and start 4 ns gate, or 12 ns, or 16 ns."
2386  if (vl0_realtime_bottom[IZERO]==1) {
2387  //cout << "findahit from " << IZERO-nstepback << " to " << IZERO-nstepback+(int)(L1_COINCIDENCE[0]/TRIGTIMESTEP)-1 << "\n";
2388  if (findahit(vl0_realtime_top,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[0][0]/TRIGTIMESTEP)) ||
2389  findahit(vl0_realtime_middle,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[0][1]/TRIGTIMESTEP))) {
2390 
2391  vl1_realtime_bottom.push_back(1);
2392 
2393  }
2394  else
2395  vl1_realtime_bottom.push_back(0);
2396 
2397  }
2398  else {
2399  vl1_realtime_bottom.push_back(0);
2400 
2401  }
2402 
2403  if (vl0_realtime_middle[IZERO]==1) {
2404  if (findahit(vl0_realtime_top,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[1][0]/TRIGTIMESTEP)) ||
2405  findahit(vl0_realtime_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[1][1]/TRIGTIMESTEP))) {
2406 
2407  vl1_realtime_middle.push_back(1);
2408 
2409  }
2410  else
2411  vl1_realtime_middle.push_back(0);
2412 
2413  }
2414  else {
2415  vl1_realtime_middle.push_back(0);
2416 
2417  }
2418 
2419  if (vl0_realtime_top[IZERO]==1) {
2420  if (findahit(vl0_realtime_middle,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[2][0]/TRIGTIMESTEP)) ||
2421  findahit(vl0_realtime_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_MOREGENERAL[2][1]/TRIGTIMESTEP))) {
2422 
2423  vl1_realtime_top.push_back(1);
2424 
2425  }
2426  else
2427  vl1_realtime_top.push_back(0);
2428 
2429  }
2430  else {
2431  vl1_realtime_top.push_back(0);
2432 
2433  }
2434 
2435  if (vl1_realtime_bottom[vl1_realtime_bottom.size()-1]==1 || vl1_realtime_top[vl1_realtime_top.size()-1]==1 || vl1_realtime_middle[vl1_realtime_middle.size()-1]==1)
2436  return 1;
2437  else return 0;
2438 }
2439 int GlobalTrigger::L1Anita4LR_ScA_TwoPhiSectors(int IZERO,int ipolar,
2440  vector<int> &v1l0_realtime_bottomleft, vector<int> &v2l0_realtime_bottomleft,
2441  vector<int> &v1l0_realtime_bottomright, vector<int> &v2l0_realtime_bottomright,
2442  vector<int> &v1l0_realtime_middleleft, vector<int> &v2l0_realtime_middleleft,
2443  vector<int> &v1l0_realtime_middleright, vector<int> &v2l0_realtime_middleright,
2444  vector<int> &v1l0_realtime_topleft, vector<int> &v2l0_realtime_topleft,
2445  vector<int> &v1l0_realtime_topright, vector<int> &v2l0_realtime_topright,
2446  vector<int> &vl1_realtime_bottom,
2447  vector<int> &vl1_realtime_middle,
2448  vector<int> &vl1_realtime_top) {
2449 
2450 
2451  //if (IZERO>=42 && IZERO<=44 && iphi==13)
2452  //cout << "inside _TwoPhiSectors. IZERO, ipolar are " << IZERO << "\t" << ipolar << "\n";
2453 
2454  vector<int> vleft;
2455  vector<int> vright;
2456  if (ipolar==0) {
2457  vleft=v1l0_realtime_bottomleft;
2458  vright=v1l0_realtime_bottomright;
2459  }
2460  else if (ipolar==1) {
2461  vleft=v2l0_realtime_bottomleft;
2462  vright=v2l0_realtime_bottomright;
2463  }
2464 
2465  if (vleft[IZERO]==1 || vright[IZERO]==1) {
2466  // if (IZERO>=40 && IZERO<=44 && iphi==13)
2467  //cout << "got a bottom trigger. iphi, izero, ipolar is " << iphi << "\t" << IZERO << "\t" << ipolar << "\t" << vleft[IZERO] << "\t" << vright[IZERO] << "\n";
2468  if (PartofL1Anita4LR_ScA_TwoPhiSectors(1,ipolar,IZERO,v1l0_realtime_middleleft, v2l0_realtime_middleleft,
2469  v1l0_realtime_middleright, v2l0_realtime_middleright,
2470  vl1_realtime_middle) &&
2471  PartofL1Anita4LR_ScA_TwoPhiSectors(0,ipolar,IZERO,v1l0_realtime_topleft, v2l0_realtime_topleft,
2472  v1l0_realtime_topright, v2l0_realtime_topright,
2473  vl1_realtime_top)) {
2474  //if (IZERO>=40 && IZERO<=44 && iphi==13)
2475  //cout << "other two layers triggered too.\n";
2476  return 1;
2477  }
2478  else {
2479  // if (IZERO>=40 && IZERO<=44 && iphi==13) {
2480  // cout << "middle is " << PartofL1Anita4LR_TwoPhiSectors(iphi,1,ipolar,IZERO,v1l0_realtime_middleleft, v2l0_realtime_middleleft,
2481  // v1l0_realtime_middleright, v2l0_realtime_middleright,
2482  // vl1_realtime_middle) << "\n";
2483  // cout << "top is " << PartofL1Anita4LR_TwoPhiSectors(iphi,0,ipolar,IZERO,v1l0_realtime_topleft, v2l0_realtime_topleft,
2484  // v1l0_realtime_topright, v2l0_realtime_topright,
2485  // vl1_realtime_top) << "\n";
2486  // cout << "other two layers did not trigger.\n";
2487  // }
2488  }
2489  }
2490  return 0;
2491 
2492 
2493 }
2494 
2495 
2496 
2497 int GlobalTrigger::PartofL1Anita4LR_ScA_TwoPhiSectors(int ilayerreverse,int ipolar,int IZERO,
2498  vector<int> &v1l0_realtime_left, vector<int> &v2l0_realtime_left,
2499  vector<int> &v1l0_realtime_right, vector<int> &v2l0_realtime_right,
2500  vector<int> &vl1_realtime) {
2501 
2502  // if (iphi==13)
2503  //cout << "ilayerreverse is " << ilayerreverse << "\n";
2504  if (WHICHLAYERSLCPRCP[ilayerreverse]==0) {
2505 
2506  vector<int> vleft;
2507  vector<int> vright;
2508 
2509  if (ipolar==0) {
2510  vleft=v1l0_realtime_left;
2511  vright=v1l0_realtime_right;
2512  }
2513  else if (ipolar==1) {
2514  vleft=v2l0_realtime_left;
2515  vright=v2l0_realtime_right;
2516 
2517  }
2518 
2519  // if (iphi==13 && IZERO>=40 && IZERO<=44) {
2520  // cout << "start, stop are " << IZERO-nstepback << "\t" << IZERO-nstepback+(int)(L1_COINCIDENCE_LR[ilayerreverse]/TRIGTIMESTEP) << "\n";
2521  // cout << "vleft is ";
2522  // for (int i=IZERO-nstepback ;i<=IZERO-nstepback+(int)(L1_COINCIDENCE_LR[ilayerreverse]/TRIGTIMESTEP);i++) {
2523  // cout << vleft[i] << " ";
2524  // }
2525  // cout << "\n";
2526  // cout << "vright is ";
2527  // for (int i=IZERO-nstepback ;i<=IZERO-nstepback+(int)(L1_COINCIDENCE_LR[ilayerreverse]/TRIGTIMESTEP);i++) {
2528  // cout << vright[i] << " ";
2529  // }
2530  // cout << "\n";
2531  // // cout << vleft[i] << " ";
2532  // // cout << "left findahit is " << findahit(vleft,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR[ilayerreverse]/TRIGTIMESTEP)) << "\n";
2533  // // cout << "right findahit is " << findahit(vleft,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR[ilayerreverse]/TRIGTIMESTEP)) << "\n";
2534  // }
2535  if (findahit(vleft,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP)) ||
2536  findahit(vright,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP))) {
2537 
2538  vl1_realtime.push_back(1);
2539  // if (IZERO>=40 && IZERO<=44)
2540  //cout << "same pol is triggered in " << ipolar << "\n";
2541  }
2542  else
2543  vl1_realtime.push_back(0);
2544  }
2545  else {
2546  if ( (findahit(v1l0_realtime_left,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP)) &&
2547  findahit(v2l0_realtime_left,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP))) ||
2548  (findahit(v1l0_realtime_right,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP)) &&
2549  findahit(v2l0_realtime_right,IZERO-nstepback,IZERO-nstepback+(int)(L1_COINCIDENCE_LR_SCA[ilayerreverse]/TRIGTIMESTEP))) ) {
2550  vl1_realtime.push_back(1);
2551  //if (IZERO>=40 && IZERO<=44)
2552  //cout << "lcp rcp is triggered in " << ipolar << "\n";
2553 
2554  }
2555  else
2556  vl1_realtime.push_back(0);
2557 
2558  }
2559  if (vl1_realtime[vl1_realtime.size()-1]) {
2560  //if (IZERO>=40 && IZERO<=44)
2561  //cout << "IZERO, ilayerreverse are " << IZERO << "\t" << ilayerreverse << " got a 1.\n";
2562  return 1;
2563  }
2564  // if (IZERO>=40 && IZERO<=44)
2565  //cout << "returning zero. IZERO, ilayerreverse are " << IZERO << "\t" << ilayerreverse << "\n";
2566 
2567  return 0;
2568 
2569 }
2570 void GlobalTrigger::L3Anita4LR_ScB_OneBin(int IZERO,Anita *anita1,std::array<std::array<vector<int>,3>,16> vl2_realtime_anita4_scb,
2571  std::array<vector<int>,16> &vl3trig_type0, std::array<vector<int>,16> &vl3trig_type1,
2572  int &thispasses_l3type0,int &thispasses_l3type1) {
2573 
2574 
2575 
2576  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2577 
2578 
2579  int iphi_next=(iphi+1+16)%16;
2580  int iphi_previous=(iphi-1+16)%16;
2581 
2582  // cout << "iphi is " << iphi << "\n";
2583  // cout << "calling the first l3.\n";
2584  if (L3or30Anita4LR_ScB_TwoPhiSectors_OneBin(IZERO,
2585  vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
2586  vl2_realtime_anita4_scb[iphi_previous], // 3 neighbors, whether 1, 2 or 3 pass
2587 
2588  2,2) ||
2589  L3or30Anita4LR_ScB_TwoPhiSectors_OneBin(IZERO,
2590  vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
2591  vl2_realtime_anita4_scb[iphi_next], // 3 neighbors, whether 1, 2 or 3 pass
2592 
2593  2,2)
2594 
2595  ) {
2596 
2597  thispasses_l3type1=1;
2598  vl3trig_type1[iphi].push_back(1);
2599 
2600  }
2601  else
2602  vl3trig_type1[iphi].push_back(0);
2603 
2604  // //cout << "calling the second l3.\n";
2605  // if ((iphi%2==0 && L3or30Anita4LR_ScB_TwoPhiSectors_OneBin( IZERO,
2606  // vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
2607  // vl2_realtime_anita4_scb[iphi_next], // 3 neighbors, whether 1, 2 or 3 pass
2608 
2609  // 1,3)
2610  // ) ||
2611  // (
2612  // iphi%2==1 && L3or30Anita4LR_ScB_TwoPhiSectors_OneBin( IZERO,
2613  // vl2_realtime_anita4_scb[iphi], // 3 neighbors, whether 1, 2 or 3 pass
2614  // vl2_realtime_anita4_scb[iphi_previous], // 3 neighbors, whether 1, 2 or 3 pass
2615 
2616  // 1,3)
2617 
2618  // )) {
2619  // thispasses_l3type0=1;
2620  // vl3trig_type0[iphi].push_back(1);
2621 
2622  // }
2623 
2624 
2625 
2626  // else
2627  // vl3trig_type0[iphi].push_back(0);
2628  }
2629 
2630 }
2631 
2632 
2633 void GlobalTrigger::L2Anita4LR_ScB_OnePhiSector_OneBin(int IZERO,vector<int> vl1_bottom,
2634  vector<int> vl1_middle,
2635  vector<int> vl1_top,
2636  std::array<vector<int>,3> &vl2_realtime_anita4_scb,int &npassesl2,int &npassesl2_type0) {
2637  // keep track of whether you get a coincidence between 1, 2 or 3 antennas in a phi sector with the right windows.
2638 
2639 
2640 
2641  // If any of them pass l1, then the 0th element of the vpartofl2_realtime_anita4_scb array goes to one.
2642  //cout << "doing the one-hits.\n";
2643 
2644 
2645  if (vl1_bottom[IZERO] || vl1_middle[IZERO] || vl1_top[IZERO]) {
2646  vl2_realtime_anita4_scb[0].push_back(1);
2647  //cout << "got a one-hit.\n";
2648  }
2649  else
2650  vl2_realtime_anita4_scb[0].push_back(0);
2651 
2652  //cout << "doing the two-hits.\n";
2653  // If you get a coincidence betw. any two, then the 1st element of the vl2_realtime_anita4_scb array goes to one.
2654  //double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2655  //int itrigbin=nstepback;
2656 
2657  // cout << "sizes are " << vl1_bottom.size() << "\t" << vl1_middle.size() << "\t" << vl1_top.size() << "\n";
2658 
2659 
2660  // while (time_thisbin<LASTTIMETOTESTL2_ANITA4LR_SCB) {
2661 
2662 
2663  if ((vl1_bottom[IZERO] &&
2664  ( findahit(vl1_middle,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[1]/TRIGTIMESTEP)) ||
2665  findahit(vl1_top,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[0]/TRIGTIMESTEP)) )) ||
2666 
2667  (vl1_middle[IZERO] &&
2668  ( findahit(vl1_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[1]/TRIGTIMESTEP)) ||
2669  findahit(vl1_top,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[2]/TRIGTIMESTEP)))) ||
2670 
2671  (vl1_top[IZERO] &&
2672  ( findahit(vl1_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[0]/TRIGTIMESTEP)) ||
2673  findahit(vl1_middle,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[2]/TRIGTIMESTEP))))
2674  ) {
2675  vl2_realtime_anita4_scb[1].push_back(1);
2676  npassesl2++;
2677  // cout << "start, stop bins are " << IZERO-nstepback << "\t" << IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[1]/TRIGTIMESTEP) << "\n";
2678  //cout << "sizes are " << vl1_bottom.size() << "\t" << vl1_middle.size() << "\t" << vl1_top.size() << "\n";
2679  //cout << "got a two-hit.\n";
2680  }
2681  else
2682  vl2_realtime_anita4_scb[1].push_back(0);
2683  //itrigbin++;
2684  //time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2685  //}
2686 
2687  //cout << "doing the three-hits.\n";
2688  // If you get a coincidence betw. all three, then the 2nd element of the vl2_realtime_anita4_scb array goes to one.
2689  //time_thisbin=(double)nstepback*TRIGTIMESTEP;
2690  //IZERO=nstepback;
2691 
2692  // while (time_thisbin<LASTTIMETOTESTL2_ANITA4LR_SCB) {
2693 
2694  if ((vl1_bottom[IZERO] &&
2695  ( findahit(vl1_middle,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[1]/TRIGTIMESTEP)) &&
2696  findahit(vl1_top,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[0]/TRIGTIMESTEP)) )) ||
2697 
2698  (vl1_middle[IZERO] &&
2699  ( findahit(vl1_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[1]/TRIGTIMESTEP)) &&
2700  findahit(vl1_top,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[2]/TRIGTIMESTEP)))) ||
2701 
2702  (vl1_top[IZERO] &&
2703  ( findahit(vl1_bottom,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[0]/TRIGTIMESTEP)) &&
2704  findahit(vl1_middle,IZERO-nstepback,IZERO-nstepback+(int)(L2_COINCIDENCE_ANITA4LR_SCB[2]/TRIGTIMESTEP))))
2705  ) {
2706  vl2_realtime_anita4_scb[2].push_back(1);
2707  npassesl2_type0++;
2708  }
2709  else
2710  vl2_realtime_anita4_scb[2].push_back(0);
2711  //itrigbin++;
2712  //time_thisbin=(double)itrigbin*TRIGTIMESTEP;
2713  //}
2714 }
2715 // ask if L3 type 1 (2 and 2) or L3 type 0 (3 and 1 or 1 and 3) passes
2716 int GlobalTrigger::L3or30Anita4LR_ScB_TwoPhiSectors_OneBin( int IZERO,
2717  std::array<vector<int>,3> vl2_realtime_anita4_scb, // 3 neighbors, whether 1, 2 or 3 pass
2718  std::array<vector<int>,3> vl2_realtime_anita4_scb_other, // 3 neighbors, whether 1, 2 or 3 pass
2719  int npass1,int npass2) {
2720 
2721 
2722 
2723  int passaone=0;
2724  //double time_thisbin=(double)nstepback*TRIGTIMESTEP;
2725  //int itrigbin=nstepback;
2726  int thispasses=0;
2727  //while (time_thisbin<LASTTIMETOTESTL1_ANITA4LR_SCB) {
2728  thispasses=0;
2729 
2730 
2731  if ((vl2_realtime_anita4_scb[npass1-1][IZERO] &&
2732  findahit(vl2_realtime_anita4_scb_other[npass2-1],IZERO-nstepback,IZERO-nstepback+(int)(L3_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP))) ||
2733  (vl2_realtime_anita4_scb_other[npass2-1][IZERO] &&
2734  findahit(vl2_realtime_anita4_scb[npass1-1],IZERO-nstepback,IZERO-nstepback+(int)(L3_COINCIDENCE_ANITA4LR_SCB/TRIGTIMESTEP)))) {
2735  thispasses=1;
2736  passaone=1;
2737  //cout << "got an l3 with " << npass1 << " and " << npass2 << "\n";
2738  }
2739 
2740  return passaone;
2741 
2742 }
2743 
2744 void GlobalTrigger::delayL0(vector<int> &vl0,double delay) {
2745  int ndelay=(int)(delay/TRIGTIMESTEP);
2746  for (int i=0;i<ndelay;i++) {
2747  vl0.insert(vl0.begin(),0);
2748  vl0.erase(vl0.end()-1);
2749  }
2750 
2751 }
2752 void GlobalTrigger::delay_AllAntennas(Anita *anita1) {
2753  for (int itriglayer=0;itriglayer<anita1->NTRIGGERLAYERS;itriglayer++) {
2754  for (int iphi=0;iphi<anita1->PHITRIG[0];iphi++) {
2755  for (int ipolar=0;ipolar<anita1->NPOL;ipolar++) {
2756 
2757 
2758  delayL0(arrayofhits[itriglayer][iphi][ipolar][4],DELAYS[itriglayer]);
2759  }
2760  }
2761  }
2762 
2763 
2764 
2765 }
2766 
2767 
void PassesTrigger(Settings *settings1, Anita *anita1, int discones_passing, int mode, int *l3trig, int l2trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX], int l1trig[Anita::NPOL][Anita::NTRIGGERLAYERS_MAX], int antennaclump, int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX], int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX], int inu, int *thispasses, bool noiseOnly=false)
Evaluate the full trigger simulation for a given payload configuration.
void L3Trigger(Settings *settings1, Anita *anita1, int loctrig[Anita::NPOL][Anita::NLAYERS_MAX][Anita::NPHI_MAX], int loctrig_nadironly[Anita::NPOL][Anita::NPHI_MAX], int discones_passing, int *l3trigy, int *thispasses)
Level 3 Trigger Function.
double timedomain_output_corrected_forplotting[2][6][HALFNFOUR]
this is just for writing out to the following tree
Definition: anita.hh:213
double rms_rfcm_e_single_event
This is in Volts, not mV!
Definition: anita.hh:261
static const int NPOL
number of polarizations
Definition: anita.hh:51
int GetRxTriggerNumbering(int ilayer, int ifold)
get antenna number based on which layer and position it is
Definition: anita.cc:208
void square_waveform_elements(const vector< double > &waveform, vector< double > &output)
Performs an element-wise squaring of the values of the input array.
Reads in and stores input settings for the run.
Definition: Settings.h:35
void GetAnitaLayerPhiSector(Settings *settings1, int i, int j, int &whichlayer, int &whichphisector)
Provides a mapping between the 4 layers and 16 phi sectors physically to the 3 layers and 16 sectors ...
int NBANDS
number of frequency sub-bands (not counting full band)
Definition: anita.hh:122
int iminbin[5]
this is the minimum bin to start
Definition: anita.hh:225
Vector ANTENNA_POSITION_START[2][NLAYERS_MAX][NPHI_MAX]
antenna positions from Kurt&#39;s measurements
Definition: anita.hh:62
void sum_aligned_waveforms(const vector< vector< double > > &waveforms, vector< double > &output)
Given a number of waveforms (usually 3) which are delay-aligned, it sums them.
double summed_power_window(const vector< double > &waveform, unsigned int start_index, unsigned int length)
Sum a window from the specified starting index.
void convert_wfm_to_3_bit(const vector< double > &wfm, double rms, vector< double > &output)
Converts a waveform by sampling it into 3 bits, after it is normalized to the RMS.
Contains everything about positions within payload and signals it sees for each event, in both the trigger and signal paths.
Definition: anita.hh:32
static const int NLAYERS_MAX
max number of layers (in smex design, it&#39;s 4)
Definition: anita.hh:59
int GetPhiSector(Settings *settings1, int i, int j)
Provides a mapping between the 4 layers and 16 phi sectors physically to the 3 layers and 16 sectors ...
double timedomain_output_allantennas[2][48][HALFNFOUR]
this is across all antennas, just the full band
Definition: anita.hh:216
void FillInNadir(Anita *anita1, int *ant)
Virtual nadir antennas had effective L2 triggers based on neighbor antennas.
void delay_align_antenna_waveforms(const vector< vector< vector< double > > > &waveforms, const vector< vector< unsigned int > > &delays, vector< vector< double > > &output)
Calculate the difference in each arrival time due to propagation delay of the signal.
int l2trig_anita4lr_forgaryanderic[16][HALFNFOUR]
when it passes 2/3
Definition: anita.hh:202
double three_bit_round(double input, bool round_zero_up=true, bool allow_zero=false)
Three bit rounding function.
static const int NPHI_MAX
max number of antennas around in phi (in smex, 16)
Definition: anita.hh:61
int NRX_PHI[NLAYERS_MAX]
number of antennas around in each layer. (radians)
Definition: anita.hh:69