Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist042_TProfile2Poly_module_error.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/// \date November 2022
10/// \author Filip Ilic
11
12#include <iostream>
13#include <fstream>
14#include <vector>
15
17{
18 TCanvas *c1 = new TCanvas("c1", "4 Malfunctioning Panels", 800, 400);
19 c1->Divide(3, 1);
20
21 // -------------------- Construct detector bins ------------------------
22 auto th2p = new TH2Poly();
23 auto avg = new TProfile2Poly();
24 auto err = new TProfile2Poly();
25
26 std::ifstream infile;
27 TString dir = gROOT->GetTutorialDir();
28 dir.Append("/hist/data/tprofile2poly_tutorial.data");
29 infile.open(dir.Data());
30
31 if (!infile) // Verify that the file was open successfully
32 {
33 std::cerr << dir.Data() << std::endl; // Report error
34 std::cerr << "Error code: " << std::strerror(errno) << std::endl; // Get some info as to why
35 return;
36 }
37
38 std::vector<std::pair<Double_t, Double_t>> allCoords;
39 Double_t a, b;
40 while (infile >> a >> b)
41 allCoords.emplace_back(a, b);
42
43 if (allCoords.size() % 3 != 0) {
44 std::cout << "[ERROR] Bad file" << std::endl;
45 return;
46 }
47
48 Double_t x[3], y[3];
49 for (Int_t i = 0; i < allCoords.size(); i += 3) {
50 x[0] = allCoords[i + 0].first;
51 y[0] = allCoords[i + 0].second;
52 x[1] = allCoords[i + 1].first;
53 y[1] = allCoords[i + 1].second;
54 x[2] = allCoords[i + 2].first;
55 y[2] = allCoords[i + 2].second;
56 th2p->AddBin(3, x, y);
57 avg->AddBin(3, x, y);
58 err->AddBin(3, x, y);
59 }
60
61 // -------------------- Generate particles ------------------------
63 for (int j = 0; j < numEvents; ++j) {
64 Double_t r1 = ran.Gaus(0, 10);
65 Double_t r2 = ran.Gaus(0, 8);
66 Double_t rok = ran.Gaus(20, 2);
67 Double_t rbad1 = ran.Gaus(1, 2);
68 Double_t rbad2 = ran.Gaus(2, 0);
69
70 Double_t val = rok;
71 // -------------------- Malfunctioning panels -------------------
72 if (th2p->IsInsideBin(4, r1, r2))
73 val = rok - rbad1;
74 if (th2p->IsInsideBin(20, r1, r2))
75 val = rok - rbad2;
76 if (th2p->IsInsideBin(13, r1, r2))
77 val = rok + rbad1;
78 if (th2p->IsInsideBin(37, r1, r2))
79 val = rok + rbad2;
80
81 // -------------------- Fill histograms ------------------------
82 th2p->Fill(r1, r2, val);
83 avg->Fill(r1, r2, val);
84 err->Fill(r1, r2, val);
85 }
86
87 // -------------------- Display end state ------------------------
88 c1->cd(1);
89 th2p->SetStats(0);
90 th2p->SetTitle("total hits");
91 th2p->Draw("COLZ");
92
93 c1->cd(2);
94 avg->SetStats(0);
95 avg->SetTitle("average charge");
96 avg->Draw("COLZ");
97
98 c1->cd(3);
99 err->SetStats(0);
100 err->SetContentToError();
101 err->SetTitle("error");
102 err->Draw("COLZ");
103}
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gROOT
Definition TROOT.h:406
The Canvas class.
Definition TCanvas.h:23
2D Histogram with Polygonal Bins
Definition TH2Poly.h:66
2D Profile Histogram with Polygonal Bins.
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:376
TString & Append(const char *cs)
Definition TString.h:572
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17