29 ifstream MapFile(
"/w/halld-scifs17exp/home/gleasonc/Software/halld_recon/src/plugins/Calibration/FCALLEDTree/CrateOrder.txt");
43 int TrackPositions = 1;
51 int xyCrates[59][59] = {0};
52 int xySlots[59][59] = {0};
55 int ChannelCrates[2800] = {0};
56 int ChannelSlots[2800] = {0};
59 while ( !MapFile.eof() )
62 if ( (TrackLine + 2)%3 == 0)
69 stringstream stream(Crates);
79 if ( stream.peek() ==
'/' )
87 for (i = 0; i < vect1.size(); i++)
101 if ( (i+1) % 3 == 0 )
112 if ( (TrackLine + 1) % 3 == 0 )
120 if ( (TrackLine) % 3 == 0 )
122 MapFile >> Positions;
128 stringstream stream(Positions);
134 if ( stream.peek() ==
'/' )
140 for (n = 0; n < vect.size(); n++)
152 xyCrates[xx+29][yy+29] = crate;
153 xySlots[xx+29][yy+29] = slot;
164 cout <<
"Please enter path to a text file containing list of run paths (run path example: /path/to/run/Run04000/led_Run04000.root): ";
167 string UserOutputDirectory;
168 cout <<
"Please enter path to the directory for output: ";
169 cin >> UserOutputDirectory;
171 ifstream RunList(UserRunList);
176 int chan[7000] = {0};
186 int chanPosition[59][59] = {0};
187 double chanXPos[2800] = {0};
188 double chanYPos[2800] = {0};
191 while ( !RunList.eof() )
200 cout <<
"There are " << HowManyRuns <<
" in selected file" << endl;
203 double GoodTimesArray[2800] = {0};
204 double GoodCratesArray[12] = {0};
205 double GoodSlotsArray[13][18] = {0};
209 double ChanTimeAvg[HowManyRuns][2800];
210 double CrateAvg[HowManyRuns][12];
211 double SlotAvg[HowManyRuns][13][18];
213 memset( ChanTimeAvg, 0,
sizeof(
int)*HowManyRuns*2800 );
214 memset( CrateAvg, 0,
sizeof(
int)*HowManyRuns*12 );
215 memset( SlotAvg, 0,
sizeof(
int)*HowManyRuns*13*18 );
218 ifstream RunList2(UserRunList);
221 for (
int iRun = 0; iRun < HowManyRuns; ++iRun)
224 TString fcalFileNamer( line );
226 TFile *fcalFile =
new TFile (fcalFileNamer);
227 cout <<
"Current file: " << fcalFileNamer << endl;
229 TTree *fcalTree = (TTree*)fcalFile->Get(
"fcalBlockHits");
231 fcalTree->SetBranchAddress(
"chan", chan);
232 fcalTree->SetBranchAddress(
"nHits", &nHits);
233 fcalTree->SetBranchAddress(
"x", x);
234 fcalTree->SetBranchAddress(
"y", y);
235 fcalTree->SetBranchAddress(
"t", t);
236 fcalTree->SetBranchAddress(
"run", &run);
237 fcalTree->SetBranchAddress(
"eTot", &eTot);
240 double ChanTimeAdder[2800] = {0};
241 double ChanTimeCounter[2800] = {0};
243 double CrateAdder[12] = {0};
244 double CrateCounter[12] = {0};
246 double SlotAdder[13][18] = {0};
247 double SlotCounter[13][18] = {0};
250 for (
int iEntry = 0; iEntry < fcalTree->GetEntries(); ++iEntry)
252 fcalTree->GetEntry(iEntry);
254 if (iEntry%10000 == 0)
256 cout << iEntry <<
" events processed" << endl;
259 for (
int iHits = 0; iHits < nHits; ++iHits)
262 int row = (int)round((x[iHits] + 116)/4.0157);
263 int column = (int)round((y[iHits] + 116)/4.0157);
265 chanPosition[row][
column] = chan[iHits];
267 chanXPos[ chan[iHits] ] = row;
268 chanYPos[ chan[iHits] ] =
column;
270 if ( eTot > 0 && t[iHits] > 0 )
273 ChanTimeAdder[ chan[iHits] ] += t[iHits];
274 ChanTimeCounter[ chan[iHits] ]++;
276 CrateAdder[ xyCrates[row][
column] ] += t[iHits];
277 CrateCounter[ xyCrates[row][
column] ] ++;
279 SlotAdder[ xyCrates[row][
column] ][ xySlots[row][
column] ] += t[iHits];
280 SlotCounter[ xyCrates[row][
column] ][ xySlots[row][
column] ] ++;
283 ChannelCrates[ chan[iHits] ] = xyCrates[row][
column];
284 ChannelSlots[ chan[iHits] ] = xySlots[row][
column];
291 for (
int iChan = 0; iChan < 2800; ++iChan)
293 if (ChanTimeCounter[iChan] != 0)
295 ChanTimeAvg[iRun][iChan] = ChanTimeAdder[iChan] / ChanTimeCounter[iChan];
300 for (
int iCrate = 0; iCrate < 12; iCrate++)
302 if (CrateCounter[iCrate] != 0)
304 CrateAvg[iRun][iCrate] = CrateAdder[iCrate] / CrateCounter[iCrate];
308 cout <<
"CrateAdder[" << iCrate <<
"] = " << CrateAdder[iCrate] <<
" and CrateCounter[" << iCrate <<
"] = " << CrateCounter[iCrate] << endl;
309 cout <<
"CrateAvg[" << iRun <<
"][" << iCrate <<
"] = " << CrateAvg[iRun][iCrate] << endl;
312 for (
int iSlot = 3; iSlot < 19; iSlot++)
315 if ( SlotCounter[iCrate][iSlot] != 0 )
317 SlotAvg[iRun][iCrate][iSlot] = SlotAdder[iCrate][iSlot] / SlotCounter[iCrate][iSlot];
328 TString GoodTimesFileNamer = UserOutputDirectory;
329 GoodTimesFileNamer +=
"/";
330 GoodTimesFileNamer +=
"GoodTimesData.txt";
332 ofstream GoodTimesFile;
333 GoodTimesFile.open(GoodTimesFileNamer);
336 TH1* channelTimes =
new TH1F(
"channelTimes",
"Chan times", 2000, 0, 100);
337 TH1* crateTimes =
new TH1F(
"crateTimes",
"crate times", 2000, 0, 100);
338 TH1* slotTimes =
new TH1F(
"slotTimes",
"slot times", 2000, 0, 100);
340 GoodTimesFile <<
"Crate GoodTime" << endl;
344 for (
int iCrates = 0; iCrates < 12; ++iCrates)
347 for (
int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
350 if ( CrateAvg[iRuns][iCrates] != 0 )
352 crateTimes->Fill( CrateAvg[iRuns][iCrates] );
358 double maxCrates = crateTimes->GetMaximumBin();
361 GoodCratesArray[iCrates] = ( maxCrates / 20 );
364 cout <<
"GoodCratesArray[" << iCrates <<
"] = " << GoodCratesArray[iCrates] <<
"\n" << endl;
366 GoodTimesFile << ( iCrates + 1 ) <<
" " << GoodCratesArray[iCrates] << endl;
371 GoodTimesFile <<
"\n\n\n" <<
"Crate/Slot GoodTime" << endl;
375 for (
int iCrater = 0; iCrater < 12; ++iCrater)
377 for (
int iSlots = 3; iSlots < 19; ++iSlots)
379 for (
int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
382 if (SlotAvg[iRuns][iCrater][iSlots] != 0)
384 slotTimes->Fill( SlotAvg[iRuns][iCrater][iSlots] );
390 double maxSlots = slotTimes->GetMaximumBin();
393 GoodSlotsArray[iCrater][iSlots] = ( maxSlots / 20 );
395 cout <<
"GoodSlotsArray[" << iCrater <<
"][" << iSlots <<
"] = " << GoodSlotsArray[iCrater][iSlots] << endl;
397 GoodTimesFile << ( iCrater + 1 ) <<
"/" << iSlots <<
" " << GoodSlotsArray[iCrater][iSlots] << endl;
404 GoodTimesFile <<
"\n\n\n" <<
"Chan GoodTimes" << endl;
407 for (
int iChans = 0; iChans < 2800; ++iChans)
410 for (
int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
413 if (ChanTimeAvg[iRuns][iChans] != 0)
415 channelTimes->Fill( ChanTimeAvg[iRuns][iChans] );
421 double max = channelTimes->GetMaximumBin();
424 GoodTimesArray[iChans] = ( max / 20 );
426 GoodTimesFile << ( iChans + 1 ) <<
" " << GoodTimesArray[iChans] << endl;
428 channelTimes->Reset();
432 cout <<
"\n" <<
"Testing, after good slots loop, GoodCratesArray[0] = " << GoodCratesArray[0] <<
"\n" << endl;
439 ifstream RunList3(UserRunList);
441 for (
int iRun = 0; iRun < HowManyRuns; ++iRun)
445 TString fcalFileNamer2( line );
447 TFile *fcalFile2 =
new TFile (fcalFileNamer2);
448 cout <<
"Current file: " << fcalFileNamer2 << endl;
450 TTree *fcalTree2 = (TTree*)fcalFile2->Get(
"fcalBlockHits");
457 memset( chan, 0,
sizeof(
int)*7000 );
458 memset( x, 0,
sizeof(
int)*7000 );
459 memset( y, 0,
sizeof(
int)*7000 );
460 memset( t, 0,
sizeof(
int)*7000 );
462 fcalTree2->SetBranchAddress(
"chan", chan);
463 fcalTree2->SetBranchAddress(
"nHits", &nHits);
464 fcalTree2->SetBranchAddress(
"x", x);
465 fcalTree2->SetBranchAddress(
"y", y);
466 fcalTree2->SetBranchAddress(
"t", t);
467 fcalTree2->SetBranchAddress(
"run", &run);
468 fcalTree2->SetBranchAddress(
"eTot", &eTot);
474 double RunChannelTimeAdder[2800] = {0};
475 double RunChannelTimeCounter[2800] = {0};
476 double RunChannelTimeAvg[2800] = {0};
478 double RunCrateTimeAdder[12] = {0};
479 double RunCrateTimeCounter[12] = {0};
480 double RunCrateTimeAvg[12] = {0};
482 double RunSlotTimeAdder[13][18] = {0};
483 double RunSlotTimeCounter[13][18] = {0};
484 double RunSlotTimeAvg[13][18] = {0};
489 for (
int iEntry = 0; iEntry < fcalTree2->GetEntries(); ++iEntry)
491 fcalTree2->GetEntry(iEntry);
496 if (iEntry%10000 == 0) cout << iEntry <<
" events processed" << endl;
498 for (
int iHits = 0; iHits < nHits; ++iHits)
501 int row = (int)round((x[iHits] + 116)/4.0157);
502 int column = (int)round((y[iHits] + 116)/4.0157);
505 chanPosition[row][
column] = chan[iHits];
508 if ( eTot > 0 && t[iHits] > 0 )
511 RunChannelTimeAdder[chan[iHits]] += t[iHits];
512 RunChannelTimeCounter[chan[iHits]]++;
514 RunCrateTimeAdder[ xyCrates[row][
column] ] += t[iHits];
515 RunCrateTimeCounter[ xyCrates[row][
column] ] ++;
517 RunSlotTimeAdder[ xyCrates[row][
column] ][ xySlots[row][
column] ] += t[iHits];
518 RunSlotTimeCounter[ xyCrates[row][
column] ][ xySlots[row][
column] ] ++;
527 cout <<
"\n\n" <<
"At end of section 3, GoodCratesArray[0] = " << GoodCratesArray[0] <<
"\n\n" << endl;
535 TString OutputFileNamer = UserOutputDirectory;
536 OutputFileNamer +=
"/";
537 OutputFileNamer += RunHold;
538 OutputFileNamer +=
"_Timing_Offsets.txt";
540 ofstream OffsetOutputFile;
541 OffsetOutputFile.open(OutputFileNamer);
543 cout <<
"OutputFileNamer is " << OutputFileNamer << endl;
545 TString OutputChanCCDB = UserOutputDirectory;
546 OutputChanCCDB +=
"/";
547 OutputChanCCDB += RunHold;
548 OutputChanCCDB +=
"_Channel_CCDB_Offsets.txt";
550 ofstream OutputChanCCDBFile;
551 OutputChanCCDBFile.open(OutputChanCCDB);
553 cout <<
"OutputChanCCDB is " << OutputChanCCDB << endl;
556 double RunChannelOffsets[2800] = {0};
557 double RunCrateOffsets[12] = {0};
558 double RunSlotOffsets[13][18] = {0};
561 int ChanOffsetCorrections[2800] = {0};
562 int CrateOffsetCorrections[12] = {0};
563 int SlotOffsetCorrections[13][18] = {0};
566 OffsetOutputFile <<
"Crate Offset" << endl;
569 for (
int iCrate = 0; iCrate < 12; iCrate++)
572 if (RunCrateTimeCounter[iCrate] != 0)
574 RunCrateTimeAvg[iCrate] = RunCrateTimeAdder[iCrate] / RunCrateTimeCounter[iCrate];
578 RunCrateOffsets[iCrate] = RunCrateTimeAvg[iCrate] - GoodCratesArray[iCrate];
581 cout <<
"RunCrateTimeAdder[" << iCrate <<
"] = " << RunCrateTimeAdder[iCrate] <<
" and RunCrateTimeCounter[" << iCrate <<
"] = " << RunCrateTimeCounter[iCrate] << endl;
582 cout <<
"RunCrateTimeAvg[" << iCrate <<
"] = " << RunCrateTimeAvg[iCrate] <<
" and GoodCratesArray[" << iCrate <<
"] = " << GoodCratesArray[iCrate] << endl;
583 cout <<
"RunCrateOffsets[" << iCrate <<
"] = " << RunCrateOffsets[iCrate] << endl;
585 cout <<
"\n" << endl;
588 if (RunCrateOffsets[iCrate] > 3)
590 CrateOffsetCorrections[iCrate] = +4;
592 else if (RunCrateOffsets[iCrate] < -3)
594 CrateOffsetCorrections[iCrate] = -4;
598 CrateOffsetCorrections[iCrate] = 0;
601 OffsetOutputFile << ( iCrate + 1 ) <<
" " << CrateOffsetCorrections[iCrate] << endl;
605 OffsetOutputFile <<
"\n\n\n" <<
"Crate/Slot Offset" << endl;
606 for (
int iCrate = 0; iCrate < 12; ++iCrate)
609 for (
int iSlot = 3; iSlot < 19; iSlot++)
612 if (RunSlotTimeCounter[iCrate][iSlot] != 0)
614 RunSlotTimeAvg[iCrate][iSlot] = RunSlotTimeAdder[iCrate][iSlot] / RunSlotTimeCounter[iCrate][iSlot];
617 RunSlotOffsets[iCrate][iSlot] = RunSlotTimeAvg[iCrate][iSlot] - GoodSlotsArray[iCrate][iSlot];
621 if (iCrate % 1 == 0 && iSlot % 111111 == 0)
623 cout <<
"RunSlotTimeAdder[" << iCrate <<
"][" << iSlot <<
"] = " << RunSlotTimeAdder[iCrate][iSlot] <<
" and RunSlotTimeCounter[" << iCrate <<
"][" << iSlot <<
" = " << RunSlotTimeCounter[iCrate][iSlot] << endl;
624 cout <<
"RunSlotTimeAvg[" << iCrate <<
"][" << iSlot <<
"] = " << RunSlotTimeAvg[iCrate][iSlot] <<
" and GoodSlotsArray[" << iCrate <<
"][" << iSlot <<
"] = " << GoodSlotsArray[iCrate][iSlot] << endl;
625 cout <<
"RunSlotOffsets[" << iCrate <<
"][" << iSlot <<
"] = " << RunSlotOffsets[iCrate][iSlot] << endl;
626 cout <<
"\n\n" << endl;
630 if (RunSlotOffsets[iCrate][iSlot] > 3)
632 SlotOffsetCorrections[iCrate][iSlot] = +4;
634 else if (RunSlotOffsets[iCrate][iSlot] < -3)
636 SlotOffsetCorrections[iCrate][iSlot] = -4;
640 SlotOffsetCorrections[iCrate][iSlot] = 0;
644 OffsetOutputFile << ( iCrate + 1 ) <<
"/" << iSlot <<
" " << SlotOffsetCorrections[iCrate][iSlot] << endl;
649 OffsetOutputFile <<
"\n\n\n" <<
"Chan Offset" << endl;
652 for (
int iChannel = 0; iChannel < 2800; iChannel++)
655 if (RunChannelTimeCounter[iChannel] != 0)
657 RunChannelTimeAvg[iChannel] = RunChannelTimeAdder[iChannel] / RunChannelTimeCounter[iChannel];
660 RunChannelOffsets[iChannel] = RunChannelTimeAvg[iChannel] - GoodTimesArray[iChannel];
663 if (RunChannelOffsets[iChannel] > 3)
665 ChanOffsetCorrections[iChannel] = +4;
667 else if (RunChannelOffsets[iChannel] < -3)
669 ChanOffsetCorrections[iChannel] = -4;
673 ChanOffsetCorrections[iChannel] = 0;
677 if ( SlotOffsetCorrections[ ChannelCrates[iChannel] ][ ChannelSlots[iChannel] ] != 0 )
683 for (
int iChannel = 0; iChannel < 2800; iChannel++)
686 if ( ChannelCrates[iChannel] == 6 && ChannelSlots[iChannel] == 4 )
688 cout <<
"RunChannelTimeAvg[" << iChannel <<
"] = " << RunChannelTimeAvg[iChannel] <<
" and RunChannelTimeAvg[" << chanPosition[ (int)round( chanXPos[iChannel] + 3 ) ][ (int)round(chanYPos[iChannel]) ] <<
"] = " << RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] + 3 ) ][ (int)round(chanYPos[iChannel]) ] ] << endl;
690 cout <<
"RunChannelTimeAvg[" << iChannel <<
"] = " << RunChannelTimeAvg[iChannel] <<
" and RunChannelTimeAvg[" << chanPosition[ (int)round( chanXPos[iChannel]) ][ (int)round(chanYPos[iChannel] - 3 ) ] <<
"] = " << RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] ) ][ (int)round(chanYPos[iChannel] - 3 ) ] ] << endl;
692 cout <<
"Difference between bad slot channel and neighboring slot channel, right: " << RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] + 3 ) ][ (int)round(chanYPos[iChannel]) ] ] <<
" and below: " << RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] ) ][ (int)round( chanYPos[iChannel] - 3 ) ] ] << endl;
694 cout <<
"Chan X and Y pos: " << chanXPos[iChannel] <<
" & " << chanYPos[iChannel] << endl;
695 cout <<
"Neighbor positions, right: " << chanXPos[iChannel] + 3 <<
", " << chanYPos[iChannel] <<
" & below: " << chanXPos[iChannel] <<
", " << chanYPos[iChannel] - 3 << endl;
698 if ( ( RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (
int)round( chanXPos[iChannel] + 3 ) ][ (int)round(chanYPos[iChannel]) ] ] ) < -2.5 )
700 ChanOffsetCorrections[iChannel] = -4;
704 if ( ( RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (
int)round( chanXPos[iChannel] ) ][ (int)round( chanYPos[iChannel] - 3 ) ] ] ) < -2.5 )
706 ChanOffsetCorrections[iChannel] = -4;
711 OffsetOutputFile << ( iChannel + 1 ) <<
" " << ChanOffsetCorrections[iChannel] << endl;
712 OutputChanCCDBFile << ChanOffsetCorrections[iChannel] << endl;
716 OffsetOutputFile.close();
717 OutputChanCCDBFile.close();
static evioFileChannel * chan