Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
JEventProcessor_TOF_TDC_shift.cc
Go to the documentation of this file.
1 #include <stdint.h>
2 #include <iomanip>
3 #include <vector>
4 #include <TMath.h>
5 #include <TCutG.h>
6 #include <TFitResult.h>
7 #include <TFitResultPtr.h>
9 #include <JANA/JApplication.h>
10 using namespace std;
11 using namespace jana;
12 
13 #include "TOF/DTOFHit.h"
14 #include "TOF/DTOFDigiHit.h"
15 #include "TOF/DTOFTDCDigiHit.h"
16 #include "TTAB/DTranslationTable.h"
17 
18 #include "DAQ/DCODAROCInfo.h"
19 
20 // Define some constants
21 const float_t ADC_BIN = 0.0625; // fADC250 pulse time resolution (ns)
22 const float_t TDC_BIN = 0.0234375; // CAEN TDC time resolution (ns)
23 
24 //----------------------------------------------------------------------------------
25 // Routine used to create our JEventProcessor
26 extern "C"{
27  void InitPlugin(JApplication *app){
28  InitJANAPlugin(app);
29  app->AddProcessor(new JEventProcessor_TOF_TDC_shift());
30  }
31 }
32 //----------------------------------------------------------------------------------
34 }
35 //----------------------------------------------------------------------------------
37 }
38 //----------------------------------------------------------------------------------
39 
41 
42  // Create root folder for ST and cd to it, store main dir
43  TDirectory *main = gDirectory;
44  filedir = main;
45  gDirectory->mkdir("TOF_TDC_shift")->cd();
46 
47  // TI remainder vs (ADC time - TDC time)
48  hrocTimeRemainder_AdcTdcTimeDiff = new TH2I("hrocTimeRemainder_AdcTdcTimeDiff",";t_{ADC} - t_{TDC};TI % 6",4000,-1500,500,6,-0.5,5.5);
49  hrocTimeRemainder_AdcTdcTimeDiff_corrected = new TH2I("hrocTimeRemainder_AdcTdcTimeDiff_corrected",";t_{ADC} - t_{TDC};TI % 6",600,-30,30,6,-0.5,5.5);
50 
51  // cd back to main directory
52  main->cd();
53 
54  return NOERROR;
55 }
56 //----------------------------------------------------------------------------------
57 jerror_t JEventProcessor_TOF_TDC_shift::brun(JEventLoop *eventLoop, int32_t runnumber) {
58 
59  char filename[200];
60  sprintf(filename,"TOF_TDC_shift_%6.6d.txt",runnumber);
61  OUTPUT.open(filename);
62  // OUTPUT << setw(6) << runnumber;
63 
64  // This is called whenever the run number changes
65  return NOERROR;
66 }
67 //----------------------------------------------------------------------------------
68 jerror_t JEventProcessor_TOF_TDC_shift::evnt(JEventLoop *eventLoop, uint64_t eventnumber) {
69 
70  // Get all data objects first so we minimize the time we hold the ROOT mutex lock
71 
72  // Each detector's hits
73  vector<const DTOFHit*> dtofhits; // TOF Hits
74  vector<const DTOFDigiHit*> dtofdigihits; // TOF DigiHits
75  vector<const DTOFTDCDigiHit*> dtoftdcdigihits; // TOF TDC DigiHits
76  vector<const DCODAROCInfo*> dcodarocinfo; // DCODAROCInfo
77 
78  // TOF
79  eventLoop->Get(dtofhits);
80  eventLoop->Get(dtofdigihits);
81  eventLoop->Get(dtoftdcdigihits);
82  eventLoop->Get(dcodarocinfo);
83 
84  Int_t TriggerBIT = -1;
85  for(UInt_t i=0;i<dcodarocinfo.size();i++){
86  Int_t rocid = dcodarocinfo[i]->rocid;
87  // We can use any roc ID besides 1,
88  // just use 11.
89  if(rocid == 11){
90  ULong64_t rocTime = dcodarocinfo[i]->timestamp;
91  TriggerBIT = rocTime % 6;
92  break;
93  }
94  }
95 
96  // FILL HISTOGRAMS
97  // Since we are filling histograms local to this plugin, it will not interfere with other ROOT operations: can use plugin-wide ROOT fill lock
98  japp->RootFillLock(this); //ACQUIRE ROOT FILL LOCK
99 
100  // Fill histogram of TI % 6 vs (ADC time - TDC time)
101  for(UInt_t tof = 0;tof<dtofdigihits.size();tof++){
102  for(UInt_t tof_TDC = 0;tof_TDC<dtoftdcdigihits.size();tof_TDC++){
103  // Don't bother with matching of ADC and TDC hits,
104  // just fill for all combinations
105 
106  // Make sure TI remainder is there
107  if(TriggerBIT != -1){
108  Double_t adc_time = dtofdigihits[tof]->pulse_time * ADC_BIN;
109  Double_t tdc_time = (Double_t)dtoftdcdigihits[tof_TDC]->time * TDC_BIN;
110  Double_t diff = adc_time - tdc_time;
111  // cout << adc_time << " " << tdc_time << " " << diff << endl;
112  hrocTimeRemainder_AdcTdcTimeDiff->Fill(diff, TriggerBIT);
113  }
114  }
115  }
116 
117  for(UInt_t tof = 0;tof<dtofhits.size();tof++){
118  double diff = dtofhits[tof]->t_fADC - dtofhits[tof]->t_TDC;
119  hrocTimeRemainder_AdcTdcTimeDiff_corrected->Fill(diff, TriggerBIT);
120  }
121 
122  japp->RootFillUnLock(this); //RELEASE ROOT FILL LOCK
123 
124  return NOERROR;
125 }
126 //----------------------------------------------------------------------------------
128  // This is called whenever the run number changes, before it is
129  // changed to give you a chance to clean up before processing
130  // events from the next run number.
131  return NOERROR;
132 }
133 //----------------------------------------------------------------------------------
135 
136  // calculate mean timing for 6 possible values of
137  // TI remainder. The TI remainder bin with the highest
138  // mean gives the shift value for this run.
139  Double_t mean[6];
140  TH1I *hproj;
141  char hname[200];
142  Double_t min = +99999;
143  Int_t shift = -1;
144  filedir->cd();
145  gDirectory->cd("TOF_TDC_shift");
146 
147  /*
148  // multiple ADC hits skew the rough mean, so make a broad cut around the main peak
149  // use TCutG to cut +- 60 ns (arbitrary) around the peak in X
150  const double twindow = 60.; // (ns)
151  TCutG time_cut("time_cut",5);
152  //time_cut.SetVarX("x");
153  //time_cut.SetVarY("y");
154  time_cut.SetPoint(0,mean_time-twindow,0.);
155  time_cut.SetPoint(1,mean_time-twindow,6.);
156  time_cut.SetPoint(2,mean_time+twindow,6.);
157  time_cut.SetPoint(3,mean_time+twindow,0.);
158  time_cut.SetPoint(4,mean_time-twindow,0.);
159  */
160 
161  const double fit_window = 20.;
162  for(Int_t i=0;i<6;i++){
163  // Get projection of (ADC time - TDC time) for each value of TI remainder
164  sprintf(hname,"TOF_TDC_shift/h%d",i);
165  hproj = (TH1I*)hrocTimeRemainder_AdcTdcTimeDiff->ProjectionX(hname,i+1,i+1);
166  //hproj = (TH1I*)hrocTimeRemainder_AdcTdcTimeDiff->ProjectionX(hname,i+1,i+1,"[time_cut]");
167  //mean[i] = hproj->GetMean();
168  double maximum = hproj->GetBinCenter(hproj->GetMaximumBin());
169  TFitResultPtr fr = hproj->Fit("gaus", "S", "", maximum - fit_window, maximum + fit_window);
170  if(fr == 0) { // fit succeeds
171  mean[i] = fr->Parameter(1);;
172  } else {
173  mean[i] = hproj->GetMean();
174  }
175  cout << "TI remainder = " << i << " mean = " << mean[i] << endl;
176  if(fabs(mean[i]) < fabs(min)){
177  min = mean[i];
178  shift = i;
179  }
180  }
181 
182  // shift value can only be 1, 3, or 5. (not true anymore due to TI updates? - sdobbs 3 Mar. 2016)
183  //if(!(shift == 1 || shift == 3 || shift == 5)) shift = -1;
184 
185  OUTPUT << shift << endl;
186  filedir->cd();
187  // Called before program exit after event processing is finished.
188  return NOERROR;
189 }
190 //----------------------------------------------------------------------------------
191 //----------------------------------------------------------------------------------
sprintf(text,"Post KinFit Cut")
TString filename
jerror_t evnt(jana::JEventLoop *eventLoop, uint64_t eventnumber)
Called every event.
JApplication * japp
jerror_t fini(void)
Called after last event of last event source has been processed.
#define OUTPUT
Definition: tw_corr.C:42
const float_t TDC_BIN
InitPlugin_t InitPlugin
jerror_t brun(jana::JEventLoop *eventLoop, int32_t runnumber)
Called everytime a new run number is detected.
jerror_t init(void)
Called once at program start.
jerror_t erun(void)
Called everytime run number changes, provided brun has been called.
const float_t ADC_BIN
int main(int argc, char *argv[])
Definition: gendoc.cc:6