Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
monitoring/timing_online/JEventProcessor_HLDetectorTiming.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // File: JEventProcessor_HLDetectorTiming.cc
4 // Created: Mon Jan 12 14:37:56 EST 2015
5 // Creator: mstaib (on Linux egbert 2.6.32-431.20.3.el6.x86_64 x86_64)
6 //
7 
9 using namespace jana;
10 
11 // Routine used to create our JEventProcessor
12 #include <JANA/JApplication.h>
13 #include <JANA/JFactory.h>
14 
15 #include "PID/DChargedTrack.h"
16 #include "PID/DEventRFBunch.h"
17 #include "TTAB/DTTabUtilities.h"
18 #include "TTAB/DTranslationTable.h"
19 #include "FCAL/DFCALGeometry.h"
20 #include "BCAL/DBCALGeometry.h"
21 #include "TRIGGER/DTrigger.h"
22 #include "HistogramTools.h"
23 
24 extern "C"{
25 void InitPlugin(JApplication *app){
26  InitJANAPlugin(app);
27  app->AddProcessor(new JEventProcessor_HLDetectorTiming());
28  app->AddFactoryGenerator(new DFactoryGenerator_p2pi()); //register the factory generator
29 }
30 } // "C"
31 
32 static int Get_FDCTDC_crate_slot(int mod, string &act_crate, int &act_slot){ //expected mod range from 1 to 48
33  int LH_module=(mod-1)%2; //low (1-48) or high (49-96) wire number (0/1)
34  int package=(mod-1)/12; //package number (0-3)
35  int cell=(mod-1-package*12)/2; //cell number (0-5)
36 
37  int rotation = -45 + 90*LH_module -60*cell;
38  if(rotation<-180)rotation+=360;
39 
40  int crate=0; // (0-3) actual crates are ROCFDC1,4,13,14
41  if(package<2){
42  crate=0;
43  if(rotation>0)crate=3;
44  } else {
45  crate=1;
46  if(rotation>0)crate=2;
47  }
48 
49  int slot=0; //(0-11) actual slots are 3-10,13-16
50  if(rotation<0){
51  if(cell==0){
52  slot=0;
53  } else if (cell==1) {
54  slot=1+LH_module;
55  } else if (cell==2) {
56  slot=3+LH_module;
57  } else {
58  slot=5;
59  }
60  } else {
61  if(cell==0){
62  slot=0;
63  } else if (cell==3) {
64  slot=1;
65  } else if (cell==4) {
66  slot=2+LH_module;
67  } else {
68  slot=4+LH_module;
69  }
70  }
71  slot+=(package%2)*6;
72 
73  //string act_crate="ROCFDC1";
74  act_crate="ROCFDC1";
75  if(crate==1)act_crate="ROCFDC4";
76  if(crate==2)act_crate="ROCFDC13";
77  if(crate==3)act_crate="ROCFDC14";
78  //int act_slot=slot+3;
79  act_slot=slot+3;
80  if(act_slot>10)act_slot+=2;
81 
82  //cout<<" "<<act_crate<<endl;
83  //cout<<" actual slot="<<act_slot<<endl;
84 
85  return crate*12+slot+1; //returns modules in crate/slot sequence (1-48)
86 
87 }
88 
89 
90 //------------------
91 // JEventProcessor_HLDetectorTiming (Constructor)
92 //------------------
94 {
95 
96 }
97 
98 //------------------
99 // ~JEventProcessor_HLDetectorTiming (Destructor)
100 //------------------
102 {
103 
104 }
105 
106 //------------------
107 // init
108 //------------------
110 {
111  BEAM_CURRENT = 50; // Assume that there is beam until first EPICs event. Set from EPICS evio data, can override on command line
112 
113  fBeamEventCounter = 0;
114  REQUIRE_BEAM = 0;
115  BEAM_EVENTS_TO_KEEP = 1000000000; // Set enormously high
116  DO_ROUGH_TIMING = 0;
117  DO_CDC_TIMING = 0;
118  DO_TDC_ADC_ALIGN = 0;
119  DO_TRACK_BASED = 0;
120  DO_VERIFY = 1;
121  DO_OPTIONAL = 0;
122  DO_REACTION = 0;
123  DO_HIGH_RESOLUTION = 0;
124 
125  USE_RF_BUNCH = 1;
126 
127  if(gPARMS){
128  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_ROUGH_TIMING", DO_ROUGH_TIMING, "Set to > 0 to do rough timing of all detectors");
129  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_CDC_TIMING", DO_CDC_TIMING, "Set to > 0 to do CDC Per channel Alignment");
130  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_TDC_ADC_ALIGN", DO_TDC_ADC_ALIGN, "Set to > 0 to do TDC/ADC alignment of SC,TOF,TAGM,TAGH");
131  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_TRACK_BASED", DO_TRACK_BASED, "Set to > 0 to do Track Based timing corrections");
132  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_HIGH_RESOLUTION", DO_HIGH_RESOLUTION, "Set to > 0 to increase the resolution of the track Based timing corrections");
133  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_VERIFY", DO_VERIFY, "Set to > 0 to verify timing with current constants");
134  gPARMS->SetDefaultParameter("HLDETECTORTIMING:REQUIRE_BEAM", REQUIRE_BEAM, "Set to 0 to skip beam current check");
135  gPARMS->SetDefaultParameter("HLDETECTORTIMING:BEAM_EVENTS_TO_KEEP", BEAM_EVENTS_TO_KEEP, "Set to the number of beam on events to use");
136  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_OPTIONAL", DO_OPTIONAL, "Set to >0 to enable optional histograms ");
137  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_REACTION", DO_REACTION, "Set to >0 to run DReaction");
138  gPARMS->SetDefaultParameter("HLDETECTORTIMING:USE_RF_BUNCH", USE_RF_BUNCH, "Set to 0 to disable use of 2 vote RF Bunch");
139  }
140 
141  // Would like the code with no arguments to simply verify the current status of the calibration
142  if (DO_ROUGH_TIMING > 0 || DO_CDC_TIMING > 0 || DO_TDC_ADC_ALIGN > 0 || DO_TRACK_BASED > 0) DO_VERIFY = 0;
143 
144  // Increase range for initial search
145  if(DO_TDC_ADC_ALIGN){
146  NBINS_TDIFF = 2800; MIN_TDIFF = -150.0; MAX_TDIFF = 550.0;
147  }
148  else{
149  NBINS_TDIFF = 200; MIN_TDIFF = -40.0; MAX_TDIFF = 40.0;
150  }
151 
152  if (DO_TRACK_BASED){
153  if (DO_HIGH_RESOLUTION) {
154  NBINS_TAGGER_TIME = 400; MIN_TAGGER_TIME = -20; MAX_TAGGER_TIME = 20;
155  NBINS_MATCHING = 1000; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
156  } else {
157  NBINS_TAGGER_TIME = 1600; MIN_TAGGER_TIME = -200; MAX_TAGGER_TIME = 400;
158  //NBINS_MATCHING = 1000; MIN_MATCHING_T = -100; MAX_MATCHING_T = 400;
159  NBINS_MATCHING = 800; MIN_MATCHING_T = -100; MAX_MATCHING_T = 100;
160  }
161  } else if (DO_VERIFY){
162  NBINS_TAGGER_TIME = 200; MIN_TAGGER_TIME = -20; MAX_TAGGER_TIME = 20;
163  NBINS_MATCHING = 1000; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
164  } else{
165  NBINS_TAGGER_TIME = 100; MIN_TAGGER_TIME = -50; MAX_TAGGER_TIME = 50;
166  NBINS_MATCHING = 100; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
167  }
168 
169  NBINS_RF_COMPARE = 200; MIN_RF_COMPARE = -2.2; MAX_RF_COMPARE = 2.2;
170 
171  return NOERROR;
172 }
173 
174 //------------------
175 // brun
176 //------------------
177 jerror_t JEventProcessor_HLDetectorTiming::brun(JEventLoop *eventLoop, int32_t runnumber)
178 {
179  // This is called whenever the run number changes
180  DApplication* app = dynamic_cast<DApplication*>(eventLoop->GetJApplication());
181  DGeometry* geom = app->GetDGeometry(runnumber);
182  geom->GetTargetZ(Z_TARGET);
183 
184  return NOERROR;
185 }
186 
187 //------------------
188 // evnt
189 //------------------
190 jerror_t JEventProcessor_HLDetectorTiming::evnt(JEventLoop *loop, uint64_t eventnumber)
191 {
192  // select events with physics events, i.e., not LED and other front panel triggers
193  const DTrigger* locTrigger = NULL;
194  loop->GetSingle(locTrigger);
195  if(locTrigger->Get_L1FrontPanelTriggerBits() != 0)
196  return NOERROR;
197 
198  // Get the particleID object for each run
199  vector<const DParticleID *> locParticleID_algos;
200  loop->Get(locParticleID_algos);
201  if(locParticleID_algos.size()<1){
202  _DBG_<<"Unable to get a DParticleID object! NO PID will be done!"<<endl;
203  return RESOURCE_UNAVAILABLE;
204  }
205  auto locParticleID = locParticleID_algos[0];
206 
207  // We want to be use some of the tools available in the RFTime factory
208  // Specifivally steping the RF back to a chosen time
209  auto locRFTimeFactory = static_cast<DRFTime_factory*>(loop->GetFactory("DRFTime"));
210 
211 
212 #if 1
213  // Get the EPICs events and update beam current. Skip event if current too low (<10 nA).
214  vector<const DEPICSvalue *> epicsValues;
215  loop->Get(epicsValues);
216  for(unsigned int j = 0; j < epicsValues.size(); j++){
217  const DEPICSvalue *thisValue = epicsValues[j];
218  if (strcmp((thisValue->name).c_str(), "IBCAD00CRCUR6") == 0){
219  BEAM_CURRENT = thisValue->fval;
220  Fill1DHistogram("HLDetectorTiming", "", "Beam Current",
221  BEAM_CURRENT,
222  "Beam Current; Beam Current [nA]; Entries",
223  100, 0, 200);
224  }
225  //cout << "EPICS Name " << (thisValue->name).c_str() << " Value " << thisValue->fval << endl;
226  }
227  // There is a caveat here when running multithreaded
228  // Another thread might be the one to catch the EPICS event
229  // and there is no way to reject events that may have come from earilier
230  // Expect number of entries in histograms to vary slightly over the same file with many threads
231  if (BEAM_CURRENT < 10.0) {
232  Fill1DHistogram("HLDetectorTiming", "" , "Beam Events",
233  0, "Beam On Events (0 = no beam, 1 = beam > 10nA)",
234  2, -0.5, 1.5);
235  if (REQUIRE_BEAM){
236  return NOERROR; // Skip events where we can't verify the beam current
237  }
238  }
239  else{
240  Fill1DHistogram("HLDetectorTiming", "" , "Beam Events",
241  1, "Beam On Events (0 = no beam, 1 = beam > 10nA)",
242  2, -0.5, 1.5);
243  fBeamEventCounter++;
244  }
245 
246  if (fBeamEventCounter >= BEAM_EVENTS_TO_KEEP) { // Able to specify beam ON events instead of just events
247  cout<< "Maximum number of Beam Events reached" << endl;
248  japp->Quit();
249  return NOERROR;
250  }
251 #endif
252 
253  // Get the objects from the event loop
254  vector<const DCDCHit *> cdcHitVector;
255  vector<const DFDCHit *> fdcHitVector;
256  vector<const DSCHit *> scHitVector;
257  vector<const DBCALUnifiedHit *> bcalUnifiedHitVector;
258  vector<const DTOFHit *> tofHitVector;
259  vector<const DFCALHit *> fcalHitVector;
260  vector<const DTAGMHit *> tagmHitVector;
261  vector<const DTAGHHit *> taghHitVector;
262 
263  loop->Get(cdcHitVector);
264  loop->Get(fdcHitVector);
265  loop->Get(scHitVector);
266  loop->Get(bcalUnifiedHitVector);
267  loop->Get(tofHitVector);
268  loop->Get(fcalHitVector);
269  loop->Get(tagmHitVector, "Calib");
270  loop->Get(taghHitVector, "Calib");
271 
272  // TTabUtilities object used for RF time conversion
273  const DTTabUtilities* locTTabUtilities = NULL;
274  loop->GetSingle(locTTabUtilities);
275  unsigned int i = 0;
276  int nBins = 2000;
277  float xMin = -500, xMax = 1500;
278 
279 #if 0
280  for (i = 0; i < cdcHitVector.size(); i++){
281  Fill1DHistogram ("HLDetectorTiming", "CDC", "CDCHit time", cdcHitVector[i]->t,
282  "CDCHit time;t [ns];", nBins, xMin, xMax);
283  if(DO_VERIFY || DO_CDC_TIMING){
284  int nStraws = 3522;
285  Fill2DHistogram("HLDetectorTiming", "CDC", "CDCHit time per Straw Raw",
286  cdcHitVector[i]->t, GetCCDBIndexCDC(cdcHitVector[i]),
287  "Hit time for each CDC wire; t [ns]; CCDB Index",
288  750, -500, 1000, nStraws, 0.5, nStraws + 0.5);
289  }
290  }
291 #endif
292 
293  for (i = 0; i < fdcHitVector.size(); i++){
294  if(fdcHitVector[i]->type == 0 ) {
295  Fill1DHistogram ("HLDetectorTiming", "FDC", "FDCHit Wire time", fdcHitVector[i]->t,
296  "FDCHit Wire time;t [ns];", nBins, xMin, xMax);
297  // Keep track of module/crate level shifts
298  // two F1TDC modules per wire layer
299  int module = 2 * fdcHitVector[i]->gLayer - 1; // layers start counting at 1
300  if(fdcHitVector[i]->element > 48)
301  module++;
302  Fill2DHistogram ("HLDetectorTiming", "FDC", "FDCHit Wire time vs. module",
303  module, fdcHitVector[i]->t,
304  "FDCHit Wire time; module/slot; t [ns];",
305  48, 0.5, 48.5, 400, -200, 600);
306 
307  }
308  else{
309  Fill1DHistogram ("HLDetectorTiming", "FDC", "FDCHit Cathode time", fdcHitVector[i]->t,
310  "FDCHit Cathode time;t [ns];", nBins, xMin, xMax);
311  }
312  }
313 
314 #if 0
315  for (i = 0; i < scHitVector.size(); i++){
316  //if(!scHitVector[i]->has_fADC || !scHitVector[i]->has_TDC) continue;
317  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit time", scHitVector[i]->t,
318  "SCHit time;t [ns];", nBins, xMin, xMax);
319  }
320  for (i = 0; i < bcalUnifiedHitVector.size(); i++){
321  int the_cell = (bcalUnifiedHitVector[i]->module - 1) * 16 + (bcalUnifiedHitVector[i]->layer - 1) * 4 + bcalUnifiedHitVector[i]->sector;
322  // There is one less layer of TDCs so the numbering relects this
323  int the_tdc_cell = (bcalUnifiedHitVector[i]->module - 1) * 12 + (bcalUnifiedHitVector[i]->layer - 1) * 4 + bcalUnifiedHitVector[i]->sector;
324  // Get the underlying associated objects
325  const DBCALHit * thisADCHit;
326  const DBCALTDCHit * thisTDCHit;
327  bcalUnifiedHitVector[i]->GetSingle(thisADCHit);
328  bcalUnifiedHitVector[i]->GetSingle(thisTDCHit);
329 
330  if (thisADCHit != NULL){ //This should never be NULL but might as well check
331  Fill1DHistogram ("HLDetectorTiming", "BCAL", "BCALHit ADC time", thisADCHit->t,
332  "BCALHit ADC time; t_{ADC} [ns]; Entries", nBins, xMin, xMax);
333 
334  if (DO_OPTIONAL){
335  if (bcalUnifiedHitVector[i]->end == 0){
336  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel ADC Hit Time",
337  the_cell, thisADCHit->t,
338  "BCALHit Upstream Per Channel Hit Time; cellID; t_{ADC} [ns] ",
339  768, 0.5, 768.5, 250, -50, 50);
340  }
341  else{
342  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel ADC Hit Time",
343  the_cell, thisADCHit->t,
344  "BCALHit Downstream Per Channel Hit Time; cellID; t_{ADC} [ns] ",
345  768, 0.5, 768.5, 250, -50, 50);
346  }
347  }
348  }
349 
350  if (thisTDCHit != NULL){
351  Fill1DHistogram ("HLDetectorTiming", "BCAL", "BCALHit TDC time", thisTDCHit->t,
352  "BCALHit TDC time; t_{TDC} [ns]; Entries", nBins, xMin, xMax);
353 
354  if (DO_OPTIONAL){
355  if (bcalUnifiedHitVector[i]->end == 0){
356  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel TDC Hit Time",
357  the_tdc_cell, thisTDCHit->t,
358  "BCALHit Upstream Per Channel TDC Hit Time; cellID; t_{TDC} [ns] ",
359  576, 0.5, 576.5, 350, -50, 300);
360  }
361  else{
362  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel TDC Hit Time",
363  the_tdc_cell, thisTDCHit->t,
364  "BCALHit Downstream Per Channel TDC Hit Time; cellID; t_{TDC} [ns] ",
365  576, 0.5, 576.5, 350, -50, 300);
366  }
367  }
368  }
369 
370  if (thisADCHit != NULL && thisTDCHit != NULL){
371  if (bcalUnifiedHitVector[i]->end == 0){
372  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel TDC-ADC Hit Time",
373  the_tdc_cell, thisTDCHit->t - thisADCHit->t,
374  "BCALHit Upstream Per Channel TDC-ADC Hit Time; cellID; t_{TDC} - t_{ADC} [ns] ",
375  576, 0.5, 576.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
376  }
377  else{
378  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel TDC-ADC Hit Time",
379  the_tdc_cell, thisTDCHit->t - thisADCHit->t,
380  "BCALHit Downstream Per Channel TDC-ADC Hit Time; cellID; t_{TDC} - t_{ADC} [ns] ",
381  576, 0.5, 576.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
382  }
383  }
384  }
385 
386  for (i = 0; i < tofHitVector.size(); i++){
387  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit time", tofHitVector[i]->t,
388  "TOFHit time;t [ns];", nBins, xMin, xMax);
389  }
390 
391  // from FCAL_online: find energy weighted average time for FCAL hits, useful as a t0
392  double fcalHitETot = 0;
393  double fcalHitEwtT = 0;
394  for (i = 0; i < fcalHitVector.size(); i++){
395  fcalHitETot += fcalHitVector[i]->E;
396  fcalHitEwtT += fcalHitVector[i]->E * fcalHitVector[i]->t;
397  }
398  fcalHitEwtT /= fcalHitETot;
399 
400  for (i = 0; i < fcalHitVector.size(); i++){
401  Fill1DHistogram ("HLDetectorTiming", "FCAL", "FCALHit time", fcalHitVector[i]->t,
402  "FCALHit time;t [ns];", nBins, xMin, xMax);
403 
404  // extract the FCAL Geometry
405  vector<const DFCALGeometry*> fcalGeomVect;
406  loop->Get( fcalGeomVect );
407  if (fcalGeomVect.size() < 1){
408  cout << "FCAL Geometry not available?" << endl;
409  return OBJECT_NOT_AVAILABLE;
410  }
411  const DFCALGeometry& fcalGeom = *(fcalGeomVect[0]);
412  Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Occupancy",
413  fcalHitVector[i]->row, fcalHitVector[i]->column,
414  "FCAL Hit Occupancy; column; row",
415  61, -1.5, 59.5, 61, -1.5, 59.5);
416  double locTime = ( fcalHitVector[i]->t - fcalHitEwtT )*k_to_nsec;
417  //Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Local Time",
418  Fill2DWeightedHistogram("HLDetectorTiming", "FCAL", "FCALHit Local Time",
419  fcalHitVector[i]->row, fcalHitVector[i]->column, locTime,
420  "FCAL Hit Local Time [ns]; column; row",
421  61, -1.5, 59.5, 61, -1.5, 59.5);
422 
423  if (DO_OPTIONAL){
424  Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Per Channel Time",
425  fcalGeom.channel(fcalHitVector[i]->row, fcalHitVector[i]->column), fcalHitVector[i]->t,
426  "FCAL Per Channel Hit time; channel; t [ns]",
427  fcalGeom.numActiveBlocks(), 0.5, fcalGeom.numActiveBlocks() + 0.5, 250, -50, 50);
428  }
429  }
430 
431  for (i = 0; i < tagmHitVector.size(); i++){
432  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit time", tagmHitVector[i]->t,
433  "TAGMHit time;t [ns];", nBins, xMin, xMax);
434  }
435  for (i = 0; i < taghHitVector.size(); i++){
436  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit time", taghHitVector[i]->t,
437  "TAGHHit time;t [ns];", nBins, xMin, xMax);
438  }
439 
440  // The detectors with both TDCs and ADCs need these two to be aligned
441  // These detectors are the SC,TAGM,TAGH,TOF
442 
443  // Break these histograms up into hits coming from the TDC and hits coming from the ADC
444  for (i = 0; i < scHitVector.size(); i++){
445  int nSCCounters = 30;
446  const DSCHit *thisSCHit = scHitVector[i];
447  if (thisSCHit->has_fADC && !thisSCHit->has_TDC){
448  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit ADC time", scHitVector[i]->t,
449  "SCHit ADC only time;t [ns];", nBins, xMin, xMax);
450  // Manual loop over hits to match out of time
451  for (auto hit = scHitVector.begin(); hit != scHitVector.end(); hit++){
452  if ((*hit)->has_TDC && !(*hit)->has_fADC){
453  if (scHitVector[i]->sector == (*hit)->sector){
454  Fill2DHistogram("HLDetectorTiming", "SC", "SCHit TDC_ADC Difference",
455  scHitVector[i]->sector, (*hit)->t_TDC - scHitVector[i]->t_fADC,
456  "SC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nSCCounters, 0.5, nSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
457  }
458  }
459  }
460  }
461  else if (!thisSCHit->has_fADC && thisSCHit->has_TDC){
462  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit TDC time", scHitVector[i]->t,
463  "SCHit TDC only time;t [ns];", nBins, xMin, xMax);
464  }
465  else{
466  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit Matched time", scHitVector[i]->t,
467  "SCHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
468  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit ADC time", scHitVector[i]->t_fADC,
469  "SCHit ADC only time;t [ns];", nBins, xMin, xMax);
470  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit TDC time", scHitVector[i]->t_TDC,
471  "SCHit TDC only time;t [ns];", nBins, xMin, xMax);
472 
473  Fill2DHistogram("HLDetectorTiming", "SC", "SCHit TDC_ADC Difference",
474  scHitVector[i]->sector, scHitVector[i]->t_TDC - scHitVector[i]->t_fADC,
475  "SC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nSCCounters, 0.5, nSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
476  }
477 
478  }
479  for (i = 0; i < tagmHitVector.size(); i++){
480 
481  const DTAGMHit *thisTAGMHit = tagmHitVector[i];
482  int nTAGMCounters = 122; // 102 + 20 including 4 fully read out columns
483 
484  if(thisTAGMHit->has_fADC && !thisTAGMHit->has_TDC){
485  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit ADC time", tagmHitVector[i]->t,
486  "TAGMHit ADC only time;t [ns];", nBins, xMin, xMax);
487  // Manual loop over hits to match out of time
488  for (auto hit = tagmHitVector.begin(); hit != tagmHitVector.end(); hit++){
489  if ((*hit)->has_TDC && !(*hit)->has_fADC){
490  if (GetCCDBIndexTAGM(tagmHitVector[i]) == GetCCDBIndexTAGM(*hit)){
491  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGMHit TDC_ADC Difference",
492  GetCCDBIndexTAGM(tagmHitVector[i]), (*hit)->t - tagmHitVector[i]->time_fadc,
493  //GetCCDBIndexTAGM(tagmHitVector[i]), (*hit)->time_tdc - tagmHitVector[i]->time_fadc,
494  "TAGM #Deltat TDC-ADC; Column ;t_{TDC} - t_{ADC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
495  }
496  }
497  }
498  }
499  else if (!thisTAGMHit->has_fADC && thisTAGMHit->has_TDC){
500  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit TDC time", tagmHitVector[i]->t,
501  "TAGMHit TDC only time;t [ns];", nBins, xMin, xMax);
502  }
503  else{
504  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit Matched time", tagmHitVector[i]->t,
505  "TAGMHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
506  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit ADC time", tagmHitVector[i]->time_fadc,
507  "TAGMHit ADC only time;t [ns];", nBins, xMin, xMax);
508  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit TDC time", tagmHitVector[i]->t,
509  "TAGMHit TDC only time;t [ns];", nBins, xMin, xMax);
510 
511  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGMHit TDC_ADC Difference",
512  GetCCDBIndexTAGM(tagmHitVector[i]), tagmHitVector[i]->t - tagmHitVector[i]->time_fadc,
513  "TAGM #Deltat TDC-ADC; Column ;t_{TDC} - t_{ADC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
514  if (DO_OPTIONAL){
515  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGM Per Channel TDC Time",
516  GetCCDBIndexTAGM(tagmHitVector[i]), tagmHitVector[i]->t,
517  "TAGM Per Channel TDC time; Column ;t_{TDC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, 100, -50, 50);
518  }
519 
520  }
521 
522  }
523  for (i = 0; i < taghHitVector.size(); i++){
524 
525  const DTAGHHit *thisTAGHHit = taghHitVector[i];
526  int nTAGHCounters = 274;
527 
528  if(thisTAGHHit->has_fADC && !thisTAGHHit->has_TDC){
529  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit ADC time", taghHitVector[i]->t,
530  "TAGHHit ADC only time;t [ns];", nBins, xMin, xMax);
531  // Manual loop over hits to match out of time
532  for (auto hit = taghHitVector.begin(); hit != taghHitVector.end(); hit++){
533  if ((*hit)->has_TDC && !(*hit)->has_fADC){
534  if (taghHitVector[i]->counter_id == (*hit)->counter_id){
535  Fill2DHistogram("HLDetectorTiming", "TAGH", "TAGHHit TDC_ADC Difference",
536  taghHitVector[i]->counter_id, (*hit)->time_tdc - taghHitVector[i]->time_fadc,
537  "TAGH #Deltat TDC-ADC; Counter ID ;t_{TDC} - t_{ADC} [ns]", nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
538  }
539  }
540  }
541  }
542  else if (!thisTAGHHit->has_fADC && thisTAGHHit->has_TDC){
543  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit TDC time", taghHitVector[i]->t,
544  "TAGHHit TDC only time;t [ns];", nBins, xMin, xMax);
545  }
546  else{
547  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit Matched time", taghHitVector[i]->t,
548  "TAGHHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
549  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit ADC time", taghHitVector[i]->time_fadc,
550  "TAGHHit ADC only time;t [ns];", nBins, xMin, xMax);
551  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit TDC time", taghHitVector[i]->time_tdc,
552  "TAGHHit TDC only time;t [ns];", nBins, xMin, xMax);
553 
554  // We want to look at the timewalk within these ADC/TDC detectors
555  Fill2DHistogram("HLDetectorTiming", "TAGH", "TAGHHit TDC_ADC Difference",
556  taghHitVector[i]->counter_id, taghHitVector[i]->time_tdc - taghHitVector[i]->time_fadc,
557  "TAGH #Deltat TDC-ADC; Counter ID ;t_{TDC} - t_{ADC} [ns]", nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
558  }
559  }
560  for (i = 0; i < tofHitVector.size(); i++){
561  const DTOFHit *thisTOFHit = tofHitVector[i];
562  int nTOFCounters = 176;
563  if(thisTOFHit->has_fADC && !thisTOFHit->has_TDC){
564  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit ADC time", tofHitVector[i]->t,
565  "TOFHit ADC only time;t [ns];", nBins, xMin, xMax);
566  // Manual loop over hits to match out of time
567  for (auto hit = tofHitVector.begin(); hit != tofHitVector.end(); hit++){
568  if ((*hit)->has_TDC && !(*hit)->has_fADC){
569  if (GetCCDBIndexTOF(tofHitVector[i]) == GetCCDBIndexTOF(*hit)){
570  Fill2DHistogram("HLDetectorTiming", "TOF", "TOFHit TDC_ADC Difference",
571  GetCCDBIndexTOF(tofHitVector[i]), (*hit)->t_TDC - tofHitVector[i]->t_fADC,
572  "TOF #Deltat TDC-ADC; CDCB Index ;t_{TDC} - t_{ADC} [ns]", nTOFCounters, 0.5, nTOFCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
573  }
574  }
575  }
576  }
577  else if (!thisTOFHit->has_fADC && thisTOFHit->has_TDC){
578  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit TDC time", tofHitVector[i]->t,
579  "TOFHit TDC only time;t [ns];", nBins, xMin, xMax);
580  }
581  else{
582  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit Matched time", tofHitVector[i]->t,
583  "TOFHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
584  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit ADC time", tofHitVector[i]->t_fADC,
585  "TOFHit ADC only time;t [ns];", nBins, xMin, xMax);
586  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit TDC time", tofHitVector[i]->t_TDC,
587  "TOFHit TDC only time;t [ns];", nBins, xMin, xMax);
588 
589  Fill2DHistogram("HLDetectorTiming", "TOF", "TOFHit TDC_ADC Difference",
590  GetCCDBIndexTOF(tofHitVector[i]), tofHitVector[i]->t_TDC - tofHitVector[i]->t_fADC,
591  "TOF #Deltat TDC-ADC; CDCB Index ;t_{TDC} - t_{ADC} [ns]", nTOFCounters, 0.5, nTOFCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
592  }
593  }
594 
595  // Next the relative times between detectors using tracking
596  // By the time we get to this point, our first guess at the timing should be fairly good.
597  // Certainly good enough to take a pass at the time based tracking
598  // This will be the final alignment step for now
599 
600  // We want to plot the delta t at the target between the SC hit and the tagger hits
601  // Some limits for these
602  float nBinsE = 160, EMin = 3.0, EMax = 12.0;
603 
604  const DEventRFBunch *thisRFBunch = NULL;
605  loop->GetSingle(thisRFBunch, "Calibrations"); // SC only hits
606 
607  if (thisRFBunch->dNumParticleVotes < 2) return NOERROR;
608 
609  // Loop over TAGM hits
610  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
611  int nTAGMColumns = 122; // Not really just columns, but a name is a name
612  if(tagmHitVector[j]->has_fADC){
613  char name [200];
614  char title[500];
615  sprintf(name, "Column %.3i Row %.1i", tagmHitVector[j]->column, tagmHitVector[j]->row);
616  sprintf(title, "TAGM Column %i t_{ADC} - t_{RF}; t_{ADC} - t_{RF} [ns]; Entries", tagmHitVector[j]->column);
617  double locShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, tagmHitVector[j]->time_fadc);
618  Fill1DHistogram("HLDetectorTiming", "TAGM_ADC_RF_Compare", name,
619  tagmHitVector[j]->time_fadc - locShiftedTime,
620  title,
621  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
622  }
623 
624  if(tagmHitVector[j]->has_TDC){
625  char name [200];
626  char title[500];
627  sprintf(name, "Column %.3i Row %.1i", tagmHitVector[j]->column, tagmHitVector[j]->row);
628  sprintf(title, "TAGM Column %i t_{TDC} - t_{RF}; t_{TDC} - t_{RF} [ns]; Entries", tagmHitVector[j]->column);
629  double locShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, tagmHitVector[j]->t);
630  Fill1DHistogram("HLDetectorTiming", "TAGM_TDC_RF_Compare", name,
631  tagmHitVector[j]->t - locShiftedTime,
632  title,
633  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
634  }
635 
636  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch Time",
637  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - thisRFBunch->dTime,
638  "#Deltat TAGM-RFBunch; CCDB Index ;t_{TAGM} - t_{SC @ target} [ns]",
639  nTAGMColumns, 0.5, nTAGMColumns + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
640  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time",
641  tagmHitVector[j]->t - thisRFBunch->dTime, tagmHitVector[j]->E,
642  "Tagger - RFBunch Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
643  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
644  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time",
645  tagmHitVector[j]->t - thisRFBunch->dTime,
646  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
647  //160, -20, 20);
648  800, -50, 50);
649  if (tagmHitVector[j]->row == 0){
650  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch 1D Time",
651  tagmHitVector[j]->t - thisRFBunch->dTime,
652  "TAGM - RFBunch Time; #Deltat_{TAGM - RFBunch} [ns]; Entries",
653  //480, -30, 30);
654  800, -50, 50);
655  }
656  }
657 
658  // Loop over TAGH hits
659  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
660  int nTAGHCounters = 274;
661 
662  if(taghHitVector[j]->has_fADC){
663  char name [200];
664  char title[500];
665  sprintf(name, "Counter ID %.3i", taghHitVector[j]->counter_id);
666  sprintf(title, "TAGH Counter ID %i t_{ADC} - t_{RF}; t_{ADC} - t_{RF} [ns]; Entries", taghHitVector[j]->counter_id);
667  double locShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, taghHitVector[j]->time_fadc);
668  Fill1DHistogram("HLDetectorTiming", "TAGH_ADC_RF_Compare", name,
669  taghHitVector[j]->time_fadc - locShiftedTime,
670  title,
671  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
672  }
673  if(taghHitVector[j]->has_TDC){
674  char name [200];
675  char title[500];
676  sprintf(name, "Counter ID %.3i", taghHitVector[j]->counter_id);
677  sprintf(title, "TAGH Counter ID %i t_{TDC} - t_{RF}; t_{TDC} - t_{RF} [ns]; Entries", taghHitVector[j]->counter_id);
678  double locShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, taghHitVector[j]->time_tdc);
679  Fill1DHistogram("HLDetectorTiming", "TAGH_TDC_RF_Compare", name,
680  taghHitVector[j]->time_tdc - locShiftedTime,
681  title,
682  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
683  }
684 
685  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - RFBunch Time",
686  taghHitVector[j]->counter_id, taghHitVector[j]->t - thisRFBunch->dTime,
687  "#Deltat TAGH-RFBunch; Counter ID ;t_{TAGH} - t_{RFBunch} [ns]",
688  nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
689 
690  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time",
691  taghHitVector[j]->t - thisRFBunch->dTime, taghHitVector[j]->E,
692  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Energy [GeV]",
693  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
694 
695  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time",
696  taghHitVector[j]->t - thisRFBunch->dTime,
697  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
698  480, -30, 30);
699  }
700 
701 
702  if (!DO_TRACK_BASED && !DO_VERIFY ) return NOERROR; // Before this stage we aren't really ready yet, so just return
703 
704  // Try using the detector matches
705  // Loop over the charged tracks
706 
707  vector<const DChargedTrack *> chargedTrackVector;
708  loop->Get(chargedTrackVector);
709 
710  for (i = 0; i < chargedTrackVector.size(); i++){
711 
712  // We only want negative particles to kick out protons
713  if (chargedTrackVector[i]->Get_Charge() > 0) continue;
714  const DChargedTrackHypothesis *pionHypothesis = chargedTrackVector[i]->Get_Hypothesis(PiMinus);
715 
716  if (pionHypothesis == NULL) continue;
717 
718  auto locTrackTimeBased = pionHypothesis->Get_TrackTimeBased();
719  double trackingFOM = TMath::Prob(locTrackTimeBased->chisq, locTrackTimeBased->Ndof);
720  // Some quality cuts for the tracks we will use
721  // Keep this minimal for now and investigate later
722  //float trackingFOMCut = 0.01;
723  //float trackingFOMCut =0.0027;
724  float trackingFOMCut = 2.87E-7;
725  int trackingNDFCut = 5;
726 
727  if(trackingFOM < trackingFOMCut) continue;
728  if( locTrackTimeBased->Ndof < trackingNDFCut) continue;
729 
730  //////////////////////////////////////////
731  // get best matches to SC/TOF/FCAL/BCAL //
732  //////////////////////////////////////////
733  auto locSCHitMatchParams = pionHypothesis->Get_SCHitMatchParams();
734  auto locTOFHitMatchParams = pionHypothesis->Get_TOFHitMatchParams();
735  auto locFCALShowerMatchParams = pionHypothesis->Get_FCALShowerMatchParams();
736  auto locBCALShowerMatchParams = pionHypothesis->Get_BCALShowerMatchParams();
737 
738  // We will only use tracks matched to the start counter for our calibration since this will be our reference for t0
739  if (locSCHitMatchParams == NULL) continue;
740 
741  // the idea will be to fix the SC time and reference the other PID detectors off of this
742 
743  // These "flightTime" corrected time are essentially that detector's estimate of the target time
744  float targetCenterCorrection = ((pionHypothesis->position()).Z() - Z_TARGET) / SPEED_OF_LIGHT;
745  float flightTimeCorrectedSCTime = locSCHitMatchParams->dHitTime - locSCHitMatchParams->dFlightTime - targetCenterCorrection;
746  char name [200];
747  char title[500];
748  sprintf(name, "Sector %.2i", locSCHitMatchParams->dSCHit->sector);
749  sprintf(title, "SC Sector %i t_{Target} - t_{RF}; t_{Target} - t_{RF} [ns]; Entries", locSCHitMatchParams->dSCHit->sector);
750  double locShiftedTime = locRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, flightTimeCorrectedSCTime);
751  Fill1DHistogram("HLDetectorTiming", "SC_Target_RF_Compare", name,
752  flightTimeCorrectedSCTime - locShiftedTime,
753  title,
754  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
755  Fill1DHistogram("HLDetectorTiming", "TRACKING", "SC - RF Time",
756  flightTimeCorrectedSCTime - thisRFBunch->dTime,
757  "t_{SC} - t_{RF} at Target; t_{SC} - t_{RF} at Target [ns]; Entries",
758  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
759 
760  // Get the pulls vector from the track
761  auto thisTimeBasedTrack = pionHypothesis->Get_TrackTimeBased();
762 
763  vector<DTrackFitter::pull_t> pulls = thisTimeBasedTrack->pulls;
764  double earliestCDCTime = 10000.;
765  double earliestFDCTime = 10000.;
766  for (size_t iPull = 0; iPull < pulls.size(); iPull++){
767  if ( pulls[iPull].cdc_hit != nullptr && pulls[iPull].tdrift < earliestCDCTime) earliestCDCTime = pulls[iPull].tdrift;
768  if ( pulls[iPull].fdc_hit != nullptr && pulls[iPull].tdrift < earliestFDCTime) earliestFDCTime = pulls[iPull].tdrift;
769  }
770 
771  // Do this the old way for the CDC
772  vector < const DCDCTrackHit *> cdcTrackHitVector;
773  pionHypothesis->Get_TrackTimeBased()->Get(cdcTrackHitVector);
774  if (cdcTrackHitVector.size() != 0){
775  float earliestTime = 10000; // Initialize high
776  for (unsigned int iCDC = 0; iCDC < cdcTrackHitVector.size(); iCDC++){
777  if (cdcTrackHitVector[iCDC]->tdrift < earliestTime) earliestTime = cdcTrackHitVector[iCDC]->tdrift;
778  }
779 
780  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest CDC Time Minus Matched SC Time",
781  earliestTime - locSCHitMatchParams->dHitTime,
782  "Earliest CDC Time Minus Matched SC Time; t_{CDC} - t_{SC} [ns];",
783  400, -50, 150);
784  }
785 
786  // Loop over TAGM hits
787  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
788  int nTAGMColumns = 122;
789  // We want to look at the timewalk within these ADC/TDC detectors
790  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - SC Target Time",
791  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - flightTimeCorrectedSCTime,
792  "#Deltat TAGM-SC; Column ;t_{TAGM} - t_{SC @ target} [ns]", nTAGMColumns, 0.5, nTAGMColumns + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
793  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC Target Time",
794  tagmHitVector[j]->t - flightTimeCorrectedSCTime, tagmHitVector[j]->E,
795  "Tagger - SC Target Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
796  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
797  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC 1D Target Time",
798  tagmHitVector[j]->t - flightTimeCorrectedSCTime,
799  "Tagger - SC Time at Target; #Deltat_{Tagger - SC} [ns]; Entries",
800  160, -20, 20);
801  }
802  // Loop over TAGH hits
803  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
804  int nTAGHCounters = 274;
805  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - SC Target Time",
806  taghHitVector[j]->counter_id, taghHitVector[j]->t - flightTimeCorrectedSCTime,
807  "#Deltat TAGH-SC; Counter ID ;t_{TAGH} - t_{SC @ target} [ns]", nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
808 
809  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC Target Time",
810  taghHitVector[j]->t - flightTimeCorrectedSCTime, taghHitVector[j]->E,
811  "Tagger - SC Target Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
812  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
813 
814  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC 1D Target Time",
815  taghHitVector[j]->t - flightTimeCorrectedSCTime,
816  "Tagger - SC Time at Target; #Deltat_{Tagger - SC} [ns]; Entries",
817  160, -20, 20);
818  }
819 
820  if (locTOFHitMatchParams != NULL){
821  // Now check the TOF matching. Do this on a full detector level.
822  float flightTimeCorrectedTOFTime = locTOFHitMatchParams->dHitTime - locTOFHitMatchParams->dFlightTime - targetCenterCorrection;
823  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TOF - SC Target Time",
824  flightTimeCorrectedTOFTime - flightTimeCorrectedSCTime,
825  "t_{TOF} - t_{SC} at Target; t_{TOF} - t_{SC} at Target [ns]; Entries",
826  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
827  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TOF - RF Time",
828  flightTimeCorrectedTOFTime - thisRFBunch->dTime,
829  "t_{TOF} - t_{RF} at Target; t_{TOF} - t_{RF} at Target [ns]; Entries",
830  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
831  // Fill the following when there is a SC/TOF match
832  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest Flight-time Corrected FDC Time",
833  earliestFDCTime,
834  "Earliest Flight-time corrected FDC Time; t_{FDC} [ns];",
835  200, -50, 150);
836  }
837 
838  if (locBCALShowerMatchParams != NULL){
839  float flightTimeCorrectedBCALTime = locBCALShowerMatchParams->dBCALShower->t - locBCALShowerMatchParams->dFlightTime - targetCenterCorrection;
840  Fill1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - SC Target Time",
841  flightTimeCorrectedBCALTime - flightTimeCorrectedSCTime,
842  "t_{BCAL} - t_{SC} at Target; t_{BCAL} - t_{SC} [ns]; Entries",
843  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
844  Fill1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time",
845  flightTimeCorrectedBCALTime - thisRFBunch->dTime,
846  "t_{BCAL} - t_{RF} at Target; t_{BCAL} - t_{RF} [ns]; Entries",
847  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
848  // Add histogram suggested by Mark Dalton
849  Fill2DHistogram("HLDetectorTiming", "TRACKING", "BCAL - SC Target Time Vs Correction",
850  locBCALShowerMatchParams->dFlightTime, flightTimeCorrectedBCALTime - flightTimeCorrectedSCTime,
851  "t_{BCAL} - t_{SC} at Target; Flight time [ns]; t_{BCAL} - t_{SC} [ns]",
852  100, 0, 20, 50, -10, 10);
853  // Fill the following when there is a SC/BCAL match.
854  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest Flight-time Corrected CDC Time",
855  earliestCDCTime,
856  "Earliest Flight-time Corrected CDC Time; t_{CDC} [ns];",
857  200, -50, 150);
858  }
859  if (locFCALShowerMatchParams != NULL){
860  float flightTimeCorrectedFCALTime = locFCALShowerMatchParams->dFCALShower->getTime() - locFCALShowerMatchParams->dFlightTime - targetCenterCorrection;
861  Fill1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - SC Target Time",
862  flightTimeCorrectedFCALTime - flightTimeCorrectedSCTime,
863  "t_{FCAL} - t_{SC} at Target; t_{FCAL} - t_{SC} [ns]; Entries",
864  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
865  Fill1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time",
866  flightTimeCorrectedFCALTime - thisRFBunch->dTime,
867  "t_{FCAL} - t_{RF} at Target; t_{FCAL} - t_{RF} [ns]; Entries",
868  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
869  }
870 
871  } // End of loop over time based tracks
872 
873  if (DO_REACTION){
874  // Trigger the analysis
875  vector<const DAnalysisResults*> locAnalysisResultsVector;
876  loop->Get(locAnalysisResultsVector);
877  // Get the time from the results and fill histograms
878  deque<const DParticleCombo*> locPassedParticleCombos;
879  locAnalysisResultsVector[0]->Get_PassedParticleCombos(locPassedParticleCombos);
880 
881  for (i=0; i < locPassedParticleCombos.size(); i++){
882  double taggerTime = locPassedParticleCombos[i]->Get_ParticleComboStep(0)->Get_InitialParticle_Measured()->time();
883  //cout << "We have a tagger time of " << taggerTime << endl;
884  //Find matching hit by time
885  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
886  if (taggerTime == tagmHitVector[j]->t){
887  int nTAGMColumns = 122;
888  // We want to look at the timewalk within these ADC/TDC detectors
889  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch Time p2pi",
890  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - thisRFBunch->dTime,
891  "#Deltat TAGM-RFBunch; Column ;t_{TAGM} - t_{SC @ target} [ns]",
892  nTAGMColumns, 0.5, nTAGMColumns + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
893  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time p2pi",
894  tagmHitVector[j]->t - thisRFBunch->dTime, tagmHitVector[j]->E,
895  "Tagger - RFBunch Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
896  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
897  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time p2pi",
898  tagmHitVector[j]->t - thisRFBunch->dTime,
899  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
900  160, -20, 20);
901  }
902  }
903  // Loop over TAGH hits
904  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
905  if (taggerTime == taghHitVector[j]->t){
906  int nTAGHCounters = 274;
907  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - RFBunch Time p2pi",
908  taghHitVector[j]->counter_id, taghHitVector[j]->t - thisRFBunch->dTime,
909  "#Deltat TAGH-RFBunch; Counter ID ;t_{TAGH} - t_{RFBunch} [ns]",
910  nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
911 
912  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time p2pi",
913  taghHitVector[j]->t - thisRFBunch->dTime, taghHitVector[j]->E,
914  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Energy [GeV]",
915  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
916 
917  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time p2pi",
918  taghHitVector[j]->t - thisRFBunch->dTime,
919  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
920  160, -20, 20);
921  }
922  }
923  }
924  }
925 #endif
926  return NOERROR;
927 }
928 
929 
930 //------------------
931 // erun
932 //------------------
934 {
935  // This is called whenever the run number changes, before it is
936  // changed to give you a chance to clean up before processing
937  // events from the next run number.
938 
939  // set some histogram properties
940 
941 
942  TH2I *fdc_time_module_hist = (TH2I*)gDirectory->Get("HLDetectorTiming/FDC/FDCHit Wire time vs. module");
943  if(fdc_time_module_hist != NULL) {
944  string act_crate;
945  int act_slot;
946  for(int ibin=1; ibin<=48; ibin++){
947  int mod = Get_FDCTDC_crate_slot(ibin, act_crate, act_slot);
948  stringstream ss;
949  ss << act_crate << "/" << act_slot;
950  fdc_time_module_hist->GetXaxis()->SetBinLabel(ibin, ss.str().c_str());
951  }
952  fdc_time_module_hist->LabelsOption("v");
953  }
954 
955 
956  return NOERROR;
957 }
958 
959 //------------------
960 // fini
961 //------------------
963 {
964  // Called before program exit after event processing is finished.
965  //Here is where we do the fits to the data to see if we have a reasonable alignment
966  SortDirectories(); //Defined in HistogramTools.h
967 
968  return NOERROR;
969 }
970 
972  // Returns the CCDB index of a particular hit
973  // This
974  int plane = thisHit->plane;
975  int bar = thisHit->bar;
976  int end = thisHit->end;
977  // 44 bars per plane
978  int CCDBIndex = plane * 88 + end * 44 + bar;
979  return CCDBIndex;
980 }
981 
983  return 0;
984 }
985 
987  // Since there are a few counters where each row is read out seperately this is a bit of a mess
988  int row = thisHit->row;
989  int column = thisHit->column;
990 
991  int CCDBIndex = column + row;
992  if (column > 9) CCDBIndex += 5;
993  if (column > 27) CCDBIndex += 5;
994  if (column > 81) CCDBIndex += 5;
995  if (column > 99) CCDBIndex += 5;
996 
997  return CCDBIndex;
998 }
999 
1001 
1002  int ring = thisHit->ring;
1003  int straw = thisHit->straw;
1004 
1005  int CCDBIndex = GetCCDBIndexCDC(ring, straw);
1006  return CCDBIndex;
1007 }
1008 
1009 int JEventProcessor_HLDetectorTiming::GetCCDBIndexCDC(int ring, int straw){
1010 
1011  //int Nstraws[28] = {42, 42, 54, 54, 66, 66, 80, 80, 93, 93, 106, 106, 123, 123, 135, 135, 146, 146, 158, 158, 170, 170, 182, 182, 197, 197, 209, 209};
1012  int StartIndex[28] = {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};
1013 
1014  int CCDBIndex = StartIndex[ring - 1] + straw;
1015  return CCDBIndex;
1016 }
1017 
DRFTime_factory * locRFTimeFactory
jerror_t fini(void)
Called after last event of last event source has been processed.
int end
Definition: DTOFHit.h:23
#define SPEED_OF_LIGHT
Int_t layer
bool has_TDC
Definition: DSCHit.h:25
int plane
Definition: DTOFHit.h:21
bool has_fADC
Definition: DTOFHit.h:29
double fval
Definition: DEPICSvalue.h:68
int row
Definition: DTAGMHit.h:20
bool has_TDC
Definition: DTAGHHit.h:26
sprintf(text,"Post KinFit Cut")
const DVector3 & position(void) const
const DTrackTimeBased * Get_TrackTimeBased(void) const
uint32_t Get_L1FrontPanelTriggerBits(void) const
shared_ptr< const DTOFHitMatchParams > Get_TOFHitMatchParams(void) const
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
JApplication * japp
void Fill2DWeightedHistogram(const char *plugin, const char *directoryName, const char *name, const double valueX, const double valueY, const double weight, const char *title, int nBinsX, double xmin, double xmax, int nBinsY, double ymin, double ymax, bool print=false)
void SortDirectories()
bool has_fADC
Definition: DTAGMHit.h:27
Definition: DSCHit.h:14
InitPlugin_t InitPlugin
DGeometry * GetDGeometry(unsigned int run_number)
#define _DBG_
Definition: HDEVIO.h:12
int ring
Definition: DCDCHit.h:18
bool has_TDC
Definition: DTAGMHit.h:27
void Fill1DHistogram(const char *plugin, const char *directoryName, const char *name, const double value, const char *title, int nBins, double xmin, double xmax, bool print=false)
static int Get_FDCTDC_crate_slot(int mod, string &act_crate, int &act_slot)
string name
Definition: DEPICSvalue.h:64
bool has_TDC
Definition: DTOFHit.h:30
void Fill2DHistogram(const char *plugin, const char *directoryName, const char *name, const double valueX, const double valueY, const char *title, int nBinsX, double xmin, double xmax, int nBinsY, double ymin, double ymax, bool print=false)
float t
Definition: DBCALHit.h:31
shared_ptr< const DSCHitMatchParams > Get_SCHitMatchParams(void) const
void cdc_hit(Int_t &, Int_t &, Int_t &, Int_t[], Int_t, Int_t, Int_t, Int_t, Int_t, Int_t)
Definition: fa125fns.h:55
#define Z_TARGET
Definition: DRiemannFit.cc:12
shared_ptr< const DFCALShowerMatchParams > Get_FCALShowerMatchParams(void) const
vector< DTrackFitter::pull_t > pulls
Holds pulls used in chisq calc. (not including off-diagonals)
int channel(int row, int column) const
const double k_to_nsec
Definition: units.h:72
bool has_fADC
Definition: DTAGHHit.h:26
int bar
Definition: DTOFHit.h:22
bool has_fADC
Definition: DSCHit.h:24
shared_ptr< const DBCALShowerMatchParams > Get_BCALShowerMatchParams(void) const
File: DTOFHit.h Created: Tue Jan 18 16:15:26 EST 2011 Creator: B. Zihlmann Purpose: Container class t...
Definition: DTOFHit.h:16
int column
Definition: DTAGMHit.h:21
int GetCCDBIndexTAGM(unsigned int column, unsigned int row)
unsigned int dNumParticleVotes
Definition: DEventRFBunch.h:32
double Step_TimeToNearInputTime(double locTimeToStep, double locTimeToStepTo) const
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
bool GetTargetZ(double &z_target) const
z-location of center of target
Definition: DGeometry.cc:1933
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
int straw
Definition: DCDCHit.h:19
int numActiveBlocks() const
Definition: DFCALGeometry.h:56
A DEPICSvalue object holds information for a single EPICS value read from the data stream...
Definition: DEPICSvalue.h:37