CMS 3D CMS Logo

StatisticsPlots.cc
Go to the documentation of this file.
1 #include "StatisticsPlots.h"
2 #include <vector>
3 #include <algorithm>
4 #include <string>
5 #include <map>
6 #include <iostream>
7 #include "TPad.h"
8 #include "TH1D.h"
9 #include "TFile.h"
10 #include "TH2F.h"
11 #include "TH1F.h"
12 #include "TText.h"
14 #include "TCanvas.h"
15 #include "TGraphAsymmErrors.h"
16 
17 void DeadTimeAPVCycle(TH1F* hist, const std::vector<int>& bins) {
18 
19  float ntotodd = 0;
20  float ntoteven = 0;
21  float nspecialodd = 0;
22  float nspecialeven = 0;
23  unsigned int nbintotodd = 0;
24  unsigned int nbintoteven = 0;
25  unsigned int nbinspecialodd = 0;
26  unsigned int nbinspecialeven = 0;
27 
28  for(int i = 1; i < hist->GetNbinsX()+1; ++i) {
29 
30  bool isSpecial = false;
31  for(unsigned int special = 0; special < bins.size() ; ++special) {
32  if(i==bins[special]) {
33  isSpecial=true;
34  break;
35  }
36  }
37 
38  if(i%2==0) {
39  if(isSpecial) {
40  ++nbinspecialeven;
41  nspecialeven += hist->GetBinContent(i);
42  }
43  else {
44  ++nbintoteven;
45  ntoteven += hist->GetBinContent(i);
46  }
47  }
48  else {
49  if(isSpecial) {
50  ++nbinspecialodd;
51  nspecialodd += hist->GetBinContent(i);
52  }
53  else {
54  ++nbintotodd;
55  ntotodd += hist->GetBinContent(i);
56  }
57  }
58  }
59  std::cout << "Summary" << std::endl;
60  std::cout << "Odd events " << ntotodd << " special " << nspecialodd << std::endl;
61  std::cout << "Odd bins " << nbintotodd << " special " << nbinspecialodd << std::endl;
62  std::cout << "Odd bins populations" << float(ntotodd)/nbintotodd << " special " << float(nspecialodd)/nbinspecialodd << std::endl;
63  std::cout << "Even events " << ntoteven << " special " << nspecialeven << std::endl;
64  std::cout << "Even bins " << nbintoteven << " special " << nbinspecialeven << std::endl;
65  std::cout << "Even bins populations" << float(ntoteven)/nbintoteven << " special " << float(nspecialeven)/nbinspecialeven << std::endl;
66 
67  float oddloss = nspecialodd -nbinspecialodd*ntotodd/nbintotodd;
68  float evenloss = nspecialeven -nbinspecialeven*ntoteven/nbintoteven;
69 
70  float fracloss = (oddloss+evenloss)/(ntotodd + ntoteven + nspecialodd + nspecialeven);
71 
72  std::cout << "Loss " << oddloss << " " << evenloss << " " << fracloss << std::endl;
73 
74 }
75 
76 TH1F* CombinedHisto(TFile& ff, const char* module, const char* histname) {
77 
78  CommonAnalyzer castat(&ff,"",module);
79 
80  TH1F* result = nullptr;
81 
82  std::vector<unsigned int> runs = castat.getRunList();
83  std::sort(runs.begin(),runs.end());
84 
85  {
86  for(unsigned int i=0;i<runs.size();++i) {
87 
88  char runlabel[100];
89  sprintf(runlabel,"%d",runs[i]);
90  char runpath[100];
91  sprintf(runpath,"run_%d",runs[i]);
92  castat.setPath(runpath);
93 
94  TH1F* hist = (TH1F*)castat.getObject(histname);
95 
96  if(hist) {
97  if(result==nullptr) {
98  result = new TH1F(*hist);
99  result->Reset();
100  }
101  result->Add(hist);
102  std::cout << hist->GetTitle() << " added: " << hist->GetEntries() << " " << result->GetEntries() << std::endl;
103  }
104 
105  }
106  }
107 
108  return result;
109 
110 }
111 
112 TH1F* TimeRatio(TFile& ff, const char* modulen, const char* moduled, const int irun, const int rebin) {
113 
114  CommonAnalyzer castatn(&ff,"",modulen);
115  CommonAnalyzer castatd(&ff,"",moduled);
116 
117 
118  char runlabel[100];
119  sprintf(runlabel,"%d",irun);
120  char runpath[100];
121  sprintf(runpath,"run_%d",irun);
122  castatn.setPath(runpath);
123  castatd.setPath(runpath);
124 
125  TH1F* ratio = nullptr;
126 
127  TH1F* orbitn=nullptr;
128  if(orbitn==nullptr) orbitn = (TH1F*)castatn.getObject("orbit");
129  TH1F* orbitd=nullptr;
130  if(orbitd==nullptr) orbitd = (TH1F*)castatd.getObject("orbit");
131  if(orbitn != nullptr && orbitd != nullptr) {
132  orbitn->Rebin(rebin);
133  orbitd->Rebin(rebin);
134  ratio = new TH1F(*orbitd);
135  ratio->Reset();
136  ratio->Divide(orbitn,orbitd);
137  }
138  return ratio;
139 
140 }
141 
142 TH1D* SummaryHisto(TFile& ff, const char* module) {
143 
144  CommonAnalyzer castat(&ff,"",module);
145 
146  TH1D* nevents = new TH1D("nevents","Number of events vs run",10,0.,10.);
147  nevents->SetCanExtend(TH1::kXaxis);
148 
149  std::vector<unsigned int> runs = castat.getRunList();
150  std::sort(runs.begin(),runs.end());
151 
152  {
153  for(unsigned int i=0;i<runs.size();++i) {
154 
155  char runlabel[100];
156  sprintf(runlabel,"%d",runs[i]);
157  char runpath[100];
158  sprintf(runpath,"run_%d",runs[i]);
159  castat.setPath(runpath);
160 
161 
162  TH1F* orbit=nullptr;
163  if(orbit==nullptr) orbit = (TH1F*)castat.getObject("orbit");
164  if(orbit) {
165  // fill the summary
166  nevents->Fill(runlabel,orbit->GetEntries());
167  }
168  }
169  }
170  return nevents;
171 
172 }
173 
174 TH1D* SummaryHistoRatio(TFile& f1, const char* mod1, TFile& f2, const char* mod2, const char* hname) {
175 
176  TH1D* denom = SummaryHisto(f1,mod1);
177  TH1D* numer = SummaryHisto(f2,mod2);
178 
179  TH1D* ratio = (TH1D*)denom->Clone(hname);
180  ratio->SetTitle("Fraction of events vs run");
181  ratio->Sumw2();
182  ratio->Reset();
183  ratio->SetDirectory(nullptr);
184  ratio->Divide(numer,denom,1,1,"b");
185 
186  return ratio;
187 
188 }
189 
190 TGraphAsymmErrors* SummaryHistoRatioGraph(TFile& f1, const char* mod1, TFile& f2, const char* mod2, const char* /* hname */) {
191 
192  TH1D* denom = SummaryHisto(f1,mod1);
193  TH1D* numer = SummaryHisto(f2,mod2);
194 
195  TGraphAsymmErrors* ratio = new TGraphAsymmErrors;;
196 
197  ratio->BayesDivide(numer,denom);
198 
199  return ratio;
200 
201 }
202 
203 TH2F* Combined2DHisto(TFile& ff, const char* module, const char* histname) {
204 
205  CommonAnalyzer castat(&ff,"",module);
206 
207  TH2F* result = nullptr;
208 
209  std::vector<unsigned int> runs = castat.getRunList();
210  std::sort(runs.begin(),runs.end());
211 
212  {
213  for(unsigned int i=0;i<runs.size();++i) {
214 
215  char runlabel[100];
216  sprintf(runlabel,"%d",runs[i]);
217  char runpath[100];
218  sprintf(runpath,"run_%d",runs[i]);
219  castat.setPath(runpath);
220 
221  TH2F* hist = (TH2F*)castat.getObject(histname);
222 
223  if(hist) {
224  if(result==nullptr) {
225  result = new TH2F(*hist);
226  result->Reset();
227  }
228  result->Add(hist);
229  std::cout << hist->GetTitle() << " added: " << hist->GetEntries() << " " << result->GetEntries() << std::endl;
230  }
231 
232  }
233  }
234 
235  return result;
236 
237 }
238 
239 
240 
241 void StatisticsPlots(const char* fullname, const char* module, const char* label, const char* postfix, const char* shortname,
242  const char* outtrunk) {
243 
244  char modfull[300];
245  sprintf(modfull,"%s%s",module,postfix);
246  char labfull[300];
247  sprintf(labfull,"%s%s",label,postfix);
248 
249  char dirname[400];
250  sprintf(dirname,"%s",shortname);
251 
252  // char fullname[300];
253  // if(strlen(family)==0) { sprintf(fullname,"rootfiles/Tracking_PFG_%s.root",filename);}
254  // else { sprintf(fullname,"rootfiles/%s.root",dirname); }
255 
256  TFile ff(fullname);
257 
258  // Colliding events
259 
260  /*
261  char modname[300];
262  sprintf(modname,"trackcount%s",postfix);
263 
264  CommonAnalyzer castatall(&ff,"",modname);
265 
266  TH1F* ntrk = (TH1F*)castatall.getObject("ntrk");
267  if (ntrk) {
268  std::cout << " All runs " << ntrk->GetEntries() << std::endl;
269  delete ntrk;
270  }
271  */
272 
273  // sprintf(modname,"eventtimedistribution%s",postfix);
274 
275  CommonAnalyzer castat(&ff,"",modfull);
276 
277  // Summary histograms
278 
279  TH1D* nevents = new TH1D("nevents","Number of events vs run",10,0.,10.);
280  nevents->SetCanExtend(TH1::kXaxis);
281 
282 
283  std::vector<unsigned int> runs = castat.getRunList();
284  std::sort(runs.begin(),runs.end());
285 
286  {
287 
288  std::cout << "Collision Events" << std::endl;
289 
290  for(unsigned int i=0;i<runs.size();++i) {
291 
292  char runlabel[100];
293  sprintf(runlabel,"%d",runs[i]);
294  char runpath[100];
295  sprintf(runpath,"run_%d",runs[i]);
296  castat.setPath(runpath);
297 
298  TH1* orbit=nullptr;
299  TH2F* orbitvsbx = (TH2F*)castat.getObject("orbitvsbxincycle");
300  if(orbitvsbx==nullptr) orbitvsbx = (TH2F*)castat.getObject("orbitvsbx");
301  if (orbitvsbx) {
302  std::cout << runpath << " " << orbitvsbx->GetEntries() << std::endl;
303  // prepare plot
304  new TCanvas;
305  if(orbitvsbx->GetEntries()>0) {
306  orbit = orbitvsbx->ProjectionY();
307  }
308  delete orbitvsbx;
309  }
310  if(orbit==nullptr) orbit = (TH1F*)castat.getObject("orbit");
311  if(orbit) {
312  orbit->GetXaxis()->SetTitle("orbit");
313  orbit->SetTitle(runpath);
314  //normalize to get the rate
315  orbit->Scale(11223,"width");
316  orbit->GetYaxis()->SetTitle("Rate (Hz)");
317  // fill the summary
318  nevents->Fill(runlabel,orbit->GetEntries());
319 
320  //
321  orbit->Draw();
322  std::string plotfilename;
323  plotfilename += outtrunk;
324  plotfilename += dirname;
325  plotfilename += "/orbit_";
326  plotfilename += labfull;
327  plotfilename += "_";
328  plotfilename += dirname;
329  plotfilename += "_";
330  plotfilename += runpath;
331  plotfilename += ".gif";
332  gPad->Print(plotfilename.c_str());
333  delete orbit;
334  }
335 
336  TH1F* dbx = (TH1F*)castat.getObject("dbx");
337  if (dbx) {
338  // prepare plot
339  if(dbx->GetEntries()>0) {
340  dbx->Draw();
341  gPad->SetLogy(1);
342  std::string plotfilename;
343  plotfilename += outtrunk;
344  plotfilename += dirname;
345  plotfilename += "/dbx_";
346  plotfilename += labfull;
347  plotfilename += "_";
348  plotfilename += dirname;
349  plotfilename += "_";
350  plotfilename += runpath;
351  plotfilename += ".gif";
352  gPad->Print(plotfilename.c_str());
353  delete dbx;
354  }
355  gPad->SetLogy(0);
356  }
357  TH1F* bx = (TH1F*)castat.getObject("bx");
358  if (bx) {
359  // prepare plot
360  if(bx->GetEntries()>0) {
361  bx->SetLineColor(kRed);
362  bx->Draw();
363  gPad->SetLogy(1);
364  std::string plotfilename;
365  plotfilename += outtrunk;
366  plotfilename += dirname;
367  plotfilename += "/bx_";
368  plotfilename += labfull;
369  plotfilename += "_";
370  plotfilename += dirname;
371  plotfilename += "_";
372  plotfilename += runpath;
373  plotfilename += ".gif";
374  gPad->Print(plotfilename.c_str());
375  delete bx;
376  }
377  gPad->SetLogy(0);
378  }
379  }
380  }
381 
382  if(!runs.empty()) {
383  std::string plotfilename;
384  plotfilename = outtrunk;
385  plotfilename += dirname;
386  plotfilename += "/nevents_";
387  plotfilename += labfull;
388  plotfilename += "_";
389  plotfilename += dirname;
390  plotfilename += ".gif";
391 
392  TCanvas * cwide = new TCanvas(plotfilename.c_str(),plotfilename.c_str(),1500,500);
393  nevents->Draw();
394 
395  char nentries[100];
396  sprintf(nentries,"%.0f",nevents->GetSumOfWeights());
397  TText ttlabel;
398  ttlabel.DrawTextNDC(.8,.7,nentries);
399 
400  std::cout << nentries << std::endl;
401 
402  gPad->Print(plotfilename.c_str());
403  delete cwide;
404  }
405  delete nevents;
406 
407  ff.Close();
408 
409 }
410 
TH1D * SummaryHisto(TFile &ff, const char *module)
TH1F * CombinedHisto(TFile &ff, const char *module, const char *histname)
TObject * getObject(const char *name) const
void StatisticsPlots(const char *fullname, const char *module, const char *label, const char *postfix, const char *shortname, const char *outtrunk)
TH1D * SummaryHistoRatio(TFile &f1, const char *mod1, TFile &f2, const char *mod2, const char *hname)
TGraphAsymmErrors * SummaryHistoRatioGraph(TFile &f1, const char *mod1, TFile &f2, const char *mod2, const char *)
char const * label
constexpr bool isSpecial(const float t)
TH1F * TimeRatio(TFile &ff, const char *modulen, const char *moduled, const int irun, const int rebin)
void setPath(const char *path)
void DeadTimeAPVCycle(TH1F *hist, const std::vector< int > &bins)
const std::vector< unsigned int > getRunList() const
TH2F * Combined2DHisto(TFile &ff, const char *module, const char *histname)
Definition: vlib.h:208