Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calibration/HLDetectorTiming/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 "CCAL/DCCALGeometry.h"
22 #include "TRIGGER/DTrigger.h"
23 #include "HistogramTools.h"
24 
27 #include "CCAL/DCCALHit.h"
28 #include "CCAL/DCCALShower.h"
29 #include "DIRC/DDIRCPmtHit.h"
31 
32 extern "C"{
33 void InitPlugin(JApplication *app){
34  InitJANAPlugin(app);
35  app->AddProcessor(new JEventProcessor_HLDetectorTiming());
36  app->AddFactoryGenerator(new DFactoryGenerator_p2pi()); //register the factory generator
37 }
38 } // "C"
39 
40 static int Get_FDCTDC_crate_slot(int mod, string &act_crate, int &act_slot){ //expected mod range from 1 to 48
41  int LH_module=(mod-1)%2; //low (1-48) or high (49-96) wire number (0/1)
42  int package=(mod-1)/12; //package number (0-3)
43  int cell=(mod-1-package*12)/2; //cell number (0-5)
44 
45  int rotation = -45 + 90*LH_module -60*cell;
46  if(rotation<-180)rotation+=360;
47 
48  int crate=0; // (0-3) actual crates are ROCFDC1,4,13,14
49  if(package<2){
50  crate=0;
51  if(rotation>0)crate=3;
52  } else {
53  crate=1;
54  if(rotation>0)crate=2;
55  }
56 
57  int slot=0; //(0-11) actual slots are 3-10,13-16
58  if(rotation<0){
59  if(cell==0){
60  slot=0;
61  } else if (cell==1) {
62  slot=1+LH_module;
63  } else if (cell==2) {
64  slot=3+LH_module;
65  } else {
66  slot=5;
67  }
68  } else {
69  if(cell==0){
70  slot=0;
71  } else if (cell==3) {
72  slot=1;
73  } else if (cell==4) {
74  slot=2+LH_module;
75  } else {
76  slot=4+LH_module;
77  }
78  }
79  slot+=(package%2)*6;
80 
81  //string act_crate="ROCFDC1";
82  act_crate="ROCFDC1";
83  if(crate==1)act_crate="ROCFDC4";
84  if(crate==2)act_crate="ROCFDC13";
85  if(crate==3)act_crate="ROCFDC14";
86  //int act_slot=slot+3;
87  act_slot=slot+3;
88  if(act_slot>10)act_slot+=2;
89 
90  //cout<<" "<<act_crate<<endl;
91  //cout<<" actual slot="<<act_slot<<endl;
92 
93  return crate*12+slot+1; //returns modules in crate/slot sequence (1-48)
94 
95 }
96 
97 
98 //------------------
99 // JEventProcessor_HLDetectorTiming (Constructor)
100 //------------------
102 {
103 
104 }
105 
106 //------------------
107 // ~JEventProcessor_HLDetectorTiming (Destructor)
108 //------------------
110 {
111 
112 }
113 
114 //------------------
115 // init
116 //------------------
118 {
119  BEAM_CURRENT = 50; // Assume that there is beam until first EPICs event. Set from EPICS evio data, can override on command line
120 
121  fBeamEventCounter = 0;
122  dMaxDIRCChannels = 108*64;
123 
124  REQUIRE_BEAM = 0;
125  BEAM_EVENTS_TO_KEEP = 1000000000; // Set enormously high
126  DO_ROUGH_TIMING = 0;
127  DO_CDC_TIMING = 0;
128  DO_TDC_ADC_ALIGN = 0;
129  DO_TRACK_BASED = 0;
130  DO_VERIFY = 1;
131  DO_OPTIONAL = 0;
132  DO_REACTION = 0;
133  DO_HIGH_RESOLUTION = 0;
134 
135  USE_RF_BUNCH = 1;
136 
137  NO_TRACKS = false;
138 
139  if(gPARMS){
140  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_ROUGH_TIMING", DO_ROUGH_TIMING, "Set to > 0 to do rough timing of all detectors");
141  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_CDC_TIMING", DO_CDC_TIMING, "Set to > 0 to do CDC Per channel Alignment");
142  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_TDC_ADC_ALIGN", DO_TDC_ADC_ALIGN, "Set to > 0 to do TDC/ADC alignment of SC,TOF,TAGM,TAGH");
143  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_TRACK_BASED", DO_TRACK_BASED, "Set to > 0 to do Track Based timing corrections");
144  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_HIGH_RESOLUTION", DO_HIGH_RESOLUTION, "Set to > 0 to increase the resolution of the track Based timing corrections");
145  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_VERIFY", DO_VERIFY, "Set to > 0 to verify timing with current constants");
146  gPARMS->SetDefaultParameter("HLDETECTORTIMING:REQUIRE_BEAM", REQUIRE_BEAM, "Set to 0 to skip beam current check");
147  gPARMS->SetDefaultParameter("HLDETECTORTIMING:BEAM_EVENTS_TO_KEEP", BEAM_EVENTS_TO_KEEP, "Set to the number of beam on events to use");
148  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_OPTIONAL", DO_OPTIONAL, "Set to >0 to enable optional histograms ");
149  gPARMS->SetDefaultParameter("HLDETECTORTIMING:DO_REACTION", DO_REACTION, "Set to >0 to run DReaction");
150  gPARMS->SetDefaultParameter("HLDETECTORTIMING:USE_RF_BUNCH", USE_RF_BUNCH, "Set to 0 to disable use of 2 vote RF Bunch");
151  gPARMS->SetDefaultParameter("HLDETECTORTIMING:NO_TRACKS", NO_TRACKS, "Don't use tracking information for timing calibrations");
152  }
153 
154  // Would like the code with no arguments to simply verify the current status of the calibration
155  if (DO_ROUGH_TIMING > 0 || DO_CDC_TIMING > 0 || DO_TDC_ADC_ALIGN > 0 || DO_TRACK_BASED > 0) DO_VERIFY = 0;
156 
157  // Increase range for initial search
158  if(DO_TDC_ADC_ALIGN){
159  NBINS_TDIFF = 2800; MIN_TDIFF = -150.0; MAX_TDIFF = 550.0;
160  }
161  else{
162  NBINS_TDIFF = 200; MIN_TDIFF = -40.0; MAX_TDIFF = 40.0;
163  }
164 
165  // DEBUG
166  //NBINS_TDIFF = 2800; MIN_TDIFF = -200.0; MAX_TDIFF = 500.0;
167 
168 
169  if (DO_TRACK_BASED){
170  if (DO_HIGH_RESOLUTION) {
171  NBINS_TAGGER_TIME = 400; MIN_TAGGER_TIME = -20; MAX_TAGGER_TIME = 20;
172  NBINS_MATCHING = 1000; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
173  } else {
174  NBINS_TAGGER_TIME = 1600; MIN_TAGGER_TIME = -200; MAX_TAGGER_TIME = 400;
175  //NBINS_MATCHING = 1000; MIN_MATCHING_T = -100; MAX_MATCHING_T = 400;
176  NBINS_MATCHING = 800; MIN_MATCHING_T = -100; MAX_MATCHING_T = 100;
177  }
178  } else if (DO_VERIFY){
179  NBINS_TAGGER_TIME = 200; MIN_TAGGER_TIME = -20; MAX_TAGGER_TIME = 20;
180  NBINS_MATCHING = 1000; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
181  } else{
182  NBINS_TAGGER_TIME = 100; MIN_TAGGER_TIME = -50; MAX_TAGGER_TIME = 50;
183  NBINS_MATCHING = 100; MIN_MATCHING_T = -10; MAX_MATCHING_T = 10;
184  }
185 
186  NBINS_RF_COMPARE = 200; MIN_RF_COMPARE = -2.2; MAX_RF_COMPARE = 2.2;
187 
188  return NOERROR;
189 }
190 
191 //------------------
192 // brun
193 //------------------
194 jerror_t JEventProcessor_HLDetectorTiming::brun(JEventLoop *eventLoop, int32_t runnumber)
195 {
196  // This is called whenever the run number changes
197  DApplication* app = dynamic_cast<DApplication*>(eventLoop->GetJApplication());
198  DGeometry* geom = app->GetDGeometry(runnumber);
199  geom->GetTargetZ(Z_TARGET);
200 
201  return NOERROR;
202 }
203 
204 //------------------
205 // evnt
206 //------------------
207 jerror_t JEventProcessor_HLDetectorTiming::evnt(JEventLoop *loop, uint64_t eventnumber)
208 {
209  // select events with physics events, i.e., not LED and other front panel triggers
210  const DTrigger* locTrigger = NULL;
211  loop->GetSingle(locTrigger);
212  if(locTrigger->Get_L1FrontPanelTriggerBits() != 0)
213  return NOERROR;
214 
215  if(!locTrigger->Get_IsPhysicsEvent())
216  return NOERROR;
217 
218  // Get the particleID object for each run
219  vector<const DParticleID *> locParticleID_algos;
220  loop->Get(locParticleID_algos);
221  if(locParticleID_algos.size()<1){
222  _DBG_<<"Unable to get a DParticleID object! NO PID will be done!"<<endl;
223  return RESOURCE_UNAVAILABLE;
224  }
225  auto locParticleID = locParticleID_algos[0];
226 
227  // We want to be use some of the tools available in the RFTime factory
228  // Specifivally steping the RF back to a chosen time
229  auto dRFTimeFactory = static_cast<DRFTime_factory*>(loop->GetFactory("DRFTime"));
230 
231  vector<const DDIRCGeometry*> locDIRCGeometryVec;
232  loop->Get(locDIRCGeometryVec);
233  const DDIRCGeometry* locDIRCGeometry = locDIRCGeometryVec[0];
234 
235  // Initialize DIRC LUT
236  const DDIRCLut* dDIRCLut = nullptr;
237  loop->GetSingle(dDIRCLut);
238 
239  // Get the EPICs events and update beam current. Skip event if current too low (<10 nA).
240  vector<const DEPICSvalue *> epicsValues;
241  loop->Get(epicsValues);
242  for(unsigned int j = 0; j < epicsValues.size(); j++){
243  const DEPICSvalue *thisValue = epicsValues[j];
244  if (strcmp((thisValue->name).c_str(), "IBCAD00CRCUR6") == 0){
245  BEAM_CURRENT = thisValue->fval;
246  Fill1DHistogram("HLDetectorTiming", "", "Beam Current",
247  BEAM_CURRENT,
248  "Beam Current; Beam Current [nA]; Entries",
249  100, 0, 200);
250  }
251  //cout << "EPICS Name " << (thisValue->name).c_str() << " Value " << thisValue->fval << endl;
252  }
253  // There is a caveat here when running multithreaded
254  // Another thread might be the one to catch the EPICS event
255  // and there is no way to reject events that may have come from earilier
256  // Expect number of entries in histograms to vary slightly over the same file with many threads
257  if (BEAM_CURRENT < 10.0) {
258  Fill1DHistogram("HLDetectorTiming", "" , "Beam Events",
259  0, "Beam On Events (0 = no beam, 1 = beam > 10nA)",
260  2, -0.5, 1.5);
261  if (REQUIRE_BEAM){
262  return NOERROR; // Skip events where we can't verify the beam current
263  }
264  }
265  else{
266  Fill1DHistogram("HLDetectorTiming", "" , "Beam Events",
267  1, "Beam On Events (0 = no beam, 1 = beam > 10nA)",
268  2, -0.5, 1.5);
269  fBeamEventCounter++;
270  }
271 
272  if (fBeamEventCounter >= BEAM_EVENTS_TO_KEEP) { // Able to specify beam ON events instead of just events
273  cout<< "Maximum number of Beam Events reached" << endl;
274  japp->Quit();
275  return NOERROR;
276  }
277 
278  // Get the objects from the event loop
279  vector<const DCDCHit *> cdcHitVector;
280  vector<const DFDCHit *> fdcHitVector;
281  vector<const DSCHit *> scHitVector;
282  vector<const DBCALUnifiedHit *> bcalUnifiedHitVector;
283  vector<const DTOFHit *> tofHitVector;
284  vector<const DFCALHit *> fcalHitVector;
285  vector<const DCCALHit *> ccalHitVector;
286  vector<const DDIRCPmtHit *> dircPmtHitVector;
287  vector<const DTAGMHit *> tagmHitVector;
288  vector<const DTAGHHit *> taghHitVector;
289  vector<const DPSHit *> psHitVector;
290  vector<const DPSCHit *> pscHitVector;
291 
292  loop->Get(cdcHitVector);
293  loop->Get(fdcHitVector);
294  loop->Get(scHitVector);
295  loop->Get(bcalUnifiedHitVector);
296  loop->Get(tofHitVector);
297  loop->Get(fcalHitVector);
298  loop->Get(ccalHitVector);
299  loop->Get(dircPmtHitVector);
300  loop->Get(psHitVector);
301  loop->Get(pscHitVector);
302  loop->Get(tagmHitVector, "Calib");
303  loop->Get(taghHitVector, "Calib");
304 
305  // TTabUtilities object used for RF time conversion
306  const DTTabUtilities* locTTabUtilities = NULL;
307  loop->GetSingle(locTTabUtilities);
308 
309  unsigned int i = 0;
310  int nBins = 2000;
311  float xMin = -500, xMax = 1500;
312  for (i = 0; i < cdcHitVector.size(); i++){
313  Fill1DHistogram ("HLDetectorTiming", "CDC", "CDCHit time", cdcHitVector[i]->t,
314  "CDCHit time;t [ns];", nBins, xMin, xMax);
315  if(DO_VERIFY || DO_CDC_TIMING){
316  int nStraws = 3522;
317  Fill2DHistogram("HLDetectorTiming", "CDC", "CDCHit time per Straw Raw",
318  cdcHitVector[i]->t, GetCCDBIndexCDC(cdcHitVector[i]),
319  "Hit time for each CDC wire; t [ns]; CCDB Index",
320  750, -500, 1000, nStraws, 0.5, nStraws + 0.5);
321  }
322  }
323 
324  for (i = 0; i < fdcHitVector.size(); i++){
325  if(fdcHitVector[i]->type == 0 ) {
326  Fill1DHistogram ("HLDetectorTiming", "FDC", "FDCHit Wire time", fdcHitVector[i]->t,
327  "FDCHit Wire time;t [ns];", nBins, xMin, xMax);
328  // Keep track of module/crate level shifts
329  // two F1TDC modules per wire layer
330  int module = 2 * fdcHitVector[i]->gLayer - 1; // layers start counting at 1
331  if(fdcHitVector[i]->element > 48)
332  module++;
333  Fill2DHistogram ("HLDetectorTiming", "FDC", "FDCHit Wire time vs. module",
334  module, fdcHitVector[i]->t,
335  "FDCHit Wire time; module/slot; t [ns];",
336  48, 0.5, 48.5, 400, -200, 600);
337 
338  }
339  else{
340  Fill1DHistogram ("HLDetectorTiming", "FDC", "FDCHit Cathode time", fdcHitVector[i]->t,
341  "FDCHit Cathode time;t [ns];", nBins, xMin, xMax);
342  }
343  }
344 
345  for (i = 0; i < scHitVector.size(); i++){
346  //if(!scHitVector[i]->has_fADC || !scHitVector[i]->has_TDC) continue;
347  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit time", scHitVector[i]->t,
348  "SCHit time;t [ns];", nBins, xMin, xMax);
349  }
350 
351  for (i = 0; i < dircPmtHitVector.size(); i++){
352  Fill1DHistogram ("HLDetectorTiming", "DIRC", "DIRCHit time", dircPmtHitVector[i]->t,
353  "DIRCHit time;t [ns];", nBins, xMin, xMax);
354 
355  if(dircPmtHitVector[i]->ch < DDIRCPmtHit_factory::DIRC_MAX_CHANNELS) {
356  Fill2DHistogram ("HLDetectorTiming", "DIRC", "DIRCHit North Per Channel TDC Hit Time",
357  dircPmtHitVector[i]->ch, dircPmtHitVector[i]->t,
358  "DIRCHit North Per Channel TDC Hit Time; channel ID; t_{TDC} [ns] ",
360  (double)DDIRCPmtHit_factory::DIRC_MAX_CHANNELS+0.5, 500, -50, 150);
361  } else {
362  Fill2DHistogram ("HLDetectorTiming", "DIRC", "DIRCHit South Per Channel TDC Hit Time",
363  dircPmtHitVector[i]->ch-DDIRCPmtHit_factory::DIRC_MAX_CHANNELS, dircPmtHitVector[i]->t,
364  "DIRCHit South Per Channel TDC Hit Time; channel ID; t_{TDC} [ns] ",
366  (double)DDIRCPmtHit_factory::DIRC_MAX_CHANNELS+0.5, 500, -50, 150);
367  }
368  }
369 
370  for (i = 0; i < bcalUnifiedHitVector.size(); i++){
371  int the_cell = (bcalUnifiedHitVector[i]->module - 1) * 16 + (bcalUnifiedHitVector[i]->layer - 1) * 4 + bcalUnifiedHitVector[i]->sector;
372  // There is one less layer of TDCs so the numbering relects this
373  int the_tdc_cell = (bcalUnifiedHitVector[i]->module - 1) * 12 + (bcalUnifiedHitVector[i]->layer - 1) * 4 + bcalUnifiedHitVector[i]->sector;
374  // Get the underlying associated objects
375  const DBCALHit * thisADCHit;
376  const DBCALTDCHit * thisTDCHit;
377  bcalUnifiedHitVector[i]->GetSingle(thisADCHit);
378  bcalUnifiedHitVector[i]->GetSingle(thisTDCHit);
379 
380  if (thisADCHit != NULL){ //This should never be NULL but might as well check
381  Fill1DHistogram ("HLDetectorTiming", "BCAL", "BCALHit ADC time", thisADCHit->t,
382  "BCALHit ADC time; t_{ADC} [ns]; Entries", nBins, xMin, xMax);
383 
384  if (DO_OPTIONAL){
385  if (bcalUnifiedHitVector[i]->end == 0){
386  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel ADC Hit Time",
387  the_cell, thisADCHit->t,
388  "BCALHit Upstream Per Channel Hit Time; cellID; t_{ADC} [ns] ",
389  768, 0.5, 768.5, 250, -50, 50);
390  }
391  else{
392  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel ADC Hit Time",
393  the_cell, thisADCHit->t,
394  "BCALHit Downstream Per Channel Hit Time; cellID; t_{ADC} [ns] ",
395  768, 0.5, 768.5, 250, -50, 50);
396  }
397  }
398  }
399 
400  if (thisTDCHit != NULL){
401  Fill1DHistogram ("HLDetectorTiming", "BCAL", "BCALHit TDC time", thisTDCHit->t,
402  "BCALHit TDC time; t_{TDC} [ns]; Entries", nBins, xMin, xMax);
403 
404  if (DO_OPTIONAL){
405  if (bcalUnifiedHitVector[i]->end == 0){
406  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel TDC Hit Time",
407  the_tdc_cell, thisTDCHit->t,
408  "BCALHit Upstream Per Channel TDC Hit Time; cellID; t_{TDC} [ns] ",
409  576, 0.5, 576.5, 350, -50, 300);
410  }
411  else{
412  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel TDC Hit Time",
413  the_tdc_cell, thisTDCHit->t,
414  "BCALHit Downstream Per Channel TDC Hit Time; cellID; t_{TDC} [ns] ",
415  576, 0.5, 576.5, 350, -50, 300);
416  }
417  }
418  }
419 
420  if (thisADCHit != NULL && thisTDCHit != NULL){
421  if (bcalUnifiedHitVector[i]->end == 0){
422  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Upstream Per Channel TDC-ADC Hit Time",
423  the_tdc_cell, thisTDCHit->t - thisADCHit->t,
424  "BCALHit Upstream Per Channel TDC-ADC Hit Time; cellID; t_{TDC} - t_{ADC} [ns] ",
425  576, 0.5, 576.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
426  }
427  else{
428  Fill2DHistogram ("HLDetectorTiming", "BCAL", "BCALHit Downstream Per Channel TDC-ADC Hit Time",
429  the_tdc_cell, thisTDCHit->t - thisADCHit->t,
430  "BCALHit Downstream Per Channel TDC-ADC Hit Time; cellID; t_{TDC} - t_{ADC} [ns] ",
431  576, 0.5, 576.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
432  }
433  }
434  }
435 
436  for (i = 0; i < tofHitVector.size(); i++){
437  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit time", tofHitVector[i]->t,
438  "TOFHit time;t [ns];", nBins, xMin, xMax);
439  }
440 
441  for (i = 0; i < psHitVector.size(); i++){
442  int nColumns = 145*2;
443  Fill1DHistogram ("HLDetectorTiming", "PS", "PSHit time", psHitVector[i]->t,
444  "PSHit time;t [ns];", nBins, xMin, xMax);
445 
446  Fill2DHistogram("HLDetectorTiming", "PS", "PSHit time per Column",
447  psHitVector[i]->t, psHitVector[i]->column+psHitVector[i]->arm*nColumns/2, //GetCCDBIndexPS(psHitVector[i]),
448  "Hit time for each PS column; t [ns]; CCDB Index",
449  nBins, xMin, xMax, nColumns, 0.5, nColumns + 0.5);
450  }
451 
452  // from FCAL_online: find energy weighted average time for FCAL hits, useful as a t0
453  double fcalHitETot = 0;
454  double fcalHitEwtT = 0;
455  for (i = 0; i < fcalHitVector.size(); i++){
456  fcalHitETot += fcalHitVector[i]->E;
457  fcalHitEwtT += fcalHitVector[i]->E * fcalHitVector[i]->t;
458  }
459  fcalHitEwtT /= fcalHitETot;
460 
461  // extract the FCAL Geometry
462  vector<const DFCALGeometry*> fcalGeomVect;
463  loop->Get( fcalGeomVect );
464  if (fcalGeomVect.size() < 1){
465  cout << "FCAL Geometry not available?" << endl;
466  return OBJECT_NOT_AVAILABLE;
467  }
468  const DFCALGeometry& fcalGeom = *(fcalGeomVect[0]);
469 
470  for (i = 0; i < fcalHitVector.size(); i++){
471  Fill1DHistogram ("HLDetectorTiming", "FCAL", "FCALHit time", fcalHitVector[i]->t,
472  "FCALHit time;t [ns];", nBins, xMin, xMax);
473 
474  Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Occupancy",
475  fcalHitVector[i]->row, fcalHitVector[i]->column,
476  "FCAL Hit Occupancy; column; row",
477  61, -1.5, 59.5, 61, -1.5, 59.5);
478  double locTime = ( fcalHitVector[i]->t - fcalHitEwtT )*k_to_nsec;
479  //Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Local Time",
480  Fill2DWeightedHistogram("HLDetectorTiming", "FCAL", "FCALHit Local Time",
481  fcalHitVector[i]->row, fcalHitVector[i]->column, locTime,
482  "FCAL Hit Local Time [ns]; column; row",
483  61, -1.5, 59.5, 61, -1.5, 59.5);
484 
485  if (DO_OPTIONAL){
486  Fill2DHistogram("HLDetectorTiming", "FCAL", "FCALHit Per Channel Time",
487  fcalGeom.channel(fcalHitVector[i]->row, fcalHitVector[i]->column), fcalHitVector[i]->t,
488  "FCAL Per Channel Hit time; channel; t [ns]",
489  fcalGeom.numActiveBlocks(), 0.5, fcalGeom.numActiveBlocks() + 0.5, 250, -50, 50);
490  }
491  }
492 
493  Fill1DHistogram ("HLDetectorTiming", "FCAL", "FCAL total energy", fcalHitETot,
494  "FCAL total energy;", 400, 0, 8000);
495 
496 
497  // Do the same thing for the CCAL as a start
498  double ccalHitETot = 0;
499  double ccalHitEwtT = 0;
500  for (i = 0; i < ccalHitVector.size(); i++){
501  ccalHitETot += ccalHitVector[i]->E;
502  ccalHitEwtT += ccalHitVector[i]->E * ccalHitVector[i]->t;
503  }
504  ccalHitEwtT /= ccalHitETot;
505 
506  // extract the CCAL Geometry
507  vector<const DCCALGeometry*> ccalGeomVect;
508  loop->Get( ccalGeomVect );
509  if (ccalGeomVect.size() < 1){
510  cout << "CCAL Geometry not available?" << endl;
511  } else {
512  for (i = 0; i < ccalHitVector.size(); i++){
513  Fill1DHistogram ("HLDetectorTiming", "CCAL", "CCALHit time", ccalHitVector[i]->t,
514  "CCALHit time;t [ns];", nBins, xMin, xMax);
515 
516  const DCCALGeometry& ccalGeom = *(ccalGeomVect[0]);
517  for (i = 0; i < ccalHitVector.size(); i++) {
518  Fill2DHistogram("HLDetectorTiming", "CCAL", "CCALHit Occupancy",
519  ccalHitVector[i]->row, ccalHitVector[i]->column,
520  "CCAL Hit Occupancy; column; row",
521  13, -1.5, 11.5, 13, -1.5, 11.5);
522  double locTime = ( ccalHitVector[i]->t - ccalHitEwtT )*k_to_nsec;
523  //Fill2DHistogram("HLDetectorTiming", "CCAL", "CCALHit Local Time",
524  Fill2DWeightedHistogram("HLDetectorTiming", "CCAL", "CCALHit Local Time",
525  ccalHitVector[i]->row, ccalHitVector[i]->column, locTime,
526  "CCAL Hit Local Time [ns]; column; row",
527  13, -1.5, 11.5, 13, -1.5, 11.5);
528  }
529  }
530  }
531 
532  for (i = 0; i < tagmHitVector.size(); i++){
533  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit time", tagmHitVector[i]->t,
534  "TAGMHit time;t [ns];", nBins, xMin, xMax);
535  }
536  for (i = 0; i < taghHitVector.size(); i++){
537  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit time", taghHitVector[i]->t,
538  "TAGHHit time;t [ns];", nBins, xMin, xMax);
539  }
540 
541  // The detectors with both TDCs and ADCs need these two to be aligned
542  // These detectors are the SC,TAGM,TAGH,TOF,PSC
543 
544  // Break these histograms up into hits coming from the TDC and hits coming from the ADC
545  for (i = 0; i < scHitVector.size(); i++){
546  int nSCCounters = 30;
547  const DSCHit *thisSCHit = scHitVector[i];
548  if (thisSCHit->has_fADC && !thisSCHit->has_TDC){
549  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit ADC time", scHitVector[i]->t,
550  "SCHit ADC only time;t [ns];", nBins, xMin, xMax);
551  // Manual loop over hits to match out of time
552  for (auto hit = scHitVector.begin(); hit != scHitVector.end(); hit++){
553  if ((*hit)->has_TDC && !(*hit)->has_fADC){
554  if (scHitVector[i]->sector == (*hit)->sector){
555  Fill2DHistogram("HLDetectorTiming", "SC", "SCHit TDC_ADC Difference",
556  scHitVector[i]->sector, (*hit)->t_TDC - scHitVector[i]->t_fADC,
557  "SC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nSCCounters, 0.5, nSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
558  }
559  }
560  }
561  }
562  else if (!thisSCHit->has_fADC && thisSCHit->has_TDC){
563  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit TDC time", scHitVector[i]->t,
564  "SCHit TDC only time;t [ns];", nBins, xMin, xMax);
565  }
566  else{
567  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit Matched time", scHitVector[i]->t,
568  "SCHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
569  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit ADC time", scHitVector[i]->t_fADC,
570  "SCHit ADC only time;t [ns];", nBins, xMin, xMax);
571  Fill1DHistogram ("HLDetectorTiming", "SC", "SCHit TDC time", scHitVector[i]->t_TDC,
572  "SCHit TDC only time;t [ns];", nBins, xMin, xMax);
573 
574  Fill2DHistogram("HLDetectorTiming", "SC", "SCHit TDC_ADC Difference",
575  scHitVector[i]->sector, scHitVector[i]->t_TDC - scHitVector[i]->t_fADC,
576  "SC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nSCCounters, 0.5, nSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
577  }
578 
579  }
580  for (i = 0; i < tagmHitVector.size(); i++){
581 
582  const DTAGMHit *thisTAGMHit = tagmHitVector[i];
583  int nTAGMCounters = 122; // 102 + 20 including 4 fully read out columns
584 
585  if(thisTAGMHit->has_fADC && !thisTAGMHit->has_TDC){
586  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit ADC time", tagmHitVector[i]->t,
587  "TAGMHit ADC only time;t [ns];", nBins, xMin, xMax);
588  // Manual loop over hits to match out of time
589  for (auto hit = tagmHitVector.begin(); hit != tagmHitVector.end(); hit++){
590  if ((*hit)->has_TDC && !(*hit)->has_fADC){
591  if (GetCCDBIndexTAGM(tagmHitVector[i]) == GetCCDBIndexTAGM(*hit)){
592  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGMHit TDC_ADC Difference",
593  GetCCDBIndexTAGM(tagmHitVector[i]), (*hit)->t - tagmHitVector[i]->time_fadc,
594  //GetCCDBIndexTAGM(tagmHitVector[i]), (*hit)->time_tdc - tagmHitVector[i]->time_fadc,
595  "TAGM #Deltat TDC-ADC; Column ;t_{TDC} - t_{ADC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
596  }
597  }
598  }
599  }
600  else if (!thisTAGMHit->has_fADC && thisTAGMHit->has_TDC){
601  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit TDC time", tagmHitVector[i]->t,
602  "TAGMHit TDC only time;t [ns];", nBins, xMin, xMax);
603  }
604  else{
605  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit Matched time", tagmHitVector[i]->t,
606  "TAGMHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
607  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit ADC time", tagmHitVector[i]->time_fadc,
608  "TAGMHit ADC only time;t [ns];", nBins, xMin, xMax);
609  Fill1DHistogram ("HLDetectorTiming", "TAGM", "TAGMHit TDC time", tagmHitVector[i]->t,
610  "TAGMHit TDC only time;t [ns];", nBins, xMin, xMax);
611 
612  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGMHit TDC_ADC Difference",
613  GetCCDBIndexTAGM(tagmHitVector[i]), tagmHitVector[i]->t - tagmHitVector[i]->time_fadc,
614  "TAGM #Deltat TDC-ADC; Column ;t_{TDC} - t_{ADC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
615  if (DO_OPTIONAL){
616  Fill2DHistogram("HLDetectorTiming", "TAGM", "TAGM Per Channel TDC Time",
617  GetCCDBIndexTAGM(tagmHitVector[i]), tagmHitVector[i]->t,
618  "TAGM Per Channel TDC time; Column ;t_{TDC} [ns]", nTAGMCounters, 0.5, nTAGMCounters + 0.5, 100, -50, 50);
619  }
620 
621  }
622 
623  }
624  for (i = 0; i < taghHitVector.size(); i++){
625 
626  const DTAGHHit *thisTAGHHit = taghHitVector[i];
627  int nTAGHCounters = 274;
628 
629  if(thisTAGHHit->has_fADC && !thisTAGHHit->has_TDC){
630  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit ADC time", taghHitVector[i]->t,
631  "TAGHHit ADC only time;t [ns];", nBins, xMin, xMax);
632  // Manual loop over hits to match out of time
633  for (auto hit = taghHitVector.begin(); hit != taghHitVector.end(); hit++){
634  if ((*hit)->has_TDC && !(*hit)->has_fADC){
635  if (taghHitVector[i]->counter_id == (*hit)->counter_id){
636  Fill2DHistogram("HLDetectorTiming", "TAGH", "TAGHHit TDC_ADC Difference",
637  taghHitVector[i]->counter_id, (*hit)->time_tdc - taghHitVector[i]->time_fadc,
638  "TAGH #Deltat TDC-ADC; Counter ID ;t_{TDC} - t_{ADC} [ns]", nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
639  }
640  }
641  }
642  }
643  else if (!thisTAGHHit->has_fADC && thisTAGHHit->has_TDC){
644  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit TDC time", taghHitVector[i]->t,
645  "TAGHHit TDC only time;t [ns];", nBins, xMin, xMax);
646  }
647  else{
648  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit Matched time", taghHitVector[i]->t,
649  "TAGHHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
650  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit ADC time", taghHitVector[i]->time_fadc,
651  "TAGHHit ADC only time;t [ns];", nBins, xMin, xMax);
652  Fill1DHistogram ("HLDetectorTiming", "TAGH", "TAGHHit TDC time", taghHitVector[i]->time_tdc,
653  "TAGHHit TDC only time;t [ns];", nBins, xMin, xMax);
654 
655  // We want to look at the timewalk within these ADC/TDC detectors
656  Fill2DHistogram("HLDetectorTiming", "TAGH", "TAGHHit TDC_ADC Difference",
657  taghHitVector[i]->counter_id, taghHitVector[i]->time_tdc - taghHitVector[i]->time_fadc,
658  "TAGH #Deltat TDC-ADC; Counter ID ;t_{TDC} - t_{ADC} [ns]", nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
659  }
660  }
661  for (i = 0; i < tofHitVector.size(); i++){
662  const DTOFHit *thisTOFHit = tofHitVector[i];
663  int nTOFCounters = 176;
664  if(thisTOFHit->has_fADC && !thisTOFHit->has_TDC){
665  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit ADC time", tofHitVector[i]->t,
666  "TOFHit ADC only time;t [ns];", nBins, xMin, xMax);
667  // Manual loop over hits to match out of time
668  for (auto hit = tofHitVector.begin(); hit != tofHitVector.end(); hit++){
669  if ((*hit)->has_TDC && !(*hit)->has_fADC){
670  if (GetCCDBIndexTOF(tofHitVector[i]) == GetCCDBIndexTOF(*hit)){
671  Fill2DHistogram("HLDetectorTiming", "TOF", "TOFHit TDC_ADC Difference",
672  GetCCDBIndexTOF(tofHitVector[i]), (*hit)->t_TDC - tofHitVector[i]->t_fADC,
673  "TOF #Deltat TDC-ADC; CDCB Index ;t_{TDC} - t_{ADC} [ns]", nTOFCounters, 0.5, nTOFCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
674  }
675  }
676  }
677  }
678  else if (!thisTOFHit->has_fADC && thisTOFHit->has_TDC){
679  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit TDC time", tofHitVector[i]->t,
680  "TOFHit TDC only time;t [ns];", nBins, xMin, xMax);
681  }
682  else{
683  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit Matched time", tofHitVector[i]->t,
684  "TOFHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
685  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit ADC time", tofHitVector[i]->t_fADC,
686  "TOFHit ADC only time;t [ns];", nBins, xMin, xMax);
687  Fill1DHistogram ("HLDetectorTiming", "TOF", "TOFHit TDC time", tofHitVector[i]->t_TDC,
688  "TOFHit TDC only time;t [ns];", nBins, xMin, xMax);
689 
690  Fill2DHistogram("HLDetectorTiming", "TOF", "TOFHit TDC_ADC Difference",
691  GetCCDBIndexTOF(tofHitVector[i]), tofHitVector[i]->t_TDC - tofHitVector[i]->t_fADC,
692  "TOF #Deltat TDC-ADC; CDCB Index ;t_{TDC} - t_{ADC} [ns]", nTOFCounters, 0.5, nTOFCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
693  }
694  }
695  for (i = 0; i < pscHitVector.size(); i++){
696  int nPSCCounters = 16;
697  const DPSCHit *thisPSCHit = pscHitVector[i];
698  if (thisPSCHit->has_fADC && !thisPSCHit->has_TDC){
699  Fill1DHistogram ("HLDetectorTiming", "PS", "PSCHit ADC time", pscHitVector[i]->t,
700  "PSCHit ADC only time;t [ns];", nBins, xMin, xMax);
701  // Manual loop over hits to match out of time
702  for (auto hit = pscHitVector.begin(); hit != pscHitVector.end(); hit++){
703  if ((*hit)->has_TDC && !(*hit)->has_fADC){
704  if ( (pscHitVector[i]->arm == (*hit)->arm) && (pscHitVector[i]->module == (*hit)->module) ) {
705  Fill2DHistogram("HLDetectorTiming", "PS", "PSCHit TDC_ADC Difference",
706  pscHitVector[i]->module+pscHitVector[i]->arm*nPSCCounters/2, (*hit)->time_tdc - pscHitVector[i]->time_fadc,
707  "PSC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nPSCCounters, 0.5, nPSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
708  }
709  }
710  }
711  }
712  else if (!thisPSCHit->has_fADC && thisPSCHit->has_TDC){
713  Fill1DHistogram ("HLDetectorTiming", "PS", "PSCHit TDC time", pscHitVector[i]->t,
714  "PSCHit TDC only time;t [ns];", nBins, xMin, xMax);
715  }
716  else{
717  Fill1DHistogram ("HLDetectorTiming", "PS", "PSCHit Matched time", pscHitVector[i]->t,
718  "PSCHit Matched ADC/TDC time;t [ns];", nBins, xMin, xMax);
719  Fill1DHistogram ("HLDetectorTiming", "PS", "PSCHit ADC time", pscHitVector[i]->time_fadc,
720  "PSCHit ADC only time;t [ns];", nBins, xMin, xMax);
721  Fill1DHistogram ("HLDetectorTiming", "PS", "PSCHit TDC time", pscHitVector[i]->time_tdc,
722  "PSCHit TDC only time;t [ns];", nBins, xMin, xMax);
723 
724  Fill2DHistogram("HLDetectorTiming", "PS", "PSCHit TDC_ADC Difference",
725  pscHitVector[i]->module+pscHitVector[i]->arm*nPSCCounters/2, pscHitVector[i]->time_tdc - pscHitVector[i]->time_fadc,
726  "PSC #Deltat TDC-ADC; Sector ;t_{TDC} - t_{ADC} [ns]", nPSCCounters, 0.5, nPSCCounters + 0.5, NBINS_TDIFF, MIN_TDIFF, MAX_TDIFF);
727  }
728  }
729 
730  // Next the relative times between detectors using tracking
731  // By the time we get to this point, our first guess at the timing should be fairly good.
732  // Certainly good enough to take a pass at the time based tracking
733  // This will be the final alignment step for now
734 
735  // We want to plot the delta t at the target between the SC hit and the tagger hits
736  // Some limits for these
737  float nBinsE = 160, EMin = 3.0, EMax = 12.0;
738 
739 
740  const DEventRFBunch *thisRFBunch = NULL;
741  if(NO_TRACKS) {
742  // If the drift chambers are turned off, we'll need to use the neutral showers
743  loop->GetSingle(thisRFBunch, "CalorimeterOnly");
744  } else {
745  loop->GetSingle(thisRFBunch, "Calibrations"); // SC only hits
746  }
747  if (thisRFBunch->dNumParticleVotes < 2) return NOERROR;
748 
749  // Loop over TAGM hits
750  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
751  int nTAGMColumns = 122; // Not really just columns, but a name is a name
752  if(tagmHitVector[j]->has_fADC){
753  char name [200];
754  char title[500];
755  sprintf(name, "Column %.3i Row %.1i", tagmHitVector[j]->column, tagmHitVector[j]->row);
756  sprintf(title, "TAGM Column %i t_{ADC} - t_{RF}; t_{ADC} - t_{RF} [ns]; Entries", tagmHitVector[j]->column);
757  double locShiftedTime = dRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, tagmHitVector[j]->time_fadc);
758  Fill1DHistogram("HLDetectorTiming", "TAGM_ADC_RF_Compare", name,
759  tagmHitVector[j]->time_fadc - locShiftedTime,
760  title,
761  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
762  }
763 
764  if(tagmHitVector[j]->has_TDC){
765  char name [200];
766  char title[500];
767  sprintf(name, "Column %.3i Row %.1i", tagmHitVector[j]->column, tagmHitVector[j]->row);
768  sprintf(title, "TAGM Column %i t_{TDC} - t_{RF}; t_{TDC} - t_{RF} [ns]; Entries", tagmHitVector[j]->column);
769  double locShiftedTime = dRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, tagmHitVector[j]->t);
770  Fill1DHistogram("HLDetectorTiming", "TAGM_TDC_RF_Compare", name,
771  tagmHitVector[j]->t - locShiftedTime,
772  title,
773  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
774  }
775 
776  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch Time",
777  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - thisRFBunch->dTime,
778  "#Deltat TAGM-RFBunch; CCDB Index ;t_{TAGM} - t_{SC @ target} [ns]",
779  nTAGMColumns, 0.5, nTAGMColumns + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
780  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time",
781  tagmHitVector[j]->t - thisRFBunch->dTime, tagmHitVector[j]->E,
782  "Tagger - RFBunch Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
783  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
784  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time",
785  tagmHitVector[j]->t - thisRFBunch->dTime,
786  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
787  //160, -20, 20);
788  800, -50, 50);
789  if (tagmHitVector[j]->row == 0){
790  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch 1D Time",
791  tagmHitVector[j]->t - thisRFBunch->dTime,
792  "TAGM - RFBunch Time; #Deltat_{TAGM - RFBunch} [ns]; Entries",
793  //480, -30, 30);
794  800, -50, 50);
795  }
796  }
797 
798  // Loop over TAGH hits
799  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
800  int nTAGHCounters = 274;
801 
802  if(taghHitVector[j]->has_fADC){
803  char name [200];
804  char title[500];
805  sprintf(name, "Counter ID %.3i", taghHitVector[j]->counter_id);
806  sprintf(title, "TAGH Counter ID %i t_{ADC} - t_{RF}; t_{ADC} - t_{RF} [ns]; Entries", taghHitVector[j]->counter_id);
807  double locShiftedTime = dRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, taghHitVector[j]->time_fadc);
808  Fill1DHistogram("HLDetectorTiming", "TAGH_ADC_RF_Compare", name,
809  taghHitVector[j]->time_fadc - locShiftedTime,
810  title,
811  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
812  }
813  if(taghHitVector[j]->has_TDC){
814  char name [200];
815  char title[500];
816  sprintf(name, "Counter ID %.3i", taghHitVector[j]->counter_id);
817  sprintf(title, "TAGH Counter ID %i t_{TDC} - t_{RF}; t_{TDC} - t_{RF} [ns]; Entries", taghHitVector[j]->counter_id);
818  double locShiftedTime = dRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, taghHitVector[j]->time_tdc);
819  Fill1DHistogram("HLDetectorTiming", "TAGH_TDC_RF_Compare", name,
820  taghHitVector[j]->time_tdc - locShiftedTime,
821  title,
822  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
823  }
824 
825  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - RFBunch Time",
826  taghHitVector[j]->counter_id, taghHitVector[j]->t - thisRFBunch->dTime,
827  "#Deltat TAGH-RFBunch; Counter ID ;t_{TAGH} - t_{RFBunch} [ns]",
828  nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
829 
830  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time",
831  taghHitVector[j]->t - thisRFBunch->dTime, taghHitVector[j]->E,
832  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Energy [GeV]",
833  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
834 
835  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time",
836  taghHitVector[j]->t - thisRFBunch->dTime,
837  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
838  480, -30, 30);
839  }
840 
841  // now loop over neutral showers to align calorimeters
842  vector<const DNeutralShower *> neutralShowerVector;
843  loop->Get(neutralShowerVector);
844 
845  DVector3 locTargetCenter(0.,0.,Z_TARGET);
846 
847  //double largest_fcal_time =
848 
849  for (i = 0; i < neutralShowerVector.size(); i++){
850  double locPathLength = (neutralShowerVector[i]->dSpacetimeVertex.Vect() - locTargetCenter).Mag();
851  double locDeltaT = neutralShowerVector[i]->dSpacetimeVertex.T() - locPathLength/29.9792458 - thisRFBunch->dTime;
852 
853  // to eliminate low-energy tails and other reconstruction problems, require minimum energies
854  // E(FCAL) > 200 MeV, E(BCAL) > 100 MeV
855  if(neutralShowerVector[i]->dDetectorSystem == SYS_FCAL) {
856  Fill2DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time vs. Energy (Neutral)", neutralShowerVector[i]->dEnergy, locDeltaT,
857  "Shower Energy [GeV]; t_{FCAL} - t_{RF} at Target (Neutral); t_{FCAL} - t_{RF} [ns]; Entries",
858  100, 0., 10., NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
859  if(neutralShowerVector[i]->dEnergy > 0.2) {
860  Fill1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time (Neutral)", locDeltaT,
861  "t_{FCAL} - t_{RF} at Target (Neutral); t_{FCAL} - t_{RF} [ns]; Entries",
862  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
863  }
864  } else {
865  Fill2DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time vs. Energy (Neutral)", neutralShowerVector[i]->dEnergy, locDeltaT,
866  "Shower Energy [GeV];t_{BCAL} - t_{RF} at Target (Neutral); t_{BCAL} - t_{RF} [ns]; Entries",
867  100, 0., 10., NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
868  if(neutralShowerVector[i]->dEnergy > 0.1) {
869  Fill1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time (Neutral)", locDeltaT,
870  "t_{BCAL} - t_{RF} at Target (Neutral); t_{BCAL} - t_{RF} [ns]; Entries",
871  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
872  }
873  }
874 
875  } // End of loop over neutral showers
876 
877  vector<const DCCALShower *> ccalShowerVector;
878  loop->Get(ccalShowerVector);
879 
880  for (i = 0; i < ccalShowerVector.size(); i++){
881  DVector3 locShowerPos(ccalShowerVector[i]->x, ccalShowerVector[i]->y, ccalShowerVector[i]->z);
882  //DVector3 locShowerPos(ccalShowerVector[i]->x, ccalShowerVector[i]->y, 1279.77);
883  double locPathLength = (locShowerPos - locTargetCenter).Mag();
884  double locDeltaT = ccalShowerVector[i]->time - locPathLength/29.9792458 - thisRFBunch->dTime;
885 
886  Fill2DHistogram("HLDetectorTiming", "TRACKING", "CCAL - RF Time vs. Energy (Neutral)", ccalShowerVector[i]->E, locDeltaT,
887  "Shower Energy [GeV];t_{CCAL} - t_{RF} at Target (Neutral); t_{CCAL} - t_{RF} [ns]; Entries",
888  100, 0., 10., 200, -20, 20);
889 
890  // to eliminate low-energy tails and other reconstruction problems, require minimum energies
891  if(ccalShowerVector[i]->E > 0.1) {
892  Fill1DHistogram("HLDetectorTiming", "TRACKING", "CCAL - RF Time (Neutral)", locDeltaT,
893  "t_{CCAL} - t_{RF} at Target (Neutral); t_{CCAL} - t_{RF} [ns]; Entries",
894  500, -50, 50);
895  //NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
896  }
897  }
898 
899  // we went this far just to align the tagger with the RF time, nothing else to do without tracks
900  if(NO_TRACKS)
901  return NOERROR;
902 
903  if (!DO_TRACK_BASED && !DO_VERIFY ) return NOERROR; // Before this stage we aren't really ready yet, so just return
904 
905  // Try using the detector matches
906  // Loop over the charged tracks
907 
908  vector<const DChargedTrack *> chargedTrackVector;
909  loop->Get(chargedTrackVector);
910 
911  for (i = 0; i < chargedTrackVector.size(); i++){
912 
913  // We only want negative particles to kick out protons
914  if (chargedTrackVector[i]->Get_Charge() > 0) continue;
915  const DChargedTrackHypothesis *pionHypothesis = chargedTrackVector[i]->Get_Hypothesis(PiMinus);
916 
917  if (pionHypothesis == NULL) continue;
918 
919  auto locTrackTimeBased = pionHypothesis->Get_TrackTimeBased();
920  double trackingFOM = TMath::Prob(locTrackTimeBased->chisq, locTrackTimeBased->Ndof);
921  // Some quality cuts for the tracks we will use
922  // Keep this minimal for now and investigate later
923  //float trackingFOMCut = 0.01;
924  //float trackingFOMCut =0.0027;
925  float trackingFOMCut = 2.87E-7;
926  int trackingNDFCut = 5;
927 
928  if(trackingFOM < trackingFOMCut) continue;
929  if( locTrackTimeBased->Ndof < trackingNDFCut) continue;
930 
931  //////////////////////////////////////////
932  // get best matches to SC/TOF/FCAL/BCAL //
933  //////////////////////////////////////////
934  auto locSCHitMatchParams = pionHypothesis->Get_SCHitMatchParams();
935  auto locTOFHitMatchParams = pionHypothesis->Get_TOFHitMatchParams();
936  auto locFCALShowerMatchParams = pionHypothesis->Get_FCALShowerMatchParams();
937  auto locBCALShowerMatchParams = pionHypothesis->Get_BCALShowerMatchParams();
938 
939  // We will only use tracks matched to the start counter for our calibration since this will be our reference for t0
940  if (locSCHitMatchParams == NULL) continue;
941 
942  // the idea will be to fix the SC time and reference the other PID detectors off of this
943 
944  // These "flightTime" corrected time are essentially that detector's estimate of the target time
945  float targetCenterCorrection = ((pionHypothesis->position()).Z() - Z_TARGET) / SPEED_OF_LIGHT;
946  float flightTimeCorrectedSCTime = locSCHitMatchParams->dHitTime - locSCHitMatchParams->dFlightTime - targetCenterCorrection;
947  char name [200];
948  char title[500];
949  sprintf(name, "Sector %.2i", locSCHitMatchParams->dSCHit->sector);
950  sprintf(title, "SC Sector %i t_{Target} - t_{RF}; t_{Target} - t_{RF} [ns]; Entries", locSCHitMatchParams->dSCHit->sector);
951  double locShiftedTime = dRFTimeFactory->Step_TimeToNearInputTime(thisRFBunch->dTime, flightTimeCorrectedSCTime);
952  double locSCDeltaT = flightTimeCorrectedSCTime - thisRFBunch->dTime;
953  Fill1DHistogram("HLDetectorTiming", "SC_Target_RF_Compare_all", name,
954  flightTimeCorrectedSCTime - locShiftedTime,
955  title,
956  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
957  Fill1DHistogram("HLDetectorTiming", "TRACKING", "SC - RF Time (all)",
958  flightTimeCorrectedSCTime - thisRFBunch->dTime,
959  "t_{SC} - t_{RF} at Target; t_{SC} - t_{RF} at Target [ns]; Entries",
960  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
961 
962  // Stay away from the nose section, since the propagation time corrections are not stable there.
963  // cut corresponds to ~50 cm path length through the SC - not too far into the nose section
964  // but enough to get some statistics
965 
966  // need to get the projected hit position at the SC in order to cut on it
967  DVector3 IntersectionPoint, IntersectionMomentum;
968  vector<DTrackFitter::Extrapolation_t> extrapolations = locTrackTimeBased->extrapolations.at(SYS_START);
969  shared_ptr<DSCHitMatchParams> locSCHitMatchParams2;
970  bool sc_match_pid = locParticleID->Cut_MatchDistance(extrapolations, locSCHitMatchParams->dSCHit, locSCHitMatchParams->dSCHit->t, locSCHitMatchParams2,
971  true, &IntersectionPoint, &IntersectionMomentum);
972  double locSCzIntersection = IntersectionPoint.z();
973  if( locSCzIntersection < 83. ) {
974  Fill1DHistogram("HLDetectorTiming", "SC_Target_RF_Compare", name,
975  flightTimeCorrectedSCTime - locShiftedTime,
976  title,
977  NBINS_RF_COMPARE, MIN_RF_COMPARE, MAX_RF_COMPARE);
978  Fill1DHistogram("HLDetectorTiming", "TRACKING", "SC - RF Time",
979  flightTimeCorrectedSCTime - thisRFBunch->dTime,
980  "t_{SC} - t_{RF} at Target; t_{SC} - t_{RF} at Target [ns]; Entries",
981  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
982  Fill2DHistogram("HLDetectorTiming", "TRACKING", "SC - RF Time vs. Sector",
983  locSCHitMatchParams->dSCHit->sector, locSCDeltaT,
984  "t_{SC} - t_{RF} at Target; Sector; t_{SC} - t_{RF} at Target [ns];",
985  30, 0.5, 30.5, 800, -20., 20.);
986  }
987 
988  // Get the pulls vector from the track
989  auto thisTimeBasedTrack = pionHypothesis->Get_TrackTimeBased();
990 
991  vector<DTrackFitter::pull_t> pulls = thisTimeBasedTrack->pulls;
992  double earliestCDCTime = 10000.;
993  double earliestFDCTime = 10000.;
994  for (size_t iPull = 0; iPull < pulls.size(); iPull++){
995  if ( pulls[iPull].cdc_hit != nullptr && pulls[iPull].tdrift < earliestCDCTime) earliestCDCTime = pulls[iPull].tdrift;
996  if ( pulls[iPull].fdc_hit != nullptr && pulls[iPull].tdrift < earliestFDCTime) earliestFDCTime = pulls[iPull].tdrift;
997  }
998 
999  // Do this the old way for the CDC
1000  vector < const DCDCTrackHit *> cdcTrackHitVector;
1001  pionHypothesis->Get_TrackTimeBased()->Get(cdcTrackHitVector);
1002  if (cdcTrackHitVector.size() != 0){
1003  float earliestTime = 10000; // Initialize high
1004  for (unsigned int iCDC = 0; iCDC < cdcTrackHitVector.size(); iCDC++){
1005  if (cdcTrackHitVector[iCDC]->tdrift < earliestTime) earliestTime = cdcTrackHitVector[iCDC]->tdrift;
1006  }
1007 
1008  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest CDC Time Minus Matched SC Time",
1009  earliestTime - locSCHitMatchParams->dHitTime,
1010  "Earliest CDC Time Minus Matched SC Time; t_{CDC} - t_{SC} [ns];",
1011  400, -50, 150);
1012  }
1013 
1014  // Loop over TAGM hits
1015  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
1016  int nTAGMColumns = 122;
1017  // We want to look at the timewalk within these ADC/TDC detectors
1018  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - SC Target Time",
1019  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - flightTimeCorrectedSCTime,
1020  "#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);
1021  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC Target Time",
1022  tagmHitVector[j]->t - flightTimeCorrectedSCTime, tagmHitVector[j]->E,
1023  "Tagger - SC Target Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
1024  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
1025  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC 1D Target Time",
1026  tagmHitVector[j]->t - flightTimeCorrectedSCTime,
1027  "Tagger - SC Time at Target; #Deltat_{Tagger - SC} [ns]; Entries",
1028  160, -20, 20);
1029  }
1030  // Loop over TAGH hits
1031  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
1032  int nTAGHCounters = 274;
1033  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - SC Target Time",
1034  taghHitVector[j]->counter_id, taghHitVector[j]->t - flightTimeCorrectedSCTime,
1035  "#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);
1036 
1037  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC Target Time",
1038  taghHitVector[j]->t - flightTimeCorrectedSCTime, taghHitVector[j]->E,
1039  "Tagger - SC Target Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
1040  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
1041 
1042  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - SC 1D Target Time",
1043  taghHitVector[j]->t - flightTimeCorrectedSCTime,
1044  "Tagger - SC Time at Target; #Deltat_{Tagger - SC} [ns]; Entries",
1045  160, -20, 20);
1046  }
1047 
1048  if (locTOFHitMatchParams != NULL){
1049  // Now check the TOF matching. Do this on a full detector level.
1050  float flightTimeCorrectedTOFTime = locTOFHitMatchParams->dHitTime - locTOFHitMatchParams->dFlightTime - targetCenterCorrection;
1051  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TOF - SC Target Time",
1052  flightTimeCorrectedTOFTime - flightTimeCorrectedSCTime,
1053  "t_{TOF} - t_{SC} at Target; t_{TOF} - t_{SC} at Target [ns]; Entries",
1054  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1055  Fill1DHistogram("HLDetectorTiming", "TRACKING", "TOF - RF Time",
1056  flightTimeCorrectedTOFTime - thisRFBunch->dTime,
1057  "t_{TOF} - t_{RF} at Target; t_{TOF} - t_{RF} at Target [ns]; Entries",
1058  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1059  // Fill the following when there is a SC/TOF match
1060  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest Flight-time Corrected FDC Time",
1061  earliestFDCTime,
1062  "Earliest Flight-time corrected FDC Time; t_{FDC} [ns];",
1063  200, -50, 150);
1064 
1065  // get DIRC match parameters (contains LUT information)
1066  const DDetectorMatches* locDetectorMatches = NULL;
1067  loop->GetSingle(locDetectorMatches);
1068  DDetectorMatches &locDetectorMatch = (DDetectorMatches&)locDetectorMatches[0];
1069  shared_ptr<const DDIRCMatchParams> locDIRCMatchParams;
1070  bool foundDIRC = locParticleID->Get_DIRCMatchParams(locTrackTimeBased, locDetectorMatches, locDIRCMatchParams);
1071 
1072  // For DIRC calibrations, select tracks which have a good TOF match
1073  if(foundDIRC && locTOFHitMatchParams->dDeltaXToHit < 10.0 && locTOFHitMatchParams->dDeltaYToHit < 10.0) {
1074 
1075  // Get match parameters
1076  TVector3 posInBar = locDIRCMatchParams->dExtrapolatedPos;
1077  TVector3 momInBar = locDIRCMatchParams->dExtrapolatedMom;
1078  double locExpectedThetaC = locDIRCMatchParams->dExpectedThetaC;
1079  double locExtrapolatedTime = locDIRCMatchParams->dExtrapolatedTime;
1080  int locBar = locDIRCGeometry->GetBar(posInBar.Y());
1081 
1082  Particle_t locPID = locTrackTimeBased->PID();
1083  double locMass = ParticleMass(locPID);
1084  double locAngle = dDIRCLut->CalcAngle(momInBar, locMass);
1085  map<Particle_t, double> locExpectedAngle = dDIRCLut->CalcExpectedAngles(momInBar);
1086 
1087  // get map of DIRCMatches to PMT hits
1088  map<shared_ptr<const DDIRCMatchParams>, vector<const DDIRCPmtHit*> > locDIRCTrackMatchParamsMap;
1089  locDetectorMatch.Get_DIRCTrackMatchParamsMap(locDIRCTrackMatchParamsMap);
1090  map<Particle_t, double> logLikelihoodSum;
1091 
1092  // loop over associated hits for LUT diagnostic plots
1093  for(uint loc_i=0; loc_i<dircPmtHitVector.size(); loc_i++) {
1094  vector<pair<double, double>> locDIRCPhotons = dDIRCLut->CalcPhoton(dircPmtHitVector[loc_i], locExtrapolatedTime, posInBar, momInBar, locExpectedAngle, locAngle, locPID, logLikelihoodSum);
1095  double locHitTime = dircPmtHitVector[loc_i]->t - locExtrapolatedTime;
1096  int locChannel = dircPmtHitVector[loc_i]->ch%dMaxDIRCChannels;
1097 
1098  if(locDIRCPhotons.size() > 0) {
1099  // loop over candidate photons
1100  for(uint loc_j = 0; loc_j<locDIRCPhotons.size(); loc_j++) {
1101  double locDeltaT = locDIRCPhotons[loc_j].first - locHitTime;
1102 
1103  if(locChannel < DDIRCPmtHit_factory::DIRC_MAX_CHANNELS) {
1104  Fill2DHistogram ("HLDetectorTiming", "DIRC", "DIRCHit North Per Channel t_{DIRC} - t_{track}",
1105  locChannel, locDeltaT,
1106  "DIRCHit North Per Channel t_{DIRC} - t_{track}; channel ID; t_{DIRC} - t_{track} [ns] ",
1108  (double)DDIRCPmtHit_factory::DIRC_MAX_CHANNELS+0.5, 400, -50, 50);
1109  } else {
1110  Fill2DHistogram ("HLDetectorTiming", "DIRC", "DIRCHit South Per Channel t_{DIRC} - t_{track}",
1111  locChannel-DDIRCPmtHit_factory::DIRC_MAX_CHANNELS, locDeltaT,
1112  "DIRCHit South Per Channel t_{DIRC} - t_{track}; channel ID; t_{DIRC} - t_{track} [ns] ",
1114  (double)DDIRCPmtHit_factory::DIRC_MAX_CHANNELS+0.5, 400, -50, 50);
1115  }
1116  }
1117  }
1118  }
1119  }
1120  }
1121  if (locBCALShowerMatchParams != NULL){
1122  float flightTimeCorrectedBCALTime = locBCALShowerMatchParams->dBCALShower->t - locBCALShowerMatchParams->dFlightTime - targetCenterCorrection;
1123  Fill1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - SC Target Time",
1124  flightTimeCorrectedBCALTime - flightTimeCorrectedSCTime,
1125  "t_{BCAL} - t_{SC} at Target; t_{BCAL} - t_{SC} [ns]; Entries",
1126  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1127  Fill1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time",
1128  flightTimeCorrectedBCALTime - thisRFBunch->dTime,
1129  "t_{BCAL} - t_{RF} at Target; t_{BCAL} - t_{RF} [ns]; Entries",
1130  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1131  // Add histogram suggested by Mark Dalton
1132  Fill2DHistogram("HLDetectorTiming", "TRACKING", "BCAL - SC Target Time Vs Correction",
1133  locBCALShowerMatchParams->dFlightTime, flightTimeCorrectedBCALTime - flightTimeCorrectedSCTime,
1134  "t_{BCAL} - t_{SC} at Target; Flight time [ns]; t_{BCAL} - t_{SC} [ns]",
1135  100, 0, 20, 50, -10, 10);
1136  // Fill the following when there is a SC/BCAL match.
1137  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Earliest Flight-time Corrected CDC Time",
1138  earliestCDCTime,
1139  "Earliest Flight-time Corrected CDC Time; t_{CDC} [ns];",
1140  200, -50, 150);
1141  }
1142  if (locFCALShowerMatchParams != NULL){
1143  float flightTimeCorrectedFCALTime = locFCALShowerMatchParams->dFCALShower->getTime() - locFCALShowerMatchParams->dFlightTime - targetCenterCorrection;
1144  Fill1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - SC Target Time",
1145  flightTimeCorrectedFCALTime - flightTimeCorrectedSCTime,
1146  "t_{FCAL} - t_{SC} at Target; t_{FCAL} - t_{SC} [ns]; Entries",
1147  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1148  Fill1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time",
1149  flightTimeCorrectedFCALTime - thisRFBunch->dTime,
1150  "t_{FCAL} - t_{RF} at Target; t_{FCAL} - t_{RF} [ns]; Entries",
1151  NBINS_MATCHING, MIN_MATCHING_T, MAX_MATCHING_T);
1152  }
1153 
1154  } // End of loop over time based tracks
1155 
1156  if (DO_REACTION){
1157  // Trigger the analysis
1158  vector<const DAnalysisResults*> locAnalysisResultsVector;
1159  loop->Get(locAnalysisResultsVector);
1160  // Get the time from the results and fill histograms
1161  deque<const DParticleCombo*> locPassedParticleCombos;
1162  locAnalysisResultsVector[0]->Get_PassedParticleCombos(locPassedParticleCombos);
1163 
1164  for (i=0; i < locPassedParticleCombos.size(); i++){
1165  double taggerTime = locPassedParticleCombos[i]->Get_ParticleComboStep(0)->Get_InitialParticle_Measured()->time();
1166  //cout << "We have a tagger time of " << taggerTime << endl;
1167  //Find matching hit by time
1168  for (unsigned int j = 0 ; j < tagmHitVector.size(); j++){
1169  if (taggerTime == tagmHitVector[j]->t){
1170  int nTAGMColumns = 122;
1171  // We want to look at the timewalk within these ADC/TDC detectors
1172  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch Time p2pi",
1173  GetCCDBIndexTAGM(tagmHitVector[j]), tagmHitVector[j]->t - thisRFBunch->dTime,
1174  "#Deltat TAGM-RFBunch; Column ;t_{TAGM} - t_{SC @ target} [ns]",
1175  nTAGMColumns, 0.5, nTAGMColumns + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
1176  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time p2pi",
1177  tagmHitVector[j]->t - thisRFBunch->dTime, tagmHitVector[j]->E,
1178  "Tagger - RFBunch Time; #Deltat_{Tagger - SC} [ns]; Energy [GeV]",
1179  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
1180  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time p2pi",
1181  tagmHitVector[j]->t - thisRFBunch->dTime,
1182  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
1183  160, -20, 20);
1184  }
1185  }
1186  // Loop over TAGH hits
1187  for (unsigned int j = 0 ; j < taghHitVector.size(); j++){
1188  if (taggerTime == taghHitVector[j]->t){
1189  int nTAGHCounters = 274;
1190  Fill2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - RFBunch Time p2pi",
1191  taghHitVector[j]->counter_id, taghHitVector[j]->t - thisRFBunch->dTime,
1192  "#Deltat TAGH-RFBunch; Counter ID ;t_{TAGH} - t_{RFBunch} [ns]",
1193  nTAGHCounters, 0.5, nTAGHCounters + 0.5, NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME);
1194 
1195  Fill2DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch Time p2pi",
1196  taghHitVector[j]->t - thisRFBunch->dTime, taghHitVector[j]->E,
1197  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Energy [GeV]",
1198  NBINS_TAGGER_TIME,MIN_TAGGER_TIME,MAX_TAGGER_TIME, nBinsE, EMin, EMax);
1199 
1200  Fill1DHistogram("HLDetectorTiming", "TRACKING", "Tagger - RFBunch 1D Time p2pi",
1201  taghHitVector[j]->t - thisRFBunch->dTime,
1202  "Tagger - RFBunch Time; #Deltat_{Tagger - RFBunch} [ns]; Entries",
1203  160, -20, 20);
1204  }
1205  }
1206  }
1207  }
1208 
1209  return NOERROR;
1210 }
1211 
1212 
1213 //------------------
1214 // erun
1215 //------------------
1217 {
1218  // This is called whenever the run number changes, before it is
1219  // changed to give you a chance to clean up before processing
1220  // events from the next run number.
1221 
1222  // set some histogram properties
1223 
1224 
1225  TH2I *fdc_time_module_hist = (TH2I*)gDirectory->Get("HLDetectorTiming/FDC/FDCHit Wire time vs. module");
1226  if(fdc_time_module_hist != NULL) {
1227  string act_crate;
1228  int act_slot;
1229  for(int ibin=1; ibin<=48; ibin++){
1230  int mod = Get_FDCTDC_crate_slot(ibin, act_crate, act_slot);
1231  stringstream ss;
1232  ss << act_crate << "/" << act_slot;
1233  fdc_time_module_hist->GetXaxis()->SetBinLabel(ibin, ss.str().c_str());
1234  }
1235  fdc_time_module_hist->LabelsOption("v");
1236  }
1237 
1238 
1239  return NOERROR;
1240 }
1241 
1242 //------------------
1243 // fini
1244 //------------------
1246 {
1247  // Called before program exit after event processing is finished.
1248  //Here is where we do the fits to the data to see if we have a reasonable alignment
1249  SortDirectories(); //Defined in HistogramTools.h
1250 
1251  return NOERROR;
1252 }
1253 
1255  // Returns the CCDB index of a particular hit
1256  // This
1257  int plane = thisHit->plane;
1258  int bar = thisHit->bar;
1259  int end = thisHit->end;
1260  // 44 bars per plane
1261  int CCDBIndex = plane * 88 + end * 44 + bar;
1262  return CCDBIndex;
1263 }
1264 
1266  return 0;
1267 }
1268 
1270  // Since there are a few counters where each row is read out seperately this is a bit of a mess
1271  int row = thisHit->row;
1272  int column = thisHit->column;
1273 
1274  int CCDBIndex = column + row;
1275  if (column > 9) CCDBIndex += 5;
1276  if (column > 27) CCDBIndex += 5;
1277  if (column > 81) CCDBIndex += 5;
1278  if (column > 99) CCDBIndex += 5;
1279 
1280  return CCDBIndex;
1281 }
1282 
1284 
1285  int ring = thisHit->ring;
1286  int straw = thisHit->straw;
1287 
1288  int CCDBIndex = GetCCDBIndexCDC(ring, straw);
1289  return CCDBIndex;
1290 }
1291 
1293 
1294  //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};
1295  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};
1296 
1297  int CCDBIndex = StartIndex[ring - 1] + straw;
1298  return CCDBIndex;
1299 }
1300 
vector< pair< double, double > > CalcPhoton(const DDIRCPmtHit *locDIRCHit, double locFlightTime, TVector3 posInBar, TVector3 momInBar, map< Particle_t, double > locExpectedAngle, double locAngle, Particle_t locPID, map< Particle_t, double > &logLikelihoodSum, int &nPhotonsThetaC, double &meanThetaC, double &meanDeltaT, bool &isGood) const
Definition: DDIRCLut.cc:212
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
double CalcAngle(TVector3 momInBar, double locMass) const
Definition: DDIRCLut.cc:379
Int_t layer
bool Get_DIRCTrackMatchParamsMap(map< shared_ptr< const DDIRCMatchParams >, vector< const DDIRCPmtHit * > > &locDIRCTrackMatchParamsMap)
bool has_TDC
Definition: DSCHit.h:25
TVector3 DVector3
Definition: DVector3.h:14
int plane
Definition: DTOFHit.h:21
bool has_fADC
Definition: DTOFHit.h:29
double fval
Definition: DEPICSvalue.h:68
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
int row
Definition: DTAGMHit.h:20
bool has_TDC
Definition: DTAGHHit.h:26
sprintf(text,"Post KinFit Cut")
#define y
const DVector3 & position(void) const
const DTrackTimeBased * Get_TrackTimeBased(void) const
uint32_t Get_L1FrontPanelTriggerBits(void) const
bool has_TDC
Definition: DPSCHit.h:27
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)
bool Get_IsPhysicsEvent(void) const
bool has_fADC
Definition: DPSCHit.h:27
void SortDirectories()
bool has_fADC
Definition: DTAGMHit.h:27
Definition: DSCHit.h:14
InitPlugin_t InitPlugin
DGeometry * GetDGeometry(unsigned int run_number)
Definition: GlueX.h:22
#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 const int DIRC_MAX_CHANNELS
string name
Definition: DEPICSvalue.h:64
bool Get_DIRCMatchParams(const DTrackingData *locTrack, shared_ptr< const DDIRCMatchParams > &locMatchParams) const
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
static double ParticleMass(Particle_t p)
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
static int Get_FDCTDC_crate_slot(int mod, string &act_crate, int &act_slot)
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)
map< Particle_t, double > CalcExpectedAngles(TVector3 momInBar) const
Definition: DDIRCLut.cc:383
unsigned int dNumParticleVotes
Definition: DEventRFBunch.h:32
double Step_TimeToNearInputTime(double locTimeToStep, double locTimeToStepTo) const
DRFTime_factory * dRFTimeFactory
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
int GetBar(float y) const
Particle_t
Definition: particleType.h:12