LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simjob.cc
Go to the documentation of this file.
1 #include "lcio.h"
2 
3 #include "IO/LCWriter.h"
4 #include "EVENT/LCIO.h"
5 #include "DATA/LCFloatVec.h"
6 #include "DATA/LCIntVec.h"
7 
8 #include "IMPL/LCEventImpl.h"
9 #include "IMPL/LCRunHeaderImpl.h"
10 #include "IMPL/LCCollectionVec.h"
12 #include "IMPL/SimTrackerHitImpl.h"
13 #include "IMPL/MCParticleImpl.h"
14 //#include "IMPL/LCFlagImpl.h"
15 #include "IMPL/LCTOOLS.h"
16 
18 #include "IMPL/TrackerDataImpl.h"
19 #include "IMPL/TrackerPulseImpl.h"
20 
22 #include "UTIL/LCTime.h"
23 //#include "UTIL/BitField64.h"
24 #include "UTIL/CellIDEncoder.h"
25 #include "UTIL/LCTypedVector.h"
26 #include "UTIL/LCSplitWriter.h"
27 #include "UTIL/BitSet32.h"
28 
29 // #include "UTIL/LCIOTypeInfo.h"
30 
31 
32 #include <cstdlib>
33 #include <iostream>
34 #include <sstream>
35 
36 
37 using namespace std ;
38 using namespace lcio ;
39 
40 static const int NRUN = 10 ;
41 static const int NEVENT = 10 ; // events
42 static const int NMCPART = 10 ; // mc particles per event
43 static const int NHITS = 50 ; // calorimeter hits per event
44 
45 static string FILEN = "simjob.slcio" ;
46 
47 
48 //struct MyTrackLink : public LCObjectLinkTraits< SimTrackerHit, MyTrackLink > {} ;
49 
53 int main(int argc, char** argv ){
54 
55  try{
56 
57 
58  // loop over runs
59  for(int rn=0;rn<NRUN;rn++){
60 
61  // create sio writer
62  LCWriter* lcWrt = LCFactory::getInstance()->createLCWriter() ;
63 
64  //LCWriter* lcWrt = new LCSplitWriter( LCFactory::getInstance()->createLCWriter(), 20000 ) ;
65 
66  if( argc > 1 ) { FILEN = argv[1] ; }
67 
68  // lcWrt->setCompressionLevel(0 ) ;
69 
70  if( rn==0 ){
71  // turn off compression for first run ...
72  lcWrt->setCompressionLevel( 0 ) ;
73  lcWrt->open( FILEN , LCIO::WRITE_NEW ) ;
74 
75  }else{
76  lcWrt->setCompressionLevel( 9 ) ;
77  lcWrt->open( FILEN , LCIO::WRITE_APPEND ) ;
78  }
79  // NB: in order to test writing multiple files we create a new LCWriter
80  // for every run even though we are in fact writing to one file only;
81  // so for a simple job writing one file the
82  // 'createLCWriter/open' and 'close/delete' will be outside the run loop...
83 
84 
85  LCRunHeaderImpl* runHdr = new LCRunHeaderImpl ;
86  runHdr->setRunNumber( rn ) ;
87 
88  string detName("D09TileHcal") ;
89  runHdr->setDetectorName( detName ) ;
90 
91  stringstream description ;
92  description << " run: " << rn <<" just for testing lcio - no physics !" ;
93  runHdr->setDescription( description.str() ) ;
94 
95  string ecalName("ECAL007") ;
96  runHdr->addActiveSubdetector( ecalName ) ;
97 
98  string tpcName("TPC4711") ;
99  runHdr->addActiveSubdetector( tpcName ) ;
100 
101 
102  // add some parameters to the run header
103 // StringVec sv1 ;
104 // sv1.push_back("simjob.cc") ;
105 // runHdr->parameters().setValues( "SimulationProgram" , sv1 ) ;
106  runHdr->parameters().setValue( "SimulationProgram" , "simjob.cc" ) ;
107  IntVec iv(3) ;
108  iv[0] = 1 ;
109  iv[1] = 2 ;
110  iv[2] = 3 ;
111  runHdr->parameters().setValues( "SomeIndices" , iv ) ;
112 
113  lcWrt->writeRunHeader( runHdr ) ;
114 
115  // EventLoop - create some events and write them to the file
116  for(int i=0;i<NEVENT;i++){
117 
118  // we need to use the implementation classes here
119  LCEventImpl* evt = new LCEventImpl() ;
120 
121 
122  evt->setRunNumber( rn ) ;
123  evt->setEventNumber( i ) ;
124  LCTime now ;
125  evt->setTimeStamp( now.timeStamp() ) ;
126  evt->setDetectorName( detName ) ;
127 
128  evt->setWeight( 1.*rand()/RAND_MAX ) ;
129 
130  evt->parameters().setValue("Description"," event can have it's own set of parameters" ) ;
131  evt->parameters().setValue("Thrust", (float) 0.671 ) ;
132 
133  FloatVec fv ;
134  fv.push_back( 1.1 ) ;
135  fv.push_back( 2.2 ) ;
136  fv.push_back( 3.3 ) ;
137  evt->parameters().setValues( "SomeNumbers" , fv ) ;
138 
139  DoubleVec dv ;
140  dv.push_back( 1.111111111111111111111111111111111111111111111111 ) ;
141  dv.push_back( 2.222222222222222222222222222222222222222222222222 ) ;
142  dv.push_back( 3.333333333333333333333333333333333333333333333333 ) ;
143  evt->parameters().setValues( "SomeDoubleNumbers" , dv ) ;
144 
145 
146  // create and add some mc particles
147  LCCollectionVec* mcVec = new LCCollectionVec( LCIO::MCPARTICLE ) ;
148 
149  // debug only - don't write MCParticles to output file:
150  // mcVec->setTransient() ;
151 
152  // debug only - add the same particle to more than one collection
153  //LCCollectionVec* mcVec2 = new LCCollectionVec( LCIO::MCPARTICLE ) ;
154 
155  MCParticleImpl* mom = new MCParticleImpl ;
156  mom->setPDG( 1 ) ;
157  float p0[3] = { 0. , 0. , 1000. } ;
158  mom->setMomentum( p0 ) ;
159  mom->setMass( 3.01 ) ;
160 
161 
162  for(int j=0;j<NMCPART;j++){
163 
164  MCParticleImpl* mcp = new MCParticleImpl ;
165 
166  mcp->setPDG( 1000 * (j+1) ) ;
167  float p[3] = { float(j*1.) , float(4./1024.) , float(8./1024.) } ;
168  mcp->setMomentum( p ) ;
169  mcp->setMass( .135 ) ;
170 
171  // create and add some daughters
172  for(int k=0;k<3;k++){
173  MCParticleImpl* d1 = new MCParticleImpl ;
174 
175  d1->setPDG( 1000 * (j+1) + 100 * (k+1) ) ;
176  float pd1[3] = { float(k*1.) , float(4.1) , float(8.1) } ;
177  d1->setMomentum( pd1 ) ;
178  d1->setMass( .135 ) ;
179 
180  for(int l=0;l<2;l++){
181  MCParticleImpl* d2 = new MCParticleImpl ;
182 
183  d2->setPDG( 1000 * (j+1) + 100 * (k+1) + 10 * (l+1) ) ;
184  float pd2[3] = { float(l*1.) , float(0.41) , float(4.1) } ;
185  d2->setMomentum( pd2 ) ;
186  d2->setMass( .135 ) ;
187 
188  double ep[3] = { 1.111111 , 2.2222222, 3.3333333 } ;
189  d2->setEndpoint( ep ) ;
190  // d2->setSimulatorStatus( 1234 ) ;
191  d2->setCreatedInSimulation(true) ;
192  d2->setBackscatter(true) ;
193  d2->setDecayedInTracker(true) ;
194  d2->setDecayedInCalorimeter(false);
195  d2->setHasLeftDetector(false) ;
196  d2->setStopped(true) ;
197 
198  d2->addParent( d1 );
199  mcVec->push_back( d2 ) ;
200 
201  // debug only - add the same particle to more than one collection
202  //mcVec2->push_back( d2 ) ;
203  }
204  d1->addParent( mcp );
205  mcVec->push_back( d1 ) ;
206  }
207 
208  mcp->addParent( mom );
209  mcVec->push_back( mcp ) ;
210  }
211  mcVec->push_back( mom ) ;
212 
213  // now add some calorimeter hits
214  LCCollectionVec* calVec = new LCCollectionVec( LCIO::SIMCALORIMETERHIT ) ;
215 
216  // set flag for long format (including position )
217  // and PDG and cellid1
218  // LCFlagImpl chFlag(0) ;
219  // chFlag.setBit( LCIO::CHBIT_LONG ) ;
220  // chFlag.setBit( LCIO::CHBIT_STEP ) ;
221  // calVec->setFlag( chFlag.getFlag() ) ;
222 
223  calVec->setFlag( UTIL::make_bitset32( LCIO::CHBIT_LONG, LCIO::CHBIT_STEP ) );
224 
225 
226 
227  std::string cellIDEncoding( "M:3,S-1:3,I:9,J:9,K-1:6") ;// old Mokka convention
228 
229 // std::string cellIDEncoding( "M:3,S-1:3,I:9,J:9,K-1:6,Bla:34:6") ;// for testing cellid1
230 
231  CellIDEncoder<SimCalorimeterHitImpl> b( cellIDEncoding , calVec ) ;
232 
233  for(int j=0;j<NHITS;j++){
234 
235  SimCalorimeterHitImpl* hit = new SimCalorimeterHitImpl ;
236 
237  hit->setEnergy( 3.1415 * rand()/RAND_MAX ) ;
238 
239  float pos[3] = { float(1.1* rand()/RAND_MAX) , float(2.2* rand()/RAND_MAX) , float(3.3* rand()/RAND_MAX) } ;
240 
241  // cell indices
242  b["M"] = j % 8 ;
243  b["S-1"] = (j+2) % 8 ;
244  b["I"] = j % 512 ;
245  b["J"] = (j+128) % 512 ;
246  b["K-1"] = (j+32) % 64 ;
247 
248  b.setCellID( hit ) ;
249 
250 // hit->setCellID0( b.lowWord() ) ;
251 // hit->setCellID1( b.highWord() ) ;
252 
253  hit->setPosition( pos ) ;
254 
255  calVec->push_back( hit ) ;
256 
257  // assign the hits randomly to MC particles
258  double rnd = .99999*rand()/RAND_MAX ;
259  int mcIndx = static_cast<int>( NMCPART * rnd ) ;
260 
261  // in order to access a MCParticle, we need a dynamic cast as the
262  // LCCollection returns an LCIOObject - this is like vectors in Java
263  hit->addMCParticleContribution( dynamic_cast<MCParticle*>(mcVec->getElementAt( mcIndx )) ,
264  0.314159, 0.1155, 42., 1, pos ) ;
265 
266  }
267 
268  // -------- data can be modified as long as is not not made persistent --------
269 
270  for(int j=0;j<NHITS;j++){
271  SimCalorimeterHitImpl* existingHit
272  = dynamic_cast<SimCalorimeterHitImpl*>( calVec->getElementAt(j) ) ; // << Ok now
273 
274  existingHit->addMCParticleContribution( dynamic_cast<MCParticle*>
275  (mcVec->getElementAt(0)),
276  0.1, 0. ) ;
277  }
278 
279 
280  // and finally some tracker hits
281  // with some user extensions (4 floats and 2 ints) per track:
282  // we just need to create parallel collections of float and int vectors
283  LCCollectionVec* trkVec = new LCCollectionVec( LCIO::SIMTRACKERHIT ) ;
284  LCCollectionVec* extFVec = new LCCollectionVec( LCIO::LCFLOATVEC ) ;
285  LCCollectionVec* extIVec = new LCCollectionVec( LCIO::LCINTVEC ) ;
286 
287  // LCFlagImpl thFlag(0) ;
288  // thFlag.setBit( LCIO::THBIT_MOMENTUM ) ;
289  // trkVec->setFlag( thFlag.getFlag() ) ;
290  trkVec->setFlag( UTIL::make_bitset32( LCIO::THBIT_MOMENTUM ) ) ;
291 
292  LCTypedVector<MCParticle> mcpTV( mcVec ) ;
293 
294  CellIDEncoder<SimTrackerHitImpl> cd( "i:8,j:8,k:8" ,trkVec ) ;
295 
296  for(int j=0;j<NHITS;j++){
297 
298  SimTrackerHitImpl* hit = new SimTrackerHitImpl ;
299 
300  cd["i"] = j ;
301  cd["j"] = j + 100 ;
302  cd["k"] = j + 200 ;
303 
304  cd.setCellID( hit ) ;
305 
306  LCFloatVec* extF = new LCFloatVec ;
307  LCIntVec* extI = new LCIntVec ;
308 
309  //hit->setdEdx( 30e-9 ) ;
310  hit->setEDep( 30e-9 ) ;
311 
312  double pos[3] = { 1.1* rand()/RAND_MAX , 2.2* rand()/RAND_MAX , 3.3* rand()/RAND_MAX } ;
313 
314  hit->setPosition( pos ) ;
315 
316  // assign the hits randomly to MC particles
317  float rnd = .99999*rand()/RAND_MAX ;
318  int mcIndx = static_cast<int>( NMCPART * rnd ) ;
319 
320 
321 // hit->setMCParticle( dynamic_cast<MCParticle*>(mcVec->getElementAt( mcIndx ) ) ) ;
322  hit->setMCParticle( mcpTV[ mcIndx ] ) ;
323 
324  hit->setMomentum( 1. , 2. , 3. ) ;
325  hit->setPathLength( .042 ) ;
326 
327  // fill the extension vectors (4 floats, 2 ints)
328  extF->push_back( 3.14159 ) ;
329  for(int k=0;k<3;k++) extF->push_back( pos[k] * 0.1 ) ;
330 
331  extI->push_back( 123456789 ) ;
332  extI->push_back( mcIndx ) ;
333 
334  // add the hit and the extensions to their corresponding collections
335  trkVec->push_back( hit ) ;
336  extFVec->push_back( extF ) ;
337  extIVec->push_back( extI ) ;
338  }
339 
340 
341  // add all collections to the event
342  evt->addCollection( mcVec , "MCParticle" ) ;
343 
344  //deubg only
345  //evt->addCollection( mcVec2, "MCParticle2" ) ;
346 
347  evt->addCollection( calVec , ecalName ) ;
348  evt->addCollection( trkVec , tpcName ) ;
349  evt->addCollection( extFVec , tpcName+"UserFloatExtension" ) ;
350  evt->addCollection( extIVec , tpcName+"UserIntExtension" ) ;
351 
352  // test: add a collection for one event only:
353 // if( rn == NRUN-1 && i == 0 ) { // first event o last run
354  if( rn == 1 && i == 0 ) { // first event o last run
355  LCCollectionVec* addExtVec = new LCCollectionVec( LCIO::LCFLOATVEC ) ;
356  LCFloatVec* addExt = new LCFloatVec ;
357  addExt->push_back( 1. );
358  addExt->push_back( 2. );
359  addExt->push_back( 3. );
360  addExt->push_back( 4. );
361  addExtVec->push_back( addExt ) ;
362  evt->addCollection( addExtVec , "AdditionalExtension" ) ;
363  }
364  //---- write a subset of MCParticle to the event ------
365  LCCollectionVec* mcSubVec = new LCCollectionVec( LCIO::MCPARTICLE ) ;
366  mcSubVec->setSubset(true) ;
367 
368  for(int j=0;j< mcVec->getNumberOfElements() ; j++ ){
369 
370  MCParticle* p = dynamic_cast< MCParticle*>( mcVec->getElementAt(j) ) ;
371  if( p->getDaughters().size() == 0 )
372  mcSubVec->addElement( p ) ;
373  }
374  evt->addCollection( mcSubVec , "FinalMCParticles" ) ;
375  //-----------------------------------------------------
376 
377 
378 
379  // even though this is a simjob we can store 'real data' objects :)
380 
381 #define WRITE_TRACKERRAWDATA 1
382 #ifdef WRITE_TRACKERRAWDATA
383  //--- write some new TPC raw data collections to the file
384  LCCollectionVec* tpcRawVec = new LCCollectionVec( LCIO::TRACKERRAWDATA ) ;
385 
386  for(int j=0;j<NHITS;j++){
387 
388  TrackerRawDataImpl* tpcRaw = new TrackerRawDataImpl ;
389 
390  tpcRaw->setCellID0( j ) ;
391  tpcRaw->setTime( -j ) ;
392 
393  if( j % 2 ) { // test two ways of setting the charge
394  ShortVec adcValues ;
395  adcValues.push_back( 42 ) ;
396  adcValues.push_back( 43 ) ;
397  adcValues.push_back( 44 ) ;
398  adcValues.push_back( 45 ) ;
399  tpcRaw->setADCValues( adcValues ) ;
400  } else {
401  tpcRaw->adcValues().push_back( 42 ) ;
402  tpcRaw->adcValues().push_back( 43 ) ;
403  tpcRaw->adcValues().push_back( 44 ) ;
404  tpcRaw->adcValues().push_back( 45 ) ;
405  }
406  tpcRawVec->addElement( tpcRaw ) ;
407  }
408  evt->addCollection( tpcRawVec , "TrackerRawDataExample" ) ;
409 
410  //---- test new relation navigator object
411  LCRelationNavigator relNav( LCIO::TRACKERRAWDATA, LCIO::SIMTRACKERHIT ) ;
412 
413  for(int j=0;j<NHITS;j++){
414  relNav.addRelation( tpcRawVec->getElementAt(j) , trkVec->getElementAt(j) , 0.42 ) ;
415 
416 // tpcRawVec->getElementAt(j)->link<MyTrackLink>() =
417 // (*tpcRawVec)[j]->link< MyTrackLink >() =
418 // dynamic_cast<SimTrackerHit*>( (*trkVec)[j] );
419 
420  }
421  evt->addCollection( relNav.createLCCollection() , "TPCRawFADCMCTruth" ) ;
422 
423 
424  //------ corrected data
425 
426  LCCollectionVec* tpcCorrectedVec = new LCCollectionVec( LCIO::TRACKERDATA ) ;
427 
428  for(int j=0;j<NHITS;j++){
429 
430  TrackerDataImpl* tpcCorrected = new TrackerDataImpl ;
431 
432  tpcCorrected->setCellID0( j ) ;
433  tpcCorrected->setTime( -j ) ;
434 
435  tpcCorrected->chargeValues().push_back( 42.12345 ) ;
436  tpcCorrected->chargeValues().push_back( 43.09876 ) ;
437  tpcCorrected->chargeValues().push_back( 44.12345 ) ;
438  tpcCorrected->chargeValues().push_back( 45.09876 ) ;
439 
440  tpcCorrectedVec->addElement( tpcCorrected ) ;
441  }
442  evt->addCollection( tpcCorrectedVec , "TrackerDataExample" ) ;
443 
444  // ------ pulses
445 
446  LCCollectionVec* tpcPulseVec = new LCCollectionVec( LCIO::TRACKERPULSE ) ;
447 
448  IntVec qualityBits ;
449  qualityBits.push_back(0) ;
450  qualityBits.push_back(1) ;
451 
452  StringVec bitNames ;
453  bitNames.push_back("GOOD") ;
454  bitNames.push_back("BAD") ;
455 
456  tpcPulseVec->parameters().setValues("TrackerPulseQualityNames", bitNames );
457  tpcPulseVec->parameters().setValues("TrackerPulseQualityValues", qualityBits );
458 
459  for(int j=0;j<NHITS;j++){
460 
461  TrackerPulseImpl* tpcPulse = new TrackerPulseImpl ;
462 
463  tpcPulse->setCellID0( j ) ;
464  tpcPulse->setTime( 3.1415 + 0.1 * j ) ;
465  tpcPulse->setCharge( 3.1415 - 0.1 * j ) ;
466 
467  if( j % 2 ) {
468  tpcPulse->setQualityBit( qualityBits[0] ) ;
469  } else {
470 
471  tpcPulse->setQualityBit( qualityBits[1] ) ;
472 
473  TrackerData* corr =
474  dynamic_cast<TrackerData*> ( tpcCorrectedVec->getElementAt(j) ) ;
475  tpcPulse->setTrackerData( corr ) ;
476  }
477 
478  tpcPulseVec->addElement( tpcPulse ) ;
479  }
480  evt->addCollection( tpcPulseVec , "TrackerPulseExample" ) ;
481 
482  //-----------------------------------------------------
483 #endif // WRITE_TRACKERRAWDATA
484 
485 #define WRITE_VTXRAWHITS 1
486 #ifdef WRITE_VTXRAWHITS
487 
488  //--- write some VTX raw hits to the file - using the TrackerPulse
489  LCCollectionVec* vtxRawVec = new LCCollectionVec( LCIO::TRACKERPULSE ) ;
490 
491  for(int j=0;j<NHITS;j++){
492 
493  TrackerPulseImpl* vtxRaw = new TrackerPulseImpl ;
494 
495  vtxRaw->setCellID0( 0xBebaFeca ) ;
496  vtxRaw->setCellID1( 0xCafeBabe ) ;
497  vtxRaw->setTime( j ) ;
498  vtxRaw->setCharge( 42 + j ) ;
499 
500  vtxRawVec->addElement( vtxRaw ) ;
501  }
502  evt->addCollection( vtxRawVec , "SiliconRawHitExample" ) ;
503 
504  //-----------------------------------------------------
505 #endif // WRITE_VTXRAWHITS
506 
507 
508 
509  // write the event to the file
510  lcWrt->writeEvent( evt ) ;
511 
512  // dump the event to the screen
513  LCTOOLS::dumpEvent( evt ) ;
514 
515  // ------------ IMPORTANT ------------- !
516  // we created the event so we need to delete it ...
517  delete evt ;
518  // -------------------------------------
519 
520  // dont use this (compatibility with Fortran simjob.F)
521  // if( ! (i%100) ) cout << ". " << flush ;
522 
523  } // evt loop
524 
525  delete runHdr ;
526 
527  lcWrt->close() ;
528  delete lcWrt ;
529 
530  } // run loop
531 
532  cout << endl
533  << " created " << NRUN << " runs with " << NRUN*NEVENT << " events"
534  << endl << endl ;
535 
536 
537  // ----- some testing code for the lctypename template -----
538 // std::cout << lctypename<MCParticle>() << std::endl ;
539 // std::cout << lctypename<MCParticleImpl>() << std::endl ;
540 
541 // std::cout << lctypename<ReconstructedParticle>() << std::endl ;
542 
543 // std::cout << lctypename<SimTrackerHit>() << std::endl ;
544 // std::cout << lctypename<SimTrackerHitImpl>() << std::endl ;
545 
546 // SimTrackerHitImpl sth ;
547 // std::cout << lctypename( &sth ) << std::endl ;
548 
549 // LCObject* obj = &sth ;
550 // std::cout << lctypename( obj ) << std::endl ;
551 
552 
553 
554 
555  } catch( Exception& ex){
556 
557  cout << " an excpetion occured: " << endl ;
558  cout << " " << ex.what() << endl ;
559  return 1 ;
560  }
561 
562  return 0 ;
563 }
564 
std::vector< std::string > StringVec
Vector of strings.
Definition: LCIOSTLTypes.h:16
T rand(T...args)
static string FILEN
Definition: simjob.cc:45
std::vector< float > FloatVec
Vector of floats.
Definition: LCIOSTLTypes.h:18
T endl(T...args)
static const int NEVENT
Definition: simjob.cc:41
std::vector< double > DoubleVec
Vector of doubles.
Definition: LCIOSTLTypes.h:20
STL class.
T push_back(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...
static const int NMCPART
Definition: simjob.cc:42
T str(T...args)
static const int NHITS
Definition: simjob.cc:43
std::vector< int > IntVec
Vector of ints.
Definition: LCIOSTLTypes.h:22
LCRunHeader * runHdr
Definition: lsh.cc:79
BitSet32 make_bitset32(int bit0)
Convenient helper that creates a BitSet32 with bit0 set.
Definition: BitSet32.h:49
void dumpEvent(EVENT::LCEvent *event)
std::vector< short > ShortVec
Vector of shorts.
Definition: LCIOSTLTypes.h:24
static const int NRUN
Definition: simjob.cc:40