Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_cdc_emu.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_NSJ_cdc_m.cc
4 // Created: Thu Apr 21 10:38:27 EDT 2016
5 // Creator: njarvis (on Linux egbert 2.6.32-573.3.1.el6.x86_64 x86_64)
6 //
7 
8 // read in thresholds from RunLog configuration files, filenames are hardcoded except for the run number. TH and TL are hardcoded
9 
10 // CDC:RUN_CONFIG specifies run number to use parameter files from, defaults to current run
11 
12 // const int DIFFS_ONLY only write out events with differing fa125 output & emulation if this =1
13 
14 // writes out reported and emulated fa125 quantities to tree_m.root
15 // the events appear out of order if run multithreaded
16 
17 // uses fa125fns.h NOT new emulation in Df125EmulatorAlgorithm_v2
18 // (these should produce identical results)
19 
20 
22 
23 
24 
25 //static TTree *cdctree = NULL;
26 
27 static Int_t roc25h[19][72];
28 static Int_t roc26h[19][72];
29 static Int_t roc27h[19][72];
30 static Int_t roc28h[19][72];
31 
32 
33 
34 extern "C"{
35 void InitPlugin(JApplication *app){
36  InitJANAPlugin(app);
37  app->AddProcessor(new JEventProcessor_cdc_emu());
38 }
39 } // "C"
40 
41 //define static local variable //declared in header file
43 
44 //------------------
45 // JEventProcessor_cdc_emu (Constructor)
46 //------------------
48 {
49 
50 }
51 
52 //------------------
53 // ~JEventProcessor_cdc_emu (Destructor)
54 //------------------
56 {
57 
58 }
59 
60 //------------------
61 // init
62 //------------------
64 {
65 
66 
67  RUN_CONFIG = 0;
68  gPARMS->SetDefaultParameter("CDC:RUN_CONFIG",RUN_CONFIG,"Run to be used as source of configuration files");
69 
70  DIFFS_ONLY = 0;
71  gPARMS->SetDefaultParameter("CDC:DIFFS_ONLY",DIFFS_ONLY,"Record (0) all event or (1) only those events where difference are found");
72 
73 
74  //TTREE INTERFACE
75  //MUST DELETE WHEN FINISHED: OR ELSE DATA WON'T BE SAVED!!!
77 
78  //TTREE BRANCHES
79  DTreeBranchRegister locTreeBranchRegister;
80 
81  locTreeBranchRegister.Register_Single<ULong64_t>("eventnum");
82  locTreeBranchRegister.Register_Single<UInt_t>("rocid"); //uint32_t
83  locTreeBranchRegister.Register_Single<UInt_t>("slot");
84  locTreeBranchRegister.Register_Single<UInt_t>("channel");
85  locTreeBranchRegister.Register_Single<UInt_t>("nsamples_pedestal");
86  locTreeBranchRegister.Register_Single<UInt_t>("nsamples_integral");
87 
88  locTreeBranchRegister.Register_Single<Int_t>("ring");
89  locTreeBranchRegister.Register_Single<Int_t>("straw");
90  locTreeBranchRegister.Register_Single<Int_t>("n");
91  locTreeBranchRegister.Register_Single<UInt_t>("time");
92  locTreeBranchRegister.Register_Single<UInt_t>("q");
93  locTreeBranchRegister.Register_Single<UInt_t>("pedestal");
94  locTreeBranchRegister.Register_Single<UInt_t>("integral");
95  locTreeBranchRegister.Register_Single<UInt_t>("amp");
96  locTreeBranchRegister.Register_Single<UInt_t>("overflows");
97 
98  locTreeBranchRegister.Register_Single<Bool_t>("emulated");
99  locTreeBranchRegister.Register_Single<Bool_t>("m_hitfound");
100 
101  locTreeBranchRegister.Register_Single<UInt_t>("m_hitsample");
102  locTreeBranchRegister.Register_Single<UInt_t>("m_time");
103  locTreeBranchRegister.Register_Single<UInt_t>("m_q");
104  locTreeBranchRegister.Register_Single<UInt_t>("m_pedestal");
105  locTreeBranchRegister.Register_Single<UInt_t>("m_integral");
106  locTreeBranchRegister.Register_Single<UInt_t>("m_amp");
107  locTreeBranchRegister.Register_Single<UInt_t>("m_overflows");
108 
109  locTreeBranchRegister.Register_Single<UInt_t>("m_initped");
110  locTreeBranchRegister.Register_Single<UInt_t>("m_net_integral");
111 
112  locTreeBranchRegister.Register_Single<UInt_t>("s_pedestal");
113  locTreeBranchRegister.Register_Single<UInt_t>("s_integral");
114  locTreeBranchRegister.Register_Single<UInt_t>("s_amp");
115 
116  locTreeBranchRegister.Register_Single<UInt_t>("m_H");
117  locTreeBranchRegister.Register_Single<UInt_t>("m_TH");
118  locTreeBranchRegister.Register_Single<UInt_t>("m_TL");
119 
120  locTreeBranchRegister.Register_Single<UInt_t>("diffs");
121 
122  locTreeBranchRegister.Register_Single<Int_t>("d_time");
123  locTreeBranchRegister.Register_Single<Int_t>("d_q");
124  locTreeBranchRegister.Register_Single<Int_t>("d_pedestal");
125  locTreeBranchRegister.Register_Single<Int_t>("d_integral");
126  locTreeBranchRegister.Register_Single<Int_t>("d_amp");
127  locTreeBranchRegister.Register_Single<Int_t>("d_overflows");
128 
129  locTreeBranchRegister.Register_Single<UInt_t>("NSAMPLES");
130  locTreeBranchRegister.Register_FundamentalArray<UInt_t>("adc", "NSAMPLES");
131 
132  /*
133  //SEARCH
134  locTreeBranchRegister.Register_Single<UChar_t>("NumMatchingSCHits");
135  locTreeBranchRegister.Register_FundamentalArray<Float_t>("TrackHitDeltaT", "NumMatchingSCHits"); //is signed: SC - Track
136 
137  */
138 
139 
140  //REGISTER BRANCHES
141  dTreeInterface->Create_Branches(locTreeBranchRegister);
142 
143 
144 
145  return NOERROR;
146 
147 
148 }
149 
150 //------------------
151 // brun
152 //------------------
153 jerror_t JEventProcessor_cdc_emu::brun(JEventLoop *eventLoop, int32_t runnumber)
154 {
155  // This is called whenever the run number changes
156 
157 
158  //read in thresholds file
159 
160 
161  // printf("brun:: RUN_CONFIG %06i\n",RUN_CONFIG);
162 
163  Int_t runfile = runnumber;
164 
165  //printf("Run number %06i\n",runnumber);
166 
167  if (RUN_CONFIG > 0) runfile = RUN_CONFIG; //use this instead of runnumber
168 
169  int i;
170 
171  Char_t line[200];
172  Int_t slotnum=0;
173  Int_t junk;
174  Float_t fjunk;
175  Char_t filename[500];
176 
177  Int_t *harray;
178 
179  for (int iroc=1; iroc<5; iroc++) {
180 
181 
182  sprintf(filename,"/raid12/gluex/rawdata2/Run%06i/RunLog%06i/CDC/COM/roccdc%i_fadc125_fall2018.cnf",runfile,runfile,iroc);
183 
184 
185  // sprintf(filename,"/raid12/gluex/rawdata2/Run%06i/RunLog%06i/CDC/COM/roccdc%i_fadc125_default.cnf",runfile,runfile,iroc);
186 
187 
188  // sprintf(filename,"/raid12/gluex/rawdata2/Run%06i/RunLog%06i/CDC/COM/roccdc%i_fadc125_default6sigmas.cnf",runfile,runfile,iroc);
189 
190  // sprintf(filename,"/raid12/gluex/rawdata2/Run011017/RunLog011017/CDC/COM/roccdc%i_fadc125_default.cnf",iroc);
191 
192  printf("Looking for config parameter file %s\n",filename);
193 
194  FILE *config = fopen(filename,"r");
195 
196  if (!config) printf("Could not open config file for cdcroc%i\n",iroc);
197  if (!config) break;
198 
199 
200 
201  if (iroc==1) harray = &roc25h[0][0];
202  if (iroc==2) harray = &roc26h[0][0];
203  if (iroc==3) harray = &roc27h[0][0];
204  if (iroc==4) harray = &roc28h[0][0];
205 
206 
207 
208  fscanf(config,"\n"); //new line
209  fscanf(config,"################\n");
210  fscanf(config,"CRATE %s\n",line);
211  //printf("%s \n",line); // print roc name
212  fscanf(config,"################\n");
213 
214  fscanf(config,"\n\n"); //2 blank lines
215 
216  while (!feof(config)) {
217 
218  fscanf(config,"%s\n",line);
219  if (!strcmp(line, "################\n")) break;
220 
221  // fscanf(config,"FADC125_SLOTS %i\n",&slotnum);
222  fscanf(config,"%s %i\n",line,&slotnum);
223  fscanf(config,"################\n");
224 
225  //printf("%s slot %i\n",line,slotnum);
226 
227  fscanf(config,"\n\n"); //2 empty lines
228 
229 
230  //fscanf(config,"FADC125_DAC_CH_00_17 ");
231  fscanf(config,"%s ",line);
232  //printf("DAC: %s\n",line);
233  if (!strcmp(line,"FADC125_DAC_CH_00_17 ")) printf("mismatch\n");
234  if (!strcmp(line,"FADC125_DAC_CH_00_17 ")) break;
235 
236  for (i=0; i<18; i++) fscanf(config," %i",&junk);
237  fscanf(config,"\n");
238 
239  fscanf(config,"FADC125_DAC_CH_18_35 ");
240  for (i=0; i<18; i++) fscanf(config," %i",&junk);
241  fscanf(config,"\n");
242 
243  fscanf(config,"FADC125_DAC_CH_36_53 ");
244  for (i=0; i<18; i++) fscanf(config," %i",&junk);
245  fscanf(config,"\n");
246 
247  fscanf(config,"FADC125_DAC_CH_54_71 ");
248  for (i=0; i<18; i++) fscanf(config," %i",&junk);
249  fscanf(config,"\n");
250 
251  fscanf(config,"\n\n"); //2 empty lines
252 
253 
254  //fscanf(config,"FADC125_THR_CH_00_17 ");
255  fscanf(config,"%s ",line);
256  //printf("THR: %s\n",line);
257  if (!strcmp(line,"FADC125_THR_CH_00_17 ")) printf("THR mismatch\n");
258  // fscanf(config,"FADC125_THR_CH_00_17 ");
259 
260  for (i=0; i<18; i++) fscanf(config," %i",harray+72*slotnum+i);
261  // for (i=0; i<18; i++) fscanf(config," %i",&harray[slotnum][i]);
262  fscanf(config,"\n");
263 
264 
265  fscanf(config,"FADC125_THR_CH_18_35 ");
266  for (i=18; i<36; i++) fscanf(config," %i",harray+72*slotnum+i);
267  // for (i=18; i<36; i++) fscanf(config," %i",&harray[slotnum][i]);
268  fscanf(config,"\n");
269 
270  fscanf(config,"FADC125_THR_CH_36_53 ");
271  for (i=36; i<54; i++) fscanf(config," %i",harray+72*slotnum+i);
272  // for (i=36; i<54; i++) fscanf(config," %i",&harray[slotnum][i]);
273  fscanf(config,"\n");
274 
275  fscanf(config,"FADC125_THR_CH_54_71 ");
276  for (i=54; i<72; i++) fscanf(config," %i",harray+72*slotnum+i);
277  // for (i=54; i<72; i++) fscanf(config," %i",&harray[slotnum][i]);
278  fscanf(config,"\n");
279 
280 
281  fscanf(config,"\n\n"); //2 empty lines
282 
283 
284  //fscanf(config,"FADC125_BL_CH_00_17 ");
285  fscanf(config,"%s ",line);
286  //printf("BL: %s\n",line);
287  if (!strcmp(line,"FADC125_BL_CH_00_17 ")) printf("BL mismatch\n");
288  if (!strcmp(line,"FADC125_BL_CH_00_17 ")) break;
289 
290 
291  // fscanf(config,"FADC125_BL_CH_00_17 ");
292  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
293  fscanf(config,"\n");
294 
295  fscanf(config,"FADC125_BL_CH_18_35 ");
296  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
297  fscanf(config,"\n");
298 
299  fscanf(config,"FADC125_BL_CH_36_53 ");
300  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
301  fscanf(config,"\n");
302 
303  fscanf(config,"FADC125_BL_CH_54_71 ");
304  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
305  fscanf(config,"\n");
306 
307  fscanf(config,"\n\n"); //2 empty lines
308 
309 
310  //fscanf(config,"FADC125_SIG_CH_00_17 ");
311  fscanf(config,"%s ",line);
312  //printf("SIG: %s\n",line);
313  if (!strcmp(line,"FADC125_SIG_CH_00_17 ")) printf("SIG mismatch\n");
314  if (!strcmp(line,"FADC125_SIG_CH_00_17 ")) break;
315 
316  // fscanf(config,"FADC125_SIG_CH_00_17 ");
317  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
318  fscanf(config,"\n");
319 
320  fscanf(config,"FADC125_SIG_CH_18_35 ");
321  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
322  fscanf(config,"\n");
323 
324  fscanf(config,"FADC125_SIG_CH_36_53 ");
325  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
326  fscanf(config,"\n");
327 
328  fscanf(config,"FADC125_SIG_CH_54_71 ");
329  for (i=0; i<18; i++) fscanf(config," %f",&fjunk);
330  fscanf(config,"\n");
331 
332  fscanf(config,"\n"); //1 empty line
333 
334 
335  //fscanf(config,"FADC125_CH_ENB %*s %*s %*s\n");
336  fscanf(config,"%s %*s %*s %*s\n",line);
337  //printf("ENB: %s\n",line);
338  fscanf(config,"\n"); //1 empty line
339  //fscanf(config,"FADC125_CH_DIS %*s %*s %*s\n");
340  fscanf(config,"%s %*s %*s %*s\n",line);
341  //printf("DIS: %s\n",line);
342  fscanf(config,"\n"); //1 empty line
343 
344  fscanf(config,"FADC125_TH %i\n",&junk);
345  // printf("runconfig TH %i\n",junk);
346  fscanf(config,"FADC125_TL %i\n",&junk);
347  // printf("runconfig TL %i\n",junk);
348 
349  fscanf(config,"\n"); //1 empty line
350  }
351  // end of one roc.
352 
353  fclose(config);
354 
355 
356 
357 
358  /*
359  printf("\nroc %i\n",iroc);
360 
361  for (slotnum=3; slotnum<19; slotnum++) {
362  if (slotnum==11||slotnum==12) continue;
363  for (i=0; i<72; i++) {
364  if (i>4 && i<70) continue;
365  printf("hit thres slot %i ch %i %i\n",slotnum,i,*(harray+72*slotnum+i));
366  }
367  }
368  */
369 
370  for (slotnum=3; slotnum<18; slotnum++) {
371 
372  if (slotnum==11 || slotnum==12) continue;
373  if (iroc==2 && slotnum==17) continue;
374  if (iroc==3 && slotnum==17) continue;
375 
376  for (i=0; i<72; i++) {
377  if (*(harray+72*slotnum+i)==0) printf("hit thres not set for roccdc %i (rocid %i) slot %i ch %i\n",iroc,iroc+24,slotnum,i);
378  }
379  }
380 
381 
382 
383  }
384 
385 
386 
387 
388 
389 
390  return NOERROR;
391 }
392 
393 //------------------
394 // evnt
395 //------------------
396 jerror_t JEventProcessor_cdc_emu::evnt(JEventLoop *loop, uint64_t eventnumber)
397 {
398  // This is called for every event. Use of common resources like writing
399  // to a file or filling a histogram should be mutex protected. Using
400  // loop->Get(...) to get reconstructed objects (and thereby activating the
401  // reconstruction algorithm) should be done outside of any mutex lock
402  // since multiple threads may call this method at the same time.
403  // Here's an example:
404  //
405  // vector<const MyDataClass*> mydataclasses;
406  // loop->Get(mydataclasses);
407  //
408  // japp->RootWriteLock();
409  // ... fill historgrams or trees ...
410  // japp->RootUnLock();
411 
412 
413  // vector<const DCODAEventInfo*> info;
414 
415  // ULong64_t timestamp = 0;
416  // uint64_t avg_timestamp = 0;
417 
418  // loop->Get(info);
419 
420  // if (info.size() != 0) {
421  // // printf("found eventinfo \n");
422  // //cout << "info size " << info.size() << endl;
423  // timestamp = (ULong64_t)info[0]->avg_timestamp;
424  // }
425 
426 
427 
428  // get raw data for cdc
429  vector<const DCDCDigiHit*> digihits;
430  loop->Get(digihits);
431  uint32_t nd = (uint32_t)digihits.size();
432 
433  vector<const Df125WindowRawData*> wrdvector;
434  loop->Get(wrdvector);
435  uint32_t nw = (uint32_t)wrdvector.size();
436 
437  uint32_t nhits = nd;
438  if (nw>nd) nhits = nw;
439 
440 
441  if (nhits) {
442 
443 
444  // const Int_t WRITE_DIFFS_ONLY = 1; // only write out events where emulation and fadc results differ
445 
446  const Int_t NSAMPLES = 200;
447 
448 
449  const int straw_offset[29] = {0,0,42,84,138,192,258,324,404,484,577,670,776,882,1005,1128,1263,1398,1544,1690,1848,2006,2176,2346,2528,2710,2907,3104,3313};
450 
451  ULong64_t eventnum;
452  uint32_t rocid;
453  uint32_t slot;
454  uint32_t channel;
455  uint32_t nsamples_pedestal;
456  uint32_t nsamples_integral;
457 
458  Int_t ring;
459  Int_t straw;
460  Int_t n;
461  uint32_t time;
462  uint32_t q;
463  uint32_t pedestal;
464  uint32_t integral;
465  uint32_t amp;
466  uint32_t overflows;
467 
468  bool emulated;
469 
470  uint32_t m_hitsample;
471  uint32_t m_time;
472  uint32_t m_q;
473  uint32_t m_pedestal;
474  uint32_t m_integral;
475  uint32_t m_amp;
476  uint32_t m_overflows;
477 
478  uint32_t m_initped;
479  uint32_t m_net_integral;
480 
481  uint32_t s_pedestal;
482  uint32_t s_integral;
483  uint32_t s_amp;
484 
485  uint32_t m_H;
486  uint32_t m_TH;
487  uint32_t m_TL;
488 
489  uint32_t diffs;
490 
491  Int_t d_time;
492  Int_t d_q;
493  Int_t d_pedestal;
494  Int_t d_integral;
495  Int_t d_amp;
496  Int_t d_overflows;
497 
498 
499  Int_t adc[NSAMPLES];
500 
501 
502  //different types for the fa125 algos, annoyingly
503  Int_t tmp_time,tmp_q,tmp_overflows,tmp_pedestal,tmp_amp;
504  Long_t tmp_integral;
505 
506  // Int_t ndec;
507 
508  Int_t HIT_THRES = 0; //now defined later
509  // const Int_t HIT_THRES = 120; //110 for run 3923, 115 for run 4623, 120 for 4701+
510  const Int_t HIGH_THRESHOLD = 60;
511  const Int_t LOW_THRESHOLD = 10;
512 
513  const Int_t NU = 20; //number of samples sent to time algo
514  const Int_t PED = 5; //sample to be used as pedestal for timing is in place 5
515  const Int_t PG = 4;
516  const Int_t XTHR_SAMPLE = PED + PG;
517 
518  const Int_t NPED = 16;
519  const Int_t NPED2 = 16;
520 
521  const Int_t IBIT = 4;
522  const Int_t ABIT = 3;
523  const Int_t PBIT = 0;
524 
525  const Int_t WINDOW_START = NPED; //first sample after ped
526  const Int_t WINDOW_END = 179; // last sample in buffer is NW-1, so WE is NW-1-NU
527  const Int_t INT_END = 300;
528 
529  const uint32_t OMAX = 7; // field max for overflows
530  const uint32_t PMAX = 255; //field max for pedestal
531  const uint32_t AMAX = 511; //field max for max amp
532  const uint32_t IMAX = 16383; //field max for integral
533 
534 
535  Int_t adc_subset[NU];
536 
537  Int_t hitfound=0; //hit found or not (1=found,0=not)
538  Int_t hitsample=0; // if hit found, sample number of threshold crossing
539  Int_t timesample=0;
540 
541  Int_t i,j;
542 
543  eventnum = (ULong64_t)eventnumber;
544 
545  uint32_t id = 0;
546 
547  uint32_t w_rocid, w_slot, w_channel;
548 
549  bool paired;
550 
551  const DCDCHit *hit = NULL;
552  const DCDCDigiHit *digihit = NULL;
553  const Df125CDCPulse *cp = NULL;
554  const Df125Config *cf = NULL;
555  const Df125WindowRawData *wrd = NULL;
556 
557  for (id=0; id<nd; id++) {
558 
559  digihit = digihits[id];
560 
561  digihit->GetSingle(cp);
562 
563  digihit->GetSingle(hit);
564 
565  if (!cp) printf("\nno cp - event %lu digihit %i\n\n",(long unsigned int)eventnum,(int)id);
566  if (!cp) continue;
567 
568  // cp->GetSingle(cf);
569 
570  // if (!cf) printf("\nno cf - event %lu digihit %i\n\n",(long unsigned int)eventnum,(int)id);
571  // if (!cf) continue;
572 
573  // m_H = (uint32_t)cf->H;
574  // //printf ("config H is %i file H is %i \n",m_H,);
575 
576 
577  cp->GetSingle(wrd);
578 
579  if (!wrd) printf("\nno wrd - event %lu roc %i slot %i chan %i \n\n",(long unsigned int)eventnum,cp->rocid,cp->slot,cp->channel);
580  if (!wrd) continue;
581 
582 
583  rocid = cp->rocid;
584  slot = cp->slot;
585  channel = cp->channel;
586 
587  w_rocid = wrd->rocid;
588  w_slot = wrd->slot;
589  w_channel = wrd->channel;
590 
591 
592 
593  paired = 1;
594 
595  if (rocid != w_rocid) paired = 0;
596  if (slot != w_slot) paired = 0;
597  if (channel != w_channel) paired = 0;
598  //if (itrigger != w_itrigger) paired = 0;
599  //if (itrigger != tt->itrigger) paired = 0;
600 
601 
602  if (!paired) {
603  cout << "Event " << eventnum << endl;
604  cout << "cdcpulse roc " << rocid << " slot " << slot << " chan " << channel << endl;
605  cout << "wrd roc " << w_rocid << " slot " << w_slot << " chan " << w_channel << endl;
606  //printf("cdcpulse roc %i slot %i chan %i trig %i \n",rocid,slot,channel,itrigger);
607  //printf("wrd roc %i slot %i chan %i trig %i \n",w_rocid,w_slot,w_channel,w_itrigger);
608  }
609 
610  // if (!paired) continue;
611 
612 
613  ring = digihit->ring;
614  straw = digihit->straw;
615 
616  n = straw_offset[ring] + straw;
617 
618  time = digihit->pulse_time;
619  integral = digihit->pulse_integral;
620  pedestal = digihit->pedestal;
621 
622  q = cp->time_quality_bit;
623  overflows = cp->overflow_count;
624  amp = cp->first_max_amp;
625 
626 
627  nsamples_pedestal = cp->nsamples_pedestal;
628  nsamples_integral = cp->nsamples_integral;
629 
630  if (hit) {
631 
632  cout << "hit found " << endl;
633 
634  printf("roc %i slot %i chan %i time %i ped %i integral %i q %f \n",rocid,slot,channel,time,pedestal,integral,hit->q);
635 
636  // cout <<
637 
638  // cout << "CP nsamples_pedestal " << cp->nsamples_pedestal << endl;
639  // cout << "hit nsamples_pedestal " << hit->nsamples_pedestal << endl;
640  }
641 
642 
643  emulated = cp->emulated;
644 
645 
646  for (j=0; j<(Int_t)wrd->samples.size(); j++) {
647  adc[j] = (Int_t)wrd->samples[j];
648  if (adc[j] > 4095) adc[j] = 4095;
649  }
650 
651  for (j=(Int_t)wrd->samples.size(); j<NSAMPLES; j++) {
652  adc[j] = 0;
653  }
654 
655  // //check for repeated words (2 samples) //**** CHANGED THIS!
656  // m_norpt = 0;
657  // for (j=3; j<(Int_t)wrd->samples.size(); j+=2) {
658  // if ((adc[j]==adc[j-2]) && (adc[j-1]==adc[j-3])) m_norpt++;
659  // }
660  // if (!m_norpt) printf("repeated WRD words eventnum %llu roc %i slot %i chan %i trig %i \n",eventnum,w_rocid,w_slot,w_channel,w_itrigger);
661 
662  m_hitsample = 0;
663 
664  m_time = 0;
665  m_q = 0;
666  m_overflows = 0;
667  m_pedestal = 0;
668  m_amp = 0;
669  m_integral = 0;
670  m_initped = 0;
671  m_net_integral = 0;
672 
673  diffs = 0;
674  d_time = 0;
675  d_q = 0;
676  d_overflows = 0;
677  d_pedestal = 0;
678  d_amp = 0;
679  d_integral = 0;
680 
681  tmp_time = 0;
682  tmp_q = 0;
683  tmp_overflows = 0;
684  tmp_pedestal = 0;
685  tmp_amp = 0;
686  tmp_integral = 0;
687 
688  hitfound = 0;
689  hitsample = 0;
690 
691 
692  for (j=0; j<NU; j++) {
693  adc_subset[j] = 0;
694  }
695 
696 
697  if (rocid==25) {
698  HIT_THRES = roc25h[slot][channel];
699  } else if (rocid==26) {
700  HIT_THRES = roc26h[slot][channel];
701  } else if (rocid==27) {
702  HIT_THRES = roc27h[slot][channel];
703  } else if (rocid==28) {
704  HIT_THRES = roc28h[slot][channel];
705  }
706 
707  m_H = HIT_THRES; //just for output tree
708  m_TH = HIGH_THRESHOLD;
709  m_TL = LOW_THRESHOLD;
710 
711  // printf("rocid %i slot %i channel %i config H %i Beni's file H %i\n",rocid,slot,channel,m_H,HIT_THRES);
712 
713 
714  m_initped = 0;
715  for (j=WINDOW_START-NPED; j<WINDOW_START; j++) {
716  m_initped += (uint32_t)adc[j];
717  }
718 
719  m_initped = (uint32_t)(m_initped/(uint32_t)NPED);
720 
721  // const Int_t WINDOW_START = NPED; //first sample after ped
722  // look for hit using mean pedestal of NPED samples before trigger
723  // cdc_hit first tests adc[window_start+pg]
724 
725  // cdc_hit(hitfound, hitsample, tmp_pedestal, adc, WINDOW_START, WINDOW_END, HIT_THRES, NPED, NPED2, PG);
726 
727 
728 
729 
730 
731 //alt code
732 
733 
734  tmp_pedestal=0; //pedestal
735  Int_t threshold=0;
736 
737 
738  // calc pedestal as mean of NPED samples before trigger
739  for (i=0; i<NPED; i++) {
740  tmp_pedestal += adc[WINDOW_START-NPED+i];
741  }
742 
743  tmp_pedestal = ( NPED==0 ? 0:(tmp_pedestal/NPED) ); // Integer div is ok as fpga will do 2 rightshifts
744 
745  threshold = tmp_pedestal + HIT_THRES;
746 
747  // look for threshold crossing
748  i = WINDOW_START - 1 + PG;
749  hitfound = 0;
750 
751 
752  //now look for adc val rising above threshold
753 
754  while ((hitfound==0) && (i<WINDOW_END-1)) {
755 
756  i++;
757 
758  if (adc[i] >= threshold) {
759  if (adc[i+1] >= threshold) {
760  hitfound = 1;
761  hitsample = i;
762  }
763  }
764  }
765 
766  if (hitfound == 1) {
767 
768  //calculate new pedestal ending just before the hit
769 
770  tmp_pedestal = 0;
771 
772  for (i=0; i<NPED2; i++) {
773  tmp_pedestal += adc[hitsample-PG-i];
774  }
775 
776  tmp_pedestal = ( NPED2==0 ? 0:(tmp_pedestal/NPED2) );
777  }
778 
779 //end alt code
780 
781  m_pedestal = (uint32_t)tmp_pedestal;
782 
783 
784  if (hitfound==1) {
785 
786  m_hitsample = (uint32_t)hitsample;
787 
788  for (j=0; j<NU; j++) {
789  adc_subset[j] = adc[hitsample+j-XTHR_SAMPLE];
790  }
791 
792  //eg hitsample=20
793  //subset[0] = adc[20-9=11]
794  //subset[8] = adc[19]
795 
796 
797 
798  cdc_time(tmp_time, tmp_q, adc_subset, NU, PG, HIGH_THRESHOLD, LOW_THRESHOLD);
799 
800  timesample = hitsample-XTHR_SAMPLE + (Int_t)(0.1*tmp_time); //sample number containing leading edge sample
801 
802  cdc_integral(tmp_integral, tmp_overflows, timesample, adc, WINDOW_END, INT_END);
803  tmp_amp = (Int_t)adc[timesample];
804  int pktime=0;
805  cdc_max(tmp_amp, pktime, timesample, adc, WINDOW_END);
806 
807 
808  m_time = (uint32_t)10*(uint32_t)(hitsample-XTHR_SAMPLE) + (uint32_t)tmp_time; // integer number * 0.1 samples
809 
810  // // printf("time %i em_time %i timesample %i \n",time,tmp_time,timesample);
811  // // printf("unscaled integ %i em_integ %i diff %i adc[WINDOW_END] %i \n",u_integral,tmp_integral,tmp_integral-u_integral,adc[WINDOW_END]);
812 
813 
814  m_q = (uint32_t)tmp_q;
815  m_integral = (uint32_t)tmp_integral;
816  m_overflows = (uint32_t)tmp_overflows;
817  m_amp = (uint32_t)tmp_amp;
818 
819  if (m_integral > m_pedestal*(WINDOW_END-timesample) ) m_net_integral = m_integral - m_pedestal*(WINDOW_END-timesample);
820 
821 
822  s_pedestal = m_pedestal>>PBIT;
823  if (s_pedestal > PMAX) s_pedestal = PMAX;
824 
825  s_amp = m_amp>>ABIT;
826  if (s_amp > AMAX) s_amp = AMAX;
827 
828  s_integral = m_integral>>IBIT;
829  if (s_integral > IMAX) s_integral = IMAX;
830 
831  d_time = (Int_t)m_time - (Int_t)time;
832 
833  if ((m_q > 0) && (q == 0) ) d_q = 1; //because m_q can be 0-9
834  if ((m_q == 0) && (q > 0) ) d_q = 1; //because m_q can be 0-9
835 
836  d_overflows = (Int_t)m_overflows - (Int_t)overflows;
837  if ((overflows==OMAX) && (m_overflows > overflows)) d_overflows = 0;
838 
839  d_pedestal = (Int_t)s_pedestal - (Int_t)pedestal;
840 
841  d_amp = (Int_t)s_amp - (Int_t)amp;
842 
843  d_integral = (Int_t)s_integral - (Int_t)integral;
844 
845 
846  if (d_time || d_q || d_overflows || d_pedestal || d_amp || d_integral) diffs = 1;
847 
848 
849  } //if hitfound
850 
851 
852 
853  if ((DIFFS_ONLY && diffs) || !DIFFS_ONLY) {
854 
855  dTreeFillData.Fill_Single<ULong64_t>("eventnum",eventnum);
856  dTreeFillData.Fill_Single<UInt_t>("rocid",rocid);
857  dTreeFillData.Fill_Single<UInt_t>("slot",slot);
858  dTreeFillData.Fill_Single<UInt_t>("channel",channel);
859  dTreeFillData.Fill_Single<UInt_t>("nsamples_pedestal",nsamples_pedestal);
860  dTreeFillData.Fill_Single<UInt_t>("nsamples_integral",nsamples_integral);
861 
862  dTreeFillData.Fill_Single<Int_t>("ring",ring);
863  dTreeFillData.Fill_Single<Int_t>("straw",straw);
864  dTreeFillData.Fill_Single<Int_t>("n",n);
865  dTreeFillData.Fill_Single<UInt_t>("time",time);
866  dTreeFillData.Fill_Single<UInt_t>("q",q);
867  dTreeFillData.Fill_Single<UInt_t>("pedestal",pedestal);
868  dTreeFillData.Fill_Single<UInt_t>("integral",integral);
869  dTreeFillData.Fill_Single<UInt_t>("amp",amp);
870  dTreeFillData.Fill_Single<UInt_t>("overflows",overflows);
871 
872  dTreeFillData.Fill_Single<Bool_t>("emulated",emulated);
873  dTreeFillData.Fill_Single<Bool_t>("m_hitfound",hitfound);
874 
875  dTreeFillData.Fill_Single<UInt_t>("m_hitsample",m_hitsample);
876  dTreeFillData.Fill_Single<UInt_t>("m_time",m_time);
877  dTreeFillData.Fill_Single<UInt_t>("m_q",m_q);
878  dTreeFillData.Fill_Single<UInt_t>("m_pedestal",m_pedestal);
879  dTreeFillData.Fill_Single<UInt_t>("m_integral",m_integral);
880  dTreeFillData.Fill_Single<UInt_t>("m_amp",m_amp);
881  dTreeFillData.Fill_Single<UInt_t>("m_overflows",m_overflows);
882 
883  dTreeFillData.Fill_Single<UInt_t>("m_initped",m_initped);
884  dTreeFillData.Fill_Single<UInt_t>("m_net_integral",m_net_integral);
885 
886  dTreeFillData.Fill_Single<UInt_t>("s_pedestal",s_pedestal);
887  dTreeFillData.Fill_Single<UInt_t>("s_integral",s_integral);
888  dTreeFillData.Fill_Single<UInt_t>("s_amp",s_amp);
889 
890  dTreeFillData.Fill_Single<UInt_t>("m_H",m_H);
891  dTreeFillData.Fill_Single<UInt_t>("m_TH",m_TH);
892  dTreeFillData.Fill_Single<UInt_t>("m_TL",m_TL);
893 
894  dTreeFillData.Fill_Single<UInt_t>("diffs",diffs);
895 
896  dTreeFillData.Fill_Single<Int_t>("d_time",d_time);
897  dTreeFillData.Fill_Single<Int_t>("d_q",d_q);
898  dTreeFillData.Fill_Single<Int_t>("d_pedestal",d_pedestal);
899  dTreeFillData.Fill_Single<Int_t>("d_integral",d_integral);
900  dTreeFillData.Fill_Single<Int_t>("d_amp",d_amp);
901  dTreeFillData.Fill_Single<Int_t>("d_overflows",d_overflows);
902 
903  size_t index = 0;
904  for (j=0; j<NSAMPLES; j++) {
905  dTreeFillData.Fill_Array<UInt_t>("adc",adc[j],index);
906  index++;
907  }
908 
909  //FILL ARRAY SIZE
910  dTreeFillData.Fill_Single<UInt_t>("NSAMPLES",index);
911 
912  /*
913  vector <Int_t>samples;
914 
915  for (j=0; j<3; j++) samples.push_back(adc[j]);
916 
917  //FILL ARRAYS
918  size_t locArrayIndex = 0;
919 
920  for(size_t loc_i = 0; loc_i < samples.size(); ++loc_i)
921  {
922  Float_t temp = (Float_t)samples[loc_i];
923 
924  dTreeFillData.Fill_Array<Float_t>("TrackHitDeltaT", temp, locArrayIndex); //is signed: SC - Track
925 
926  ++locArrayIndex;
927 
928  }
929 
930 
931  //FILL ARRAY SIZE
932  dTreeFillData.Fill_Single<UChar_t>("NumMatchingSCHits", locArrayIndex);
933 
934  */
935 
936 
937 
938 
939 
940 
941  //FILL TTREE
943 
944 
945 
946  }
947 
948 
949 
950  } // for each hit (equiv)
951 
952  } // if (nhits)
953 
954 
955  return NOERROR;
956 }
957 
958 //------------------
959 // erun
960 //------------------
962 {
963  // This is called whenever the run number changes, before it is
964  // changed to give you a chance to clean up before processing
965  // events from the next run number.
966  return NOERROR;
967 }
968 
969 //------------------
970 // fini
971 //------------------
973 {
974  // Called before program exit after event processing is finished.
975 
976  delete dTreeInterface; //saves trees to file, closes file
977 
978 
979  return NOERROR;
980 }
981 
uint32_t first_max_amp
from second word
Definition: Df125CDCPulse.h:68
void cdc_max(Int_t &, Int_t &, Int_t, Int_t[], Int_t)
Definition: fa125fns.h:135
if(locHist_BCALShowerPhiVsZ!=NULL)
void cdc_time(Int_t &, Int_t &, Int_t[], Int_t, Int_t, Int_t, Int_t)
Definition: fa125fns.h:172
const double PMAX
sprintf(text,"Post KinFit Cut")
TString filename
void Register_Single(string locBranchName)
jerror_t init(void)
Called once at program start.
void Fill_Array(string locBranchName, const DType &locData, size_t locArrayIndex)
bool Create_Branches(const DTreeBranchRegister &locTreeBranchRegister)
uint32_t overflow_count
from first word
Definition: Df125CDCPulse.h:65
static char index(char c)
Definition: base64.cpp:115
static Int_t roc26h[19][72]
uint32_t pulse_time
identified pulse time as returned by FPGA algorithm
Definition: DCDCDigiHit.h:22
uint32_t nsamples_pedestal
number of samples used in integral
Definition: Df125CDCPulse.h:71
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
void Fill(DTreeFillData &locTreeFillData)
static DTreeInterface * Create_DTreeInterface(string locTreeName, string locFileName)
InitPlugin_t InitPlugin
uint32_t pulse_integral
identified pulse integral as returned by FPGA algorithm
Definition: DCDCDigiHit.h:21
vector< uint16_t > samples
jerror_t fini(void)
Called after last event of last event source has been processed.
void Register_FundamentalArray(string locBranchName, string locArraySizeName, size_t locInitialArraySize=10)
uint32_t time_quality_bit
from first word
Definition: Df125CDCPulse.h:64
static Int_t roc27h[19][72]
float q
Definition: DCDCHit.h:20
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
static Int_t roc25h[19][72]
uint32_t channel
Definition: DDAQAddress.h:34
uint32_t nsamples_integral
number of samples used in pedestal
Definition: Df125CDCPulse.h:72
static TH1I * pedestal[nChan]
uint32_t rocid
Definition: DDAQAddress.h:32
static Int_t roc28h[19][72]
bool emulated
true if emulated values are copied to the main input
Definition: Df125CDCPulse.h:73
printf("string=%s", string)
TCanvas * cp
uint32_t pedestal
pedestal info used by FPGA (if any)
Definition: DCDCDigiHit.h:23
static thread_local DTreeFillData dTreeFillData
void cdc_integral(Long_t &, Int_t &, Int_t, Int_t[], Int_t, Int_t)
Definition: fa125fns.h:107
uint32_t slot
Definition: DDAQAddress.h:33
void Fill_Single(string locBranchName, const DType &locData)