Logo ROOT  
Reference Guide
tprofile2polyRealisticModuleError.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// Simulate faulty detector panel w.r.t. particle charge
5///
6/// \macro_image
7/// \macro_code
8///
9/// \author Filip Ilic
10
11#include <iostream>
12#include <fstream>
13using namespace std;
14
15void tprofile2polyRealisticModuleError(Int_t numEvents = 1000000)
16{
17 TCanvas *c1 = new TCanvas("c1", "4 Malfunctioning Panels", 800, 400);
18 c1->Divide(3, 1);
19
20 // -------------------- Construct detector bins ------------------------
21 auto th2p = new TH2Poly();
22 auto avg = new TProfile2Poly();
23 auto err = new TProfile2Poly();
24
25 ifstream infile;
26 TString dir = gROOT->GetTutorialDir();
27 dir.Append("/hist/data/tprofile2poly_tutorial.data");
28 infile.open(dir.Data());
29
30 if (!infile) // Verify that the file was open successfully
31 {
32 std::cerr << dir.Data() << std::endl; // Report error
33 std::cerr << "Error code: " << strerror(errno) << std::endl; // Get some info as to why
34 return;
35 }
36
37 vector<pair<Double_t, Double_t>> allCoords;
38 Double_t a, b;
39 while (infile >> a >> b) {
40 pair<Double_t, Double_t> coord(a, b);
41 allCoords.push_back(coord);
42 }
43
44 if (allCoords.size() % 3 != 0) {
45 cout << "[ERROR] Bad file" << endl;
46 return;
47 }
48
49 Double_t x[3], y[3];
50 for (Int_t i = 0; i < allCoords.size(); i += 3) {
51 x[0] = allCoords[i + 0].first;
52 y[0] = allCoords[i + 0].second;
53 x[1] = allCoords[i + 1].first;
54 y[1] = allCoords[i + 1].second;
55 x[2] = allCoords[i + 2].first;
56 y[2] = allCoords[i + 2].second;
57 th2p->AddBin(3, x, y);
58 avg->AddBin(3, x, y);
59 err->AddBin(3, x, y);
60 }
61
62 // -------------------- Generate particles ------------------------
63 TRandom ran;
64 for (int j = 0; j < numEvents; ++j) {
65 Double_t r1 = ran.Gaus(0, 10);
66 Double_t r2 = ran.Gaus(0, 8);
67 Double_t rok = ran.Gaus(20, 2);
68 Double_t rbad1 = ran.Gaus(1, 2);
69 Double_t rbad2 = ran.Gaus(2, 0);
70
71 Double_t val = rok;
72 // -------------------- Malfunctioning panels -------------------
73 if (th2p->IsInsideBin(4, r1, r2)) val = rok - rbad1;
74 if (th2p->IsInsideBin(20, r1, r2)) val = rok - rbad2;
75 if (th2p->IsInsideBin(13, r1, r2)) val = rok + rbad1;
76 if (th2p->IsInsideBin(37, r1, r2)) val = rok + rbad2;
77
78 // -------------------- Fill histograms ------------------------
79 th2p->Fill(r1, r2, val);
80 avg->Fill(r1, r2, val);
81 err->Fill(r1, r2, val);
82 }
83
84 // -------------------- Display end state ------------------------
85 c1->cd(1);
86 th2p->SetStats(0);
87 th2p->SetTitle("total hits");
88 th2p->Draw("COLZ");
89
90 c1->cd(2);
91 avg->SetStats(0);
92 avg->SetTitle("average charge");
93 avg->Draw("COLZ");
94
95 c1->cd(3);
96 err->SetStats(0);
97 err->SetContentToError();
98 err->SetTitle("error");
99 err->Draw("COLZ");
100}
int Int_t
Definition: RtypesCore.h:45
double Double_t
Definition: RtypesCore.h:59
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t b
#define gROOT
Definition: TROOT.h:404
The Canvas class.
Definition: TCanvas.h:23
2D Histogram with Polygonal Bins
Definition: TH2Poly.h:66
2D Profile Histogram with Polygonal Bins.
Definition: TProfile2Poly.h:58
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:274
Basic string class.
Definition: TString.h:136
const char * Data() const
Definition: TString.h:369
TString & Append(const char *cs)
Definition: TString.h:564
Double_t y[n]
Definition: legend1.C:17
return c1
Definition: legend1.C:41
Double_t x[n]
Definition: legend1.C:17
auto * a
Definition: textangle.C:12