All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
VTXNoiseClusters.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 //#ifdef USE_ROOT
3 #include "VTXNoiseClusters.h"
4 #include "VXDClusterParameters.h"
5 
6 #include "VXDGeometry.h"
7 
8 #ifdef MARLIN_USE_AIDA
9 #include <marlin/AIDAProcessor.h>
10 #endif
11 
12 // STUFF needed for GEAR
13 #include <marlin/Global.h>
14 #include <gear/GEAR.h>
15 #include <gear/VXDParameters.h>
16 #include <gear/VXDLayerLayout.h>
17 
18 #include <iostream>
19 #include <sstream>
20 
21 #include <EVENT/LCCollection.h>
22 #include <IMPL/LCCollectionVec.h>
23 #include <IMPL/SimTrackerHitImpl.h>
24 
25 // #include <CLHEP/Random/RandGauss.h>
26 #include <gsl/gsl_randist.h>
27 
28 #include <cmath>
29 #include <math.h>
30 
31 #include "TFile.h"
32 
33 using namespace lcio ;
34 using namespace marlin ;
35 using namespace std ;
36 
38 
39 
40 VTXNoiseClusters::VTXNoiseClusters() : Processor("VTXNoiseClusters") {
41 
42  // modify processor description
43  _description = "VTXNoiseClusters adds SimTrackerHits with salt'n pepper noise clusters" ;
44 
45 
46  // register steering parameters: name, description, class-variable, default value
47 
48  FloatVec densityDefault(6) ;
49  for(int i=0 ;i<6; i++ )
50  densityDefault[i] = 0.0 ;
51 
52 
53  registerProcessorParameter( "HitDensityPerLayer_VTX" ,
54  "hit densities (hits/cm^2) per VXD layer" ,
55  _densities ,
56  densityDefault ) ;
57 
58 
59 
60  StringVec rootDefault(7) ;
61  rootDefault[0] = "sap2VXD05_1.root" ;
62  rootDefault[1] = "hst1" ;
63  rootDefault[2] = "hst2" ;
64  rootDefault[3] = "hst3" ;
65  rootDefault[4] = "hst4" ;
66  rootDefault[5] = "hst5" ;
67  rootDefault[6] = "hst6" ;
68 
69  registerProcessorParameter( "RootHistograms" ,
70  "root file name and histogram names (one per layer)" ,
71  _rootNames ,
72  rootDefault ) ;
73 
74 
75  // Input collection
76  registerInputCollection( LCIO::SIMTRACKERHIT,
77  "VTXCollectionName" ,
78  "Name of the VTX SimTrackerHit collection" ,
79  _colNameVTX ,
80  std::string("VXDCollection") ) ;
81 
82 
83  registerProcessorParameter( "RandomSeed" ,
84  "random seed - default 42" ,
85  _ranSeed ,
86  int(42) ) ;
87 
88 }
89 
90 
92 
93  // usually a good idea to
94  printParameters() ;
95 
96  _nRun = 0 ;
97  _nEvt = 0 ;
98 
99  _vxdGeo = new VXDGeometry( Global::GEAR ) ;
100 
101  // initialize gsl random generator
102  // ranlux algorithm of Lüscher, which produces 'luxury random numbers'
103  _rng = gsl_rng_alloc(gsl_rng_ranlxs2) ;
104 
105  gsl_rng_default_seed = _ranSeed ;
106 
107 
108 
109 
110  const gear::VXDParameters& gearVXD = Global::GEAR->getVXDParameters() ;
111  const gear::VXDLayerLayout& layerVXD = gearVXD.getVXDLayerLayout();
112 
113  if( (unsigned) layerVXD.getNLayers() != _rootNames.size() - 1 ){
114 
115  streamlog_out( ERROR ) << " *************************************************** " << std::endl
116  << " wrong number of histograms: " << _rootNames.size()
117  << " for " << layerVXD.getNLayers() << " VXD layers "
118  << " - do nothing ! " << std::endl
119  << " *************************************************** " << std::endl ;
120 
121  }
122 
123 
124  const std::string& filename = _rootNames[0] ;
125 
126  _hfile = new TFile( filename.c_str() );
127 
128  if( ! _hfile->IsOpen() ) {
129 
130  std::string mess(" can't open root file :") ;
131  mess += _rootNames[0] ;
132 
133  throw Exception( mess ) ;
134  }
135 
136  streamlog_out(DEBUG4) << "Read histograms from file : " << filename << endl;
137 
138 
139  // _hfile->ls() ;
140 
141  _hist.resize( layerVXD.getNLayers() ) ;
142 
143  for(int i=0;i<layerVXD.getNLayers(); ++i ){
144 
145 
146  streamlog_out(DEBUG4) << " " << _rootNames[i+1].c_str() << ": .... " ;
147 
148  _hist[i] = dynamic_cast<TH2F*> ( _hfile->Get( _rootNames[i+1].c_str() ) ) ;
149 
150  if( !_hist[i] ) {
151  std::string mess(" can't read histogram :") ;
152  mess += _rootNames[i+1] ;
153  throw Exception( mess ) ;
154  }
155  streamlog_out(DEBUG4) << " OK at :" << _hist[i] << std::endl ;
156 
157  streamlog_out(DEBUG) << " 2D histo - bins (x,y): "
158  << _hist[i]->GetNbinsX() << ", "
159  << _hist[i]->GetXaxis()->GetBinLowEdge(1) << ", "
160  << _hist[i]->GetXaxis()->GetBinLowEdge( _hist[i]->GetNbinsX() )
161  + _hist[i]->GetXaxis()->GetBinWidth( _hist[i]->GetNbinsX() ) << " , "
162  << _hist[i]->GetNbinsY() << ", "
163  << _hist[i]->GetYaxis()->GetBinLowEdge(1) << ", "
164  << _hist[i]->GetYaxis()->GetBinLowEdge( _hist[i]->GetNbinsY() )
165  + _hist[i]->GetYaxis()->GetBinWidth( _hist[i]->GetNbinsY() )
166  << std::endl ;
167 
168  }
169 
170 }
171 
172 
173 void VTXNoiseClusters::processRunHeader( LCRunHeader* /*run*/) {
174  _nRun++ ;
175 }
176 
177 void VTXNoiseClusters::processEvent( LCEvent * /*evt*/ ) {
178 }
179 
180 void VTXNoiseClusters::modifyEvent( LCEvent * evt ) {
181 
182  LCCollection* col = 0 ;
183 
184  try{
185 
186  col = evt->getCollection( _colNameVTX ) ;
187 
188  }
189  catch(DataNotAvailableException &e){
190 
191 
192  streamlog_out( WARNING ) << " VTX collection " << _colNameVTX
193  << " not found - do nothing ! " << std::endl ;
194 
195 
196  return ;
197 
198  }
199 
200 // //fg ++++++++++++++ test and debug code ++++++++++++++++++++++
201 // _vxdGeo->test() ;
202 // if( col != 0 ){
203 // for( int i=0 ; i < col->getNumberOfElements() ; ++i ){
204 // SimTrackerHit* sth = dynamic_cast<SimTrackerHit*>( col->getElementAt(i) ) ;
205 // gear::Vector3D pos( sth->getPosition()[0], sth->getPosition()[1], sth->getPosition()[2] ) ;
206 // std::pair<int,int> id = _vxdGeo->getLadderID( pos ) ;
207 // if( id.first < 0 ) {
208 // streamlog_out( WARNING ) << " VTX hit outside sensitive : "
209 // << pos
210 // << " in gear: "
211 // << Global::GEAR->getVXDParameters().isPointInSensitive( pos )
212 // << std::endl ;
213 // } else {
214 // gear::Vector3D lad = _vxdGeo->lab2Ladder( pos , id.first, id.second ) ;
215 // streamlog_out( DEBUG4 ) << " %%% " << lad ;
216 // }
217 // }
218 // }
219 // return ;
220 // //fg ++++++++++++++ test and debug code ++++++++++++++++++++++
221 
222 
223  //get VXD geometry info
224  const gear::VXDParameters& gearVXD = Global::GEAR->getVXDParameters() ;
225  const gear::VXDLayerLayout& layerVXD = gearVXD.getVXDLayerLayout();
226 
227 
228  if( (unsigned) layerVXD.getNLayers() != _densities.size() ){
229 
230 
231  streamlog_out( ERROR ) << " *************************************************** " << std::endl
232  << " wrong number of hit densities: " << _densities.size()
233  << " for " << layerVXD.getNLayers() << " VXD layers "
234  << " - do nothing ! " << std::endl
235  << " *************************************************** " << std::endl ;
236 
237  return ;
238 
239  }
240 
241  double hgap = gearVXD.getShellGap() / 2. ;
242 
243  for( int i=0 ; i < layerVXD.getNLayers() ; i++ ) {
244 
245  double width = layerVXD.getSensitiveWidth (i) ;
246  double len = layerVXD.getSensitiveLength (i) ;
247 
248  // area of one double ladder (+z and -z)
249  double area = 2 * len * width / 100. ; // mm^2 to cm^2
250 
251  int nLad = layerVXD.getNLadders(i) ;
252 
253  for( int j=0 ; j < nLad ; j++ ) {
254 
255  int nHit = gsl_ran_poisson( _rng , _densities[ i ] * area ) ;
256 
257  streamlog_out( DEBUG ) << " layer: " << i << " - ladder: " << j
258  << " density: " << _densities[ i ]
259  << " area: " << area
260  << " #hits: " << nHit
261  << std::endl ;
262 
263  for( int k=0 ; k< nHit ; k++){
264 
265  double l1 = gsl_rng_uniform( _rng ) ;
266  double l2 = gsl_rng_uniform( _rng ) ;
267 
268  // ------ compute a point on the ladder
269  // - (x,y) origin is in the center of the ladder:
270  double k1 = 0.5 - l1 ;
271  // - z origin is at z==0 in the lab
272 
273  double k2 = ( (k % 2) ? -1. : 1. ) * l2 ;
274 
275  gear::Vector3D lad( 0 , k1 * width, k2 * len + hgap ) ;
276 
277  gear::Vector3D lab = _vxdGeo->ladder2LabPos( lad, i , j ) ;
278 
279  // double check if point is in sensitive
280  if( ! gearVXD.isPointInSensitive( lab ) ){
281  streamlog_out( WARNING ) << " created hit outside sensitve volume " << lab << std::endl ;
282  }
283 
284  SimTrackerHitImpl *hit = new SimTrackerHitImpl() ;
285 
286  double pos[3] = { lab[0] , lab[1] , lab[2] } ;
287 
288  hit->setPosition( pos ) ;
289 
290  hit->setEDep( 0. ) ; // FIXME: which dedx should be used for noise hits
291 
292  //FIXME: encode a proper cellID
293  hit->setCellID0( i + 1 ) ; // fg: here we'd like to have ladder id as well ....
294 
295 
296  // now we need to add some cluster parameters to the hit :
297  double cluZ, cluRPhi ;
298 
299 
300  _hist[i]->GetRandom2( cluZ, cluRPhi ) ;
301 
302 
303  // --- cluster axes in ladder frame
304  gear::Vector3D axisAlad( 0, cluRPhi/2. , 0 ) ;
305  gear::Vector3D axisBlad( 0, 0 , cluZ/2. ) ;
306 
307  // --- cluster axes in lab frame
308  // gear::Vector3D axisAlab = _vxdGeo->ladder2LabDir( axisAlad , i , j ) ;
309  // gear::Vector3D axisBlab = _vxdGeo->ladder2LabDir( axisBlad, i , j ) ;
310 
311  // hit position in ladder frame:
312 
313 
314  hit->ext< ClusterParams >() = new VXDClusterParameters( lad, axisAlad , axisBlad , i , j ) ;
315 
316 
317  col->addElement( hit ) ;
318 
319  }
320  }
321  }
322 
323 
324  _nEvt ++ ;
325 }
326 
327 
328 
329  void VTXNoiseClusters::check( LCEvent * evt ) {
330 
331 
332 #ifdef MARLIN_USE_AIDA
333  struct H1D{
334  enum {
335  hitsLayer1,
336  hitsLayer2,
337  hitsLayer3,
338  hitsLayer4,
339  hitsLayer5,
340  hitsLayer6,
341  size
342  } ;
343  };
344 
345  if( isFirstEvent() ) {
346 
347  _hist1DVec.resize( H1D::size ) ;
348 
349  float hitMax = 100000. ;
350  _hist1DVec[ H1D::hitsLayer1 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer1",
351  "hits Layer 1",
352  100, 0. ,hitMax ) ;
353  _hist1DVec[ H1D::hitsLayer2 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer2",
354  "hits Layer 2",
355  100, 0. , hitMax ) ;
356  _hist1DVec[ H1D::hitsLayer3 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer3",
357  "hits Layer 3",
358  100, 0. , hitMax ) ;
359  _hist1DVec[ H1D::hitsLayer4 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer4",
360  "hits Layer 4",
361  100, 0. ,hitMax ) ;
362  _hist1DVec[ H1D::hitsLayer5 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer5",
363  "hits Layer 5",
364  100, 0. , hitMax ) ;
365  _hist1DVec[ H1D::hitsLayer6 ] = AIDAProcessor::histogramFactory(this)->createHistogram1D( "hitsLayer6",
366  "hits Layer 6",
367  100, 0. , hitMax ) ;
368 
369 
370  _hist2DVec.resize( H1D::size ) ;
371 
372  for(int i=0 ; i < H1D::size ; ++i ){
373 
374  std::stringstream name("clusSizLayer") ;
375  name << i ;
376 
377  std::stringstream comment("cluster sizes in layer ") ;
378  comment << i ;
379 
380 
381  int nx = _hist[i]->GetNbinsX() ;
382  float xMin = _hist[i]->GetXaxis()->GetBinLowEdge(1) ;
383  float xMax = _hist[i]->GetXaxis()->GetBinLowEdge( _hist[i]->GetNbinsX() )
384  + _hist[i]->GetXaxis()->GetBinWidth( _hist[i]->GetNbinsX() ) ;
385 
386  int ny = _hist[i]->GetNbinsY() ;
387  float yMin = _hist[i]->GetYaxis()->GetBinLowEdge(1) ;
388  float yMax = _hist[i]->GetYaxis()->GetBinLowEdge( _hist[i]->GetNbinsY() )
389  + _hist[i]->GetYaxis()->GetBinWidth( _hist[i]->GetNbinsY() ) ;
390 
391 
392  _hist2DVec[i] = AIDAProcessor::histogramFactory(this)
393  ->createHistogram2D( name.str() ,
394  comment.str() ,
395  nx, xMin, xMax ,
396  ny, yMin, yMax ) ;
397 
398  }
399 
400 
401 
402  }
403 #endif
404 
405 
406 
407  LCCollection* vxdCol = 0 ;
408 
409 // int nhit = 0 ;
410  int nHitL1 = 0 ;
411  int nHitL2 = 0 ;
412  int nHitL3 = 0 ;
413  int nHitL4 = 0 ;
414  int nHitL5 = 0 ;
415  int nHitL6 = 0 ;
416 
417 
418  try {
419  vxdCol = evt->getCollection( _colNameVTX ) ;
420 
421  int nH = vxdCol->getNumberOfElements() ;
422 
423 
424  for(int i=0; i<nH ; ++i){
425 
426  SimTrackerHit* sth = dynamic_cast<SimTrackerHit*>( vxdCol->getElementAt(i) ) ;
427 
428  int layer = ( sth->getCellID0() & 0xff ) ;
429 
430  // --- cluster axes in lab frame
431  VXDClusterParameters* cluP = sth->ext< ClusterParams >() ;
432 
433 
434  // gear::Vector3D pos( sth->getPosition()[0] , sth->getPosition()[1] , sth->getPosition()[2] ) ;
435 
436  if( cluP != 0 ) { // physics hits have no cluster parameters
437 
438  // now get cluster axis vector:
439 
440  gear::Vector3D axisAlab = cluP->getClusterAxisA() ; //- pos ;
441  gear::Vector3D axisBlab = cluP->getClusterAxisB() ; //- pos ;
442 
443  double cluA = axisAlab.r() ;
444  double cluB = axisBlab.r() ;
445 
446  // streamlog_out( DEBUG ) << " axisAlab " << axisAlab
447  // << " axisBlab " << axisBlab << std::endl ;
448 
449 #ifdef MARLIN_USE_AIDA
450  _hist2DVec[ layer-1 ]->fill( cluB *2. , cluA * 2. ) ;
451 #endif
452  }
453 
454  switch (layer) {
455 
456  case 1 :
457  nHitL1++ ;
458  break ;
459  case 2 :
460  nHitL2++ ;
461  break ;
462  case 3 :
463  nHitL3++ ;
464  break ;
465  case 4 :
466  nHitL4++ ;
467  break ;
468  case 5 :
469  nHitL5++ ;
470  break ;
471  case 6 :
472  nHitL6++ ;
473  break ;
474  }
475 
476  }
477  } catch( DataNotAvailableException& e) {}
478 
479 #ifdef MARLIN_USE_AIDA
480  _hist1DVec[ H1D::hitsLayer1 ]->fill( nHitL1 ) ;
481  _hist1DVec[ H1D::hitsLayer2 ]->fill( nHitL2 ) ;
482  _hist1DVec[ H1D::hitsLayer3 ]->fill( nHitL3 ) ;
483  _hist1DVec[ H1D::hitsLayer4 ]->fill( nHitL4 ) ;
484  _hist1DVec[ H1D::hitsLayer5 ]->fill( nHitL5 ) ;
485  _hist1DVec[ H1D::hitsLayer6 ]->fill( nHitL6 ) ;
486 #endif
487 
488 }
489 
490 
492 
493  std::cout << "VTXNoiseClusters::end() " << name()
494  << " processed " << _nEvt << " events in " << _nRun << " runs "
495  << std::endl ;
496 }
497 
498 
499 //#endif // USE_ROOT
void modifyEvent(LCEvent *evt)
Called for every event - the working horse.
virtual void check(LCEvent *evt)
virtual void processEvent(LCEvent *evt)
VTXNoiseClusters aVTXNoiseClusters
gear::Vector3D ladder2LabPos(gear::Vector3D ladderPos, int layerID, int ladderID)
Convert a position in local ladder coordinates (x_ladder==0 is the middle of the sensitive) to the la...
Definition: VXDGeometry.cc:151
CLHEP::Hep3Vector Vector3D
static const float k
Allows to use VXDClusterParameters as runtime extension object.
======= VXDGeometry ========== Helper class for VXD geomtry transformations: from lab frame to ladd...
Definition: VXDGeometry.h:53
gear::Vector3D getClusterAxisA()
std::vector< TH2F * > _hist
virtual void end()
Called after data processing for clean up.
static const float e
VXDGeometry * _vxdGeo
std::string _colNameVTX
gear::Vector3D getClusterAxisB()
virtual void init()
Called at the begin of the job before anything is read.
virtual const std::string & name() const
virtual void processRunHeader(LCRunHeader *run)
Called for every run.
std::vector< std::string > StringVec
Definition: SiStripClus.h:56