-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotRate.C
More file actions
92 lines (68 loc) · 2.4 KB
/
plotRate.C
File metadata and controls
92 lines (68 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <fstream>
#include <TH1F.h>
#include <TFile.h>
#include <TTree.h>
#include <TGraph.h>
#include <TCanvas.h>
#include <TVectorT.h>
using namespace std;
void helper(){
printf("Usage: ./plotRate listOfAnalyzedFiles.txt\n");
exit(-1);
}
void FILE_NOT_FOUND(const char *filename){
printf("Could not find %s, exiting the program.\n", filename);
exit(-1);
}
void plotRate(char* filename){
// Create a graph
TGraph *graph = new TGraph();
int fileCounter = 1;
// Open txt file
ifstream _inFile;
_inFile.open(filename);
if (!_inFile.is_open()) FILE_NOT_FOUND(filename);
string line;
while (getline(_inFile, line)){
// Open rootfile
TFile* _rootfile = new TFile(line.c_str());
if (_rootfile->IsZombie()) FILE_NOT_FOUND(line.c_str());
// Get Tree
auto t_selectedEvts = (TTree*) _rootfile->Get("SelectedEvents");
// Create histogram
auto hEnergySelectedEvents = new TH1D("hEnergySelectedEvents", "", 1000, 0, 100);
// Fill histogram
t_selectedEvts->Draw("E >> hEnergySelectedEvents", "", "goff");
// Get runtime
double runtime = ((TVectorT<double> *) _rootfile->Get("runtime"))->Max();
double binWidth = hEnergySelectedEvents->GetBinWidth(1);
//double aliveSeg = 126;
double scaling = 1 / (runtime * binWidth); //* aliveSeg * 24.2);
hEnergySelectedEvents->Scale(scaling);
// Integration start and end energy
double startEnergy = 15;
double endEnergy = 50;
// Calculate the integration bin
int binMin = hEnergySelectedEvents->GetXaxis()->FindBin(startEnergy);
int binMax = hEnergySelectedEvents->GetXaxis()->FindBin(endEnergy);
binMax = (binMax > hEnergySelectedEvents->GetNbinsX()) ? hEnergySelectedEvents->GetNbinsX() : binMax;
// Integrate
double rate = hEnergySelectedEvents->Integral(binMin, binMax, "width");
graph->SetPoint(fileCounter -1, fileCounter, rate);
fileCounter++;
// Close file to avoid memory leak
_rootfile->Close();
}
// Free memory
_inFile.close();
TCanvas *c = new TCanvas("c", "", 800, 500);
graph->Draw();
c->SaveAs("RateMuon.pdf");
}
int main(int argc, char* argv[]){
// Check the number of argument
if (argc != 2) helper();
plotRate(argv[1]);
return 0;
}