LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
recjob.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCWriter.h"
4 #include "IO/LCReader.h"
5 #include "IO/LCEventListener.h"
6 #include "IO/LCRunListener.h"
7 
8 #include "EVENT/LCIO.h"
9 #include "EVENT/TrackerRawData.h"
10 
11 #include "IMPL/LCEventImpl.h"
12 #include "IMPL/LCCollectionVec.h"
15 #include "IMPL/MCParticleImpl.h"
16 #include "IMPL/TrackerHitImpl.h"
17 #include "IMPL/TrackImpl.h"
18 #include "IMPL/ClusterImpl.h"
20 #include "IMPL/VertexImpl.h"
21 #include "UTIL/LCTOOLS.h"
22 #include "UTIL/PIDHandler.h"
23 #include "IMPL/LCRelationImpl.h"
24 
26 #include "UTIL/BitSet32.h"
27 
28 #include "CalibrationConstant.h"
29 
30 // M_PI is non ansi ...
31 #define M_PI 3.14159265358979323846
32 
33 #include <iostream>
34 #include <algorithm>
35 
36 using namespace std ;
37 using namespace lcio ;
38 
39 static std::string FILEN = "simjob.slcio" ;
40 static std::string OUTFILEN = "recjob.slcio" ;
41 static const int nRecP = 10 ; // number of reconstructed particles
42 
54 class RunEventProcessor : public LCRunListener, public LCEventListener{
55 
56 protected:
57  LCWriter* lcWrt{} ;
58  int nEvent{0} ;
59 
60 public:
61 
62  RunEventProcessor(const RunEventProcessor&) = delete ;
63  RunEventProcessor operator=(const RunEventProcessor&) = delete ;
64 
65  RunEventProcessor() : nEvent(0) {
66 
67  // open outputfile
68  lcWrt = LCFactory::getInstance()->createLCWriter() ;
69 
70  try{
71  lcWrt->setCompressionLevel( 9 ) ;
72  lcWrt->open( OUTFILEN , LCIO::WRITE_NEW ) ;
73  }
74 
75  catch(IOException& e){
76  cout << "[RunEventProcessor()] Can't open file for writing - "
77  << e.what() << endl ;
78  exit(1) ;
79  }
80 
81  }
82 
84 
85  // close outputfile
86  lcWrt->close() ;
87 
88  cout << endl
89  << " added collection: 'SomeClusters' and 'SomeTracks'"
90  << " to " << nEvent <<" events"
91  << " and added one extra MCParticle to each event."
92  << endl << endl ;
93  delete lcWrt ;
94  }
95 
96  void processEvent( LCEvent* evt ) { /* used for 'read only' access*/
97 
98 
99  // this is our event loop code
100 
101  // read collection with MCParticles
102  // LCCollection* mcVec = evt->getCollection( LCIO::MCPARTICLE ) ;
103  // int NMCPART = mcVec->getNumberOfElements() ;
104 
105 
106  // ---- trying to modify objects here would cause a ReadOnlyExcpetion. e.g. -----
107  // for(int i=0 ; i< NMCPART ; i++ ){
108  // MCParticleImpl* part = dynamic_cast<MCParticleImpl*>( mcVec->getElementAt( i )) ;
109  // part->setPDG(1234) ; // <<<<< ------- will cause ReadOnlyException ---------
110  // }
111  // ---- also adding sth. to en existing collection is not allowed here ----
112  // MCParticleImpl* part = new MCParticleImpl ;
113  // part->setPDG( 1234 ) ;
114  // part->setParent( dynamic_cast<MCParticle*>( mcVec->getElementAt(0) )) ;
115  // mcVec->addElement( part ) ; // <<<<< ------- will cause ReadOnlyException ---------
116 
117 
118 
119  // create some tracks and add them to the event
120  std::string tpcHitName( "TrackerRawDataExample" ) ;
121 
122  // in order to be able to point back to hits, we need to create
123  // generic TrackerHits from the TrackerRawDatas first
124 
125  LCCollection* tpcHits = evt->getCollection( tpcHitName) ;
126 
127 
128  LCCollectionVec* trkhitVec = new LCCollectionVec( LCIO::TRACKERHIT ) ;
129  int nTrackerRawDatas = tpcHits->getNumberOfElements() ;
130 
131  for(int j=0;j<nTrackerRawDatas;j++){
132  TrackerHitImpl* trkHit = new TrackerHitImpl ;
133 
134  TrackerRawData* tpcRawHit = dynamic_cast<TrackerRawData*> ( tpcHits->getElementAt(j) ) ;
135 
136  trkHit->setTime( tpcRawHit->getTime() ) ;
137 
138  int cellID = tpcRawHit->getCellID0() ;
139  double pos[3] = { double (cellID & 0xff) , double ( (cellID & 0xff00)>>8 ), double( (cellID & 0xff0000)>>16 ) } ;
140  trkHit->setPosition( pos ) ;
141 
142  trkHit->rawHits().push_back( tpcRawHit ) ; // store the original raw data hit
143  trkHit->rawHits().push_back( tpcRawHit ) ; // for testing add the same raw hit twice
144 
145  FloatVec cov(6) ;
146  cov[0] = 1. ;
147  cov[1] = 2. ;
148  cov[2] = 3. ;
149  cov[3] = 4. ;
150  cov[4] = 5. ;
151  cov[5] = 6. ;
152  trkHit->setCovMatrix( cov ) ;
153 
154  trkhitVec->addElement( trkHit ) ;
155  }
156 
157  // set the parameters to decode the type information in the collection
158  // for the time being this has to be done manually
159  // in the future we should provide a more convenient mechanism to
160  // decode this sort of meta information
161  StringVec typeNames ;
162  IntVec typeValues ;
163  typeNames.push_back( LCIO::TPCHIT ) ;
164  typeValues.push_back( 1 ) ;
165  trkhitVec->parameters().setValues("TrackerHitTypeNames" , typeNames ) ;
166  trkhitVec->parameters().setValues("TrackerHitTypeValues" , typeValues ) ;
167 
168 
169  evt->addCollection( trkhitVec , "TrackerHits") ;
170 
171 
172  LCCollectionVec* trkVec = new LCCollectionVec( LCIO::TRACK ) ;
173 
174  // if we want to point back to the hits we need to set the flag
175  // LCFlagImpl trkFlag(0) ;
176  // trkFlag.setBit( LCIO::TRBIT_HITS ) ;
177  // trkVec->setFlag( trkFlag.getFlag() ) ;
178 
179  trkVec->setFlag( UTIL::make_bitset32( LCIO::TRBIT_HITS ) ) ;
180 
181 
182  int nTrk = 5 ;
183  for( int i=0; i < nTrk ; i ++ ){
184 
185  TrackImpl* trk = new TrackImpl ;
186  trk->setTypeBit( 7 ) ;
187  trk->setOmega( (i+1)*1.1 ) ;
188  trk->setTanLambda( (i+1)* M_PI / 10. ) ;
189  trk->setPhi( (i+1)* M_PI / 5. ) ;
190  trk->setD0( i+1 ) ;
191  trk->setZ0( (i+1)*10. ) ;
192  trk->setChi2( 1.01 ) ;
193  trk->setNdf( 42 ) ;
194 
195  trk->setRadiusOfInnermostHit( 3.141592 ) ;
196 
197  // set the hit numbers
198  const int NTRACKER = 3 ;
199  const int VTXINDEX = 0 ;
200  const int SITINDEX = 1 ;
201  const int TPCINDEX = 2 ;
202  StringVec trackerNames ;
203  trackerNames.resize( NTRACKER ) ;
204  trackerNames[VTXINDEX] = "VTX" ;
205  trackerNames[SITINDEX] = "SIT" ;
206  trackerNames[TPCINDEX] = "TPC" ;
207 
208  trkVec->parameters().setValues( "TrackSubdetectorNames" , trackerNames ) ;
209 
210  trk->subdetectorHitNumbers().resize( NTRACKER ) ;
211  trk->subdetectorHitNumbers()[ VTXINDEX ] = 12 ;
212  trk->subdetectorHitNumbers()[ SITINDEX ] = 24 ;
213  trk->subdetectorHitNumbers()[ TPCINDEX ] = 36 ;
214 
215  trk->setdEdx( 3.14159 ) ;
216  trk->setdEdxError( 42. ) ;
217  float cov[15] = { 1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15. } ;
218  trk->setCovMatrix( cov ) ;
219  float ref[3] = { 12. ,123456789. , .0987654321 } ;
220  trk->setReferencePoint( ref ) ;
221 
222  // add some random hits
223  int iHit1 = (int) ( double (trkhitVec->size()) * rand() / RAND_MAX ) ;
224  int iHit2 = (int) ( double (trkhitVec->size()) * rand() / RAND_MAX ) ;
225  int iHit3 = (int) ( double (trkhitVec->size()) * rand() / RAND_MAX ) ;
226 
227  trk->addHit( dynamic_cast<TrackerHit*>( (*trkhitVec)[iHit1] ) ) ;
228  trk->addHit( dynamic_cast<TrackerHit*>( (*trkhitVec)[iHit2] ) ) ;
229  trk->addHit( dynamic_cast<TrackerHit*>( (*trkhitVec)[iHit3] ) ) ;
230 
231 
232  // add tracks that where used to create this track
233  if( trkVec->size() > 1 ){
234  trk->addTrack( dynamic_cast<TrackImpl*> ( (*trkVec)[ trkVec->size() - 1 ] ) ) ;
235  trk->addTrack( dynamic_cast<TrackImpl*> ( (*trkVec)[ trkVec->size() - 2 ] ) ) ;
236  }
237 
238  trkVec->addElement( trk ) ;
239  }
240 
241 
242  evt->addCollection( trkVec , "SomeTracks" ) ;
243 
244 
245  // create some clusters and add them to the event
246  std::string simcalHitName( "ECAL007" ) ;
247 
248  LCCollection* simcalHits = evt->getCollection( simcalHitName ) ;
249 
250  // // create a collection with copied simhits and modify these
251  // // (test copy constructor - NOT YET AVAILABLE FOR OTHER CLASSES !)
252  // LCCollection* modifiedSimCalHits = new LCCollectionVec( LCIO::SIMCALORIMETERHIT );
253 
254  LCCollectionVec* clusterVec = new LCCollectionVec( LCIO::CLUSTER ) ;
255  LCCollectionVec* calHits = new LCCollectionVec( LCIO::CALORIMETERHIT ) ;
256  // in order to be able to point back to hits, we need to create
257  // generic CalorimeterHits from the SimCalorimeterHits first
258 
259 
260  LCCollectionVec* scRel = new LCCollectionVec(LCIO::LCRELATION ) ;
261  scRel->parameters().setValue( "RelationFromType" , LCIO::CALORIMETERHIT ) ;
262  scRel->parameters().setValue( "RelationToType" , LCIO::SIMCALORIMETERHIT ) ;
263 
264  int nSimHits = simcalHits->getNumberOfElements() ;
265  for(int j=0;j<nSimHits;j++){
266 
267  CalorimeterHitImpl* calHit = new CalorimeterHitImpl ;
268  SimCalorimeterHit* simcalHit = dynamic_cast<SimCalorimeterHit*> ( simcalHits->getElementAt(j) ) ;
269 
270  // std::cout << " adding new calorimeter hit and relation : " << j << " : " << calHit << " - " << simcalHit << std::endl ;
271 
272  calHit->setEnergy( simcalHit->getEnergy() ) ;
273  calHit->setCellID0( simcalHit->getCellID0() ) ;
274  calHit->setPosition( simcalHit->getPosition()) ;
275 
276  // scRel->addRelation( calHit , simcalHit , 0.5 ) ;
277  // scRel->addRelation( calHit , simcalHit , 0.5 ) ;
278  scRel->addElement( new LCRelationImpl( calHit , simcalHit , 0.5 ) ) ;
279  scRel->addElement( new LCRelationImpl( calHit , simcalHit , 0.5 ) ) ;
280  scRel->addElement( new LCRelationImpl( calHit , simcalHit , 0.5 ) ) ;
281  calHits->addElement( calHit ) ;
282 
283  // // create a copy of sim hit and modify it
284  // SimCalorimeterHitImpl* mSimHit = new SimCalorimeterHitImpl( *simcalHit ) ;
285  // mSimHit->setEnergy( mSimHit->getEnergy() * 1000. ) ;
286  // modifiedSimCalHits->addElement( mSimHit ) ;
287 
288  }
289  evt->addCollection( calHits , "CalorimeterHits") ;
290  // evt->addCollection( modifiedSimCalHits , "ModifiedSimCalorimeterHits") ;
291 
292  // LCFlagImpl relFlag(0) ;
293  // relFlag.setBit( LCIO::LCREL_WEIGHTED ) ;
294  // scRel->setFlag( relFlag.getFlag() ) ;
295  scRel->setFlag( UTIL::make_bitset32( LCIO::LCREL_WEIGHTED )) ;
296 
297  evt->addCollection( scRel , "CalorimeterHitsSimRel" ) ;
298  // evt->addRelation( scRel , "CalorimeterHitsSimRel" ) ;
299 
300 
301 
302 
303  if( evt->getEventNumber() == 0 && evt->getRunNumber() == 0 ) {
304 
305 
306  //------ the following is some example code on how to access the relation: --------------
307  // create a navigation object from a collection
308  LCRelationNavigator rel( scRel ) ;
309 
310  std::cout << "Relation example for first event: "
311  << " [" << rel.getFromType() << " - " << rel.getToType() << "] "
312  << std::endl ;
313 
314  int nCalHits = calHits->getNumberOfElements() ;
315  for(int j=0; j < nCalHits ; j++){
316  CalorimeterHit* calHit = dynamic_cast<CalorimeterHit*>( calHits->getElementAt(j) ) ;
317 
318  std::cout << " relations for object " << dec << calHit->id()
319  ; // << std::endl ;
320 
321  const LCObjectVec& simHits = rel.getRelatedToObjects( calHit ) ;
322  const FloatVec& weights = rel.getRelatedToWeights( calHit ) ;
323 
324  for(int k=0,nSH = simHits.size(); k<nSH;k++){
325 
326  std::cout << " [" << simHits[k]->id() << "] ("
327  << weights[k] << ") " ;
328  }
329  std::cout << dec << std::endl ;
330  }
331 
332 
333  // -------------------------------------------------------------------------------------
334 
335  // add some calibration constants as generic user objects
336 
337  LCCollectionVec* calVec = new LCCollectionVec( LCIO::LCGENERICOBJECT ) ;
338  for(int j=0;j<nCalHits;j++){
339 
340  CalorimeterHit* calHit = dynamic_cast<CalorimeterHit*>( calHits->getElementAt(j) ) ;
341 
342  CalibrationConstant* cCon = new CalibrationConstant( calHit->getCellID0() ,
343  1.*j , 0.01*j );
344  calVec->addElement( cCon ) ;
345  }
346 
347  evt->addCollection( calVec , "Calibration" ) ;
348  }
349 
350  // debug test: add empty collection of LCGenericObjects
351  LCCollectionVec* emtpyCol = new LCCollectionVec( LCIO::LCGENERICOBJECT ) ;
352  evt->addCollection( emtpyCol , "EmptyLCGenericObject" ) ;
353 
354  // -------------------------------------------------------------------------------------
355 
356  // if we want to point back to the hits we need to set the flag
357  // LCFlagImpl clusterFlag(0) ;
358  // clusterFlag.setBit( LCIO::CLBIT_HITS ) ;
359  // clusterVec->setFlag( clusterFlag.getFlag() ) ;
360  clusterVec->setFlag( UTIL::make_bitset32( LCIO::CLBIT_HITS )) ;
361 
362  StringVec shapeParams ;
363  shapeParams.push_back("Shape_trans") ;
364  shapeParams.push_back("Shape_long") ;
365  shapeParams.push_back("Shape_axis_x") ;
366  shapeParams.push_back("Shape_axis_y") ;
367  shapeParams.push_back("Shape_axis_z") ;
368  shapeParams.push_back("Shape_quality") ;
369 
370  clusterVec->parameters().setValues( "ClusterShapeParameters" , shapeParams ) ;
371 
372  // IntVec algoIDs ;
373  // enum {
374  // RunEventProcessorID = 1 ,
375  // anotherAlgorithmID,
376  // andYetAnotherAlgorithmID
377  // } ;
378 
379  // algoIDs.push_back( RunEventProcessorID ) ;
380  // algoIDs.push_back( anotherAlgorithmID ) ;
381  // algoIDs.push_back( andYetAnotherAlgorithmID ) ;
382 
383  // StringVec algoNames ;
384  // algoNames.push_back("recojob-RunEventProcessor") ;
385  // algoNames.push_back("anotherAlgorithm") ;
386  // algoNames.push_back("andYetAnotherAlgorithm") ;
387 
388  // clusterVec->parameters().setValues( "PIDAlgorithmTypeName" , algoNames ) ;
389  // clusterVec->parameters().setValues( "PIDAlgorithmTypeID" , algoIDs ) ;
390 
391 
392 
393  if( calHits ){
394 
395  int nHits = calHits->getNumberOfElements() ;
396  int nCluster = nHits / 10 ;
397 
398 
399 
400  PIDHandler cluPidHandler( clusterVec ) ;
401 
402  StringVec pNames ;
403  pNames.push_back( "param0" );
404  pNames.push_back( "param1" );
405  pNames.push_back( "param2" );
406 
407  IntVec algoIDs(3) ;
408  algoIDs[0] = cluPidHandler.addAlgorithm( "recojobRunEventProcessor" , pNames ) ;
409  algoIDs[1] = cluPidHandler.addAlgorithm( "anotherAlgorithm" , pNames ) ;
410  algoIDs[2] = cluPidHandler.addAlgorithm( "andYetAnotherAlgorithm" , pNames ) ;
411 
412  for( int i=0; i < nCluster ; i ++ ){
413 
414  ClusterImpl* cluster = new ClusterImpl ;
415 
416  // int type = ( Cluster::COMBINED << 16 | Cluster::CHARGED ) ;
417  cluster->setTypeBit( 1 ) ;
418  cluster->setTypeBit( 7 ) ;
419  cluster->setTypeBit( 11 ) ;
420 
421  cluster->setEnergy( (i+1)*1.1 ) ;
422  float pos[3] = { 12. ,123456789. , .0987654321 } ;
423  cluster->setPosition( pos ) ;
424  float errpos[6] = { 1.,2.,3.,4.,5.,6.} ;
425  cluster->setPositionError( errpos ) ;
426  cluster->setITheta( (i+1)* M_PI / 10. ) ;
427  cluster->setIPhi( (i+1)* M_PI / 5. ) ;
428  float errdir[6] = { 1.,2.,3.} ;
429  cluster->setDirectionError( errdir ) ;
430 
431  // set the cluster ashape variables
432  float shapeArray[6] = { 1.,2.,3.,3.,2.,1.} ;
433  FloatVec shape ;
434  copy( &shapeArray[0] , &shapeArray[5] , back_inserter( shape ) ) ;
435  cluster->setShape( shape ) ;
436 
437  // // add some particle ids
438  // int nPID = 5 ;
439  // for(int j=0;j<nPID;j++){
440  // ParticleIDImpl* pid = new ParticleIDImpl ;
441  // pid->setLikelihood( (double) j / nPID ) ;
442  // pid->setType( j ) ;
443  // pid->setPDG( -11 ) ;
444  // pid->setAlgorithmType( RunEventProcessorID ) ;
445 
446  // for(int k=0;k<3;k++){
447  // pid->addParameter( k*.1 ) ;
448  // }
449  // cluster->addParticleID( pid ) ;
450  // }
451  // add some particle ids
452  int nPID = algoIDs.size() ;
453 
454  for(int j=0;j<nPID;j++){
455 
456 
457  // some parameters
458  FloatVec fv( pNames.size() ) ;
459  for( unsigned k=0 ; k < pNames.size() ; k++){
460  fv[k] = j*1000.+k*.1 ;
461  }
462 
463  cluPidHandler.setParticleID( cluster,
464  j , // user type
465  22 , // PDG
466  1.*j / nPID , // likelihood
467  algoIDs[j] ,
468  fv ) ;
469 
470  }
471 
472 
473  // add some subdetector energies
474  const int NCALORIMETER = 2 ;
475  const int ECALINDEX = 0 ;
476  const int HCALINDEX = 1 ;
477  StringVec detNames ;
478  detNames.resize( NCALORIMETER ) ;
479  detNames[ECALINDEX] = "Ecal" ;
480  detNames[HCALINDEX] = "Hcal" ;
481  clusterVec->parameters().setValues( "ClusterSubdetectorNames" , detNames ) ;
482 
483 
484  cluster->subdetectorEnergies().resize( NCALORIMETER ) ;
485  cluster->subdetectorEnergies()[ ECALINDEX ] = 42.42 ;
486  cluster->subdetectorEnergies()[ HCALINDEX ] = 24.24 ;
487 
488  // add some random hits
489  int iHit1 = (int) ( double (calHits->size()) * rand() / RAND_MAX ) ;
490  int iHit2 = (int) ( double (calHits->size()) * rand() / RAND_MAX ) ;
491  int iHit3 = (int) ( double (calHits->size()) * rand() / RAND_MAX ) ;
492 
493  cluster->addHit( dynamic_cast<CalorimeterHit*>( (*calHits)[iHit1] ) , 1.0 ) ;
494  cluster->addHit( dynamic_cast<CalorimeterHit*>( (*calHits)[iHit2] ) , 2.0 ) ;
495  cluster->addHit( dynamic_cast<CalorimeterHit*>( (*calHits)[iHit3] ) , 3.0 ) ;
496 
497  // add clusters that where used to create this cluster
498  if( clusterVec->size() > 1 ){
499  cluster->addCluster( dynamic_cast<ClusterImpl*>
500  ( (*clusterVec)[ clusterVec->size() - 1 ] ) ) ;
501  cluster->addCluster( dynamic_cast<ClusterImpl*>
502  ( (*clusterVec)[ clusterVec->size() - 2 ] ) ) ;
503  }
504 
505 
506  clusterVec->addElement( cluster ) ;
507  }
508  }
509 
510  evt->addCollection( clusterVec , "SomeClusters" ) ;
511 
512  // add some vertices
513  LCCollectionVec* vertexVec = new LCCollectionVec( LCIO::VERTEX ) ;
514 
515  //EXP: INDEX MAP - UNDER DEVELOPMENT
516  //UTIL::IndexMap imvtx(vertexVec, "AlgorithmNames", "AlgorithmTypes");
517 
518  for(int i=0; i < (nRecP+1); i++){
519  VertexImpl* vtx = new VertexImpl ;
520  if(i==0){
521  vtx->setPrimary(true);
522  }else{
523  vtx->setPrimary(false);
524  }
525  /*
526  //EXP: INDEX MAP - UNDER DEVELOPMENT
527 
528  switch(i){
529  case 0: vtx->setAlgorithmType( imvtx.encode( "ZvTop" ) ); break;
530  case 1: vtx->setAlgorithmType( imvtx.encode( "ZvKin" ) ); break;
531  case 5: vtx->setAlgorithmType( imvtx.encode( "SimAnnealing" ) ); break;
532  default: break;
533  }
534  */
535 
536  //EXP: INDEX MAP V2 - UNDER DEVELOPMENT
537  switch(rand()%7){
538  case 0: vtx->setAlgorithmType( "ZvTop" ); break;
539  case 1: vtx->setAlgorithmType( "ZvKin" ); break;
540  case 2: vtx->setAlgorithmType( "42" ); break;
541  case 3: vtx->setAlgorithmType( "SimAnnealing" ); break;
542  case 5: vtx->setAlgorithmType( "_Test" ); break;
543  default: break;
544  }
545 
546  vtx->setChi2(1+i*.01);
547  vtx->setProbability(0.0032+i*.01);
548  vtx->setPosition(0.3453+i*.01,.45345354+i*.01,2.345354+i*.01);
549 
550  FloatVec cov(6) ;
551  cov[0] = 1. ;
552  cov[1] = 2. ;
553  cov[2] = 3. ;
554  cov[3] = 4. ;
555  cov[4] = 5. ;
556  cov[5] = 6. ;
557  vtx->setCovMatrix( cov ) ;
558  for(int j=0;j<3;j++){
559  vtx->addParameter( j*.1 ) ;
560  }
561 
562  vertexVec->addElement ( vtx ) ;
563  }
564 
565  evt->addCollection( vertexVec, "SomeVertices" ) ;
566 
567  // add some reconstructed particles
568  LCCollectionVec* particleVec = new LCCollectionVec( LCIO::RECONSTRUCTEDPARTICLE ) ;
569  // particleVec->parameters().setValues( "PIDAlgorithmTypeName" , algoNames ) ;
570  // particleVec->parameters().setValues( "PIDAlgorithmTypeID" , algoIDs ) ;
571 
572  if( particleVec ){
573 
574  PIDHandler recPIDHandler ( particleVec ) ;
575 
576  StringVec pNames ;
577  pNames.push_back( "param0" );
578  pNames.push_back( "param1" );
579  pNames.push_back( "param2" );
580  pNames.push_back( "param3" );
581  pNames.push_back( "param4" );
582 
583  IntVec aIDs(4) ;
584  aIDs[0] = recPIDHandler.addAlgorithm( "recojobRunEventProcessor" , pNames ) ;
585  aIDs[1] = recPIDHandler.addAlgorithm( "anotherAlgorithm" , pNames ) ;
586  aIDs[2] = recPIDHandler.addAlgorithm( "andYetAnotherAlgorithm" , pNames ) ;
587  aIDs[3] = recPIDHandler.addAlgorithm( "andEvenAFourthAlgorithm" , pNames ) ;
588 
589  for(int i=0;i<nRecP;i++){
590  ReconstructedParticleImpl * part = new ReconstructedParticleImpl ;
591  part->setType( 42 ) ;
592 
593  float p[3] = { 1.1 , 2.2 , 3.3 } ;
594  part->setMomentum( p ) ;
595  part->setEnergy( i*101.101 ) ;
596 
597  float covA[] = { 1.,2.,3.,4.,5.,6.,7.,8.,9.,10. } ;
598  FloatVec cov(10) ;
599  for(int j=0;j<10;j++) cov[j] = covA[j] ;
600 
601 
602  part->setCovMatrix( cov) ;
603  part->setMass( 0.511*i ) ;
604  part->setCharge( -2./3. ) ;
605  float x[3] = { 10.,20.,30. } ;
606  part->setReferencePoint( x ) ;
607 
608  //associate vertices
609  part->setStartVertex( dynamic_cast<Vertex*>( vertexVec->getElementAt(i) ) ) ;
610  VertexImpl* v = dynamic_cast<VertexImpl*>( vertexVec->getElementAt(i+1) ) ;
611  //part->setEndVertex( v ) ;
612  //associate particles to vertices
613  v->setAssociatedParticle( dynamic_cast<ReconstructedParticle*>( part ) ) ;
614 
615  // // add some particle ids
616  // int nPID = 5 ;
617  // for(int j=0;j<nPID;j++){
618  // ParticleIDImpl* pid = new ParticleIDImpl ;
619  // pid->setLikelihood( (double) j / nPID ) ;
620  // pid->setType( j ) ;
621  // pid->setPDG( -11 ) ;
622  // pid->setAlgorithmType( algoIDs[0] ) ;
623  // for(int k=0;k<3;k++){
624  // pid->addParameter( k*.1 ) ;
625  // }
626  // part->addParticleID( pid ) ;
627  // if( j == 2 )
628  // part->setParticleIDUsed( pid ) ;
629  // }
630 
631  // add some particle ids
632  int nPID = aIDs.size() ;
633 
634  for(int j=0;j<nPID;j++){
635 
636  // some parameters
637  FloatVec fv( pNames.size() ) ;
638  for( unsigned k=0 ; k < pNames.size() ; k++){
639  fv[k] = j*1000.+k*.1 ;
640  }
641 
642  recPIDHandler.setParticleID( part,
643  j*j , // user type
644  -11 , // PDG
645  42.*j / nPID , // likelihood
646  aIDs[j] ,
647  fv ) ;
648 
649  recPIDHandler.setParticleID( part,
650  j*j , // user type
651  13 , // PDG
652  42.*j / nPID , // likelihood
653  aIDs[j] ,
654  fv ) ;
655 
656  if( j == 2 )
657 
658  recPIDHandler.setParticleIDUsed( part, aIDs[j] ) ;
659 
660  }
661 
662  part->setGoodnessOfPID( 0.7 ) ;
663 
664  // some other particles
665  if( i > 1 ){
666  ReconstructedParticle* p1 =
667  dynamic_cast<ReconstructedParticle*> ( particleVec->getElementAt(i-1) ) ;
668  ReconstructedParticle* p2 =
669  dynamic_cast<ReconstructedParticle*> ( particleVec->getElementAt(i-2) ) ;
670  part->addParticle( p1 ) ;
671  part->addParticle( p2 ) ;
672  }
673  //a track
674  int iTrk = (int) ( double (trkVec->size()) * rand() / RAND_MAX ) ;
675  Track* trk = dynamic_cast<Track*> ( trkVec->getElementAt( iTrk ) ) ;
676  part->addTrack( trk ) ;
677 
678  // a cluster
679  int iClu = (int) ( double (clusterVec->size()) * rand() / RAND_MAX ) ;
680  Cluster* clu = dynamic_cast<Cluster*> ( clusterVec->getElementAt( iClu ) ) ;
681  part->addCluster( clu ) ;
682 
683  // // and finaly an MCParticle
684  // LCCollection* mcVec = evt->getCollection( LCIO::MCPARTICLE ) ;
685  // int iMCP = (int) ( double (mcVec->getNumberOfElements()) * rand() / RAND_MAX ) ;
686  // MCParticle* mcp = dynamic_cast<MCParticle*>( mcVec->getElementAt( iMCP ) ) ;
687  // part->addMCParticle( mcp , 0.5 ) ;
688 
689  particleVec->addElement( part ) ;
690  }
691  }
692 
693  evt->addCollection( particleVec, "ReconstructedParticle" ) ;
694 
695  nEvent ++ ;
696 
697 
698 
699  LCTOOLS::dumpEvent( evt ) ;
700 
701 
702  lcWrt->writeEvent( evt ) ;
703  }
704 
705  void modifyEvent( LCEvent * evt ) {
706 
707  // here we can modify existing objects that have been read from a stream:
708  LCCollection* mcVec = evt->getCollection( LCIO::MCPARTICLE ) ;
709  int NMCPART = mcVec->getNumberOfElements() ;
710  for(int i=0 ; i< NMCPART ; i++ ){
711  // in order to have access to the set-methods we need to cast to the implementation
712  // of MCParticle
713  MCParticleImpl* part = dynamic_cast<MCParticleImpl*>( mcVec->getElementAt(i)) ;
714  part->setPDG(1234) ; // <<<<< modifying persistent data
715  }
716  // or we could add sth. to existing collections
717  MCParticleImpl* part = new MCParticleImpl ;
718  part->setPDG( 1234 ) ;
719  part->addParent( dynamic_cast<MCParticle*>( mcVec->getElementAt(0) )) ;
720  mcVec->addElement( part ) ; // <<<< adding to collections
721 
722  }
723 
724 
725  void processRunHeader( LCRunHeader* run){
726 
727  // just copy run headers to the outputfile
728  lcWrt->writeRunHeader( run ) ;
729  }
730 
731  void modifyRunHeader(LCRunHeader* /*run*/){/* we don't modify anything */;}
732 
733 
734 } ;
735 
736 //=============================================================================
737 
738 int main(int argc, char** argv ){
739 
740  srand(1234) ;
741 
742  // create reader and writer for input and output streams
743  LCReader* lcReader = LCFactory::getInstance()->createLCReader() ;
744 
745 
746  // read file name from command line
747  if( argc > 1 ) { FILEN = argv[1] ; }
748  if( argc > 2 ) { OUTFILEN = argv[2] ; }
749 
750  lcReader->open( FILEN ) ;
751  // we could catch the exception here - but this not really needed
752  // as long as we exit anyhow if the file could not be opened...
753  // try{ lcReader->open( FILEN ) ; }
754  // catch( IOException& e){
755  // cout << "Can't open file : " << e.what() << endl ;
756  // exit(1) ;
757  // }
758 
759  // create a new RunEventProcessor, register it with the reader
760  // and read and proccess the whole stream
761  {
762  RunEventProcessor evtProc ;
763 
764  lcReader->registerLCRunListener( &evtProc ) ;
765  lcReader->registerLCEventListener( &evtProc ) ;
766 
767  lcReader->readStream() ;
768 
769  // lcReader->readStream( 5) ; // debugging: only read 4 events
770  // lcReader->readStream( 10000 ) ; // debugging: provoke EndOfDataException
771 
772  }
773 
774  lcReader->close() ;
775  delete lcReader ;
776  return 0 ;
777 }
778 
779 //=============================================================================
780 
T copy(T...args)
std::vector< std::string > StringVec
Vector of strings.
Definition: LCIOSTLTypes.h:16
T rand(T...args)
std::vector< LCObject * > LCObjectVec
Vector of (pointers to) LCObjects.
Definition: LCObject.h:17
#define M_PI
Definition: recjob.cc:31
std::vector< float > FloatVec
Vector of floats.
Definition: LCIOSTLTypes.h:18
T endl(T...args)
T srand(T...args)
Little tool that copies LCIO files on an event by event and run by run basis, thus fixing files that ...
Definition: copyfix.cc:23
T resize(T...args)
STL class.
void processEvent(LCEvent *evt)
Definition: recjob.cc:96
T push_back(T...args)
static const int NMCPART
static const int nRecP
Definition: recjob.cc:41
T exit(T...args)
int main(int argc, char **argv)
Simple program that opens existing LCIO files and appends the records needed for direct access - if t...
void processRunHeader(LCRunHeader *run)
Definition: recjob.cc:725
Example for a simple calibration class based on the LCFixedObject template.
T ref(T...args)
std::vector< int > IntVec
Vector of ints.
Definition: LCIOSTLTypes.h:22
static std::string FILEN
Definition: recjob.cc:39
T back_inserter(T...args)
static std::string OUTFILEN
Definition: recjob.cc:40
LCReader * lcReader
Definition: lsh.cc:78
void modifyRunHeader(LCRunHeader *)
Definition: recjob.cc:731
T dec(T...args)
BitSet32 make_bitset32(int bit0)
Convenient helper that creates a BitSet32 with bit0 set.
Definition: BitSet32.h:49
void dumpEvent(EVENT::LCEvent *event)
void modifyEvent(LCEvent *evt)
Definition: recjob.cc:705