/[PAMELA software]/PamCut/CollectionActions/Histo1DActions/Histo1DAction/Histo1DAction.h
ViewVC logotype

Contents of /PamCut/CollectionActions/Histo1DActions/Histo1DAction/Histo1DAction.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations) (download)
Tue Mar 23 17:27:38 2010 UTC (14 years, 8 months ago) by pam-fi
Branch: MAIN
CVS Tags: Root_V8, MergedToHEAD_1, nuclei_reproc, MergedFromV8_1, BeforeMergingFromV8_1, V9, HEAD
Branch point for: V8
Changes since 1.5: +6 -1 lines
File MIME type: text/plain
Documentation updated.

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 * This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis().
89 *
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 /*! 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 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 /*! @brief The vector containing the limits of the bins(from lower to higher). */
131 std::vector<float> _bins;
132 /*! @brief A vector containing the value of the histogram for each bin. */
133 vector<HistoType> _histo;
134 /*! @brief The ROOT histogram. */
135 TH1 *_rootHisto;
136
137 /*! @brief Base name of the output file. */
138 TString _outFileBase;
139 /*! @brief Output file open mode (UPDATE or RECREATE, see documentation of TFile). */
140 TString _mode;
141 /*! @brief Title for the ROOT histogram. */
142 TString _title;
143 /*! @brief Axis labels for the ROOT histogram. */
144 TString _xLabel;
145
146 private:
147
148 HistoType _underflow, _overflow;
149 bool _outRoot;
150 bool _outText;
151 void _CreateHisto();
152 void _InitHistos();
153 };
154
155 template<class HistoType>
156 void Histo1DAction<HistoType>::_CreateHisto() {
157
158 // No ROOT histogram for generic type; see template specializations in .cpp file.
159 _rootHisto = NULL;
160 }
161 // Specializations for _CreateHistos(). See Histo1DAction.cpp
162 template<>
163 void Histo1DAction<Int_t>::_CreateHisto();
164
165 template<>
166 void Histo1DAction<Float_t>::_CreateHisto();
167
168 template<>
169 void Histo1DAction<Double_t>::_CreateHisto();
170
171 template<class HistoType>
172 void Histo1DAction<HistoType>::_InitHistos() {
173
174 _CreateHisto();
175 if (_bins.size() < 2) // SetXAxis not called by the main program, or wrongly filled (only 1 bin limit)
176 SetXAxis("Default X", 10, 0., 1.);
177
178 if (_rootHisto) {
179 Double_t *auxArray = new Double_t[_bins.size()];
180
181 for (unsigned int i = 0; i < _bins.size(); i++) {
182 auxArray[i] = _bins[i];
183 }
184
185 _rootHisto->SetBins(_bins.size() - 1, auxArray);
186 _rootHisto->SetName(GetName());
187 _rootHisto->SetTitle(_title);
188 _rootHisto->SetXTitle(_xLabel);
189 _rootHisto->SetYTitle("Events");
190
191 delete[] auxArray;
192
193 }
194
195 _histo.resize(_bins.size() - 1);
196
197 }
198
199 template<class HistoType>
200 Histo1DAction<HistoType>::~Histo1DAction() {
201
202 delete _rootHisto;
203 _rootHisto = NULL;
204
205 }
206
207 template<class HistoType>
208 Histo1DAction<HistoType>::Histo1DAction(const char *actionName, TString title, TString outFileBase, TString mode,
209 bool outRoot, bool outText) :
210 CollectionAction(actionName), _bins(0), _histo(0), _rootHisto(NULL), _outFileBase(outFileBase), _mode(mode), _title(
211 title), _xLabel(""), _underflow(0), _overflow(0), _outRoot(outRoot), _outText(outText) {
212
213 }
214
215 template<class HistoType>
216 void Histo1DAction<HistoType>::SetXAxis(TString label, vector<float> &bins) {
217
218 _bins = bins;
219 _xLabel = label;
220
221 }
222
223 template<class HistoType>
224 void Histo1DAction<HistoType>::SetXAxis(TString label, TString binsFile) {
225
226 // Reading the bins from file
227 ifstream binListFile;
228 binListFile.open(binsFile);
229
230 TString auxString;
231 _bins.resize(0);
232 while (!binListFile.eof()) {
233 binListFile >> auxString;
234 if (auxString != "") {
235 _bins.push_back(auxString.Atof());
236 }
237 }
238 binListFile.close();
239
240 _xLabel = label;
241
242 }
243
244 template<class HistoType>
245 void Histo1DAction<HistoType>::SetXAxis(TString label, unsigned int nBins, float min, float max, bool logBinning) {
246
247 _bins.resize(nBins + 1);
248
249 if (!logBinning || (logBinning && min <= 0.)) {
250
251 #ifdef DEBUGPAMCUT
252 if (logbinning && rigMin <= 0.)
253 cout << "Warning: logarithmic binning was chosen for X axis but min <= 0. Using linear binning."
254 #endif
255
256 float step = (max - min) / nBins;
257 for (unsigned int i = 0; i < nBins + 1; i++) {
258 _bins[i] = min + i * step;
259 }
260
261 }
262 else {
263
264 double maxExp = log10(max / min);
265 for (unsigned int i = 0; i < nBins + 1; i++) {
266 _bins[i] = min * pow(10., (double) i / ((double) nBins) * maxExp);
267 }
268
269 }
270
271 _xLabel = label;
272 }
273
274 template<class HistoType>
275 inline void Histo1DAction<HistoType>::Fill(double value, double weight) {
276
277 _rootHisto->Fill(value, weight);
278
279 if (value < _bins[0]) {
280 _underflow += (HistoType) weight;
281 return;
282 }
283
284 if (value > _bins[_bins.size() - 1]) {
285 _overflow += (HistoType) weight;
286 return;
287 }
288
289 int xBin = 1;
290 while (value >= _bins[xBin]) {
291 xBin++;
292 }
293 xBin--;
294
295 _histo[xBin] += (HistoType) weight;
296 }
297
298 template<class HistoType>
299 void Histo1DAction<HistoType>::Finalize() {
300
301 if (_outFileBase != "") {
302 // Write the ROOT file
303 if (_rootHisto && _outRoot) {
304 TFile outRootFile((_outFileBase + ".root"), _mode);
305 outRootFile.cd();
306 _rootHisto->Write();
307 outRootFile.Close();
308 }
309
310 //Write the text file
311 if (_outText) {
312 ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out);
313 for (unsigned int i = 0; i < _histo.size(); i++) {
314 outTextFile << _histo[i] << "\n";
315 }
316 outTextFile << endl;
317 outTextFile.close();
318 }
319 }
320
321 }
322 #endif /* HISTO1DACTION_H_ */

  ViewVC Help
Powered by ViewVC 1.1.23