/[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.1 - (hide annotations) (download)
Fri Sep 25 15:36:45 2009 UTC (15 years, 2 months ago) by pam-fi
Branch: MAIN
File MIME type: text/plain
Added to repository.

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

  ViewVC Help
Powered by ViewVC 1.1.23