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>
13#include <vector>
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 std::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: " << std::strerror(errno) << std::endl; // Get some info as to why
34 return;
35 }
36
37 std::vector<std::pair<Double_t, Double_t>> allCoords;
38 Double_t a, b;
39 while (infile >> a >> b)
40 allCoords.emplace_back(a, b);
41
42 if (allCoords.size() % 3 != 0) {
43 std::cout << "[ERROR] Bad file" << std::endl;
44 return;
45 }
46
47 Double_t x[3], y[3];
48 for (Int_t i = 0; i < allCoords.size(); i += 3) {
49 x[0] = allCoords[i + 0].first;
50 y[0] = allCoords[i + 0].second;
51 x[1] = allCoords[i + 1].first;
52 y[1] = allCoords[i + 1].second;
53 x[2] = allCoords[i + 2].first;
54 y[2] = allCoords[i + 2].second;
55 th2p->AddBin(3, x, y);
56 avg->AddBin(3, x, y);
57 err->AddBin(3, x, y);
58 }
59
60 // -------------------- Generate particles ------------------------
61 TRandom ran;
62 for (int j = 0; j < numEvents; ++j) {
63 Double_t r1 = ran.Gaus(0, 10);
64 Double_t r2 = ran.Gaus(0, 8);
65 Double_t rok = ran.Gaus(20, 2);
66 Double_t rbad1 = ran.Gaus(1, 2);
67 Double_t rbad2 = ran.Gaus(2, 0);
68
69 Double_t val = rok;
70 // -------------------- Malfunctioning panels -------------------
71 if (th2p->IsInsideBin(4, r1, r2)) val = rok - rbad1;
72 if (th2p->IsInsideBin(20, r1, r2)) val = rok - rbad2;
73 if (th2p->IsInsideBin(13, r1, r2)) val = rok + rbad1;
74 if (th2p->IsInsideBin(37, r1, r2)) val = rok + rbad2;
75
76 // -------------------- Fill histograms ------------------------
77 th2p->Fill(r1, r2, val);
78 avg->Fill(r1, r2, val);
79 err->Fill(r1, r2, val);
80 }
81
82 // -------------------- Display end state ------------------------
83 c1->cd(1);
84 th2p->SetStats(0);
85 th2p->SetTitle("total hits");
86 th2p->Draw("COLZ");
87
88 c1->cd(2);
89 avg->SetStats(0);
90 avg->SetTitle("average charge");
91 avg->Draw("COLZ");
92
93 c1->cd(3);
94 err->SetStats(0);
95 err->SetContentToError();
96 err->SetTitle("error");
97 err->Draw("COLZ");
98}
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
TArc a
Definition: textangle.C:12