CondenseRunList.C
Go to the documentation of this file.
1 #include <vector>
2 #include <iostream>
3 #include <fstream>
4 #include <iomanip>
5 #include <sstream>
6 #include <string>
7 #include <streambuf>
8 #include <cstring>
9 #include <algorithm>
10 
11 #include <TFile.h>
12 #include <TString.h>
13 #include <TSystem.h>
14 #include <TMath.h>
15 #include <TDatime.h>
16 
17 #include "RunHistory/RunHistory.h"
18 
19 using namespace std;
20 
21 struct SubEntry{
22 
23  int run;
24  int subrun;
25  int nspills;
26  int pass_all;
27  int tstart;
28  int tstop;
29 
30 };
31 
32 struct RunInfo{
33 
34  int run;
35  float pot;
36  int rhstart;
37  int rhstop;
38  int allspills;
39  vector<int> nspills;
40  vector<int> subrun;
41  vector<int> tstart;
42  vector<int> tstop;
43  vector<int> pass_all;
44 
45 };
46 
47 TString PrintTime(int inp);
48 
50 
51  ofstream runfile;
52  runfile.open("NuMIList.csv");
53 
54  ifstream fileListItr("list_sort.txt");
55 
56  int run, subrun, nspills, pass_all, tstart, tstop;
57 
58  vector<SubEntry> allsubs;
59  SubEntry emptysub;
60  SubEntry subentry;
61 
62  while(fileListItr >> run >> subrun >> nspills >> pass_all >> tstart >> tstop){
63 
64  subentry = emptysub;
65 
66  subentry.run = run;
67  subentry.subrun = subrun;
68  subentry.nspills = nspills;
69  subentry.pass_all = pass_all;
70  subentry.tstart = tstart;
71  subentry.tstop = tstop;
72 
73  allsubs.push_back(subentry);
74 
75  }
76 
77  int numFiles = allsubs.size();
78 
79  vector<RunInfo> allruns;
80  RunInfo emptystruct;
81  RunInfo runstruct;
82 
83  runstruct.allspills = 0;
84 
85  int pass_any = 0;
86  int nextrun = -1;
87 
89 /* RunHistory DataBase Settings*/
90 /* rh.SetIfAnyMasking(true);
91  rh.SetAnalysisDiBlockMasking(true);
92 */
93  rh.SetDetector("FarDet");
94 
95 
96 
98  rh.SetIfAnyMasking(true);
99  rh.SetIfCustomMasking(false);
101  // rh.SetDiBlockMaskTag("v5.9");
102 
103 
104  for(int i=0; i<numFiles; i++){
105 
106  subentry = allsubs[i];
107 
108  runstruct.allspills += subentry.nspills;
109  pass_any += subentry.pass_all;
110 
111  runstruct.nspills.push_back(subentry.nspills);
112  runstruct.subrun.push_back(subentry.subrun);
113  runstruct.pass_all.push_back(subentry.pass_all);
114 
115  TDatime tda(subentry.tstart);
116  int kGMT = tda.Convert(1) - tda.Convert(0);
117  runstruct.tstart.push_back(subentry.tstart - kGMT);
118 
119  tda.Set(subentry.tstop);
120  kGMT = tda.Convert(1) - tda.Convert(0);
121  runstruct.tstop.push_back(subentry.tstop - kGMT);
122 
123  if(i<numFiles-1) nextrun = allsubs[i+1].run;
124 
125  if(nextrun != subentry.run || i == numFiles - 1){
126 
127  //If run passed automaical quality cuts
128  if(pass_any>0){
129  std::cout<<"Current Run: "<<subentry.run<<std::endl;
130 
131  rh.SetRunNumber(subentry.run);
132 
133  runstruct.pot = rh.NPOT();
134  std::cout<<"POT is: "<<runstruct.pot<<std::endl;
135 
136  if(runstruct.pot > 0){
137 
138  if(rh.GoodDiBlockMask()){
139  runstruct.run = subentry.run;
140  runstruct.rhstart = rh.TStart();
141  runstruct.rhstop = rh.TStop();
142  allruns.push_back(runstruct);
143  }
144 
145  }
146 
147  }
148 
149  runstruct = emptystruct;
150  pass_any = 0;
151  runstruct.allspills = 0;
152 
153  }
154 
155  if(i%(numFiles/20)==0) cout << 100*i/numFiles << "% done" << endl;
156 
157  }
158 
159  numFiles = 0;
160  float totalpot = 0;
161  int nruns = allruns.size();
162 
163  vector<RunInfo> goodruns;
164 
165  for(int i=0; i<nruns; i++){
166 
167  int nsubs = allruns[i].subrun.size();
168  int rhstart = allruns[i].rhstart;
169  int rhstop = allruns[i].rhstop;
170 
171  runstruct = emptystruct;
172  runstruct.run = allruns[i].run;
173  runstruct.pot = allruns[i].pot;
174  runstruct.allspills = allruns[i].allspills;
175 
176  int diffstart = rhstart - allruns[i].tstart[0];
177  int diffstop = rhstop - allruns[i].tstop[nsubs-1];
178 
179 
180  int tolerance = 60;
181 
182  /*
183  bool matchRH = true;
184  if(TMath::Abs(diffstart) > tolerance || TMath::Abs(diffstop) > tolerance){
185  cout << "Run " << allruns[i].run
186  << ": Diff Start = " << diffstart
187  << "; Diff Stop = " << diffstop
188  << endl;
189  matchRH = false;
190  }
191  if(TMath::Abs(diffstart - diffstop) > tolerance){
192  cout << "Run " << allruns[i].run
193  << ": Diff Start - Stop = " << diffstart - diffstop
194  << endl;
195  }
196  */
197 
198  vector<int> dvec;
199 
200  for(int j=0; j<nsubs; j++){
201 
202  if(!allruns[i].pass_all[j]) continue;
203 
204  dvec.push_back(allruns[i].tstop[j] - allruns[i].tstart[j]);
205 
206  }
207 
208  int ngood = dvec.size();
209 
210  int dmid = TMath::Median(ngood,&dvec[0]);
211 
212  for(int j=0; j<nsubs; j++){
213 
214  int estart = 0;
215  if(j>0) estart = allruns[i].tstop[j-1];
216  else estart = rhstart;
217 
218  if(j==0 && allruns[i].subrun[0] != 0){
219  estart += dmid * allruns[i].subrun[0];
220  }
221  if(j>0 && allruns[i].subrun[j] != allruns[i].subrun[j-1] + 1){
222  estart += dmid * (allruns[i].subrun[j] - allruns[i].subrun[j-1] - 1);
223  }
224 
225  int omstart = allruns[i].tstart[j];
226 
227  int isgood = allruns[i].pass_all[j];
228 
229  if(TMath::Abs(omstart - estart) > tolerance){
230 
231  if(isgood){
232  cout << allruns[i].run << "_" << allruns[i].subrun[j] << ":" << endl;
233  cout << "OnMon start time = " << TDatime(omstart).AsString() << endl;
234  if(j==0) cout << "RunHistory start time = " << TDatime(estart).AsString() << endl;
235  else cout << "Previous subrun stop time = " << TDatime(estart).AsString() << endl;
236  cout << "Difference = " << PrintTime(omstart - estart) << endl;
237  if(j==0) cout << "Use RunHistory start time." << endl;
238  else cout << "Use previous subrun stop time." << endl;
239  cout << endl;
240  }
241 
242  omstart = estart;
243  }
244 
245  allruns[i].tstart[j] = omstart;
246 
247  int estop = omstart + dmid;
248 
249  int omstop = allruns[i].tstop[j];
250 
251  int nbstop = 0;
252  if(j<nsubs-1) nbstop = allruns[i].tstart[j+1];
253  else nbstop = rhstop;
254 
255  if(TMath::Abs(omstop - estop) > tolerance || TMath::Abs(omstop - nbstop) > tolerance){
256 
257  TString action = "";
258 
259  if(TMath::Abs(estop - nbstop) <= tolerance){
260  action += "E1 and E2 agree: Use average of E1 and E2.";
261  omstop = 0.5 * estop + 0.5 * nbstop;
262  }
263  else if(TMath::Abs(omstop - estop) > tolerance && TMath::Abs(omstop - nbstop) > tolerance){
264  action += "OnMon, E1 and E2 all disagree: ";
265  if(j<nsubs-1){
266  if(TMath::Abs(allruns[i].tstop[j+1] - allruns[i].tstart[j+1] - dmid) < tolerance){
267  action += "Next subrun seems good. Use next subrun start time.";
268  omstop = nbstop;
269  }
270  else if(TMath::Abs(dmid - (rhstop - rhstart)/nsubs) <= tolerance){
271  action += "Next subrun may not be good, but median seems good. Use estimate based on median.";
272  omstop = estop;
273  }
274  else{
275  action += "Neither median nor next subrun seem reliable. Keep OnMon timestamp.";
276  }
277  }
278  }
279  else if(TMath::Abs(omstop - nbstop) <= tolerance){
280  action += "OnMon and E2 agree";
281  if(j < nsubs-1 && TMath::Abs(TMath::Abs(omstop - estop) - 3600) < tolerance){
282  action += ", but this seems like a Daylight Saving issue: Use estimate based on median.";
283  omstop = estop;
284  }
285  else{
286  action += ": Keep OnMon timestamp.";
287  }
288  }
289  else{
290  action += "OnMon and E1 agree: Keep OnMon timestamp.";
291  }
292 
293  if(isgood && allruns[i].tstop[j] != omstop){
294  cout << allruns[i].run << "_" << allruns[i].subrun[j] << ":" << endl;
295  cout << "OnMon stop time = " << TDatime(allruns[i].tstop[j]).AsString() << endl;
296  cout << "E1: Based on median duration, stop time should be " << TDatime(estop).AsString() << endl;
297  if(j==nsubs-1) cout << "E2: RunHistory stop time = " << TDatime(nbstop).AsString() << endl;
298  else cout << "E2: Next subrun start time = " << TDatime(nbstop).AsString() << endl;
299  cout << "Difference to E1 = " << PrintTime(allruns[i].tstop[j] - estop) << "; ";
300  cout << "Difference to E2 = " << PrintTime(allruns[i].tstop[j] - nbstop) << endl;
301  cout << action << endl << endl;
302  }
303 
304  }
305 
306  allruns[i].tstop[j] = omstop;
307 
308  }
309 
310  for(int j=0; j<nsubs; j++){
311 
312  if(!allruns[i].pass_all[j]) continue;
313 
314  runstruct.subrun.push_back(allruns[i].subrun[j]);
315  runstruct.nspills.push_back(allruns[i].nspills[j]);
316 
317  runstruct.tstart.push_back(allruns[i].tstart[j]);
318  runstruct.tstop.push_back(allruns[i].tstop[j]);
319  }
320 
321  goodruns.push_back(runstruct);
322 std::cout<< "The size of the goodruns is: "<<goodruns.size()<<std::endl;
323 
324  }
325 
326  nruns = goodruns.size();
327 
328 std::cout<<"Number of runs are: "<<nruns<<std::endl;
329 
330  numFiles = 0;
331 
332  for(int i=0; i<nruns; i++){
333 
334  for(int j=0; j<int(goodruns[i].subrun.size()); j++){
335 
336  float pot = goodruns[i].pot * goodruns[i].nspills[j] / goodruns[i].allspills;
337  totalpot += pot;
338  numFiles++;
339 
340  runfile << TString::Format("%d,%02d,%d,%d,%.1f", goodruns[i].run, goodruns[i].subrun[j],
341  goodruns[i].tstart[j], goodruns[i].tstop[j],
342  pot) << endl;
343 
344  if(numFiles%1000 == 0) cout << numFiles << " files done" << endl;
345 
346  }
347 
348  }
349 
350  runfile.close();
351 
352  cout << "Finished " << numFiles << " files" << endl;
353  cout << "Total of " << totalpot << " PoT selected." << endl;
354 
355 }
356 
357 TString PrintTime(int inp){
358 
359  int t = TMath::Abs(inp);
360 
361  int sec = t%60;
362  int min = (t/60)%60;
363  int hour = (t/3600)%24;
364  int days = (t/(3600*24))%365;
365  int years = (t/(3600*24*365));
366 
367  TString out = "";
368 
369  if(t>=365*24*3600) out += TString::Format("%d years, ",years);
370  if(t>=24*3600) out += TString::Format("%d days, ",days);
371  if(t>=3600) out += TString::Format("%dh",hour);
372  if(t>=60) out += TString::Format("%dm",min);
373  out += TString::Format("%ds",sec);
374 
375  return out;
376 
377 }
378 
vector< int > subrun
TH2 * rh
Definition: drawXsec.C:5
void SetDetector(novadaq::cnv::DetId det)
Definition: RunHistory.cxx:325
Int_t pass_all
Definition: SimpleIterate.C:42
void SetGetDiBlockMaskFromCondb(bool f)
Definition: RunHistory.h:367
void SetIfCustomMasking(bool f)
set from fcl: do we want to use a custom mask?
Definition: RunHistory.h:365
vector< int > tstart
void SetRunNumber(int run)
Definition: RunHistory.cxx:317
TString PrintTime(int inp)
#define pot
void SetDataType(nova::dbi::DataType dt)
Definition: RunHistory.h:448
const double j
Definition: BetheBloch.cxx:29
vector< int > nspills
OStream cout
Definition: OStream.cxx:6
ifstream fileListItr("ManBadSubruns.txt")
vector< int > tstop
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
T min(const caf::Proxy< T > &a, T b)
int GoodDiBlockMask(int subrun=-1, bool reload=false)
void CondenseRunList()
vector< int > pass_all
void SetIfAnyMasking(bool f)
set from fcl: do we want to use any masking at all? If no, other masking options irrelevant ...
Definition: RunHistory.h:363