1 |
/* |
2 |
* LiveTimeAction.cpp |
3 |
* |
4 |
* Created on: 13/lug/2009 |
5 |
* Author: Nicola Mori |
6 |
*/ |
7 |
|
8 |
/*! @file LiveTimeAction.cpp The LiveTimeAction class implementation file. */ |
9 |
|
10 |
#include "LiveTimeAction.h" |
11 |
|
12 |
void LiveTimeAction::_InitHistos(vector<float> &bins) { |
13 |
|
14 |
_bins = bins; |
15 |
|
16 |
// Initializing histograms |
17 |
_textHisto.assign(_bins.size() - 1, 0); |
18 |
_rootHisto.SetName("ltHisto"); |
19 |
_rootHisto.SetTitle(Form("Live time (thr.: %.2f)", _thresholdCoeff)); |
20 |
_rootHisto.GetXaxis()->SetTitle("R [GV]"); |
21 |
_rootHisto.GetYaxis()->SetTitle("Events"); |
22 |
|
23 |
Double_t *auxArray = new Double_t[_bins.size()]; |
24 |
|
25 |
for (unsigned int i = 1; i < _bins.size(); i++) { |
26 |
auxArray[i] = _bins[i]; |
27 |
} |
28 |
|
29 |
_rootHisto.SetBins(_bins.size() - 1, auxArray); |
30 |
|
31 |
delete[] auxArray; |
32 |
} |
33 |
|
34 |
LiveTimeAction::LiveTimeAction(const char *actionName, TString outFileBase, vector<float> &bins, float thresholdCoeff) : |
35 |
CollectionAction(actionName), _outFileBase(outFileBase), _bins(0), _rootHisto(), _textHisto(0), _thresholdCoeff( |
36 |
thresholdCoeff), _totalNorm(0.), _infBin(0.), _nGoodHisto(0), _nGoodInfBin(0) |
37 |
#ifdef DEBUGPAMCUT |
38 |
, _outUp(0), _outDown(0) |
39 |
#endif |
40 |
{ |
41 |
_InitHistos(bins); |
42 |
} |
43 |
|
44 |
LiveTimeAction::LiveTimeAction(const char *actionName, TString outFileBase, TString rigBinsFile, float thresholdCoeff) : |
45 |
CollectionAction(actionName), _outFileBase(outFileBase), _bins(0), _rootHisto(), _textHisto(0), _thresholdCoeff( |
46 |
thresholdCoeff), _totalNorm(0.), _infBin(0.), _nGoodHisto(0), _nGoodInfBin(0) |
47 |
#ifdef DEBUGPAMCUT |
48 |
, _outUp(0), _outDown(0) |
49 |
#endif |
50 |
{ |
51 |
|
52 |
// Reading the bins from file |
53 |
ifstream rigBinListFile; |
54 |
rigBinListFile.open(rigBinsFile); |
55 |
|
56 |
TString auxString; |
57 |
vector<float> bins(0); |
58 |
while (!rigBinListFile.eof()) { |
59 |
rigBinListFile >> auxString; |
60 |
if (auxString != "") { |
61 |
bins.push_back(auxString.Atof()); |
62 |
} |
63 |
} |
64 |
rigBinListFile.close(); |
65 |
|
66 |
_InitHistos(bins); |
67 |
|
68 |
} |
69 |
|
70 |
void LiveTimeAction::OnGood(PamLevel2 *event) { |
71 |
|
72 |
double lt = 0.16 * (double) event->GetTrigLevel2()->dltime[0] / 1000.; // In seconds |
73 |
|
74 |
// cout << 0.16 * event->GetTrigLevel2()->dltime[0] / 1000. << endl; |
75 |
// cout << lt << endl; |
76 |
|
77 |
float cRig = _thresholdCoeff * event->GetOrbitalInfo()->GetCutoffSVL(); |
78 |
if (cRig >= _bins[0]) { |
79 |
int i = 1; |
80 |
int binningSize = _bins.size(); |
81 |
bool found = false; |
82 |
while (!found && i < binningSize) { |
83 |
if (cRig < _bins[i]) // less than bin[i-1] maximum edge |
84 |
found = true; |
85 |
else |
86 |
i++; |
87 |
} |
88 |
if (found) { |
89 |
_totalNorm += lt; // use double for sums of many real numbers |
90 |
_textHisto[i - 1] += lt; // use double for sums of many real numbers |
91 |
_rootHisto.Fill(cRig, lt); |
92 |
_nGoodHisto++; |
93 |
} |
94 |
|
95 |
#ifdef DEBUGPAMCUT |
96 |
else |
97 |
_outUp++; |
98 |
#endif |
99 |
} |
100 |
|
101 |
else if (cRig > 0.) { |
102 |
_infBin += lt; |
103 |
_nGoodInfBin++; |
104 |
#ifdef DEBUGPAMCUT |
105 |
_outDown++; |
106 |
#endif |
107 |
} |
108 |
} |
109 |
|
110 |
void LiveTimeAction::Finalize() { |
111 |
|
112 |
if (_outFileBase != "") { |
113 |
|
114 |
// Write the text file |
115 |
ofstream outTextFile((_outFileBase + "-" + GetName() + ".txt").Data(), ios_base::out); |
116 |
streamsize oldPrec = cout.precision(); |
117 |
streamsize newPrec = 4; |
118 |
outTextFile.precision(newPrec); |
119 |
outTextFile.setf(ios::fixed, ios::floatfield); |
120 |
for (unsigned int i = 0; i < _textHisto.size(); i++) |
121 |
outTextFile << _textHisto[i] << "\n"; |
122 |
outTextFile.close(); |
123 |
outTextFile.open((_outFileBase + "-" + GetName() + "-InfBin.txt").Data(), ios_base::out); |
124 |
outTextFile << GetLTInfBin() << endl; |
125 |
outTextFile.close(); |
126 |
outTextFile.precision(oldPrec); |
127 |
outTextFile << resetiosflags(ios::floatfield); |
128 |
|
129 |
// Write the ROOT file |
130 |
TFile outRootFile((_outFileBase + "-" + GetName() + ".root"), "RECREATE"); |
131 |
outRootFile.cd(); |
132 |
_rootHisto.Write(); |
133 |
outRootFile.Close(); |
134 |
|
135 |
} |
136 |
|
137 |
#ifdef DEBUGPAMCUT |
138 |
cout << "Debug informations from " << GetName() << ":\n"; |
139 |
cout << " Events below the lowest rigidity: " << _outDown << "\n"; |
140 |
cout << " Events above the highest rigidity: " << _outUp << endl; |
141 |
#endif |
142 |
} |