MarlinTPC  1.2.0
ZBinTemplateProcessor.h
1 #ifndef Z_BIN_TEMPLATE_PROCESSOR_H
2 #define Z_BIN_TEMPLATE_PROCESSOR_H 1
3 
4 //LCIO
5 #include <lcio.h>
6 
7 //MARLIN
8 #include "marlin/Processor.h"
9 //C++
10 #include <iostream>
11 #include <cmath>
12 #include <sstream>
13 //using namespace std;
14 
15 //LCIO
16 #include <IMPL/LCCollectionVec.h>
17 #include <EVENT/Track.h>
18 #include <EVENT/TrackerHit.h>
19 
20 // Aida
21 //#ifdef MARLIN_USE_AIDA // the whole processor does not make sense without aida
22 #include <AIDA/AIDA.h>
23 #include <marlin/AIDAProcessor.h>
24 #include <AIDA/IHistogramFactory.h>
25 #include <AIDA/IHistogram1D.h>
26 #include <AIDA/IHistogram2D.h>
27 // endif
28 
29 #include <marlin/Global.h>
30 
31 namespace AIDA{
32  class IHistogram1D;
33  class IHistogram2D;
34 }
35 
36 
37 namespace marlintpc
38 {
39 
75  template<unsigned int N1DHISTOS, unsigned int N2DHISTOS>
76  class ZBinTemplateProcessor : public marlin::Processor
77  {
78  public:
79 
84  virtual Processor* newProcessor() { throw Exception("You cannot create an instance of ZBinTemplateProcessor"); }
85 
86  virtual ~ZBinTemplateProcessor() {}
87 
89  virtual void init() {
90 
91  // usually a good idea to
92  printParameters() ;
93 
94  // check parameter:
95  // _nZBins >=0
96  if (_nZBins < 0)
97  {
98  streamlog_out(WARNING3) << "invalid _nZBins < 0 ; setting to 0"
99  << std::endl;
100  _nZBins = 0;
101  }
102 
103  // _minZ < _maxZ
104  if ( _maxZ <= _minZ )
105  {
106  streamlog_out(WARNING3) << "invalid z range, setting to 0"
107  << std::endl;
108 
109  _nZBins = 0;
110  }
111 
112  // calculate width of a z bin
113  if (_nZBins>0)
115  else
116  _zBinWidth=1;
117 
118  //resize histo arrays to correct number of z bins
119  for (unsigned int i = 0 ; i < N1DHISTOS ; i++)
120  {
121  _1DHistos[i].resize(_nZBins +1);
122  }
123 
124  for (unsigned int i = 0 ; i < N2DHISTOS ; i++)
125  {
126  _2DHistos[i].resize(_nZBins +1);
127  }
128 
129  // get histograms from the AIDAProcessor
130 
131  // loop the z bins
132  for (int zBin=0; zBin<=_nZBins ; zBin++)
133  {
134  // create unique names and titles for all histograms
135  std::stringstream zRangeString;
136  std::stringstream zIndexString;
137  if (zBin < _nZBins)
138  // no extention for the histo without z cut
139  {
140  zRangeString << " "<<_minZ + _zBinWidth*zBin
141  << " < z < "
142  << _minZ + _zBinWidth*(zBin+1);
143  zIndexString << zBin ;
144  }
145 
146 
147  // now get all histograms in this z bin from the AIDAProcessor
148  for (unsigned int histoIndex = 0 ; histoIndex < N1DHISTOS ; histoIndex ++)
149  {
150  streamlog_out(DEBUG3) << "_1DHistos["<<histoIndex<<"]["<<zBin<<"]" << std::endl;
151 
152  _1DHistos[histoIndex][zBin] = marlin::AIDAProcessor::histogramFactory(this)->
153  createHistogram1D( _histo1DBaseNames[histoIndex] + zIndexString.str(),
154  _histo1DTitles[histoIndex] + zRangeString.str() ,
155  _nBins1D[histoIndex],
156  _histoRanges1D[histoIndex].min,
157  _histoRanges1D[histoIndex].max );
158  }
159 
160  for (unsigned int histoIndex = 0 ; histoIndex < N2DHISTOS ; histoIndex ++)
161  {
162 
163  _2DHistos[histoIndex][zBin] = marlin::AIDAProcessor::histogramFactory(this)->
164  createHistogram2D( _histo2DBaseNames[histoIndex] + zIndexString.str(),
165  _histo2DTitles[histoIndex] + zRangeString.str() ,
166  _nBins2D[histoIndex].nBinsX,
167  _histoRanges2D[histoIndex].xRange.min,
168  _histoRanges2D[histoIndex].xRange.max,
169  _nBins2D[histoIndex].nBinsY,
170  _histoRanges2D[histoIndex].yRange.min,
171  _histoRanges2D[histoIndex].yRange.max );
172  }
173 
174  }// for zBin
175 
176  _isInitialised=true;
177  }
178 
179 
180 
183  struct HistoRange
184  {
185  float min;
186  float max;
187  };
188 
189 
193  {
194  HistoRange xRange;
195  HistoRange yRange;
196  };
197 
201  {
202  int nBinsX;
203  int nBinsY;
204  };
205 
206  protected:
207 
208  ZBinTemplateProcessor(const std::string & typeName )
209  : marlin::Processor(typeName) , _isInitialised(false) {
210 
211  _description = "ZBinTemplateProcessor cannot be instantiated. Its a template class to derive from." ;
212 
213  registerProcessorParameter( "MaxZ" ,
214  "MaxZ Maximum z value of highest bin in case of z binning (default: 260)",
215  _maxZ,
216  float(260) );
217  registerProcessorParameter( "MinZ" ,
218  "MinZ Minimum z value of lowest bin in case of z binning (default: 0)",
219  _minZ,
220  float(0) );
221  registerProcessorParameter( "nZBins" ,
222  "Number ob bins in z, 0 means one Histo without z cut (default: 10)",
223  _nZBins,
224  int(10) );
225 
226  if (_nZBins>0)
228  else
229  _zBinWidth=1;
230 
231  // initialize all vectors for the different histo types to the templated size
232  // this should be done in the consturctor, it does not depend on processor parameters
233  _1DHistos.resize(N1DHISTOS);
234  _2DHistos.resize(N2DHISTOS);
235  _histo1DBaseNames.resize(N1DHISTOS);
236  _histo2DBaseNames.resize(N2DHISTOS);
237  _histo1DTitles.resize(N1DHISTOS);
238  _histo2DTitles.resize(N2DHISTOS);
239  _histoRanges1D.resize(N1DHISTOS);
240  _histoRanges2D.resize(N2DHISTOS);
241  _nBins1D.resize(N1DHISTOS);
242  _nBins2D.resize(N2DHISTOS);
243 
244  // now initialize the other variables for each histoIndex
245  // for those parameters which don't depend on ProcessorParameters.
246  // The zBin stuff has to be done in init()
247  for (unsigned int i = 0 ; i < N1DHISTOS ; i++)
248  {
249  std::stringstream histoNameStream;
250  histoNameStream << typeName<<"histo1D" << i << "_";
251  //streamlog_out(DEBUG3) << histoNameStream.str() << std::endl;
252 
253  // _1DHistos[i].resize(_nZBins +1);
254  _histoRanges1D[i].min=0.;
255  _histoRanges1D[i].max=1.;
256  _nBins1D[i]=100;
257  _histo1DTitles[i]=histoNameStream.str();
258  _histo1DBaseNames[i]=histoNameStream.str();
259  }
260  for (unsigned int i = 0 ; i < N2DHISTOS ; i++)
261  {
262  std::stringstream histoNameStream;
263  histoNameStream << typeName<<"histo2D" << i << "_";
264 
265  // the streamlog level is not set in the constructor yet, this is always printed
266  //streamlog_out_T(DEBUG3) << histoNameStream.str() << std::endl;
267 
268  // _2DHistos[i].resize(_nZBins +1);
269  _histoRanges2D[i].xRange.min=0.;
270  _histoRanges2D[i].xRange.max=1.;
271  _histoRanges2D[i].yRange.min=0.;
272  _histoRanges2D[i].yRange.max=1.;
273  _nBins2D[i].nBinsX=100;
274  _nBins2D[i].nBinsY=100;
275  _histo2DTitles[i]=histoNameStream.str();
276  _histo2DBaseNames[i]=histoNameStream.str();
277  }
278 
279  }
280 
281 
286  void fillHistogram(unsigned int histoIndex, float x, float z) {
287  if (!_isInitialised)
288  throw Exception("ZBinTemplateProcessor is not initialised."
289  "Did you call ZBinTemplateProcessor::init() in your init function?");
290 
291  // don't confuse histoIndex with the zIndex
292  // unsigned int histoIndex = histoIndices1D[histo];
293 
294  if (_nZBins > 0)
295  {
296  // zBin of the track (from mean z if the track)
297  int zBin = static_cast<int>( (z - _minZ) / _zBinWidth );
298 
299  if ( (zBin >= 0) && (zBin < _nZBins) )
300  {
301  _1DHistos[histoIndex][zBin]->fill(x);
302  }
303  } // in _nZBins >0
304 
305  // fill the histo without z cut
306  _1DHistos[histoIndex][_nZBins]->fill(x);
307  }
308 
309 
315  void fillHistogram(unsigned int histoIndex, float x, float y, float z) {
316 
317  if (!_isInitialised)
318  throw Exception("ZBinTemplateProcessor is not initialised."
319  "Did you call ZBinTemplateProcessor::init() in your init function?");
320 
321  // don't confuse histoIndex with the zIndex
322  // unsigned int histoIndex = histoIndices2D[histo];
323 
324  if (_nZBins > 0)
325  {
326  // zBin of the track (from mean z if the track)
327  int zBin = static_cast<int>( (z - _minZ) / _zBinWidth );
328 
329  if ( (zBin >= 0) && (zBin < _nZBins) )
330  {
331  _2DHistos[histoIndex][zBin]->fill(x,y);
332  }
333  } // in _nZBins >0
334 
335  // fill the histo without z cut
336  _1DHistos[histoIndex][_nZBins]->fill(x,y);
337  }
338 
339 
340  std::vector< std::vector<AIDA::IHistogram1D *> > _1DHistos;
341  std::vector< std::vector<AIDA::IHistogram2D *> > _2DHistos;
342 
345  std::vector< std::string > _histo1DBaseNames;
349  std::vector< std::string > _histo2DBaseNames;
353  std::vector< std::string > _histo1DTitles;
357  std::vector< std::string > _histo2DTitles;
358  std::vector< HistoRange > _histoRanges1D;
359  std::vector< HistoRange2D > _histoRanges2D;
360  std::vector< int > _nBins1D;
361  std::vector< HistoNBins2D > _nBins2D;
362 
363  float _minZ;
364  float _maxZ;
365 
366  int _nZBins;
367 
368  float _zBinWidth;
369 
370  private:
371  bool _isInitialised;
372 
373  };
374 }
375 
376 #endif // Z_BIN_TEMLATE_PROCESSOR_H
void fillHistogram(unsigned int histoIndex, float x, float y, float z)
Fill the x and y value pair into the 2D histogram set histoIndex.
Definition: ZBinTemplateProcessor.h:315
std::vector< HistoRange2D > _histoRanges2D
Vector of ranges for the 2D histograms.
Definition: ZBinTemplateProcessor.h:359
int _nZBins
Number of z bins.
Definition: ZBinTemplateProcessor.h:366
A nested convenience struct which contains the number of bins for both directions of a 2D histogram...
Definition: ZBinTemplateProcessor.h:200
std::vector< HistoNBins2D > _nBins2D
Vector with number of bins for the 1D histograms.
Definition: ZBinTemplateProcessor.h:361
std::vector< std::string > _histo2DBaseNames
Vector of base names for the 2D histograms.
Definition: ZBinTemplateProcessor.h:349
std::vector< int > _nBins1D
Vector with number of bins for the 1D histograms.
Definition: ZBinTemplateProcessor.h:360
A template to create processors wich create 1D or 2D distribution histograms, one histogram per z bin...
Definition: ZBinTemplateProcessor.h:76
float _maxZ
Maximum z value of highest bin.
Definition: ZBinTemplateProcessor.h:364
virtual void init()
Register the AIDA histograms at the AIDAProcessor.
Definition: ZBinTemplateProcessor.h:89
std::vector< std::vector< AIDA::IHistogram1D * > > _1DHistos
Vector of 1D histogram vectors.
Definition: ZBinTemplateProcessor.h:340
float _minZ
Minimum z value of lowest bin.
Definition: ZBinTemplateProcessor.h:363
A nested convenience struct which contains the minimal and maximal value of a (1D) histogram rage...
Definition: ZBinTemplateProcessor.h:183
std::vector< std::vector< AIDA::IHistogram2D * > > _2DHistos
Vector of 2D histogram vectors.
Definition: ZBinTemplateProcessor.h:341
void fillHistogram(unsigned int histoIndex, float x, float z)
Fill value x into the histogram set histoIndex.
Definition: ZBinTemplateProcessor.h:286
std::vector< std::string > _histo1DBaseNames
Vector of base names for the 1D histograms.
Definition: ZBinTemplateProcessor.h:345
A nested convenience struct which contains two 1D HistoRange objects.
Definition: ZBinTemplateProcessor.h:192
std::vector< HistoRange > _histoRanges1D
Vector of ranges for the 1D histograms.
Definition: ZBinTemplateProcessor.h:358
std::vector< std::string > _histo2DTitles
Vector of titles for the 2D histograms.
Definition: ZBinTemplateProcessor.h:357
std::vector< std::string > _histo1DTitles
Vector of titles for the 1D histograms.
Definition: ZBinTemplateProcessor.h:353
virtual Processor * newProcessor()
The newProcessor() function does not make sense for this template class, so it throws an lcio::Except...
Definition: ZBinTemplateProcessor.h:84
float _zBinWidth
Width of a bin, calculated as (_maxZ-_minZ)/_nZBins;.
Definition: ZBinTemplateProcessor.h:368