/[PAMELA software]/PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h
ViewVC logotype

Annotation of /PamCut/CollectionActions/Histo2DActions/Histo2DAction/Histo2DAction.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Thu Oct 29 09:42:44 2009 UTC (15 years, 1 month ago) by pam-fi
Branch: MAIN
Changes since 1.2: +5 -2 lines
File MIME type: text/plain
Some fixes.

1 pam-fi 1.1 /*
2     * Histo2DAction.h
3     *
4     * Created on: 01/set/2009
5     * Author: Nicola Mori
6     */
7    
8     /*! @file Histo2DAction.h The Histo2DAction class declaration file. */
9    
10     #ifndef HISTO2DACTION_H_
11     #define HISTO2DACTION_H_
12    
13     #include "../../CollectionAction/CollectionAction.h"
14    
15     #include <TH2I.h>
16     #include <TH2F.h>
17     #include <TH2D.h>
18    
19     /*! @brief An abstract action that fills a 2-dimensional histogram.
20     *
21     * This abstract class provides all the common methods needed to produce 2-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 matrix. The OnGood() method has to be implemented in children
24     * classes, with the specific physical variables to fill the histogram. Eg., a dE/dx vs. rigidity
25     * histogram class derived from Histo2DAction could have this OnGood() implementation:
26     *
27     * Fill(event->GetTrack(0)->GetTrkTrack()->GetRigidity(), event->GetTrack(0)->GetTrkTrack()->GetDEDX());
28     *
29     * Fill() automatically fills the ROOT and the matrix 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 matrix 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 ~Histo2DAction() (see the specializations in the .cpp file).
35     *
36     * */
37     template<class HistoType>
38     class Histo2DAction: public CollectionAction {
39     public:
40    
41     /*! @brief Constructor.
42     *
43     * The histogram binning is defined by the content of the bins vector.
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     Histo2DAction(const char *actionName, TString title = "", TString outFileBase = "", TString mode = "UPDATE",
55     bool outRoot = false, bool outText = false);
56    
57     /*! @brief Destructor
58     *
59     * This has to be specialized for each kind of ROOT histogram.
60     *
61     * */
62     ~Histo2DAction();
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 the Y axis using a binning read from a a vector.
87     *
88     * @param label The axis' label.
89     * @param bins A vector containing the histogram binning (with sign, eventually; from lowest to highest).
90     */
91     void SetYAxis(TString label, vector<float> &bins);
92     /*! @brief Sets the Y axis reading the binning from a file.
93     *
94     * @param label The axis' label.
95     * @param binsFile The file containing the bins (with sign, eventually; from lowest to highest).
96     */
97     void SetYAxis(TString label, TString binsFile);
98    
99     /*! @brief Sets the Y axis specifying the binning parameters.
100     *
101     * @param label The axis' label.
102     * @param nBins The number of bins.
103     * @param min The lower limit of the axis.
104     * @param max The upper limit of the axis.
105     * @param logBinning If true, bins will be logarithmically spaced.
106     */
107     void SetYAxis(TString label, unsigned int nBins, float min, float max, bool logBinning = false);
108    
109 pam-fi 1.2 /*! @brief Sets the ROOT histogram's title.
110     *
111     * @param title The histogram title as it will appear on the histogram itself.
112     */
113     void SetTitle(TString &title) {
114     _title = title;
115     }
116 pam-fi 1.1 /*! @brief Sets up the histogram
117     *
118     * This routine effectively prepares the histogram, after the desired parameters has been set by #SetXAxis() and #SetYAxis().
119     *
120     * @param events Pointer to PamLevel2 events (unused).
121     */
122     void Setup(PamLevel2 *events) {
123     CollectionAction::Setup(events);
124     _InitHistos();
125    
126     }
127    
128     /*! @ brief Post-analysis tasks.
129     *
130     * This method writes the output on a ROOT file if the proper option was set in the constructor.
131     */
132     void Finalize();
133    
134     /*! @brief Returns the histogram.
135     *
136     * Element [i][j] is the value of the i-th bin on the Y axis and on the j-th bin on the X axis. This sort
137     * of index inversion is due to logically maintain the Y values on the vertical axis of the matrix, which
138     * is indexed by the row number (which conventionally is the first index of a matrix element).
139     *
140     * @return A reference to a matrix containing the values of the bins of the histogram.
141     */
142     SimpleMatrix<HistoType> &GetHisto() {
143     return _histo;
144     }
145    
146 pam-fi 1.2 /*! Fills the ROOT and the vector histogram.
147     *
148 pam-fi 1.3 * @param xValue The value of the X coordinate associated to the event.
149     * @param yValue The value of the Y coordinate associated to the event.
150 pam-fi 1.2 * @param weight The weight which will be applied to the event.
151     */
152 pam-fi 1.1 void Fill(double xValue, double yValue, double weight = 1.);
153    
154     /*! @brief Gets the X overflow histogram.
155     *
156     * The returned vector contains the histogram filled with the X overflow events, eg.,
157     * those events whose X variable falls above the upper limit of the X axis. This histogram
158     * is binned like the Y axis.
159     *
160     * @return The X overflow histogram.
161     */
162     vector<HistoType>& GetXOverflow() {
163     return _xOverflow;
164     }
165    
166     /*! @brief Gets the X underflow histogram.
167     *
168     * The returned vector contains the histogram filled with the X underflow events, eg.,
169     * those events whose X variable falls below the lower limit of the X axis. This histogram
170     * is binned like the Y axis.
171     *
172     * @return The X underflow histogram.
173     */
174     vector<HistoType>& GetXUnderflow() {
175     return _xUnderflow;
176     }
177    
178     /*! @brief Gets the Y overflow histogram.
179     *
180     * The returned vector contains the histogram filled with the Y overflow events, eg.,
181     * those events whose Y variable falls above the upper limit of the Y axis. This histogram
182     * is binned like the X axis.
183     *
184     * @return The Y overflow histogram.
185     */
186     vector<HistoType>& GetYOverflow() {
187     return _yOverflow;
188     }
189    
190     /*! @brief Gets the Y underflow histogram.
191     *
192     * The returned vector contains the histogram filled with the Y underflow events, eg.,
193     * those events whose Y variable falls below the lower limit of the Y axis. This histogram
194     * is binned like the X axis.
195     *
196     * @return The Y underflow histogram.
197     */
198     vector<HistoType>& GetYUnderflow() {
199     return _yUnderflow;
200     }
201    
202     /*! @brief Gets the X-Y overflow histogram.
203     *
204     * This single-bin histogram is built with events which fall above the lower limits of both the
205     * X and Y axis.
206     *
207     * @return The X-Y overflow histogram.
208     */
209     HistoType GetXOverYOverflow() {
210     return _xOverYOverflow;
211     }
212    
213     /*! @brief Gets the X-Y underflow histogram.
214     *
215     * This single-bin histogram is built with events fall below the lower limits of both the X and Y axis.
216     *
217     * @return The X-Y overflow histogram.
218     */
219     HistoType GetXUnderYUnderflow() {
220     return _xUnderYUnderflow;
221     }
222     /*! @brief Gets the (X over - Y under)flow histogram.
223     *
224     * This single-bin histogram is built with events fall above the lower limit of the Xaxis and below
225     * that of the Y axis.
226     *
227     * @return The (X over -Y under)flow histogram.
228     */
229     HistoType GetXOverYUnderflow() {
230     return _xOverYUnderflow;
231     }
232    
233     /*! @brief Gets the (X under - Y over)flow histogram.
234     *
235     * This single-bin histogram is built with events fall below the lower limit of the X axis and above
236     * that of the Y axis.
237     *
238     * @return The (X under -Y over)flow histogram.
239     */
240     HistoType GetXUnderYOverflow() {
241     return _xUnderYOverflow;
242     }
243    
244     protected:
245    
246 pam-fi 1.2 /*! @brief The vector containing the limits of the X bins(from lower to higher). */
247 pam-fi 1.1 std::vector<float> _xBins;
248 pam-fi 1.2 /*! @brief The vector containing the limits of the Y bins(from lower to higher). */
249 pam-fi 1.1 std::vector<float> _yBins;
250 pam-fi 1.2 /*! @brief A matrix containing the value of the histogram for each X-Y bin. */
251 pam-fi 1.1 SimpleMatrix<HistoType> _histo;
252 pam-fi 1.2 /*! @brief The ROOT histogram. */
253 pam-fi 1.1 TH2 *_rootHisto;
254    
255     private:
256    
257     vector<HistoType> _xUnderflow, _xOverflow, _yUnderflow, _yOverflow;
258     HistoType _xUnderYUnderflow, _xOverYOverflow, _xUnderYOverflow, _xOverYUnderflow;
259     TString _outFileBase;
260     TString _mode;
261     TString _title, _xLabel, _yLabel;
262     bool _outRoot;
263     bool _outText;
264    
265     void _CreateHisto();
266     void _InitHistos();
267     };
268    
269     template<class HistoType>
270     void Histo2DAction<HistoType>::_CreateHisto() {
271    
272     // No ROOT histogram for generic type; see template specializations in .cpp file.
273     _rootHisto = NULL;
274     }
275    
276     template<class HistoType>
277     void Histo2DAction<HistoType>::_InitHistos() {
278    
279     _CreateHisto();
280    
281 pam-fi 1.2 if (_xBins.size() < 2) {
282 pam-fi 1.1 _xBins.resize(2);
283     _xBins[0] = 0.;
284     _xBins[1] = 1.;
285     }
286    
287 pam-fi 1.2 if (_yBins.size() < 2) {
288 pam-fi 1.1 _yBins.resize(2);
289     _yBins[0] = 0.;
290     _yBins[1] = 1.;
291     }
292    
293     if (_rootHisto) {
294     Double_t *auxXArray = new Double_t[_xBins.size()];
295    
296     for (unsigned int i = 0; i < _xBins.size(); i++) {
297     auxXArray[i] = _xBins[i];
298     }
299    
300     Double_t *auxYArray = new Double_t[_yBins.size()];
301    
302     for (unsigned int i = 0; i < _yBins.size(); i++) {
303     auxYArray[i] = _yBins[i];
304     }
305    
306     _rootHisto->SetBins(_xBins.size() - 1, auxXArray, _yBins.size() - 1, auxYArray);
307     _rootHisto->SetName(GetName());
308     _rootHisto->SetTitle(_title);
309     _rootHisto->SetXTitle(_xLabel);
310     _rootHisto->SetYTitle(_yLabel);
311    
312     delete[] auxXArray;
313     delete[] auxYArray;
314     }
315    
316     /* The row index (first) corresponds to the position on the vertical (Y) axis. */
317     _histo.Resize(_yBins.size() - 1, _xBins.size() - 1);
318    
319     _xUnderflow.resize(_yBins.size());
320     _xOverflow.resize(_yBins.size());
321     _yUnderflow.resize(_xBins.size());
322     _yOverflow.resize(_xBins.size());
323    
324     }
325    
326     template<class HistoType>
327     Histo2DAction<HistoType>::~Histo2DAction() {
328 pam-fi 1.3
329     delete _rootHisto;
330     _rootHisto = NULL;
331 pam-fi 1.1 }
332    
333     template<class HistoType>
334     Histo2DAction<HistoType>::Histo2DAction(const char *actionName, TString title, TString outFileBase, TString mode,
335     bool outRoot, bool outText) :
336     CollectionAction(actionName), _xBins(0), _yBins(0), _histo(0, 0), _rootHisto(NULL), _xUnderflow(0), _xOverflow(0),
337     _yUnderflow(0), _yOverflow(0), _outFileBase(outFileBase), _mode(mode), _title(title), _xLabel(""), _yLabel(""),
338     _outRoot(outRoot), _outText(outText) {
339    
340     }
341    
342     template<class HistoType>
343     void Histo2DAction<HistoType>::SetXAxis(TString label, vector<float> &bins) {
344    
345     _xBins = bins;
346     _xLabel = label;
347    
348     }
349    
350     template<class HistoType>
351     void Histo2DAction<HistoType>::SetYAxis(TString label, vector<float> &bins) {
352    
353     _yBins = bins;
354     _yLabel = label;
355    
356     }
357    
358     template<class HistoType>
359     void Histo2DAction<HistoType>::SetXAxis(TString label, TString binsFile) {
360    
361     // Reading the bins from file
362     ifstream binListFile;
363     binListFile.open(binsFile);
364    
365     TString auxString;
366     _xBins.resize(0);
367     while (!binListFile.eof()) {
368     binListFile >> auxString;
369     if (auxString != "") {
370     _xBins.push_back(auxString.Atof());
371     }
372     }
373     binListFile.close();
374    
375     _xLabel = label;
376    
377     }
378    
379     template<class HistoType>
380     void Histo2DAction<HistoType>::SetYAxis(TString label, TString binsFile) {
381    
382     // Reading the bins from file
383     ifstream binListFile;
384     binListFile.open(binsFile);
385    
386     TString auxString;
387     _yBins.resize(0);
388     while (!binListFile.eof()) {
389     binListFile >> auxString;
390     if (auxString != "") {
391     _yBins.push_back(auxString.Atof());
392     }
393     }
394     binListFile.close();
395    
396     _yLabel = label;
397    
398     }
399    
400     template<class HistoType>
401     void Histo2DAction<HistoType>::SetXAxis(TString label, unsigned int nBins, float min, float max, bool logBinning) {
402    
403     _xBins.resize(nBins + 1);
404    
405     if (!logBinning || (logBinning && min <= 0.)) {
406    
407     #ifdef DEBUGPAMCUT
408     if (logbinning && rigMin <= 0.)
409     cout << "Warning: logarithmic binning was chosen for X axis but min <= 0. Using linear binning."
410     #endif
411    
412     float step = (max - min) / nBins;
413     for (unsigned int i = 0; i < nBins + 1; i++) {
414     _xBins[i] = min + i * step;
415     }
416    
417     }
418     else {
419    
420     double maxExp = log10(max / min);
421     for (unsigned int i = 0; i < nBins + 1; i++) {
422     _xBins[i] = min * pow(10., (double) i / ((double) nBins) * maxExp);
423     }
424    
425     }
426    
427     _xLabel = label;
428     }
429    
430     template<class HistoType>
431     void Histo2DAction<HistoType>::SetYAxis(TString label, unsigned int nBins, float min, float max, bool logBinning) {
432    
433     _yBins.resize(nBins + 1);
434    
435     if (!logBinning || (logBinning && min <= 0.)) {
436    
437     #ifdef DEBUGPAMCUT
438     if (logbinning && rigMin <= 0.)
439     cout << "Warning: logarithmic binning was chosen for Y axis but min <= 0. Using linear binning."
440     #endif
441    
442     float step = (max - min) / nBins;
443     for (unsigned int i = 0; i < nBins + 1; i++) {
444     _yBins[i] = min + i * step;
445     }
446    
447     }
448     else {
449    
450     double maxExp = log10(max / min);
451     for (unsigned int i = 0; i < nBins + 1; i++) {
452     _yBins[i] = min * pow(10., (double) i / ((double) nBins) * maxExp);
453     }
454    
455     }
456    
457     _yLabel = label;
458     }
459    
460     template<class HistoType>
461     inline void Histo2DAction<HistoType>::Fill(double xValue, double yValue, double weight) {
462    
463     _rootHisto->Fill(xValue, yValue, weight);
464    
465     int xBin = 0, yBin = 0;
466    
467     bool UOflow = false;
468    
469     if (xValue < _xBins.front()) {
470     xBin = -1;
471     UOflow = true;
472     }
473    
474     if (xValue > _xBins.back()) {
475     xBin = _xBins.size();
476     UOflow = true;
477     }
478    
479     if (yValue < _yBins.front()) {
480     yBin = -1;
481     UOflow = true;
482     }
483    
484     if (yValue > _yBins.back()) {
485     yBin = _yBins.size();
486     UOflow = true;
487     }
488    
489     if (xBin == 0) {
490    
491     while (xValue >= _xBins[xBin]) {
492     xBin++;
493     }
494     xBin--;
495     }
496    
497     if (yBin == 0) {
498    
499     while (yValue >= _yBins[yBin]) {
500     yBin++;
501     }
502     yBin--;
503     }
504    
505     if (!UOflow) {
506     // No under or over flow.
507     _histo[yBin][xBin] += (HistoType) weight;
508     return;
509     }
510     else {
511     // An under or over flow has happened.
512     if (xBin == -1) {
513     if (yBin == -1) {
514     _xUnderYUnderflow += (HistoType) weight;
515     return;
516     }
517     else {
518 pam-fi 1.2 if (yBin == (int) _yBins.size()) {
519 pam-fi 1.1 _xUnderYOverflow += (HistoType) weight;
520     return;
521     }
522     else {
523     _xUnderflow[yBin] += (HistoType) weight;
524     return;
525     }
526     }
527     }
528    
529 pam-fi 1.2 if (xBin == (int) _xBins.size()) {
530 pam-fi 1.1 if (yBin == -1) {
531     _xOverYUnderflow += (HistoType) weight;
532     return;
533     }
534     else {
535 pam-fi 1.2 if (yBin == (int) _yBins.size()) {
536 pam-fi 1.1 _xOverYOverflow += (HistoType) weight;
537     return;
538     }
539     else {
540     _xOverflow[yBin] += (HistoType) weight;
541     return;
542     }
543     }
544     }
545    
546     if (yBin == -1) {
547     _yUnderflow[xBin] += (HistoType) weight;
548     return;
549     }
550    
551 pam-fi 1.2 if (yBin == (int) _yBins.size()) {
552 pam-fi 1.1 _yOverflow[xBin] += (HistoType) weight;
553     return;
554     }
555    
556     }
557     }
558    
559     template<class HistoType>
560     void Histo2DAction<HistoType>::Finalize() {
561    
562     if (_outFileBase != "") {
563     // Write the ROOT file
564     if (_rootHisto && _outRoot) {
565     TFile outRootFile((_outFileBase + ".root"), _mode);
566     outRootFile.cd();
567     _rootHisto->Write();
568     outRootFile.Close();
569     }
570    
571     //Write the text file
572     if (_outText) {
573     ofstream outTextFile((_outFileBase + ".txt").Data(), ios_base::out);
574     for (unsigned int i = 0; i < _histo.GetNRows(); i++) {
575     for (unsigned int j = 0; j < _histo.GetNCols(); j++) {
576     outTextFile << _histo[i][j] << " ";
577     }
578     outTextFile << "\n";
579     }
580     outTextFile << endl;
581     outTextFile.close();
582     }
583     }
584    
585     }
586     #endif /* HISTO2DACTION_H_ */

  ViewVC Help
Powered by ViewVC 1.1.23