Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OffsetCorrections.C
Go to the documentation of this file.
1 #include "TString.h"
2 #include "TTree.h"
3 
4 #include <limits>
5 #include <iostream>
6 #include <fstream>
7 #include <sstream>
8 #include <vector>
9 
10 using namespace std;
11 
12 
13 // *******************************************************************************************
14 // *** Script to find +/-4 ns timing offsets in FCAL ***
15 // *** Script goes through a user-provided list of runs, and searches for 4 ns timing offsets for the FCAL ***
16 // *** Script creates a text file for each run, stating which crates, slots and channels are offset and in which direction ***
17 // *******************************************************************************************
18 
19 
21 {
22 
23 
24  // ****************************************************************************************
25  // *** SECTION 1: CRATE SORTING ***
26  // ****************************************************************************************
27 
28  // *** MapFile contains info on which chan/position is in which crate ***
29  ifstream MapFile("/w/halld-scifs17exp/home/gleasonc/Software/halld_recon/src/plugins/Calibration/FCALLEDTree/CrateOrder.txt");
30  string line;
31 
32  // *** Variables to hold values from MapFile ***
33  int xx = 0;
34  int yy = 0;
35  int crate= 0;
36  int slot = 0;
37 
38  // *** Variables used for keeping track of data in MapFile ***
39  int TrackLine = 1;
40 
41  int TrackCrate = 1;
42  int TrackChan = 1;
43  int TrackPositions = 1;
44 
45  string Crates;
46  string Chans;
47  string Positions;
48 
49 
50  // *** Holds value of crate/slot number for each x,y position ***
51  int xyCrates[59][59] = {0};
52  int xySlots[59][59] = {0};
53 
54  // *** Hold crate and slot numbers for each channel ***
55  int ChannelCrates[2800] = {0};
56  int ChannelSlots[2800] = {0};
57 
58  // *** Go through MapFile, find x,y positions and their corresponding crates ***
59  while ( !MapFile.eof() )
60  {
61  // *** If statement selects crate/slot/chan section of line in MapFile ***
62  if ( (TrackLine + 2)%3 == 0)
63  {
64 
65  MapFile >> Crates;
66  TrackCrate++;
67  //cout << "Crates: " << Crates << endl;
68 
69  stringstream stream(Crates);
70  int i;
71 
72  vector<int> vect1;
73 
74 
75  while ( stream >> i)
76  {
77  vect1.push_back(i);
78 
79  if ( stream.peek() == '/' )
80  {
81  stream.ignore();
82  }
83 
84  } // *** end while i ***
85 
86 
87  for (i = 0; i < vect1.size(); i++)
88  {
89 
90  if ( (i+3) % 3 == 0 )
91  {
92  //cout << "Crates = " << vect1.at(i) << endl;
93  crate = vect1.at(i);
94 
95  }
96  if ( (i+2) % 3 == 0 )
97  {
98  slot = vect1.at(i);
99  //cout << "slot = " << vect1.at(i) << endl;
100  }
101  if ( (i+1) % 3 == 0 )
102  {
103  //cout << "'chan' = " << vect1.at(i) << endl;
104  // *** This loop does nothing but cycle through the unused string section ***
105  }
106 
107  } // *** end for i ***
108 
109  } // *** end if TrackLine + 2 ***
110 
111  // *** selects channel_no section in MapFile ***
112  if ( (TrackLine + 1) % 3 == 0 )
113  {
114  MapFile >> Chans;
115  TrackChan++;
116  //cout << "Chans: " << Chans << endl;
117  } // *** End if trackLine + 1 ***
118 
119  // *** Selects x/y section of MapFile ***
120  if ( (TrackLine) % 3 == 0 )
121  {
122  MapFile >> Positions;
123  TrackPositions++;
124  //cout << "Positions: " << Positions << endl;
125 
126  vector<int> vect;
127 
128  stringstream stream(Positions);
129  int n;
130 
131  while( stream >> n )
132  {
133  vect.push_back(n);
134  if ( stream.peek() == '/' )
135  {
136  stream.ignore();
137  }
138  } // *** end while stream >> n ***
139 
140  for (n = 0; n < vect.size(); n++)
141  {
142  if (n%2 == 0)
143  {
144  xx = vect.at(n);
145  }
146  else
147  {
148  yy = vect.at(n);
149  }
150  } // *** end for n ***
151 
152  xyCrates[xx+29][yy+29] = crate;
153  xySlots[xx+29][yy+29] = slot;
154  } // *** end if trackline + 0 ***
155  TrackLine++;
156  } // *** End of for MapFile while ***
157 
158  // **************************************************************************
159  // *** SECTION 2: CONSTRUCTING REFERENCE TIMES ARRAY ***
160  // **************************************************************************
161 
162  // *** Change this to prompt for txt file ***
163  string UserRunList;
164  cout << "Please enter path to a text file containing list of run paths (run path example: /path/to/run/Run04000/led_Run04000.root): ";
165  cin >> UserRunList;
166 
167  string UserOutputDirectory;
168  cout << "Please enter path to the directory for output: ";
169  cin >> UserOutputDirectory;
170 
171  ifstream RunList(UserRunList);
172  string line2;
173 
174  // *** Declare variables for values in fcalBlockHits tree ***
175  int nHits = 0;
176  int chan[7000] = {0};
177  float x[7000] = {0};
178  float y[7000] = {0};
179  float t[7000] = {0};
180  int run = 0;
181  float eTot = 0;
182 
183  int HowManyRuns = 0;
184 
185  // *** Save channel locations, convenient later ***
186  int chanPosition[59][59] = {0};
187  double chanXPos[2800] = {0};
188  double chanYPos[2800] = {0};
189 
190  // *** Count how many runs are listed in the file selected
191  while ( !RunList.eof() )
192  {
193  RunList >> line2;
194  ++HowManyRuns;
195  }
196 
197  // *** So we can start from top of file again later ***
198  RunList.close();
199 
200  cout << "There are " << HowManyRuns << " in selected file" << endl;
201 
202  // *** Array to hold 'good time' values ***
203  double GoodTimesArray[2800] = {0};
204  double GoodCratesArray[12] = {0};
205  double GoodSlotsArray[13][18] = {0};
206 
207 
208  // *** For calculating ***
209  double ChanTimeAvg[HowManyRuns][2800];
210  double CrateAvg[HowManyRuns][12];
211  double SlotAvg[HowManyRuns][13][18];
212 
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 );
216 
217  // *** Same file as before ***
218  ifstream RunList2(UserRunList);
219 
220  // *** Run over all files in user selected txt file, find good time value for each channel and save in array GoodTimesArray ***
221  for (int iRun = 0; iRun < HowManyRuns; ++iRun)
222  {
223  RunList2 >> line;
224  TString fcalFileNamer( line );
225 
226  TFile *fcalFile = new TFile (fcalFileNamer);
227  cout << "Current file: " << fcalFileNamer << endl;
228 
229  TTree *fcalTree = (TTree*)fcalFile->Get("fcalBlockHits");
230 
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);
238 
239  // *** Calculation variables ***
240  double ChanTimeAdder[2800] = {0};
241  double ChanTimeCounter[2800] = {0};
242 
243  double CrateAdder[12] = {0};
244  double CrateCounter[12] = {0};
245 
246  double SlotAdder[13][18] = {0};
247  double SlotCounter[13][18] = {0};
248 
249  // *** For loop goes through, adds up times for use in average calculations ***
250  for (int iEntry = 0; iEntry < fcalTree->GetEntries(); ++iEntry)
251  {
252  fcalTree->GetEntry(iEntry);
253 
254  if (iEntry%10000 == 0)
255  {
256  cout << iEntry << " events processed" << endl;
257  }
258 
259  for (int iHits = 0; iHits < nHits; ++iHits)
260  {
261 
262  int row = (int)round((x[iHits] + 116)/4.0157);
263  int column = (int)round((y[iHits] + 116)/4.0157);
264 
265  chanPosition[row][column] = chan[iHits];
266 
267  chanXPos[ chan[iHits] ] = row;
268  chanYPos[ chan[iHits] ] = column;
269 
270  if ( eTot > 0 && t[iHits] > 0 )
271  {
272 
273  ChanTimeAdder[ chan[iHits] ] += t[iHits];
274  ChanTimeCounter[ chan[iHits] ]++;
275 
276  CrateAdder[ xyCrates[row][column] ] += t[iHits];
277  CrateCounter[ xyCrates[row][column] ] ++;
278 
279  SlotAdder[ xyCrates[row][column] ][ xySlots[row][column] ] += t[iHits];
280  SlotCounter[ xyCrates[row][column] ][ xySlots[row][column] ] ++;
281 
282  // *** holding values for crate and slot for each channel for later ***
283  ChannelCrates[ chan[iHits] ] = xyCrates[row][column];
284  ChannelSlots[ chan[iHits] ] = xySlots[row][column];
285  }
286  } // *** End of iHits for loop ***
287  } // *** End of iEntry for loop ***
288 
289  // *** Calculating averages ***
290 
291  for (int iChan = 0; iChan < 2800; ++iChan)
292  {
293  if (ChanTimeCounter[iChan] != 0)
294  {
295  ChanTimeAvg[iRun][iChan] = ChanTimeAdder[iChan] / ChanTimeCounter[iChan];
296 
297  } // ** End if ChanTimeCounter **
298  }
299 
300  for (int iCrate = 0; iCrate < 12; iCrate++)
301  {
302  if (CrateCounter[iCrate] != 0)
303  {
304  CrateAvg[iRun][iCrate] = CrateAdder[iCrate] / CrateCounter[iCrate];
305  }
306 
307  // *** Testing purposes ***
308  cout << "CrateAdder[" << iCrate << "] = " << CrateAdder[iCrate] << " and CrateCounter[" << iCrate << "] = " << CrateCounter[iCrate] << endl;
309  cout << "CrateAvg[" << iRun << "][" << iCrate << "] = " << CrateAvg[iRun][iCrate] << endl;
310 
311 
312  for (int iSlot = 3; iSlot < 19; iSlot++)
313  {
314 
315  if ( SlotCounter[iCrate][iSlot] != 0 )
316  {
317  SlotAvg[iRun][iCrate][iSlot] = SlotAdder[iCrate][iSlot] / SlotCounter[iCrate][iSlot];
318  }
319  //cout << "SlotAdder[" << iCrate << "][" << iSlot << "] = " << SlotAdder[iCrate][iSlot] << " and SlotCounter[" << iCrate << "][" << iSlot << "] = " << SlotCounter[iCrate][iSlot] << endl;
320  //cout << "SlotAvg[" << iRun << "][" << iCrate << "][" << iSlot << "] = " << SlotAvg[iRun][iCrate][iSlot] << "\n" << endl;
321  }
322  } // *** End for (iCrate) ***
323  } // *** End iRun ***
324 
325  RunList2.close(); // *** Done with text file for now ***
326 
327  // *** File to hold good times for slots and such ***
328  TString GoodTimesFileNamer = UserOutputDirectory;
329  GoodTimesFileNamer += "/";
330  GoodTimesFileNamer += "GoodTimesData.txt";
331 
332  ofstream GoodTimesFile;
333  GoodTimesFile.open(GoodTimesFileNamer);
334 
335  // *** Histograms will be used several times, once for each channel/crate/slot. Histograms are filled with time values and most common time value is selected as 'good time' ***
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);
339 
340  GoodTimesFile << "Crate GoodTime" << endl;
341 
342  // *** Find good times for crates ***
343 
344  for (int iCrates = 0; iCrates < 12; ++iCrates)
345  {
346 
347  for (int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
348  {
349 
350  if ( CrateAvg[iRuns][iCrates] != 0 )
351  {
352  crateTimes->Fill( CrateAvg[iRuns][iCrates] );
353  }
354 
355  } //*** end iRuns ***
356 
357  // *** Selecting the most occuring time as the 'good time' for a crate ***
358  double maxCrates = crateTimes->GetMaximumBin();
359 
360  // *** The 1/5 factor is because we have bins in the crateTimes histogram such that every .2 ns is a bin, so 5 bins per ns ***
361  GoodCratesArray[iCrates] = ( maxCrates / 20 );
362 
363  // *** Testing purposes ***
364  cout << "GoodCratesArray[" << iCrates << "] = " << GoodCratesArray[iCrates] << "\n" << endl;
365 
366  GoodTimesFile << ( iCrates + 1 ) << " " << GoodCratesArray[iCrates] << endl;
367 
368  crateTimes->Reset(); // *** Clear out histogram so its good for next crate ***
369  } // *** End iCrates ***
370 
371  GoodTimesFile << "\n\n\n" << "Crate/Slot GoodTime" << endl;
372 
373  // *** Find good times for slots ***
374 
375  for (int iCrater = 0; iCrater < 12; ++iCrater)
376  {
377  for (int iSlots = 3; iSlots < 19; ++iSlots)
378  {
379  for (int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
380  {
381 
382  if (SlotAvg[iRuns][iCrater][iSlots] != 0)
383  {
384  slotTimes->Fill( SlotAvg[iRuns][iCrater][iSlots] );
385  }
386 
387  } // *** end iRuns ***
388 
389  // *** Selecting the most occuring time as the 'good time' for a slot ***
390  double maxSlots = slotTimes->GetMaximumBin();
391 
392  // *** The 1/5 factor is because we have bins in the slotTimes histogram such that every .2 ns is a bin, so 5 bins per ns ***
393  GoodSlotsArray[iCrater][iSlots] = ( maxSlots / 20 );
394 
395  cout << "GoodSlotsArray[" << iCrater << "][" << iSlots << "] = " << GoodSlotsArray[iCrater][iSlots] << endl;
396 
397  GoodTimesFile << ( iCrater + 1 ) << "/" << iSlots << " " << GoodSlotsArray[iCrater][iSlots] << endl;
398 
399  slotTimes->Reset(); // *** Clear out histogram so its good for next slot ***
400  } // *** End iSlots ***
401 
402  } // *** End iCrates (for iSlots good time) ***
403 
404  GoodTimesFile << "\n\n\n" << "Chan GoodTimes" << endl;
405 
406  // *** Find good times for channels ***
407  for (int iChans = 0; iChans < 2800; ++iChans)
408  {
409 
410  for (int iRuns = 0; iRuns < HowManyRuns; ++iRuns)
411  {
412 
413  if (ChanTimeAvg[iRuns][iChans] != 0)
414  {
415  channelTimes->Fill( ChanTimeAvg[iRuns][iChans] );
416  }
417 
418  } //*** end iRuns ***
419 
420  // *** Selecting the most occuring time as the 'good time' for a channel ***
421  double max = channelTimes->GetMaximumBin();
422 
423  // *** The 1/5 factor is because we have bins in the channelTimes histogram such that every .2 ns is a bin, so 5 bins per ns ***
424  GoodTimesArray[iChans] = ( max / 20 );
425 
426  GoodTimesFile << ( iChans + 1 ) << " " << GoodTimesArray[iChans] << endl;
427 
428  channelTimes->Reset(); // *** Clear out histogram so its good for next channel ***
429  } // *** End iChans ***
430 
431  // *** For testing ***
432  cout << "\n" << "Testing, after good slots loop, GoodCratesArray[0] = " << GoodCratesArray[0] << "\n" << endl;
433 
434  // ****************************************************************************************
435  // *** SECTION 3: COMPARING EACH RUN TO THE REFERENCE, FINDING OFFSETS ***
436  // ****************************************************************************************
437 
438  // *** Access text file list of runs again ***
439  ifstream RunList3(UserRunList);
440 
441  for (int iRun = 0; iRun < HowManyRuns; ++iRun)
442  {
443 
444  RunList3 >> line;
445  TString fcalFileNamer2( line );
446 
447  TFile *fcalFile2 = new TFile (fcalFileNamer2);
448  cout << "Current file: " << fcalFileNamer2 << endl;
449 
450  TTree *fcalTree2 = (TTree*)fcalFile2->Get("fcalBlockHits");
451 
452  // *** Reseting variables and declaring trees ***
453  nHits = 0;
454  run = 0;
455  eTot = 0;
456 
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 );
461 
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);
469 
470  // *** RunHold is used for naming the output text files ***
471  int RunHold = 99999;
472 
473  // *** Arrays for run by run calculations ***
474  double RunChannelTimeAdder[2800] = {0};
475  double RunChannelTimeCounter[2800] = {0};
476  double RunChannelTimeAvg[2800] = {0};
477 
478  double RunCrateTimeAdder[12] = {0};
479  double RunCrateTimeCounter[12] = {0};
480  double RunCrateTimeAvg[12] = {0};
481 
482  double RunSlotTimeAdder[13][18] = {0};
483  double RunSlotTimeCounter[13][18] = {0};
484  double RunSlotTimeAvg[13][18] = {0};
485 
486 
487 
488  // *** Loop over entries for current run, ***
489  for (int iEntry = 0; iEntry < fcalTree2->GetEntries(); ++iEntry)
490  {
491  fcalTree2->GetEntry(iEntry);
492 
493  RunHold = run;
494 
495  // *** Outputs every 2000 entries processed ***
496  if (iEntry%10000 == 0) cout << iEntry << " events processed" << endl;
497 
498  for (int iHits = 0; iHits < nHits; ++iHits)
499  {
500 
501  int row = (int)round((x[iHits] + 116)/4.0157);
502  int column = (int)round((y[iHits] + 116)/4.0157);
503 
504  // *** Assigning each position the proper channel number, useful later ***
505  chanPosition[row][column] = chan[iHits];
506 
507  // *** Adding up times for crates, slots and channels ***
508  if ( eTot > 0 && t[iHits] > 0 )
509  {
510 
511  RunChannelTimeAdder[chan[iHits]] += t[iHits];
512  RunChannelTimeCounter[chan[iHits]]++;
513 
514  RunCrateTimeAdder[ xyCrates[row][column] ] += t[iHits];
515  RunCrateTimeCounter[ xyCrates[row][column] ] ++;
516 
517  RunSlotTimeAdder[ xyCrates[row][column] ][ xySlots[row][column] ] += t[iHits];
518  RunSlotTimeCounter[ xyCrates[row][column] ][ xySlots[row][column] ] ++;
519 
520  }
521 
522  } // *** End of iHits for loop ***
523 
524  } // *** End of iEntry ***
525 
526  // *** Testing ***
527  cout << "\n\n" << "At end of section 3, GoodCratesArray[0] = " << GoodCratesArray[0] << "\n\n" << endl;
528 
529  // ****************************************************************
530  // *** SECTION 4: WRITING OUTPUT FILES ***
531  // ****************************************************************
532 
533  // *** Create an output text file ***
534 
535  TString OutputFileNamer = UserOutputDirectory;
536  OutputFileNamer += "/";
537  OutputFileNamer += RunHold;
538  OutputFileNamer += "_Timing_Offsets.txt";
539 
540  ofstream OffsetOutputFile;
541  OffsetOutputFile.open(OutputFileNamer);
542 
543  cout << "OutputFileNamer is " << OutputFileNamer << endl;
544 
545  TString OutputChanCCDB = UserOutputDirectory;
546  OutputChanCCDB += "/";
547  OutputChanCCDB += RunHold;
548  OutputChanCCDB += "_Channel_CCDB_Offsets.txt";
549 
550  ofstream OutputChanCCDBFile;
551  OutputChanCCDBFile.open(OutputChanCCDB);
552 
553  cout << "OutputChanCCDB is " << OutputChanCCDB << endl;
554 
555  // *** Array for difference between current run and good time values ***
556  double RunChannelOffsets[2800] = {0};
557  double RunCrateOffsets[12] = {0};
558  double RunSlotOffsets[13][18] = {0};
559 
560  // *** Stores either +4, -4 or 0 ***
561  int ChanOffsetCorrections[2800] = {0};
562  int CrateOffsetCorrections[12] = {0};
563  int SlotOffsetCorrections[13][18] = {0};
564 
565  // *** Finding Crate Offsets ***
566  OffsetOutputFile << "Crate Offset" << endl;
567 
568  // *** Loop through each crate and calculate the average ***
569  for (int iCrate = 0; iCrate < 12; iCrate++)
570  {
571  // *** Calculate avg time for each crate and how different it is from 'good time' value ***
572  if (RunCrateTimeCounter[iCrate] != 0)
573  {
574  RunCrateTimeAvg[iCrate] = RunCrateTimeAdder[iCrate] / RunCrateTimeCounter[iCrate];
575  }
576 
577  // *** Time average difference for current run vs reference ***
578  RunCrateOffsets[iCrate] = RunCrateTimeAvg[iCrate] - GoodCratesArray[iCrate];
579 
580  // *** Testing purposes ***
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;
584 
585  cout << "\n" << endl;
586 
587  // *** Determine if channel is offset or not, and assign each channel either 0, +4 or -4 offset ***
588  if (RunCrateOffsets[iCrate] > 3)
589  {
590  CrateOffsetCorrections[iCrate] = +4;
591  }
592  else if (RunCrateOffsets[iCrate] < -3)
593  {
594  CrateOffsetCorrections[iCrate] = -4;
595  }
596  else
597  {
598  CrateOffsetCorrections[iCrate] = 0;
599  }
600  // *** Fill the output file with correction values ***
601  OffsetOutputFile << ( iCrate + 1 ) << " " << CrateOffsetCorrections[iCrate] << endl;
602  } // *** End for(iCrate) ***
603 
604  // *** Finding Slot Offsets ***
605  OffsetOutputFile << "\n\n\n" << "Crate/Slot Offset" << endl;
606  for (int iCrate = 0; iCrate < 12; ++iCrate)
607  {
608  // *** Go through slots for each crate, find offsets ***
609  for (int iSlot = 3; iSlot < 19; iSlot++)
610  {
611  // *** Calculate avg time for each slot and how different it is from 'good time' value ***
612  if (RunSlotTimeCounter[iCrate][iSlot] != 0)
613  {
614  RunSlotTimeAvg[iCrate][iSlot] = RunSlotTimeAdder[iCrate][iSlot] / RunSlotTimeCounter[iCrate][iSlot];
615  }
616 
617  RunSlotOffsets[iCrate][iSlot] = RunSlotTimeAvg[iCrate][iSlot] - GoodSlotsArray[iCrate][iSlot];
618 
619  // *** Testing purposes ***
620 
621  if (iCrate % 1 == 0 && iSlot % 111111 == 0)
622  {
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;
627  } // *** End testing if ***
628 
629  // *** Determine if channel is offset or not, and assign each channel either 0, +4 or -4 offset ***
630  if (RunSlotOffsets[iCrate][iSlot] > 3)
631  {
632  SlotOffsetCorrections[iCrate][iSlot] = +4;
633  }
634  else if (RunSlotOffsets[iCrate][iSlot] < -3)
635  {
636  SlotOffsetCorrections[iCrate][iSlot] = -4;
637  }
638  else
639  {
640  SlotOffsetCorrections[iCrate][iSlot] = 0;
641  }
642 
643  // *** Fill the output file with correction values ***
644  OffsetOutputFile << ( iCrate + 1 ) << "/" << iSlot << " " << SlotOffsetCorrections[iCrate][iSlot] << endl;
645  } // *** End iSlot ***
646  } // *** End for (iCrate) (iSlot loop) ***
647 
648  // *** Finding Channel Offsets ***
649  OffsetOutputFile << "\n\n\n" << "Chan Offset" << endl;
650 
651  // *** Loop through each channel and calculate the average ***
652  for (int iChannel = 0; iChannel < 2800; iChannel++)
653  {
654  // *** Calculate avg time for each channel and how different it is from 'good time' value ***
655  if (RunChannelTimeCounter[iChannel] != 0)
656  {
657  RunChannelTimeAvg[iChannel] = RunChannelTimeAdder[iChannel] / RunChannelTimeCounter[iChannel];
658  }
659 
660  RunChannelOffsets[iChannel] = RunChannelTimeAvg[iChannel] - GoodTimesArray[iChannel];
661 
662  // *** Determine if channel is offset or not, and assign each channel either 0, +4 or -4 offset ***
663  if (RunChannelOffsets[iChannel] > 3)
664  {
665  ChanOffsetCorrections[iChannel] = +4;
666  }
667  else if (RunChannelOffsets[iChannel] < -3)
668  {
669  ChanOffsetCorrections[iChannel] = -4;
670  }
671  else
672  {
673  ChanOffsetCorrections[iChannel] = 0;
674  }
675 
676  // *** Forcing individual channels to offset of the slot they are in if said slot has offset ***
677  if ( SlotOffsetCorrections[ ChannelCrates[iChannel] ][ ChannelSlots[iChannel] ] != 0 )
678  {
679  //ChanOffsetCorrections[iChannel] = SlotOffsetCorrections[ ChannelCrates[iChannel] ][ ChannelSlots[iChannel] ];
680  }
681  } // *** End for(iChannel) ***
682 
683  for (int iChannel = 0; iChannel < 2800; iChannel++)
684  {
685  // *** If statement for problem slot 4 crate 7, use nearest neighbor approach to compare it to slot to right and slo below ***
686  if ( ChannelCrates[iChannel] == 6 && ChannelSlots[iChannel] == 4 )
687  {
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;
689 
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;
691 
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;
693 
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;
696 
697  // *** Compare channels in bad slot to channels in slot to the right, and if they are lower then bump them up ***
698  if ( ( RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] + 3 ) ][ (int)round(chanYPos[iChannel]) ] ] ) < -2.5 )
699  {
700  ChanOffsetCorrections[iChannel] = -4;
701  }
702 
703  // *** Compare channels in bad slot to channels in slot below, and if they are lower then bump them up ***
704  if ( ( RunChannelTimeAvg[iChannel] - RunChannelTimeAvg[ chanPosition[ (int)round( chanXPos[iChannel] ) ][ (int)round( chanYPos[iChannel] - 3 ) ] ] ) < -2.5 )
705  {
706  ChanOffsetCorrections[iChannel] = -4;
707  }
708  } // *** End if crate 7 slot 4 ***
709 
710  // *** Fill the output file with correction values ***
711  OffsetOutputFile << ( iChannel + 1 ) << " " << ChanOffsetCorrections[iChannel] << endl;
712  OutputChanCCDBFile << ChanOffsetCorrections[iChannel] << endl;
713 
714  } // *** End for iChannel 2 ***
715 
716  OffsetOutputFile.close();
717  OutputChanCCDBFile.close();
718 
719  } // *** End of iRun for loop ***
720 
721 
722 
723 } // *** End of main ***
724 
725 
726 
727 
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
#define y
static evioFileChannel * chan
void OffsetCorrections()