CMS 3D CMS Logo

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