Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Calibration/HLDetectorTiming/FitScripts/AdjustTiming.C
Go to the documentation of this file.
1 namespace ExtractTrackBasedTimingNS {
2  TFile * thisFile;
3 
4  TH1I * Get1DHistogram(const char * plugin, const char * directoryName, const char * name){
5  TH1I * histogram;
6  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
7  thisFile->GetObject(fullName, histogram);
8  if (histogram == 0){
9  cout << "Unable to find histogram " << fullName.Data() << endl;
10  return NULL;
11  }
12  return histogram;
13  }
14 
15  TH2I * Get2DHistogram(const char * plugin, const char * directoryName, const char * name){
16  TH2I * histogram;
17  TString fullName = TString(plugin) + "/" + TString(directoryName) + "/" + TString(name);
18  thisFile->GetObject(fullName, histogram);
19  if (histogram == 0){
20  cout << "Unable to find histogram " << fullName.Data() << endl;
21  return NULL;
22  }
23  return histogram;
24  }
25 };
26 
27 void GetCCDBConstants(TString path, Int_t run, TString variation, vector<double>& container, Int_t column = 1){
28  char command[256];
29 
30  sprintf(command, "ccdb dump %s:%i:%s", path.Data(), run, variation.Data());
31  FILE* inputPipe = gSystem->OpenPipe(command, "r");
32  if(inputPipe == NULL)
33  return;
34  //get the first (comment) line
35  char buff[1024];
36  if(fgets(buff, sizeof(buff), inputPipe) == NULL)
37  return;
38  //get the remaining lines
39  double entry;
40  int counter = 0;
41  while(fgets(buff, sizeof(buff), inputPipe) != NULL){
42  istringstream locConstantsStream(buff);
43  while (locConstantsStream >> entry){
44  counter++;
45  if (counter % column == 0) container.push_back(entry);
46  }
47  }
48  //Close the pipe
49  gSystem->ClosePipe(inputPipe);
50 }
51 
52 void GetCCDBConstants2D(TString path, Int_t run, TString variation, vector< vector<double> >& container, Int_t numColumns){
53  char command[256];
54 
55  sprintf(command, "ccdb dump %s:%i:%s", path.Data(), run, variation.Data());
56  FILE* inputPipe = gSystem->OpenPipe(command, "r");
57  if(inputPipe == NULL)
58  return;
59  //get the first (comment) line
60  char buff[1024];
61  if(fgets(buff, sizeof(buff), inputPipe) == NULL)
62  return;
63  //get the remaining lines
64  double entry;
65  int counter = 0;
66  vector<double> row;
67  while(fgets(buff, sizeof(buff), inputPipe) != NULL){
68  istringstream locConstantsStream(buff);
69  while (locConstantsStream >> entry){
70  counter++;
71  if(counter == numColumns) {
72  container.push_back(row);
73  row.clear();
74  counter = 0;
75  }
76  row.push_back(entry);
77  }
78  }
79  //Close the pipe
80  gSystem->ClosePipe(inputPipe);
81 }
82 
83 //Overload this function to handle the base time offsets
84 void GetCCDBConstants1(TString path, Int_t run, TString variation, double& constant1){
85  char command[256];
86  sprintf(command, "ccdb dump %s:%i:%s", path.Data(), run, variation.Data());
87  FILE* inputPipe = gSystem->OpenPipe(command, "r");
88  if(inputPipe == NULL)
89  return;
90  //get the first (comment) line
91  char buff[1024];
92  if(fgets(buff, sizeof(buff), inputPipe) == NULL)
93  return;
94  //get the line containing the values
95  while(fgets(buff, sizeof(buff), inputPipe) != NULL){
96  istringstream locConstantsStream(buff);
97  locConstantsStream >> constant1;
98  }
99  //Close the pipe
100  gSystem->ClosePipe(inputPipe);
101 }
102 
103 void GetCCDBConstants2(TString path, Int_t run, TString variation, double& constant1, double& constant2){
104  char command[256];
105  sprintf(command, "ccdb dump %s:%i:%s", path.Data(), run, variation.Data());
106  FILE* inputPipe = gSystem->OpenPipe(command, "r");
107  if(inputPipe == NULL)
108  return;
109  //get the first (comment) line
110  char buff[1024];
111  if(fgets(buff, sizeof(buff), inputPipe) == NULL)
112  return;
113  //get the line containing the values
114  while(fgets(buff, sizeof(buff), inputPipe) != NULL){
115  istringstream locConstantsStream(buff);
116  locConstantsStream >> constant1 >> constant2;
117  }
118  //Close the pipe
119  gSystem->ClosePipe(inputPipe);
120 }
121 
122 int GetCCDBIndexTAGM(unsigned int column, unsigned int row){
123  int CCDBIndex = column + row;
124  if (column > 9) CCDBIndex += 5;
125  if (column > 27) CCDBIndex += 5;
126  if (column > 81) CCDBIndex += 5;
127  if (column > 99) CCDBIndex += 5;
128 
129  return CCDBIndex;
130 }
131 
132 int GetF1TDCslotTAGH(int id) {
133  double N = 32.0; // channels per slot
134  if (id >= 132 && id <= 172) throw("TAGH: unknown id in [132,172]");
135  int HVid = (id <= 131) ? id : (id - 274 + 233);
136  return int((HVid-1)/N) + 1;
137 }
138 
139 void FindFDCPackageChamber(int plane, int &package, int &chamber) {
140  package = plane / 6 + 1;
141  chamber = plane % 6;
142  if(chamber == 0) {
143  chamber = 6;
144  package--;
145  }
146 }
147 
148 void AdjustTiming(TString fileName = "hd_root.root", int runNumber = 10390, TString variation = "default", bool verbose = false,TString prefix = ""){
149  // configuration parameters
150  bool FIX_START_COUNTER = false; // should we keep the start counter peak aligned to a certain time?
151  // this helps in certain analysis without tracks
152  double start_counter_t0 = 0.; // time we should move the start counter time peak to
153  bool DONT_SHIFT_SC_RF = true;
154  bool USE_NEUTRALS_FOR_BCAL_RF_ALIGN = false;
155  bool USE_NEUTRALS_FOR_FCAL_RF_ALIGN = true;
156 
157  // set "prefix" in case you want to ship the txt files elsewhere...
158  cout << "Performing adjustment timing fits for File: " << fileName.Data() << " Run: " << runNumber << " Variation: " << variation.Data() << endl;
159 
160  ExtractTrackBasedTimingNS::thisFile = TFile::Open( fileName , "UPDATE");
162  cout << "Unable to open file " << fileName.Data() << "...Exiting" << endl;
163  return;
164  }
165 
166  //We need the existing constants, The best we can do here is just read them from the file.
167  vector<double> sc_tdc_time_offsets;
168  vector<double> sc_fadc_time_offsets;
169  vector<double> tof_tdc_time_offsets;
170  vector<double> tof_fadc_time_offsets;
171  vector<double> tagm_tdc_time_offsets;
172  vector<double> tagm_fadc_time_offsets;
173  vector<double> tagh_tdc_time_offsets;
174  vector<double> tagh_fadc_time_offsets;
175  vector<double> tagh_counter_quality;
176  vector< vector<double> > fdc_wire_offsets_package1;
177  vector< vector<double> > fdc_wire_offsets_package2;
178  vector< vector<double> > fdc_wire_offsets_package3;
179  vector< vector<double> > fdc_wire_offsets_package4;
180 
181  double sc_t_base_fadc, sc_t_base_tdc;
182  double tof_t_base_fadc, tof_t_base_tdc;
183  double bcal_t_base_fadc, bcal_t_base_tdc;
184  double tagm_t_base_fadc, tagm_t_base_tdc;
185  double tagh_t_base_fadc, tagh_t_base_tdc;
186  double fdc_t_base_fadc, fdc_t_base_tdc;
187  double fcal_t_base;
188  double cdc_t_base;
189  double RF_Period;
190 
191  cout << "Grabbing CCDB constants..." << endl;
192  // Base times
193  GetCCDBConstants1("/CDC/base_time_offset" ,runNumber, variation, cdc_t_base);
194  GetCCDBConstants1("/FCAL/base_time_offset",runNumber, variation, fcal_t_base);
195  GetCCDBConstants1("/PHOTON_BEAM/RF/beam_period",runNumber, variation, RF_Period);
196  GetCCDBConstants2("/FDC/base_time_offset" ,runNumber, variation, fdc_t_base_fadc, fdc_t_base_tdc);
197  GetCCDBConstants2("/BCAL/base_time_offset" ,runNumber, variation, bcal_t_base_fadc, bcal_t_base_tdc);
198  GetCCDBConstants2("/PHOTON_BEAM/microscope/base_time_offset" ,runNumber, variation, tagm_t_base_fadc, tagm_t_base_tdc);
199  GetCCDBConstants2("/PHOTON_BEAM/hodoscope/base_time_offset" ,runNumber, variation, tagh_t_base_fadc, tagh_t_base_tdc);
200  GetCCDBConstants2("/START_COUNTER/base_time_offset" ,runNumber, variation, sc_t_base_fadc, sc_t_base_tdc);
201  GetCCDBConstants2("/TOF/base_time_offset" ,runNumber, variation, tof_t_base_fadc, tof_t_base_tdc);
202  // Per channel
203  //GetCCDBConstants("/BCAL/TDC_offsets" ,runNumber, variation, bcal_tdc_offsets);
204  //GetCCDBConstants("/FCAL/timing_offsets" ,runNumber, variation, fcal_adc_offsets);
205  GetCCDBConstants("/START_COUNTER/adc_timing_offsets" ,runNumber, variation, sc_fadc_time_offsets);
206  GetCCDBConstants("/START_COUNTER/tdc_timing_offsets" ,runNumber, variation, sc_tdc_time_offsets);
207  GetCCDBConstants("/PHOTON_BEAM/microscope/fadc_time_offsets" ,runNumber, variation, tagm_fadc_time_offsets,3);// Interested in 3rd column
208  GetCCDBConstants("/PHOTON_BEAM/microscope/tdc_time_offsets" ,runNumber, variation, tagm_tdc_time_offsets,3);
209  GetCCDBConstants("/PHOTON_BEAM/hodoscope/fadc_time_offsets" ,runNumber, variation, tagh_fadc_time_offsets,2);// Interested in 2nd column
210  GetCCDBConstants("/PHOTON_BEAM/hodoscope/tdc_time_offsets" ,runNumber, variation, tagh_tdc_time_offsets,2);
211  GetCCDBConstants("/PHOTON_BEAM/hodoscope/counter_quality" ,runNumber, variation, tagh_counter_quality,2);
212  GetCCDBConstants("/TOF/adc_timing_offsets",runNumber, variation, tof_fadc_time_offsets);
213  GetCCDBConstants("/TOF/timing_offsets",runNumber, variation, tof_tdc_time_offsets);
214  GetCCDBConstants2D("/FDC/package1/wire_timing_offsets", runNumber, variation, fdc_wire_offsets_package1, 96);
215  GetCCDBConstants2D("/FDC/package2/wire_timing_offsets", runNumber, variation, fdc_wire_offsets_package2, 96);
216  GetCCDBConstants2D("/FDC/package3/wire_timing_offsets", runNumber, variation, fdc_wire_offsets_package3, 96);
217  GetCCDBConstants2D("/FDC/package4/wire_timing_offsets", runNumber, variation, fdc_wire_offsets_package4, 96);
218 
219  vector< vector< vector<double> > > old_FDC_wire_offsets;
220  old_FDC_wire_offsets.push_back( fdc_wire_offsets_package1 );
221  old_FDC_wire_offsets.push_back( fdc_wire_offsets_package2 );
222  old_FDC_wire_offsets.push_back( fdc_wire_offsets_package3 );
223  old_FDC_wire_offsets.push_back( fdc_wire_offsets_package4 );
224 
225  cout << "CDC base times = " << cdc_t_base << endl;
226  cout << "FCAL base times = " << fcal_t_base << endl;
227  cout << "FDC base times = " << fdc_t_base_fadc << ", " << fdc_t_base_tdc << endl;
228  cout << "BCAL base times = " << bcal_t_base_fadc << ", " << bcal_t_base_tdc << endl;
229  cout << "SC base times = " << sc_t_base_fadc << ", " << sc_t_base_tdc << endl;
230  cout << "TOF base times = " << tof_t_base_fadc << ", " << tof_t_base_tdc << endl;
231  cout << "TAGH base times = " << tagh_t_base_fadc << ", " << tagh_t_base_tdc << endl;
232  cout << "TAGM base times = " << tagm_t_base_fadc << ", " << tagm_t_base_tdc << endl;
233 
234  cout << endl;
235  cout << "RF_Period = " << RF_Period << endl;
236  cout << endl;
237 
238  cout << "Done grabbing CCDB constants...Entering fits..." << endl;
239 
240  // Assume our alignment is okay, and mainly we need to either correct for some overall timing shifts
241  // or per-channel shifts due to some firmware timing "features". These include:
242  // - 2 ns shifts of RF time
243  // - 32 ns shifts of high-res TDC modules
244  // - 4 ns shifts of fADC250 channels
245  // - FDC TDC crate shifts
246  // Note that BCAL/FCAL 4ns shifts are handled with different plugins
247 
248  //When the RF is present we can try to simply pick out the correct beam bucket for each of the runs
249  //First just a simple check to see if we have the appropriate data
250  bool useRF = false;
251  TH1I *testHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TAGH_TDC_RF_Compare","Counter ID 001");
252  if (testHist != NULL){ // Not great since we rely on channel 1 working, but can be craftier later.
253  cout << "Using RF Times for Calibration" << endl;
254  useRF = true;
255  }
256  ofstream outFile;
257  TH2I *thisHist;
258  thisHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - SC Target Time");
259 
260 
261  if (useRF) thisHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch Time");
262  if (thisHist != NULL){
263  //Statistics on these histograms are really quite low we will have to rebin and do some interpolation
264  outFile.open(prefix + "tagm_tdc_timing_offsets.txt", ios::out | ios::trunc);
265  outFile.close(); // clear file
266  outFile.open(prefix + "tagm_adc_timing_offsets.txt", ios::out | ios::trunc);
267  outFile.close(); // clear file
268  int nBinsX = thisHist->GetNbinsX();
269  int nBinsY = thisHist->GetNbinsY();
270  TH1D * selectedTAGMOffset = new TH1D("selectedTAGMOffset", "Selected TAGM Offset; Column; Offset [ns]", nBinsX, 0.5, nBinsX + 0.5);
271  TH1I * TAGMOffsetDistribution = new TH1I("TAGMOffsetDistribution", "TAGM Offset; TAGM Offset [ns]; Entries", 500, -250, 250);
272  for (int i = 1 ; i <= nBinsX; i++){
273  TH1D *projY = thisHist->ProjectionY("temp", i, i);
274  // Scan over the histogram
275  //chose the correct number of bins based on the histogram
276  float nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
277  float timeWindow = 3; //ns (Full Width)
278  int binWindow = int(timeWindow / nsPerBin);
279  double maxEntries = 0;
280  double maxMean = 0;
281  for (int j = 1 ; j <= projY->GetNbinsX();j++){
282  int minBin = j;
283  int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
284  double sum = 0, nEntries = 0;
285  for (int bin = minBin; bin <= maxBin; bin++){
286  sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
287  nEntries += projY->GetBinContent(bin);
288  if (bin == maxBin){
289  if (nEntries > maxEntries) {
290  maxMean = sum / nEntries;
291  maxEntries = nEntries;
292  }
293  }
294  }
295  }
296  //In the case there is RF, our job is to pick just the number of the correct beam bunch, so that's really all we need.
297  if(useRF) {
298  int beamBucket = int((maxMean / RF_Period) + 0.5); // +0.5 to handle rounding correctly
299  selectedTAGMOffset->SetBinContent(i, beamBucket);
300  TAGMOffsetDistribution->Fill(beamBucket);
301  }
302  else{
303  selectedTAGMOffset->SetBinContent(i, maxMean);
304  TAGMOffsetDistribution->Fill(maxMean);
305  }
306  }
307  double meanOffset = TAGMOffsetDistribution->GetMean();
308  // This might be in units of beam bunches, so we need to convert
309  if (useRF) meanOffset *= RF_Period;
310  if (verbose) {
311  cout << "Dumping TAGM results...\n=======================================" << endl;
312  cout << "TAGM mean Offset = " << meanOffset << endl;
313  cout << "fADC Offsets" << endl;
314  }
315 
316  outFile.open(prefix + "tagm_adc_timing_offsets.txt", ios::out);
317  //for (int i = 1 ; i <= nBinsX; i++){
318  // Loop over rows
319  if (verbose) cout << "Column\tRow\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
320  for (unsigned int column = 1; column <= 102; column++){
321  int index = GetCCDBIndexTAGM(column, 0);
322  double valueToUse = selectedTAGMOffset->GetBinContent(index);
323  if (useRF) valueToUse *= RF_Period;
324 
325  //if (valueToUse == 0) valueToUse = meanOffset;
326  outFile << "0 " << column << " " << valueToUse + tagm_fadc_time_offsets[index-1] - meanOffset<< endl;
327  if (verbose) printf("0\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", column, valueToUse, tagm_fadc_time_offsets[index-1], meanOffset,
328  valueToUse + tagm_fadc_time_offsets[index-1] - meanOffset);
329  if (column == 9 || column == 27 || column == 81 || column == 99){
330  for (unsigned int row = 1; row <= 5; row++){
331  index = GetCCDBIndexTAGM(column, row);
332  valueToUse = selectedTAGMOffset->GetBinContent(index);
333  if (useRF) valueToUse *= RF_Period;
334  //if (valueToUse == 0) valueToUse = meanOffset;
335  outFile << row << " " << column << " " << valueToUse + tagm_fadc_time_offsets[index-1] - meanOffset<< endl;
336  if (verbose) printf("%i\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", row, column, valueToUse, tagm_fadc_time_offsets[index-1], meanOffset,
337  valueToUse + tagm_fadc_time_offsets[index-1] - meanOffset);
338  }
339  }
340  }
341  outFile.close();
342 
343  if (verbose) {
344  cout << "TDC Offsets" << endl;
345  cout << "Column\tRow\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
346  }
347  outFile.open(prefix + "tagm_tdc_timing_offsets.txt", ios::out);
348  //for (int i = 1 ; i <= nBinsX; i++){
349  // Loop over rows
350  for (unsigned int column = 1; column <= 102; column++){
351  int index = GetCCDBIndexTAGM(column, 0);
352  double valueToUse = selectedTAGMOffset->GetBinContent(index);
353  if (useRF) valueToUse *= RF_Period;
354  //if (valueToUse == 0) valueToUse = meanOffset;
355  outFile << "0 " << column << " " << valueToUse + tagm_tdc_time_offsets[index-1] - meanOffset << endl;
356  if (verbose) printf("0\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", column, valueToUse, tagm_tdc_time_offsets[index-1], meanOffset,
357  valueToUse + tagm_tdc_time_offsets[index-1] - meanOffset);
358  if (column == 9 || column == 27 || column == 81 || column == 99){
359  for (unsigned int row = 1; row <= 5; row++){
360  index = GetCCDBIndexTAGM(column, row);
361  valueToUse = selectedTAGMOffset->GetBinContent(index);
362  if (useRF) valueToUse *= RF_Period;
363  //if (valueToUse == 0) valueToUse = meanOffset;
364  outFile << row << " " << column << " " << valueToUse + tagm_tdc_time_offsets[index-1] - meanOffset << endl;
365  if (verbose) printf("%i\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", row, column, valueToUse, tagm_tdc_time_offsets[index-1], meanOffset,
366  valueToUse + tagm_tdc_time_offsets[index-1] - meanOffset);
367  }
368  }
369  }
370  outFile.close();
371  outFile.open(prefix + "tagm_base_time.txt", ios::out);
372  if (verbose) {
373  printf("TAGM ADC Base = %f - (%f) = %f\n", tagm_t_base_fadc, meanOffset, tagm_t_base_fadc - meanOffset);
374  printf("TAGM TDC Base = %f - (%f) = %f\n", tagm_t_base_tdc, meanOffset, tagm_t_base_tdc - meanOffset);
375  }
376  outFile << tagm_t_base_fadc - meanOffset << " " << tagm_t_base_tdc - meanOffset << endl;
377  outFile.close();
378 
379  }
380 
381 
382  TH1I *tagmRFalignHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "TAGM - RFBunch 1D Time");
383  if(tagmRFalignHist != NULL) {
384  double maximum = tagmRFalignHist->GetBinCenter(tagmRFalignHist->GetMaximumBin());
385  TFitResultPtr fr = tagmRFalignHist->Fit("gaus", "SQ", "", maximum - 0.3, maximum + 0.4);
386  double meanOffset = fr->Parameter(1);
387 
388  outFile.open(prefix + "tagm_base_time.txt", ios::out);
389  if (verbose) {
390  printf("TAGM ADC Base = %f - (%f) = %f\n", tagm_t_base_fadc, meanOffset, tagm_t_base_fadc - meanOffset);
391  printf("TAGM TDC Base = %f - (%f) = %f\n", tagm_t_base_tdc, meanOffset, tagm_t_base_tdc - meanOffset);
392  }
393  outFile << tagm_t_base_fadc - meanOffset << " " << tagm_t_base_tdc - meanOffset << endl;
394  outFile.close();
395  }
396 
397  thisHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - SC Target Time");
398  if (useRF) thisHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "TRACKING", "TAGH - RFBunch Time");
399  if (thisHist != NULL) {
400  outFile.open(prefix + "tagh_tdc_timing_offsets.txt", ios::out | ios::trunc);
401  outFile.close(); // clear file
402  outFile.open(prefix + "tagh_adc_timing_offsets.txt", ios::out | ios::trunc);
403  outFile.close(); // clear file
404 
405 
406  // Also realign ADCs and TDCs
407  // assuming anything larger than 28 ns is due to a TDC shift, otherwise the ADCs are shifted
408  TH2I *taghADCTDCHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "TAGH", "TAGHHit TDC_ADC Difference");
409  int nBinsX = thisHist->GetNbinsX();
410  double taghAdcOffsets[274] = { 0. };
411  double taghTdcOffsets[274] = { 0. };
412 
413  //TH1D * selectedTAGHOffset = new TH1D("selectedTAGHOffset", "Selected TAGH Offset; Column; Offset [ns]", nBinsX, 0.5, nBinsX + 0.5);
414  for (int i = 1 ; i <= nBinsX; i++) {
415  TH1D *projY = taghADCTDCHist->ProjectionY("temp", i, i);
416  // Scan over histogram to find mean offset in timeWindow with largest integral
417  double nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
418  double timeWindow = 2.0; //ns (Full Width)
419  int binWindow = int(timeWindow / nsPerBin);
420  double maxEntries = 0;
421  double maxMean = 0;
422  for (int j = 1 ; j <= projY->GetNbinsX();j++) {
423  int minBin = j;
424  int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
425  double sum = 0, nEntries = 0;
426  for (int bin = minBin; bin <= maxBin; bin++) {
427  sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
428  nEntries += projY->GetBinContent(bin);
429  if (bin == maxBin) {
430  if (nEntries > maxEntries) {
431  maxMean = sum / nEntries;
432  maxEntries = nEntries;
433  }
434  }
435  }
436  }
437  //selectedTAGHOffset->SetBinContent(i, maxMean);
438 
439  // classify offset
440  if(fabs(maxMean) > 1.5) { // only correct shifts more than 1.5 ns, to avoid specious shifts
441  if(fabs(maxMean) > 28.)
442  taghTdcOffsets[i-1] = maxMean;
443  else
444  taghAdcOffsets[i-1] = maxMean;
445  }
446  }
447 
448  // Setup histogram for determining the most probable change in offset for each F1TDC slot
449  // This is needed to account for the occasional uniform shift in offsets of the 32 counters in a slot
450  // Disable this for now, it hasn't been working well...
451  //const int NtdcSlots = 8;
452  //TH1I * tdcDist[NtdcSlots];
453  //for (int i = 1; i <= NtdcSlots; i++) {
454  // stringstream ss; ss << i;
455  // TString s = ss.str();
456  // double range = 500.0; double width = 0.1;
457  // int Nbins = range/width;
458  // double low = -0.5*range - 0.5*width;
459  // double high = 0.5*range - 0.5*width;
460  // tdcDist[i-1] = new TH1I("TAGHOffsetDistribution_"+s, "TAGH Offset (slot "+s+"); TAGH Offset [ns]; Entries", Nbins, low, high);
461  // }
462 
463  //int nBinsX = thisHist->GetNbinsX();
464  TH1D * selectedTAGHOffset = new TH1D("selectedTAGHOffset", "Selected TAGH Offset; ID; Offset [ns]", nBinsX, 0.5, nBinsX + 0.5);
465  for (int i = 1 ; i <= nBinsX; i++) {
466  TH1D *projY = thisHist->ProjectionY("temp", i, i);
467  // Scan over histogram to find mean offset in timeWindow with largest integral
468  // Choose the correct number of bins based on the histogram
469  double nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
470  double timeWindow = 2.0; // ns (Full Width)
471  int binWindow = int(timeWindow / nsPerBin);
472 
473  double maxEntries = 0;
474  double maxMean = 0;
475  for (int j = 1; j <= projY->GetNbinsX(); j++) {
476  int minBin = j;
477  int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
478  double sum = 0;
479  double nEntries = 0;
480  for (int bin = minBin; bin <= maxBin; bin++) {
481  sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
482  nEntries += projY->GetBinContent(bin);
483  if (bin == maxBin) {
484  if (nEntries > maxEntries) {
485  maxMean = sum / nEntries;
486  maxEntries = nEntries;
487  }
488  }
489  }
490  }
491 
492  if (tagh_counter_quality[i-1] == 0.0) {
493  selectedTAGHOffset->SetBinContent(i, 0);
494  continue;
495  }
496  //int tdc_slot = GetF1TDCslotTAGH(i);
497  // if the shift is too large, it's probably due to a TDC module shifting, so ignore that
498  if(fabs(maxMean) > 28.)
499  maxMean = 0.;
500  if (useRF) {
501  int beamBucket;
502  if (maxMean >= 0) beamBucket = int((maxMean / RF_Period) + 0.5); // +0.5 to handle rounding correctly
503  else beamBucket = int((maxMean / RF_Period) - 0.5);
504  selectedTAGHOffset->SetBinContent(i, beamBucket);
505  //if (maxEntries != 0.0) tdcDist[tdc_slot - 1]->Fill(beamBucket);
506  } else {
507  selectedTAGHOffset->SetBinContent(i, maxMean);
508  //if (maxEntries != 0.0) tdcDist[tdc_slot - 1]->Fill(maxMean);
509  }
510  }
511  // Most probable change in offset or beam bucket per F1TDC slot
512  //double mpDelta[NtdcSlots];
513  //for (int i = 1; i <= NtdcSlots; i++) {
514  // int mpBin = tdcDist[i-1]->GetMaximumBin();
515  // mpDelta[i-1] = (mpBin > 0) ? tdcDist[i-1]->GetBinCenter(mpBin) : 0.0;
516  // if (useRF) mpDelta[i-1] *= RF_Period;
517  // if (verbose) {
518  // cout << "TAGH most probable Offset = " << i << ", " << mpDelta[i-1] << endl;
519  // }
520  // }
521 
522  if (verbose) {
523  cout << "Dumping TAGH results...\n=======================================" << endl;
524  //cout << "Type\tChannel\tvalueToUse\toldValue\tmpDelta\tTotal" << endl;
525  cout << "Type\tChannel\tvalueToUse\toldValue\tTotal" << endl;
526  }
527 
528  double limit = 2.5; // ns
529  double ccdb_sum = 0.0;
530  for (int i = 1; i <= nBinsX; i++) ccdb_sum += tagh_tdc_time_offsets[i-1];
531  double c1_tdcOffset = 0.0;
532  outFile.open(prefix + "tagh_tdc_timing_offsets.txt");
533  for (int i = 1; i <= nBinsX; i++) {
534  if (tagh_counter_quality[i-1] == 0.0) {
535  outFile << i << " " << 0 << endl;
536  continue;
537  }
538  //int tdc_slot = GetF1TDCslotTAGH(i);
539  double delta = selectedTAGHOffset->GetBinContent(i);
540  if (useRF) delta *= RF_Period;
541  //if (ccdb_sum > 0.0 && fabs(delta - mpDelta[tdc_slot-1]) > limit) {
542  // delta = mpDelta[tdc_slot-1];
543  // }
544  double ccdb = tagh_tdc_time_offsets[i-1];
545  double offset = ccdb + delta - taghTdcOffsets[i-1];
546  //if (i == 1) c1_tdcOffset = offset;
547  //offset -= c1_tdcOffset;
548  outFile << i << " " << offset << endl;
549  //if (verbose) printf("TDC\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, mpDelta[tdc_slot-1], offset);
550  if (verbose) printf("TDC\t%i\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, offset);
551  }
552  outFile.close();
553 
554  ccdb_sum = 0.0;
555  for (int i = 1; i <= nBinsX; i++) ccdb_sum += tagh_fadc_time_offsets[i-1];
556  double c1_adcOffset = 0.0;
557  outFile.open(prefix + "tagh_adc_timing_offsets.txt");
558  for (int i = 1; i <= nBinsX; i++) {
559  if (tagh_counter_quality[i-1] == 0.0) {
560  outFile << i << " " << 0 << endl;
561  continue;
562  }
563  //int tdc_slot = GetF1TDCslotTAGH(i);
564  double delta = selectedTAGHOffset->GetBinContent(i);
565  if (useRF) delta *= RF_Period;
566  //if (ccdb_sum > 0.0 && fabs(delta - mpDelta[tdc_slot-1]) > limit) {
567  // delta = mpDelta[tdc_slot-1];
568  // }
569  double ccdb = tagh_fadc_time_offsets[i-1];
570  double offset = ccdb + delta - taghAdcOffsets[i-1];
571  if (i == 1) c1_adcOffset = offset;
572  offset -= c1_adcOffset;
573  outFile << i << " " << offset << endl;
574  //if (verbose) printf("ADC\t%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, mpDelta[tdc_slot-1], offset);
575  if (verbose) printf("ADC\t%i\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, offset);
576  }
577  outFile.close();
578 
579  outFile.open(prefix + "tagh_base_time.txt");
580  outFile << tagh_t_base_fadc - c1_adcOffset << " " << tagh_t_base_tdc - c1_tdcOffset << endl;
581  if (verbose) {
582  printf("TAGH ADC Base = %f - (%f) = %f\n", tagh_t_base_fadc, c1_adcOffset, tagh_t_base_fadc - c1_adcOffset);
583  printf("TAGH TDC Base = %f - (%f) = %f\n", tagh_t_base_tdc, c1_tdcOffset, tagh_t_base_tdc - c1_tdcOffset);
584  }
585  outFile.close();
586  }
587 
588  // We can use the RF time to calibrate the SC time
589  double meanSCOffset = 0.0; // In case we change the time of the SC, we need this in this scope
590  if(useRF){
591  TH1F * selectedSCSectorOffset = new TH1F("selectedSCSectorOffset", "Selected TDC-RF offset;Sector; Time", 30, 0.5, 30.5);
592  TH1F * selectedSCSectorOffsetDistribution = new TH1F("selectedSCSectorOffsetDistribution", "Selected TDC-RF offset;Time;Entries", 100, -3.0, 3.0);
593  TF1* f = new TF1("f","pol0(0)+gaus(1)", -3.0, 3.0);
594 
595  // Also realign ADCs and TDCs
596  // assuming anything larger than 28 ns is due to a TDC shift, otherwise the ADCs are shifted
597  double scAdcOffsets[30] = { 0. };
598  double scTdcOffsets[30] = { 0. };
599  TH2I *scADCTDCHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "SC", "SCHit TDC_ADC Difference");
600 
601  for (int sector = 1; sector <= 30; sector++){
602  // first realign ADCs and TDCs
603  TH1D *projY = scADCTDCHist->ProjectionY("temp", sector, sector);
604  float nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
605  float timeWindow = 2; //ns (Full Width)
606  int binWindow = int(timeWindow / nsPerBin);
607  double maxEntries = 0;
608  double maxMean = 0;
609  for (int j = 1 ; j <= projY->GetNbinsX();j++){
610  int minBin = j;
611  int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
612  double sum = 0, nEntries = 0;
613  for (int bin = minBin; bin <= maxBin; bin++){
614  sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
615  nEntries += projY->GetBinContent(bin);
616  if (bin == maxBin){
617  if (nEntries > maxEntries) {
618  maxMean = sum / nEntries;
619  maxEntries = nEntries;
620  }
621  }
622  }
623  }
624 
625  // classify offset
626  if(fabs(maxMean) > 1.5) { // only correct shifts more than 1.5 ns, to avoid specious shifts
627  if(fabs(maxMean) > 28.)
628  scTdcOffsets[sector-1] = maxMean;
629  else
630  scAdcOffsets[sector-1] = maxMean;
631  }
632 
633  // now align RF times
634  if(!DONT_SHIFT_SC_RF) {
635  TH1I *scRFHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "SC_Target_RF_Compare", Form("Sector %.2i", sector));
636  if (scRFHist == NULL) continue;
637  //Do the fit
638  TFitResultPtr fr = scRFHist->Fit("pol0", "SQ", "", -2, 2);
639  double p0 = fr->Parameter(0);
640 
641  f->FixParameter(0,p0);
642  f->SetParLimits(2, -2, 2);
643  f->SetParLimits(3, 0, 2);
644  f->SetParameter(1, 10);
645  f->SetParameter(2, scRFHist->GetBinCenter(scRFHist->GetMaximumBin()));
646  f->SetParameter(3, 0);
647 
648  fr = scRFHist->Fit(f, "SQ", "", -2, 2);
649  double SCOffset = fr->Parameter(2);
650  selectedSCSectorOffset->SetBinContent(sector, SCOffset);
651  selectedSCSectorOffsetDistribution->Fill(SCOffset);
652  }
653  }
654 
655  // Now write out the offsets
656  meanSCOffset = selectedSCSectorOffsetDistribution->GetMean();
657  // Move mean SC time around if we want it to end up in a particular location
658  if(FIX_START_COUNTER) { // experimental
659  TH1I *scHitTimeHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "SC", "SCHitMatchedTime");
660  double scMax = scHitTimeHist->GetBinCenter(scHitTimeHist->GetMaximumBin());
661  TFitResultPtr fr = scHitTimeHist->Fit("gaus", "S", "", scMax - 5., scMax + 5.);
662 
663  meanSCOffset = fr->Parameter(0) - meanSCOffset - start_counter_t0; // this will get it close, need to make it line up
664  }
665  if (verbose){
666  cout << "Dumping SC results...\n=======================================" << endl;
667  cout << "SC mean Offset = " << meanSCOffset << endl;
668  cout << "TDC Offsets" << endl;
669  cout << "Sector\toldValue\tValueToUse\tmeanOffset\tTotal" << endl;
670  }
671  outFile.open(prefix + "sc_tdc_timing_offsets.txt");
672  for (int sector = 1; sector <= 30; sector++){
673  outFile << sc_tdc_time_offsets[sector-1] + scTdcOffsets[sector-1] + selectedSCSectorOffset->GetBinContent(sector) - meanSCOffset << endl;
674  if (verbose) printf("%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n",sector, sc_tdc_time_offsets[sector-1], selectedSCSectorOffset->GetBinContent(sector), meanSCOffset,
675  sc_tdc_time_offsets[sector-1] - scTdcOffsets[sector-1] + selectedSCSectorOffset->GetBinContent(sector) - meanSCOffset);
676  }
677  outFile.close();
678  if (verbose){
679  cout << "ADC Offsets" << endl;
680  cout << "Sector\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
681  }
682  outFile.open(prefix + "sc_adc_timing_offsets.txt");
683  for (int sector = 1; sector <= 30; sector++){
684  outFile << sc_fadc_time_offsets[sector-1] + scAdcOffsets[sector-1] + selectedSCSectorOffset->GetBinContent(sector) - meanSCOffset << endl;
685  if (verbose) printf("%i\t%.3f\t\t%.3f\t\t%.3f\t\t%.3f\n",sector,sc_fadc_time_offsets[sector-1], selectedSCSectorOffset->GetBinContent(sector), meanSCOffset,
686  sc_fadc_time_offsets[sector-1] - scAdcOffsets[sector-1] + selectedSCSectorOffset->GetBinContent(sector) - meanSCOffset);
687  }
688  outFile.close();
689 
690  outFile.open(prefix + "sc_base_time.txt");
691  outFile << sc_t_base_fadc - meanSCOffset << " " << sc_t_base_tdc - meanSCOffset << endl;
692  if (verbose) {
693  printf("SC ADC Base = %f - (%f) = %f\n", sc_t_base_fadc, meanSCOffset, sc_t_base_fadc - meanSCOffset);
694  printf("SC TDC Base = %f - (%f) = %f\n", sc_t_base_tdc, meanSCOffset, sc_t_base_tdc - meanSCOffset);
695  }
696  outFile.close();
697  }
698 
699  TH1I *this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "TOF - RF Time");
700  if(this1DHist != NULL){
701  //Gaussian
702  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
703  TFitResultPtr fr = this1DHist->Fit("gaus", "S", "", maximum - 0.25, maximum + 0.25);
704  float mean = fr->Parameter(1);
705  outFile.open(prefix + "tof_base_time.txt");
706  if (verbose) {
707  printf("TOF ADC Base = %f - (%f) - (%f) = %f\n", tof_t_base_fadc, mean, meanSCOffset, tof_t_base_fadc - mean - meanSCOffset);
708  printf("TOF TDC Base = %f - (%f) - (%f) = %f\n", tof_t_base_tdc, mean, meanSCOffset, tof_t_base_tdc - mean - meanSCOffset);
709  }
710  outFile << tof_t_base_fadc - mean - meanSCOffset<< " " << tof_t_base_tdc - mean - meanSCOffset<< endl;
711  outFile.close();
712  }
713 
714  if(USE_NEUTRALS_FOR_BCAL_RF_ALIGN)
715  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time (Neutral)");
716  else
717  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "BCAL - RF Time");
718  if(this1DHist != NULL){
719  //Gaussian
720  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
721  TFitResultPtr fr = this1DHist->Fit("gaus", "S", "", maximum - 0.55, maximum + 0.55);
722  float mean = fr->Parameter(1);
723  outFile.open(prefix + "bcal_base_time.txt");
724  if (verbose) {
725  printf("BCAL ADC Base = %f - (%f) - (%f) = %f\n", bcal_t_base_fadc, mean, meanSCOffset, bcal_t_base_fadc - mean - meanSCOffset);
726  printf("BCAL TDC Base = %f - (%f) - (%f) = %f\n", bcal_t_base_tdc, mean, meanSCOffset, bcal_t_base_tdc - mean - meanSCOffset);
727  }
728  outFile << bcal_t_base_fadc - mean - meanSCOffset << " " << bcal_t_base_tdc - mean - meanSCOffset << endl; // TDC info not used
729  outFile.close();
730  }
731 
732  if(USE_NEUTRALS_FOR_FCAL_RF_ALIGN)
733  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time (Neutral)");
734  else
735  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "FCAL - RF Time");
736  if(this1DHist != NULL){
737  //Gaussian
738  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
739  TFitResultPtr fr = this1DHist->Fit("gaus", "S", "", maximum - 1., maximum + 1.);
740  float mean = fr->Parameter(1);
741  outFile.open(prefix + "fcal_base_time.txt");
742  if (verbose) {
743  printf("FCAL ADC Base = %f - (%f) - (%f) = %f\n",fcal_t_base, mean, meanSCOffset, fcal_t_base - mean - meanSCOffset);
744  }
745  outFile << fcal_t_base - mean - meanSCOffset<< endl;
746  outFile.close();
747  }
748 
749  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "Earliest CDC Time Minus Matched SC Time");
750  if(this1DHist != NULL){
751  //Gaussian
752  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
753  TFitResultPtr fr = this1DHist->Fit("gaus", "S", "", maximum - 10, maximum + 6);
754  float mean = fr->Parameter(1);
755  outFile.open(prefix + "cdc_base_time.txt");
756  if (verbose) {
757  printf("CDC ADC Base = %f - (%f) - (%f) = %f\n",cdc_t_base, mean, meanSCOffset, cdc_t_base - mean - meanSCOffset);
758  }
759  outFile << cdc_t_base - mean - meanSCOffset << endl;
760  outFile.close();
761  }
762 
763  // We want to account for any residual difference between the cathode and anode times.
764  double FDC_ADC_Offset = 0.0, FDC_TDC_Offset = 0.0;
765  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "FDC", "FDCHit Cathode time;1");
766  if(this1DHist != NULL){
767  Int_t firstBin = this1DHist->FindFirstBinAbove( 1 , 1); // Find first bin with content above 1 in the histogram
768  // extended range due to extra FDC noise - sdobbs, 4/9/2018
769  for (int i = 0; i <= 46; i++){
770  if ((firstBin + i) > 0) this1DHist->SetBinContent((firstBin + i), 0);
771  }
772  //Fit a gaussian to the left of the main peak
773  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
774 
775  //cout << "FDC max = " << maximum << endl;
776  //cout << "FDC first = " << firstBin << endl;
777  //this1DHist->Print("all");
778 
779  TF1 *f = new TF1("f", "gaus");
780  f->SetParameters(100, maximum, 20);
781  //this1DHist->Rebin(2);
782  TFitResultPtr fr = this1DHist->Fit(f, "S", "", maximum - 17, maximum + 7); // Cant fix value at end of range
783  double mean = fr->Parameter(1);
784  float sigma = fr->Parameter(2);
785  FDC_ADC_Offset = mean;
786  delete f;
787  }
788 
789  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "FDC", "FDCHit Wire time;1");
790  if(this1DHist != NULL){
791  Int_t firstBin = this1DHist->FindLastBinAbove( 1 , 1); // Find first bin with content above 1 in the histogram
792  for (int i = 0; i <= 25; i++){
793  if ((firstBin + i) > 0) this1DHist->SetBinContent((firstBin + i), 0);
794  }
795  //Fit a gaussian to the left of the main peak
796  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
797  TF1 *f = new TF1("f", "gaus");
798  f->SetParameters(100, maximum, 20);
799  TFitResultPtr fr = this1DHist->Fit(f, "S", "", maximum - 10, maximum + 6); // Cant fix value at end of range
800  double mean = fr->Parameter(1);
801  float sigma = fr->Parameter(2);
802  FDC_TDC_Offset = mean;
803  delete f;
804  }
805  double FDC_ADC_TDC_Offset = FDC_ADC_Offset - FDC_TDC_Offset;
806 
807  this1DHist = ExtractTrackBasedTimingNS::Get1DHistogram("HLDetectorTiming", "TRACKING", "Earliest Flight-time Corrected FDC Time");
808  float MPV = 0;
809  if(this1DHist != NULL){
810  //Landau
811  Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
812  TFitResultPtr fr = this1DHist->Fit("landau", "S", "", maximum - 3.5, maximum + 6);
813  //TFitResultPtr fr = this1DHist->Fit("landau", "S", "", maximum - 2., maximum + 6);
814  //float MPV = fr->Parameter(1);
815  MPV = fr->Parameter(1);
816  }
817 
818  //cerr << "*** MPV = " << MPV << endl;
819 
820  // see if we need to correct any FDC individual wire times. generally, assume that most of the detector is in time
821  // so the main wire timing peak is correct. then if any TDC modules deviate from this, then align them to the main peak
822  thisHist = ExtractTrackBasedTimingNS::Get2DHistogram("HLDetectorTiming", "FDC", "FDCHit Wire time vs. module");
823  double avg_FDC_TDC_wire_offsets = 0;
824  if(thisHist != NULL){
825  bool package_times_shifted[4] = { false, false, false, false };
826  double FDC_wire_offsets[4][6][96] = { 0. };
827  for(int plane=1; plane<=24; plane++) {
828  // check one half of the plane
829  char buf[50];
830  sprintf(buf,"temp_fdc_%d", 2*plane-1);
831  TH1D *projY = thisHist->ProjectionY(buf, 2*plane-1, 2*plane-1);
832  cout << " plane " << plane << endl;
833  //TH1D *projY = thisHist->ProjectionY("temp", 2*plane-1, 2*plane-1);
834  //Int_t firstBin = projY->FindLastBinAbove( 1 , 1); // Find first bin with content above 1 in the histogram
835  //for (int i = 0; i <= 15; i++){
836  // if ((firstBin + i) > 0) projY->SetBinContent((firstBin + i), 0);
837  //}
838  //Fit a gaussian to the left of the main peak
839  Double_t maximum = projY->GetBinCenter(projY->GetMaximumBin());
840  double mean1 = 40.;
841  float sigma1 = 0.;
842  TF1 *f = new TF1("f", "gaus");
843 
844  if(maximum > -190.) {
845  mean1 = maximum - MPV;
846  //cout << mean1 << " " << maximum << " " << MPV << endl;
847  // Disable fits for now, not terribly stable
848  //TF1 *f = new TF1("f", "gaus");
849  TF1 fg("fg", "gaus");
850  fg.SetParameters(100, maximum, 20);
851  TFitResultPtr fr = projY->Fit(&fg, "S", "", maximum - 10, maximum + 8); // Cant fix value at end of range
852  mean1 = fr->Parameter(1) - MPV;
853  sigma1 = fr->Parameter(2);
854  //cerr << " mean = " << mean1 << endl;
855  //delete f;
856  }
857 
858 
859  // now check the other half
860  sprintf(buf,"temp_fdc_%d", 2*plane);
861  projY = thisHist->ProjectionY(buf, 2*plane, 2*plane);
862  //projY = thisHist->ProjectionY("temp", 2*plane, 2*plane);
863  //firstBin = projY->FindLastBinAbove( 1 , 1); // Find first bin with content above 1 in the histogram
864  //for (int i = 0; i <= 15; i++){
865  // if ((firstBin + i) > 0) projY->SetBinContent((firstBin + i), 0);
866  //}
867  //Fit a gaussian to the left of the main peak
868  maximum = projY->GetBinCenter(projY->GetMaximumBin());
869  double mean2 = 40.;
870  float sigma2 = 0.;
871 
872  if(maximum > -190.) {
873  mean2 = maximum - MPV;
874  //cout << mean2 << " " << maximum << " " << MPV << endl;
875  // Disable fits for now, not terribly stable
876  //f = new TF1("f", "gaus");
877  TF1 fg2("fg2", "gaus");
878  fg2.SetParameters(100, maximum, 20);
879  TFitResultPtr fr2 = projY->Fit(&fg2, "S", "", maximum - 10, maximum + 8); // Cant fix value at end of range
880  mean2 = fr2->Parameter(1) - MPV;
881  sigma2 = fr2->Parameter(2);
882  //cerr << " mean = " << mean2 << endl;
883  //delete f;
884  }
885 
886  int package = 0;
887  int chamber = 0;
888  FindFDCPackageChamber(plane, package, chamber);
889 
890  package_times_shifted[package-1] = true;
891 
892  // handle TDC modules separately
893  for(int wire=0; wire<48; wire++) {
894  // only correct wire shifts greater than some amount
895  //if(fabs(mean1) > 8.) {
896  FDC_wire_offsets[package-1][chamber-1][wire] = mean1 + old_FDC_wire_offsets[package-1][chamber-1][wire];
897  //} else {
898  // FDC_wire_offsets[package-1][chamber-1][wire] = old_FDC_wire_offsets[package-1][chamber-1][wire];
899  //}
900  }
901 
902  for(int wire=48; wire<96; wire++) {
903  // only correct wire shifts greater than some amount
904  //if(fabs(mean2) > 8.) {
905  FDC_wire_offsets[package-1][chamber-1][wire] = mean2 + old_FDC_wire_offsets[package-1][chamber-1][wire];
906  //} else {
907  //FDC_wire_offsets[package-1][chamber-1][wire] = old_FDC_wire_offsets[package-1][chamber-1][wire];
908  //}
909  }
910 
911  // keep an average on a per-module basis
912  avg_FDC_TDC_wire_offsets += FDC_wire_offsets[package-1][chamber-1][1];
913  avg_FDC_TDC_wire_offsets += FDC_wire_offsets[package-1][chamber-1][50];
914  }
915 
916  // make the average
917  avg_FDC_TDC_wire_offsets /= 48.;
918 
919  // now write out anything we need
920  for(int package = 0; package<4; package++) {
921  if(package_times_shifted[package]) {
922  char buf[50];
923  sprintf(buf, "%sfdc_package%d_wire_offsets.txt", prefix.Data(), package+1);
924  outFile.open(buf);
925  for(int chamber=0; chamber<6; chamber++) {
926  for(int wire=0; wire<96; wire++) {
927  outFile << (FDC_wire_offsets[package][chamber][wire] - avg_FDC_TDC_wire_offsets)/2. << " ";
928  }
929  outFile << endl;
930  }
931  outFile.close();
932  }
933  }
934  }
935 
936  // now we can finally write out the FDC offsets
937  outFile.open(prefix + "fdc_base_time.txt");
938  if (verbose) {
939  printf("FDC ADC Base = %f - (%f) - (%f) - (%f) = %f\n",fdc_t_base_fadc, MPV, meanSCOffset, FDC_ADC_TDC_Offset, fdc_t_base_fadc - MPV - meanSCOffset - FDC_ADC_TDC_Offset);
940  printf("FDC TDC Base = %f - (%f) - (%f) = %f\n",fdc_t_base_tdc, MPV, meanSCOffset, fdc_t_base_tdc - MPV - meanSCOffset);
941  }
942  //outFile << fdc_t_base_fadc - MPV - meanSCOffset - (FDC_ADC_TDC_Offset-avg_FDC_TDC_wire_offsets) << " " << fdc_t_base_tdc - MPV - avg_FDC_TDC_wire_offsets - meanSCOffset << endl;
943  //outFile << fdc_t_base_fadc + 5. - MPV - meanSCOffset - FDC_ADC_Offset << " " << fdc_t_base_tdc + 5. - MPV - avg_FDC_TDC_wire_offsets - meanSCOffset << endl;
944  outFile << fdc_t_base_fadc - MPV - meanSCOffset - FDC_ADC_TDC_Offset << " " << fdc_t_base_tdc - avg_FDC_TDC_wire_offsets - meanSCOffset << endl;
945  outFile.close();
946 
947 
948  cout << "FDC ADC: " << fdc_t_base_fadc << " " << MPV << " " << meanSCOffset << " "<< FDC_ADC_Offset << " "
949  << (FDC_ADC_TDC_Offset-avg_FDC_TDC_wire_offsets) << " "
950  << (fdc_t_base_fadc - MPV - meanSCOffset - FDC_ADC_TDC_Offset) << endl;
951  cout << "FDC TDC: " << fdc_t_base_tdc << " " << MPV << " " << avg_FDC_TDC_wire_offsets << " " << meanSCOffset << " "
952  << (fdc_t_base_tdc - MPV - avg_FDC_TDC_wire_offsets - meanSCOffset) << endl;
953 
954 
956  return;
957 }
TFile * outFile
Definition: FitGains.C:15
sprintf(text,"Post KinFit Cut")
void AdjustTiming(TString fileName="hd_root.root", int runNumber=10390, TString variation="default", bool verbose=false, TString prefix="")
TH1I * Get1DHistogram(const char *plugin, const char *directoryName, const char *name)
static char index(char c)
Definition: base64.cpp:115
TF1 * f
Definition: FitGains.C:21
double counter
Definition: FitGains.C:151
TH2I * Get2DHistogram(const char *plugin, const char *directoryName, const char *name)
void FindFDCPackageChamber(int plane, int &package, int &chamber)
void GetCCDBConstants(TString path, Int_t run, TString variation, vector< double > &container, Int_t column=1)
Double_t sigma[NCHANNELS]
Definition: st_tw_resols.C:37
void GetCCDBConstants2(TString path, Int_t run, TString variation, double &constant1, double &constant2)
void GetCCDBConstants2D(TString path, Int_t run, TString variation, vector< vector< double > > &container, Int_t numColumns)
void GetCCDBConstants1(TString path, Int_t run, TString variation, double &constant1)
int GetCCDBIndexTAGM(unsigned int column, unsigned int row)
printf("string=%s", string)