1 namespace ExtractTrackBasedTimingNS {
4 TH1I *
Get1DHistogram(
const char * plugin,
const char * directoryName,
const char * name){
6 TString fullName = TString(plugin) +
"/" + TString(directoryName) +
"/" + TString(name);
7 thisFile->GetObject(fullName, histogram);
9 cout <<
"Unable to find histogram " << fullName.Data() << endl;
15 TH2I *
Get2DHistogram(
const char * plugin,
const char * directoryName,
const char * name){
17 TString fullName = TString(plugin) +
"/" + TString(directoryName) +
"/" + TString(name);
18 thisFile->GetObject(fullName, histogram);
20 cout <<
"Unable to find histogram " << fullName.Data() << endl;
30 sprintf(command,
"ccdb dump %s:%i:%s", path.Data(),
run, variation.Data());
31 FILE* inputPipe = gSystem->OpenPipe(command,
"r");
36 if(fgets(buff,
sizeof(buff), inputPipe) == NULL)
41 while(fgets(buff,
sizeof(buff), inputPipe) != NULL){
42 istringstream locConstantsStream(buff);
43 while (locConstantsStream >> entry){
45 if (counter %
column == 0) container.push_back(entry);
49 gSystem->ClosePipe(inputPipe);
52 void GetCCDBConstants2D(TString path, Int_t
run, TString variation, vector< vector<double> >& container, Int_t numColumns){
55 sprintf(command,
"ccdb dump %s:%i:%s", path.Data(),
run, variation.Data());
56 FILE* inputPipe = gSystem->OpenPipe(command,
"r");
61 if(fgets(buff,
sizeof(buff), inputPipe) == NULL)
67 while(fgets(buff,
sizeof(buff), inputPipe) != NULL){
68 istringstream locConstantsStream(buff);
69 while (locConstantsStream >> entry){
71 if(counter == numColumns) {
72 container.push_back(row);
80 gSystem->ClosePipe(inputPipe);
86 sprintf(command,
"ccdb dump %s:%i:%s", path.Data(),
run, variation.Data());
87 FILE* inputPipe = gSystem->OpenPipe(command,
"r");
92 if(fgets(buff,
sizeof(buff), inputPipe) == NULL)
95 while(fgets(buff,
sizeof(buff), inputPipe) != NULL){
96 istringstream locConstantsStream(buff);
97 locConstantsStream >> constant1;
100 gSystem->ClosePipe(inputPipe);
105 sprintf(command,
"ccdb dump %s:%i:%s", path.Data(),
run, variation.Data());
106 FILE* inputPipe = gSystem->OpenPipe(command,
"r");
107 if(inputPipe == NULL)
111 if(fgets(buff,
sizeof(buff), inputPipe) == NULL)
114 while(fgets(buff,
sizeof(buff), inputPipe) != NULL){
115 istringstream locConstantsStream(buff);
116 locConstantsStream >> constant1 >> constant2;
119 gSystem->ClosePipe(inputPipe);
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;
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;
140 package = plane / 6 + 1;
148 void AdjustTiming(TString fileName =
"hd_root.root",
int runNumber = 10390, TString variation =
"default",
bool verbose =
false,TString prefix =
""){
150 bool FIX_START_COUNTER =
false;
152 double start_counter_t0 = 0.;
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;
158 cout <<
"Performing adjustment timing fits for File: " << fileName.Data() <<
" Run: " << runNumber <<
" Variation: " << variation.Data() << endl;
162 cout <<
"Unable to open file " << fileName.Data() <<
"...Exiting" << endl;
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;
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;
191 cout <<
"Grabbing CCDB constants..." << endl;
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);
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);
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);
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);
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 );
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;
235 cout <<
"RF_Period = " << RF_Period << endl;
238 cout <<
"Done grabbing CCDB constants...Entering fits..." << endl;
252 if (testHist != NULL){
253 cout <<
"Using RF Times for Calibration" << endl;
262 if (thisHist != NULL){
264 outFile.open(prefix +
"tagm_tdc_timing_offsets.txt", ios::out | ios::trunc);
266 outFile.open(prefix +
"tagm_adc_timing_offsets.txt", ios::out | ios::trunc);
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);
276 float nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
277 float timeWindow = 3;
278 int binWindow = int(timeWindow / nsPerBin);
279 double maxEntries = 0;
281 for (
int j = 1 ; j <= projY->GetNbinsX();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);
289 if (nEntries > maxEntries) {
290 maxMean = sum / nEntries;
291 maxEntries = nEntries;
298 int beamBucket = int((maxMean / RF_Period) + 0.5);
299 selectedTAGMOffset->SetBinContent(i, beamBucket);
300 TAGMOffsetDistribution->Fill(beamBucket);
303 selectedTAGMOffset->SetBinContent(i, maxMean);
304 TAGMOffsetDistribution->Fill(maxMean);
307 double meanOffset = TAGMOffsetDistribution->GetMean();
309 if (useRF) meanOffset *= RF_Period;
311 cout <<
"Dumping TAGM results...\n=======================================" << endl;
312 cout <<
"TAGM mean Offset = " << meanOffset << endl;
313 cout <<
"fADC Offsets" << endl;
316 outFile.open(prefix +
"tagm_adc_timing_offsets.txt", ios::out);
319 if (verbose) cout <<
"Column\tRow\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
322 double valueToUse = selectedTAGMOffset->GetBinContent(index);
323 if (useRF) valueToUse *= RF_Period;
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);
330 for (
unsigned int row = 1; row <= 5; row++){
332 valueToUse = selectedTAGMOffset->GetBinContent(index);
333 if (useRF) valueToUse *= RF_Period;
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);
344 cout <<
"TDC Offsets" << endl;
345 cout <<
"Column\tRow\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
347 outFile.open(prefix +
"tagm_tdc_timing_offsets.txt", ios::out);
352 double valueToUse = selectedTAGMOffset->GetBinContent(index);
353 if (useRF) valueToUse *= RF_Period;
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);
359 for (
unsigned int row = 1; row <= 5; row++){
361 valueToUse = selectedTAGMOffset->GetBinContent(index);
362 if (useRF) valueToUse *= RF_Period;
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);
371 outFile.open(prefix +
"tagm_base_time.txt", ios::out);
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);
376 outFile << tagm_t_base_fadc - meanOffset <<
" " << tagm_t_base_tdc - meanOffset << endl;
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);
388 outFile.open(prefix +
"tagm_base_time.txt", ios::out);
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);
393 outFile << tagm_t_base_fadc - meanOffset <<
" " << tagm_t_base_tdc - meanOffset << endl;
399 if (thisHist != NULL) {
400 outFile.open(prefix +
"tagh_tdc_timing_offsets.txt", ios::out | ios::trunc);
402 outFile.open(prefix +
"tagh_adc_timing_offsets.txt", ios::out | ios::trunc);
409 int nBinsX = thisHist->GetNbinsX();
410 double taghAdcOffsets[274] = { 0. };
411 double taghTdcOffsets[274] = { 0. };
414 for (
int i = 1 ; i <= nBinsX; i++) {
415 TH1D *projY = taghADCTDCHist->ProjectionY(
"temp", i, i);
417 double nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
418 double timeWindow = 2.0;
419 int binWindow = int(timeWindow / nsPerBin);
420 double maxEntries = 0;
422 for (
int j = 1 ; j <= projY->GetNbinsX();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);
430 if (nEntries > maxEntries) {
431 maxMean = sum / nEntries;
432 maxEntries = nEntries;
440 if(fabs(maxMean) > 1.5) {
441 if(fabs(maxMean) > 28.)
442 taghTdcOffsets[i-1] = maxMean;
444 taghAdcOffsets[i-1] = maxMean;
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);
469 double nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
470 double timeWindow = 2.0;
471 int binWindow = int(timeWindow / nsPerBin);
473 double maxEntries = 0;
475 for (
int j = 1; j <= projY->GetNbinsX(); j++) {
477 int maxBin = (j + binWindow) <= projY->GetNbinsX() ? (j + binWindow) : projY->GetNbinsX();
480 for (
int bin = minBin; bin <= maxBin; bin++) {
481 sum += projY->GetBinContent(bin) * projY->GetBinCenter(bin);
482 nEntries += projY->GetBinContent(bin);
484 if (nEntries > maxEntries) {
485 maxMean = sum / nEntries;
486 maxEntries = nEntries;
492 if (tagh_counter_quality[i-1] == 0.0) {
493 selectedTAGHOffset->SetBinContent(i, 0);
498 if(fabs(maxMean) > 28.)
502 if (maxMean >= 0) beamBucket = int((maxMean / RF_Period) + 0.5);
503 else beamBucket = int((maxMean / RF_Period) - 0.5);
504 selectedTAGHOffset->SetBinContent(i, beamBucket);
507 selectedTAGHOffset->SetBinContent(i, maxMean);
523 cout <<
"Dumping TAGH results...\n=======================================" << endl;
525 cout <<
"Type\tChannel\tvalueToUse\toldValue\tTotal" << endl;
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;
539 double delta = selectedTAGHOffset->GetBinContent(i);
540 if (useRF) delta *= RF_Period;
544 double ccdb = tagh_tdc_time_offsets[i-1];
545 double offset = ccdb + delta - taghTdcOffsets[i-1];
548 outFile << i <<
" " << offset << endl;
550 if (verbose)
printf(
"TDC\t%i\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, offset);
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;
564 double delta = selectedTAGHOffset->GetBinContent(i);
565 if (useRF) delta *= RF_Period;
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;
575 if (verbose)
printf(
"ADC\t%i\t%.3f\t\t%.3f\t\t%.3f\n", i, delta, ccdb, offset);
579 outFile.open(prefix +
"tagh_base_time.txt");
580 outFile << tagh_t_base_fadc - c1_adcOffset <<
" " << tagh_t_base_tdc - c1_tdcOffset << endl;
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);
589 double meanSCOffset = 0.0;
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);
597 double scAdcOffsets[30] = { 0. };
598 double scTdcOffsets[30] = { 0. };
601 for (
int sector = 1; sector <= 30; sector++){
603 TH1D *projY = scADCTDCHist->ProjectionY(
"temp", sector, sector);
604 float nsPerBin = (projY->GetBinCenter(projY->GetNbinsX()) - projY->GetBinCenter(1)) / projY->GetNbinsX();
605 float timeWindow = 2;
606 int binWindow = int(timeWindow / nsPerBin);
607 double maxEntries = 0;
609 for (
int j = 1 ; j <= projY->GetNbinsX();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);
617 if (nEntries > maxEntries) {
618 maxMean = sum / nEntries;
619 maxEntries = nEntries;
626 if(fabs(maxMean) > 1.5) {
627 if(fabs(maxMean) > 28.)
628 scTdcOffsets[sector-1] = maxMean;
630 scAdcOffsets[sector-1] = maxMean;
634 if(!DONT_SHIFT_SC_RF) {
636 if (scRFHist == NULL)
continue;
638 TFitResultPtr fr = scRFHist->Fit(
"pol0",
"SQ",
"", -2, 2);
639 double p0 = fr->Parameter(0);
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);
648 fr = scRFHist->Fit(f,
"SQ",
"", -2, 2);
649 double SCOffset = fr->Parameter(2);
650 selectedSCSectorOffset->SetBinContent(sector, SCOffset);
651 selectedSCSectorOffsetDistribution->Fill(SCOffset);
656 meanSCOffset = selectedSCSectorOffsetDistribution->GetMean();
658 if(FIX_START_COUNTER) {
660 double scMax = scHitTimeHist->GetBinCenter(scHitTimeHist->GetMaximumBin());
661 TFitResultPtr fr = scHitTimeHist->Fit(
"gaus",
"S",
"", scMax - 5., scMax + 5.);
663 meanSCOffset = fr->Parameter(0) - meanSCOffset - start_counter_t0;
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;
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);
679 cout <<
"ADC Offsets" << endl;
680 cout <<
"Sector\tvalueToUse\toldValue\tmeanOffset\tTotal" << endl;
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);
690 outFile.open(prefix +
"sc_base_time.txt");
691 outFile << sc_t_base_fadc - meanSCOffset <<
" " << sc_t_base_tdc - meanSCOffset << endl;
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);
700 if(this1DHist != NULL){
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");
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);
710 outFile << tof_t_base_fadc - mean - meanSCOffset<<
" " << tof_t_base_tdc - mean - meanSCOffset<< endl;
714 if(USE_NEUTRALS_FOR_BCAL_RF_ALIGN)
718 if(this1DHist != NULL){
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");
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);
728 outFile << bcal_t_base_fadc - mean - meanSCOffset <<
" " << bcal_t_base_tdc - mean - meanSCOffset << endl;
732 if(USE_NEUTRALS_FOR_FCAL_RF_ALIGN)
736 if(this1DHist != NULL){
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");
743 printf(
"FCAL ADC Base = %f - (%f) - (%f) = %f\n",fcal_t_base, mean, meanSCOffset, fcal_t_base - mean - meanSCOffset);
745 outFile << fcal_t_base - mean - meanSCOffset<< endl;
750 if(this1DHist != NULL){
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");
757 printf(
"CDC ADC Base = %f - (%f) - (%f) = %f\n",cdc_t_base, mean, meanSCOffset, cdc_t_base - mean - meanSCOffset);
759 outFile << cdc_t_base - mean - meanSCOffset << endl;
764 double FDC_ADC_Offset = 0.0, FDC_TDC_Offset = 0.0;
766 if(this1DHist != NULL){
767 Int_t firstBin = this1DHist->FindFirstBinAbove( 1 , 1);
769 for (
int i = 0; i <= 46; i++){
770 if ((firstBin + i) > 0) this1DHist->SetBinContent((firstBin + i), 0);
773 Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
779 TF1 *
f =
new TF1(
"f",
"gaus");
780 f->SetParameters(100, maximum, 20);
782 TFitResultPtr fr = this1DHist->Fit(f,
"S",
"", maximum - 17, maximum + 7);
783 double mean = fr->Parameter(1);
784 float sigma = fr->Parameter(2);
785 FDC_ADC_Offset = mean;
790 if(this1DHist != NULL){
791 Int_t firstBin = this1DHist->FindLastBinAbove( 1 , 1);
792 for (
int i = 0; i <= 25; i++){
793 if ((firstBin + i) > 0) this1DHist->SetBinContent((firstBin + i), 0);
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);
800 double mean = fr->Parameter(1);
801 float sigma = fr->Parameter(2);
802 FDC_TDC_Offset = mean;
805 double FDC_ADC_TDC_Offset = FDC_ADC_Offset - FDC_TDC_Offset;
809 if(this1DHist != NULL){
811 Double_t maximum = this1DHist->GetBinCenter(this1DHist->GetMaximumBin());
812 TFitResultPtr fr = this1DHist->Fit(
"landau",
"S",
"", maximum - 3.5, maximum + 6);
815 MPV = fr->Parameter(1);
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++) {
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;
839 Double_t maximum = projY->GetBinCenter(projY->GetMaximumBin());
842 TF1 *
f =
new TF1(
"f",
"gaus");
844 if(maximum > -190.) {
845 mean1 = maximum - MPV;
849 TF1 fg(
"fg",
"gaus");
850 fg.SetParameters(100, maximum, 20);
851 TFitResultPtr fr = projY->Fit(&fg,
"S",
"", maximum - 10, maximum + 8);
852 mean1 = fr->Parameter(1) - MPV;
853 sigma1 = fr->Parameter(2);
860 sprintf(buf,
"temp_fdc_%d", 2*plane);
861 projY = thisHist->ProjectionY(buf, 2*plane, 2*plane);
868 maximum = projY->GetBinCenter(projY->GetMaximumBin());
872 if(maximum > -190.) {
873 mean2 = maximum - MPV;
877 TF1 fg2(
"fg2",
"gaus");
878 fg2.SetParameters(100, maximum, 20);
879 TFitResultPtr fr2 = projY->Fit(&fg2,
"S",
"", maximum - 10, maximum + 8);
880 mean2 = fr2->Parameter(1) - MPV;
881 sigma2 = fr2->Parameter(2);
890 package_times_shifted[package-1] =
true;
893 for(
int wire=0; wire<48; wire++) {
896 FDC_wire_offsets[package-1][chamber-1][wire] = mean1 + old_FDC_wire_offsets[package-1][chamber-1][wire];
902 for(
int wire=48; wire<96; wire++) {
905 FDC_wire_offsets[package-1][chamber-1][wire] = mean2 + old_FDC_wire_offsets[package-1][chamber-1][wire];
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];
917 avg_FDC_TDC_wire_offsets /= 48.;
920 for(
int package = 0; package<4; package++) {
921 if(package_times_shifted[package]) {
923 sprintf(buf,
"%sfdc_package%d_wire_offsets.txt", prefix.Data(), package+1);
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. <<
" ";
937 outFile.open(prefix +
"fdc_base_time.txt");
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);
944 outFile << fdc_t_base_fadc - MPV - meanSCOffset - FDC_ADC_TDC_Offset <<
" " << fdc_t_base_tdc - avg_FDC_TDC_wire_offsets - meanSCOffset << endl;
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;
int GetF1TDCslotTAGH(int id)
sprintf(text,"Post KinFit Cut")
void AdjustTiming(TString fileName="hd_root.root", int runNumber=10390, TString variation="default", bool verbose=false, TString prefix="")
static char index(char c)
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]
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)