plotTime.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 <TTree.h>
13 #include <TLeaf.h>
14 #include <TString.h>
15 #include <TSystem.h>
16 #include <TMath.h>
17 #include <TDatime.h>
18 #include <TGraph.h>
19 #include <TMarker.h>
20 #include <TStyle.h>
21 
22 #include "RunHistory/RunHistory.h"
23 
24 #include "SetupHistos.C"
25 
26 TString PrintTime(int t);
27 
28 void plotTime(int runNumber = 18000){
29 
30  //nova::dbi::RunHistory rh(runNumber,"FarDet");
31 
32  //int rhstart = rh.TStart();
33  //int rhstop = rh.TStop();
34 
35  TFile file("MetricsTree_All.root","read");
36  TTree *tree = (TTree*)file.Get("mytree");
37 
38  int run, subrun, pass_all;
39  int firstsec, lastsec;
40 
41  tree->SetBranchAddress("run",&run);
42  tree->SetBranchAddress("subrun",&subrun);
43  tree->SetBranchAddress("pass_all",&pass_all);
44  tree->SetBranchAddress("firstsec",&firstsec);
45  tree->SetBranchAddress("lastsec",&lastsec);
46 
47  tree->SetBranchStatus("*",0);
48  tree->SetBranchStatus("run",1);
49  tree->SetBranchStatus("subrun",1);
50  tree->SetBranchStatus("pass_all",1);
51  tree->SetBranchStatus("firstsec",1);
52  tree->SetBranchStatus("lastsec",1);
53 
54  int nentries = tree->GetEntries();
55 
56  vector<int> subs;
57  vector<int> startom;
58  vector<int> stopom;
59  vector<int> pass;
60 
61  bool runExists = false;
62 
63  for(int i = 0; i < nentries; i++){
64 
65 // if(i%(nentries/10)==0) cout << int(100.0*i/nentries+0.5) << "% done" << endl;
66 
67  tree->GetEntry(i);
68 
69  if(run != runNumber) continue;
70 
71  runExists = true;
72 
73  subs.push_back(subrun);
74  pass.push_back(pass_all);
75 
76  TDatime tda(firstsec);
77  int kGMT = tda.Convert(1) - tda.Convert(0);
78  startom.push_back(firstsec - kGMT);
79 
80  tda.Set(lastsec);
81  kGMT = tda.Convert(1) - tda.Convert(0);
82  stopom.push_back(lastsec - kGMT);
83 
84  }
85 
86  if(runExists == false)
87  return;
88 
89  nova::dbi::RunHistory rh(runNumber,"FarDet");
90 
91  int rhstart = rh.TStart();
92  int rhstop = rh.TStop();
93 
94  int nsubs = subs.size();
95 
96  vector<int> dvec;
97 
98  for(int j=0; j<nsubs; j++){
99 
100  if(!pass[j]) continue;
101 
102  dvec.push_back(stopom[j] - startom[j]);
103 
104  }
105 
106  int ngood = dvec.size();
107 
108  int dmid = TMath::Median(ngood,&dvec[0]);
109 
110  int tolerance = 60;
111 
112  vector<int> startcor = startom;
113  vector<int> stopcor = stopom;
114 
115  vector<int> estartv(nsubs,0);
116  vector<int> estopv(nsubs,0);
117  vector<int> nbstopv(nsubs,0);
118 
119  for(int j=0; j<nsubs; j++){
120 
121  int estart = 0;
122  if(j>0) estart = stopcor[j-1];
123  else estart = rhstart;
124 
125  if(j==0 && subs[0] != 0){
126  estart += dmid * subs[0];
127  }
128  if(j>0 && subs[j] != subs[j-1] + 1){
129  estart += dmid * (subs[j] - subs[j-1] - 1);
130  }
131 
132  int omstart = startom[j];
133 
134  if(TMath::Abs(omstart - estart) > tolerance){
135  cout << runNumber << "_" << subs[j] << ":" << endl;
136  cout << "OnMon start time = " << TDatime(omstart).AsString() << endl;
137  if(j==0) cout << "RunHistory start time = " << TDatime(estart).AsString() << endl;
138  else cout << "Previous subrun stop time = " << TDatime(estart).AsString() << endl;
139  cout << "Difference = " << PrintTime(omstart - estart) << endl;
140  if(j==0) cout << "Use RunHistory start time." << endl;
141  else cout << "Use previous subrun stop time." << endl;
142  cout << endl;
143  omstart = estart;
144  }
145 
146  estartv[j] = estart;
147 
148  startcor[j] = omstart;
149 
150  int estop = omstart + dmid;
151 
152  int omstop = stopom[j];
153 
154  int nbstop = 0;
155  if(j<nsubs-1) nbstop = startom[j+1];
156  else nbstop = rhstop;
157 
158  if(TMath::Abs(omstop - estop) > tolerance || TMath::Abs(omstop - nbstop) > tolerance){
159 
160  TString action = "";
161 
162  if(TMath::Abs(estop - nbstop) <= tolerance){
163  action += "E1 and E2 agree: Use average of E1 and E2.";
164  omstop = 0.5 * estop + 0.5 * nbstop;
165  }
166  else if(TMath::Abs(omstop - estop) > tolerance && TMath::Abs(omstop - nbstop) > tolerance){
167  action += "OnMon, E1 and E2 all disagree: ";
168  if(j<nsubs-1){
169  if(TMath::Abs(stopom[j+1] - startom[j+1] - dmid) < tolerance){
170  action += "Next subrun seems good. Use next subrun start time.";
171  omstop = nbstop;
172  }
173  else if(TMath::Abs(dmid - (rhstop - rhstart)/nsubs) <= tolerance){
174  action += "Next subrun may not be good, but median seems good. Use estimate based on median.";
175  omstop = estop;
176  }
177  else{
178  action += "Neither median nor next subrun seem reliable. Keep OnMon timestamp.";
179  }
180  }
181  }
182  else if(TMath::Abs(omstop - nbstop) <= tolerance){
183  action += "OnMon and E2 agree";
184  if(j < nsubs-1 && TMath::Abs(TMath::Abs(omstop - estop) - 3600) < tolerance){
185  action += ", but this seems like a Daylight Saving issue: Use estimate based on median.";
186  omstop = estop;
187  }
188  else{
189  action += ": Keep OnMon timestamp.";
190  }
191  }
192  else{
193  action += "OnMon and E1 agree: Keep OnMon timestamp.";
194  }
195 
196  if(stopom[j] != omstop){
197  cout << runNumber << "_" << subs[j] << ":" << endl;
198  cout << "OnMon stop time = " << TDatime(stopom[j]).AsString() << endl;
199  cout << "E1: Based on median duration, stop time should be " << TDatime(estop).AsString() << endl;
200  if(j==nsubs-1) cout << "E2: RunHistory stop time = " << TDatime(nbstop).AsString() << endl;
201  else cout << "E2: Next subrun start time = " << TDatime(nbstop).AsString() << endl;
202  cout << "Difference to E1 = " << PrintTime(stopom[j] - estop) << "; ";
203  cout << "Difference to E2 = " << PrintTime(stopom[j] - nbstop) << endl;
204  cout << action << endl << endl;
205  }
206 
207  }
208 
209  estopv[j] = estop;
210  nbstopv[j] = nbstop;
211 
212  stopcor[j] = omstop;
213 
214  }
215 
216  TDatime t;
217  int GMToffset = (t.Convert(true) - t.Convert());
218 
219  gSystem->Setenv("TZ","UTC");
220 
221  gStyle->SetTimeOffset(-GMToffset);
222 
223  TCanvas *c1 = MakeLongCanvas();
224 
225  int gx[2] = {rhstart - 3600, rhstop + 3600};
226  int gy[2] = {64, -1};
227 
228  TGraph *gr = new TGraph(2,gx,gy);
229 
230  SetGraph(gr,0);
231 
232  gr->SetTitle(";Central Time;Subrun Number");
233 
234  //gr->GetYaxis()->SetRangeUser(0,60)
235 
236  gr->Draw("ap");
237 
238  gr->GetXaxis()->SetTimeDisplay(1);
239 
240  TLine line;
241  TMarker mark;
242 
243  int jmin = 0;
244  int jmax = 11;
245 
246  gr->GetXaxis()->SetRangeUser(startom[jmin]-600,stopom[jmax]+600);
247  gr->GetYaxis()->SetRangeUser(subs[jmin]-1,subs[jmax]+1);
248 
249  line.SetLineWidth(3);
250  mark.SetMarkerSize(1.5);
251 
252  for(int j=jmin; j<=jmax; j++){
253 
254  mark.SetMarkerColor(kMagenta+1);
255  mark.SetMarkerStyle(34);
256  mark.DrawMarker(nbstopv[j],subs[j]+0.25);
257 
258  mark.SetMarkerColor(kBlue);
259  mark.SetMarkerStyle(29);
260  mark.DrawMarker(estartv[j],subs[j]+0.25);
261  mark.SetMarkerStyle(34);
262  mark.DrawMarker(estopv[j],subs[j]+0.25);
263 
264  mark.SetMarkerColor(kRed);
265  mark.SetMarkerStyle(29);
266  mark.DrawMarker(startom[j],subs[j]);
267  mark.SetMarkerStyle(34);
268  mark.DrawMarker(stopom[j],subs[j]);
269 
270  line.SetLineColor(kRed);
271  line.DrawLine(startom[j],subs[j],stopom[j],subs[j]);
272 
273  mark.SetMarkerColor(kGreen+2);
274  mark.SetMarkerStyle(29);
275  mark.DrawMarker(startcor[j],subs[j]+0.5);
276  mark.SetMarkerStyle(34);
277  mark.DrawMarker(stopcor[j],subs[j]+0.5);
278 
279  line.SetLineColor(kGreen+2);
280  line.DrawLine(startcor[j],subs[j]+0.5,stopcor[j],subs[j]+0.5);
281 
282  }
283 
284  c1->Update();
285 
286  line.SetLineColor(kBlack);
287  line.SetLineStyle(7);
288  line.DrawLine(rhstart,c1->GetUymin(),rhstart,c1->GetUymax());
289  line.DrawLine(rhstop,c1->GetUymin(),rhstop,c1->GetUymax());
290 
291 }
292 
293 TString PrintTime(int inp){
294 
295  int t = TMath::Abs(inp);
296 
297  int sec = t%60;
298  int min = (t/60)%60;
299  int hour = (t/3600)%24;
300  int days = (t/(3600*24))%365;
301  int years = (t/(3600*24*365));
302 
303  TString out = "";
304 
305  if(t>=365*24*3600) out += TString::Format("%d years, ",years);
306  if(t>=24*3600) out += TString::Format("%d days, ",days);
307  if(t>=3600) out += TString::Format("%dh",hour);
308  if(t>=60) out += TString::Format("%dm",min);
309  out += TString::Format("%ds",sec);
310 
311  return out;
312 
313 }
Int_t lastsec
Definition: SimpleIterate.C:27
enum BeamMode kRed
TH2 * rh
Definition: drawXsec.C:5
Int_t pass_all
Definition: SimpleIterate.C:42
TString PrintTime(int t)
Definition: plotTime.C:293
TCanvas * MakeLongCanvas()
Definition: SetupHistos.C:23
Long64_t nentries
void SetGraph(TGraph *gr, int col=1)
Definition: SetupHistos.C:142
const double j
Definition: BetheBloch.cxx:29
OStream cout
Definition: OStream.cxx:6
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
Int_t firstsec
Definition: SimpleIterate.C:26
Int_t GMToffset
Definition: AnaPlotMaker.h:43
TFile * file
Definition: cellShifts.C:17
c1
Definition: demo5.py:24
void Format(TGraph *gr, int lcol, int lsty, int lwid, int mcol, int msty, double msiz)
Definition: Style.cxx:154
enum BeamMode kGreen
enum BeamMode kBlue
void plotTime(int runNumber=18000)
Definition: plotTime.C:28