1 |
pam-fi |
1.1 |
/* |
2 |
|
|
* Histo1DAction.h |
3 |
|
|
* |
4 |
|
|
* Created on: 25/ago/2009 |
5 |
|
|
* Author: Nicola Mori |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
/*! @file Histo1DAction.h The Histo1DAction class declaration file. */ |
9 |
|
|
|
10 |
|
|
#ifndef HISTO1DACTION_H_ |
11 |
|
|
#define HISTO1DACTION_H_ |
12 |
|
|
|
13 |
|
|
#include "../../CollectionAction/CollectionAction.h" |
14 |
|
|
|
15 |
|
|
#include <TH1I.h> |
16 |
|
|
#include <TH1F.h> |
17 |
|
|
#include <TH1D.h> |
18 |
|
|
|
19 |
|
|
/*! @brief An abstract action that fills a 1-dimensional histogram. |
20 |
|
|
* |
21 |
|
|
* This abstract class provides all the common methods needed to produce 1-dimensional histograms, |
22 |
|
|
* like reading the bins from a file, setting up the root histogram and so on. It handles histograms |
23 |
|
|
* both as a ROOT histogram and as a vector. The OnGood() method has to be implemented in children |
24 |
|
|
* classes, with the specific physical variable to fill the histogram. Eg., a rigidity histogram class |
25 |
|
|
* derived from Histo1DAction could have this OnGood() implementation: |
26 |
|
|
* |
27 |
|
|
* Fill(event->GetTrack(0)->GetTrkTrack()->GetRigidity()); |
28 |
|
|
* |
29 |
|
|
* Fill() automatically fills the ROOT and the vector histogram. |
30 |
|
|
* The template argument HistoType is the type of histogram (Int_t, Double_t,...). In current |
31 |
|
|
* implementation, ROOT histograms are only supported for Int_t, Float_t and Double_t. For all the |
32 |
|
|
* other types, only the the vector histogram with text file output will be produced. If you want to |
33 |
|
|
* implement some new types of ROOT histograms, please specialize the template definitions of _CreateHisto() |
34 |
|
|
* and ~Histo1DAction() (see the specializations in the .cpp file). |
35 |
|
|
* |
36 |
|
|
* */ |
37 |
|
|
template<class HistoType> |
38 |
|
|
class Histo1DAction: public CollectionAction { |
39 |
|
|
public: |
40 |
|
|
|
41 |
|
|
/*! @brief Constructor. |
42 |
|
|
* |
43 |
|
|
* Binning is read from a text file. |
44 |
|
|
* |
45 |
|
|
* @param actionName The action's name. |
46 |
|
|
* @param title The ROOT histogram title. |
47 |
|
|
* @param outFileBase The file base name for the histogram output. If "", no output file will be produced. |
48 |
|
|
* @param mode The mode of ROOT file creation (see documentation of TFile constructor |
49 |
|
|
* in ROOT's reference guide). |
50 |
|
|
* @param outRoot If true, an output ROOT file named outFileBase + ".root" will be produced. |
51 |
|
|
* @param outText If true, an output text file named outFileBase + ".txt" will be produced. It will overwrite an |
52 |
|
|
* eventually existing file with the same name. |
53 |
|
|
*/ |
54 |
|
|
Histo1DAction(const char *actionName, TString title, TString outFileBase = "", TString mode = "UPDATE", bool outRoot = |
55 |
|
|
false, bool outText = false); |
56 |
|
|
|
57 |
|
|
/*! @brief Destructor |
58 |
|
|
* |
59 |
|
|
* This has to be specialized for each kind of ROOT histogram. |
60 |
|
|
* |
61 |
|
|
* */ |
62 |
|
|
~Histo1DAction(); |
63 |
|
|
|
64 |
|
|
/*! @brief Sets the X axis using a binning read from a a vector. |
65 |
|
|
* |
66 |
|
|
* @param label The axis' label. |
67 |
|
|
* @param bins A vector containing the histogram binning (with sign, eventually; from lowest to highest). |
68 |
|
|
*/ |
69 |
|
|
void SetXAxis(TString label, vector<float> &bins); |
70 |
|
|
/*! @brief Sets the X axis reading the binning from a file. |
71 |
|
|
* |
72 |
|
|
* @param label The axis' label. |
73 |
|
|
* @param binsFile The file containing the bins (with sign, eventually; from lowest to highest). |
74 |
|
|
*/ |
75 |
|
|
void SetXAxis(TString label, TString binsFile); |
76 |
|
|
/*! @brief Sets the X axis specifying the binning parameters. |
77 |
|
|
* |
78 |
|
|
* @param label The axis' label. |
79 |
|
|
* @param nBins The number of bins. |
80 |
|
|
* @param min The lower limit of the axis. |
81 |
|
|
* @param max The upper limit of the axis. |
82 |
|
|
* @param logBinning If true, bins will be logarithmically spaced. |
83 |
|
|
*/ |
84 |
|
|
void SetXAxis(TString label, unsigned int nBins, float min, float max, bool logBinning = false); |
85 |
|
|
|
86 |
|
|
/*! @brief Sets up the histogram |
87 |
|
|
* |
88 |
pam-fi |
1.2 |
* This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis(). |
89 |
pam-fi |
1.1 |
* |
90 |
|
|
* @param events Pointer to PamLevel2 events (unused). |
91 |
|
|
*/ |
92 |
|
|
void Setup(PamLevel2 *events) { |
93 |
|
|
CollectionAction::Setup(events); |
94 |
|
|
_InitHistos(); |
95 |
|
|
|
96 |
|
|
} |
97 |
|
|
/*! @ brief Post-analysis tasks. |
98 |
|
|
* |
99 |
|
|
* This method writes the output on a ROOT file if the proper option was set in the constructor. |
100 |
|
|
*/ |
101 |
|
|
void Finalize(); |
102 |
|
|
|
103 |
|
|
/*! @brief Returns the histogram. |
104 |
|
|
* |
105 |
|
|
* @return A reference to a vector containing the values of the bins of the histogram. |
106 |
|
|
*/ |
107 |
|
|
vector<HistoType> &GetHisto() { |
108 |
|
|
return _histo; |
109 |
|
|
} |
110 |
|
|
|
111 |
pam-fi |
1.2 |
/*! Fills the ROOT and the vector histogram. |
112 |
|
|
* |
113 |
|
|
* @param value The value of the X coordinate associated to the event. |
114 |
|
|
* @param weight The weight which will be applied to the event. |
115 |
|
|
*/ |
116 |
pam-fi |
1.1 |
void Fill(double value, double weight = 1.); |
117 |
|
|
|
118 |
|
|
/*! @brief The number of events which fell below the lower histogram limit. */ |
119 |
|
|
HistoType GetOverflow() { |
120 |
|
|
return _overflow; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
/*! @brief The number of events which fell above the upper histogram limit. */ |
124 |
|
|
HistoType GetUnderflow() { |
125 |
|
|
return _underflow; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
protected: |
129 |
|
|
|
130 |
pam-fi |
1.2 |
/*! @brief The vector containing the limits of the bins(from lower to higher). */ |
131 |
pam-fi |
1.1 |
std::vector<float> _bins; |
132 |
pam-fi |
1.2 |
/*! @brief A vector containing the value of the histogram for each bin. */ |
133 |
pam-fi |
1.1 |
vector<HistoType> _histo; |
134 |
pam-fi |
1.2 |
/*! @brief The ROOT histogram. */ |
135 |
pam-fi |
1.1 |
TH1 *_rootHisto; |
136 |
|
|
|
137 |
|
|
private: |
138 |
|
|
|
139 |
|
|
unsigned int _underflow, _overflow; |
140 |
|
|
|
141 |
|
|
TString _outFileBase; |
142 |
|
|
TString _mode; |
143 |
|
|
TString _title, _xLabel; |
144 |
|
|
bool _outRoot; |
145 |
|
|
bool _outText; |
146 |
|
|
|
147 |
|
|
void _CreateHisto(); |
148 |
|
|
void _InitHistos(); |
149 |
|
|
}; |
150 |
|
|
|
151 |
|
|
template<class HistoType> |
152 |
|
|
void Histo1DAction<HistoType>::_CreateHisto() { |
153 |
|
|
|
154 |
|
|
// No ROOT histogram for generic type; see template specializations in .cpp file. |
155 |
|
|
_rootHisto = NULL; |
156 |
|
|
} |
157 |
pam-fi |
1.3 |
// Specializations for _CreateHistos(). See Histo1DAction.cpp |
158 |
|
|
template<> |
159 |
|
|
void Histo1DAction<Int_t>::_CreateHisto(); |
160 |
|
|
|
161 |
|
|
template<> |
162 |
|
|
void Histo1DAction<Float_t>::_CreateHisto(); |
163 |
|
|
|
164 |
|
|
template<> |
165 |
|
|
void Histo1DAction<Double_t>::_CreateHisto(); |
166 |
pam-fi |
1.1 |
|
167 |
|
|
template<class HistoType> |
168 |
|
|
void Histo1DAction<HistoType>::_InitHistos() { |
169 |
|
|
|
170 |
|
|
_CreateHisto(); |
171 |
|
|
|
172 |
|
|
if (_rootHisto) { |
173 |
|
|
Double_t *auxArray = new Double_t[_bins.size()]; |
174 |
|
|
|
175 |
|
|
for (unsigned int i = 0; i < _bins.size(); i++) { |
176 |
|
|
auxArray[i] = _bins[i]; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
_rootHisto->SetBins(_bins.size() - 1, auxArray); |
180 |
|
|
_rootHisto->SetName(GetName()); |
181 |
|
|
_rootHisto->SetTitle(_title); |
182 |
|
|
_rootHisto->SetXTitle(_xLabel); |
183 |
|
|
_rootHisto->SetYTitle("Events"); |
184 |
|
|
|
185 |
|
|
delete[] auxArray; |
186 |
|
|
|
187 |
|
|
} |
188 |
|
|
|
189 |
|
|
_histo.resize(_bins.size() - 1); |
190 |
|
|
|
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
template<class HistoType> |
194 |
|
|
Histo1DAction<HistoType>::~Histo1DAction() { |
195 |
pam-fi |
1.3 |
|
196 |
|
|
delete _rootHisto; |
197 |
|
|
_rootHisto = NULL; |
198 |
|
|
|
199 |
pam-fi |
1.1 |
} |
200 |
|
|
|
201 |
|
|
template<class HistoType> |
202 |
|
|
Histo1DAction<HistoType>::Histo1DAction(const char *actionName, TString title, TString outFileBase, TString mode, |
203 |
|
|
bool outRoot, bool outText) : |
204 |
|
|
CollectionAction(actionName), _bins(0), _histo(0), _rootHisto(NULL), _outFileBase(outFileBase), _mode(mode), _title( |
205 |
|
|
title), _xLabel(""), _outRoot(outRoot), _outText(outText) { |
206 |
|
|
|
207 |
|
|
} |
208 |
|
|
|
209 |
|
|
template<class HistoType> |
210 |
|
|
void Histo1DAction<HistoType>::SetXAxis(TString label, vector<float> &bins) { |
211 |
|
|
|
212 |
|
|
_bins = bins; |
213 |
|
|
_xLabel = label; |
214 |
|
|
|
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
template<class HistoType> |
218 |
|
|
void Histo1DAction<HistoType>::SetXAxis(TString label, TString binsFile) { |
219 |
|
|
|
220 |
|
|
// Reading the bins from file |
221 |
|
|
ifstream binListFile; |
222 |
|
|
binListFile.open(binsFile); |
223 |
|
|
|
224 |
|
|
TString auxString; |
225 |
|
|
_bins.resize(0); |
226 |
|
|
while (!binListFile.eof()) { |
227 |
|
|
binListFile >> auxString; |
228 |
|
|
if (auxString != "") { |
229 |
|
|
_bins.push_back(auxString.Atof()); |
230 |
|
|
} |
231 |
|
|
} |
232 |
|
|
binListFile.close(); |
233 |
|
|
|
234 |
|
|
_xLabel = label; |
235 |
|
|
|
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
template<class HistoType> |
239 |
|
|
void Histo1DAction<HistoType>::SetXAxis(TString label, unsigned int nBins, float min, float max, bool logBinning) { |
240 |
|
|
|
241 |
|
|
_bins.resize(nBins + 1); |
242 |
|
|
|
243 |
|
|
if (!logBinning || (logBinning && min <= 0.)) { |
244 |
|
|
|
245 |
|
|
#ifdef DEBUGPAMCUT |
246 |
|
|
if (logbinning && rigMin <= 0.) |
247 |
|
|
cout << "Warning: logarithmic binning was chosen for X axis but min <= 0. Using linear binning." |
248 |
|
|
#endif |
249 |
|
|
|
250 |
|
|
float step = (max - min) / nBins; |
251 |
|
|
for (unsigned int i = 0; i < nBins + 1; i++) { |
252 |
|
|
_bins[i] = min + i * step; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
} |
256 |
|
|
else { |
257 |
|
|
|
258 |
|
|
double maxExp = log10(max / min); |
259 |
|
|
for (unsigned int i = 0; i < nBins + 1; i++) { |
260 |
|
|
_bins[i] = min * pow(10., (double) i / ((double) nBins) * maxExp); |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
_xLabel = label; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
template<class HistoType> |
269 |
|
|
inline void Histo1DAction<HistoType>::Fill(double value, double weight) { |
270 |
|
|
|
271 |
|
|
_rootHisto->Fill(value, weight); |
272 |
|
|
|
273 |
|
|
if (value < _bins[0]) { |
274 |
|
|
_underflow += (HistoType) weight; |
275 |
|
|
return; |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
if (value > _bins[_bins.size() - 1]) { |
279 |
|
|
_overflow += (HistoType) weight; |
280 |
|
|
return; |
281 |
|
|
} |
282 |
|
|
|
283 |
|
|
int xBin = 1; |
284 |
|
|
while (value >= _bins[xBin]) { |
285 |
|
|
xBin++; |
286 |
|
|
} |
287 |
|
|
xBin--; |
288 |
|
|
|
289 |
|
|
_histo[xBin] += (HistoType) weight; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
template<class HistoType> |
293 |
|
|
void Histo1DAction<HistoType>::Finalize() { |
294 |
|
|
|
295 |
|
|
if (_outFileBase != "") { |
296 |
|
|
// Write the ROOT file |
297 |
|
|
if (_rootHisto && _outRoot) { |
298 |
|
|
TFile outRootFile((_outFileBase + ".root"), _mode); |
299 |
|
|
outRootFile.cd(); |
300 |
|
|
_rootHisto->Write(); |
301 |
|
|
outRootFile.Close(); |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
//Write the text file |
305 |
|
|
if (_outText) { |
306 |
|
|
ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out); |
307 |
|
|
for (unsigned int i = 0; i < _histo.size(); i++) { |
308 |
|
|
outTextFile << _histo[i] << "\n"; |
309 |
|
|
} |
310 |
|
|
outTextFile << endl; |
311 |
|
|
outTextFile.close(); |
312 |
|
|
} |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
} |
316 |
|
|
#endif /* HISTO1DACTION_H_ */ |