00001 #include "UTIL/LCStdHepRdr.h"
00002 #include "EVENT/MCParticle.h"
00003 #include "IMPL/MCParticleImpl.h"
00004 #include "IMPL/LCEventImpl.h"
00005 #include "lcio.h"
00006 #include "EVENT/LCIO.h"
00007 #include <sstream>
00008
00009 #include <cmath>
00010 #include "Exceptions.h"
00011
00012 using namespace EVENT ;
00013 using namespace IMPL ;
00014
00015
00016 #define IDRUP_NAME "_idrup"
00017 #define EVTWGT_NAME "_weight"
00018
00019 namespace UTIL{
00020
00021 LCStdHepRdr::LCStdHepRdr(const char* evfile){
00022
00023
00024
00025 _reader = new UTIL::lStdHep(evfile,false);
00026 if(_reader->getError()) {
00027 std::stringstream description ;
00028 description << "LCStdHepRdr: no stdhep file: " << evfile << std::ends ;
00029 throw IO::IOException( description.str() );
00030 }
00031
00032
00033
00034 }
00035 LCStdHepRdr::~LCStdHepRdr(){
00036
00037 delete _reader ;
00038 }
00039
00040
00041
00042 void LCStdHepRdr::printHeader(std::ostream& os ) {
00043
00044 if( os == std::cout ) {
00045
00046 _reader->printFileHeader() ;
00047 }
00048
00049 }
00050
00051
00052
00053 void LCStdHepRdr::updateNextEvent( IMPL::LCEventImpl* evt , const char* colName ) {
00054
00055 if( evt == 0 ) {
00056 throw EVENT::Exception( " LCStdHepRdr::updateEvent - null pointer for event " );
00057 }
00058
00059 IMPL::LCCollectionVec* mcpCol = readEvent() ;
00060
00061 if( mcpCol == 0 ) {
00062
00063 throw IO::EndOfDataException( " LCStdHepRdr::updateEvent: EOF " ) ;
00064 }
00065
00066
00067
00068
00069 int idrup = mcpCol->getParameters().getIntVal( IDRUP_NAME ) ;
00070
00071 if( idrup !=0 ) {
00072
00073 evt->parameters().setValue( IDRUP_NAME , idrup ) ;
00074 }
00075
00076 double evtwgt = mcpCol->getParameters().getFloatVal( EVTWGT_NAME ) ;
00077
00078 evt->setWeight( evtwgt ) ;
00079
00080
00081
00082
00083
00084 evt->addCollection( mcpCol , colName ) ;
00085 }
00086
00087
00088
00089
00090
00091 IMPL::LCCollectionVec * LCStdHepRdr::readEvent()
00092 {
00093 IMPL::LCCollectionVec * mcVec = 0;
00094 double c_light = 299.792;
00095
00096
00097
00098
00099
00100 int errorcode = _reader->readEvent() ;
00101
00102 if( errorcode != LSH_SUCCESS ){
00103
00104 if( errorcode != LSH_ENDOFFILE ) {
00105
00106 std::stringstream description ;
00107 description << "LCStdHepRdr::readEvent: error when reading event: " << errorcode << std::ends ;
00108
00109 throw IO::IOException( description.str() );
00110 }
00111
00112 else {
00113
00114 return 0 ;
00115
00116 }
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 mcVec = new IMPL::LCCollectionVec(LCIO::MCPARTICLE);
00132 MCParticleImpl* p;
00133 MCParticleImpl* d;
00134
00135
00136
00137 int NHEP = _reader->nTracks();
00138
00139
00140 long idrup = _reader->idrup() ;
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 if( idrup != 0 ) {
00159
00160 mcVec->parameters().setValue( IDRUP_NAME , (int) idrup ) ;
00161 }
00162
00163 double evtWeight = _reader->eventweight() ;
00164
00165
00166 mcVec->parameters().setValue( EVTWGT_NAME , (float) evtWeight ) ;
00167
00168
00169 for( int IHEP=0; IHEP<NHEP; IHEP++ )
00170 {
00171
00172
00173
00174 MCParticleImpl* mcp = new MCParticleImpl();
00175
00176
00177
00178 mcp->setPDG(_reader->pid(IHEP));
00179
00180
00181
00182
00183
00184 mcp->setCharge( threeCharge( mcp->getPDG() ) / 3. ) ;
00185
00186
00187
00188
00189 float p0[3] = {_reader->Px(IHEP),_reader->Py(IHEP),_reader->Pz(IHEP)};
00190 mcp->setMomentum(p0);
00191
00192
00193
00194 mcp->setMass(_reader->M(IHEP));
00195
00196
00197
00198 double v0[3] = {_reader->X(IHEP),_reader->Y(IHEP),_reader->Z(IHEP)};
00199 mcp->setVertex(v0);
00200
00201
00202
00203 mcp->setGeneratorStatus(_reader->status(IHEP));
00204
00205
00206
00207 mcp->setSimulatorStatus(0);
00208
00209
00210
00211 mcp->setTime(_reader->T(IHEP)/c_light);
00212
00213
00214
00215 mcVec->push_back(mcp);
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 for( int IHEP=0; IHEP<NHEP; IHEP++ )
00293 {
00294
00295
00296
00297 MCParticleImpl* mcp =
00298 dynamic_cast<MCParticleImpl*>
00299 (mcVec->getElementAt(IHEP));
00300
00301
00302
00303
00304 int fd = _reader->daughter1(IHEP)%10000 - 1;
00305 int ld = _reader->daughter2(IHEP)%10000 - 1;
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315 if( (fd > -1) && (ld > -1) )
00316 {
00317 if(ld >= fd)
00318 {
00319 for(int id=fd;id<ld+1;id++)
00320 {
00321
00322
00323
00324
00325 d = dynamic_cast<MCParticleImpl*>
00326 (mcVec->getElementAt(id));
00327 int np = d->getParents().size();
00328 bool gotit = false;
00329 for(int ip=0;ip < np;ip++)
00330 {
00331 p = dynamic_cast<MCParticleImpl*>
00332 (d->getParents()[ip]);
00333 if(p == mcp)gotit = true;
00334 }
00335
00336
00337
00338 if(!gotit)d->addParent(mcp);
00339 }
00340 }
00341
00342
00343
00344 else
00345 {
00346 d = dynamic_cast<MCParticleImpl*>
00347 (mcVec->getElementAt(fd));
00348 int np = d->getParents().size();
00349 bool gotit = false;
00350 for(int ip=0;ip < np;ip++)
00351 {
00352 p = dynamic_cast<MCParticleImpl*>
00353 (d->getParents()[ip]);
00354 if(p == mcp)gotit = true;
00355 }
00356 if(!gotit)d->addParent(mcp);
00357 d = dynamic_cast<MCParticleImpl*>
00358 (mcVec->getElementAt(ld));
00359 np = d->getParents().size();
00360 gotit = false;
00361 for(int ip=0;ip < np;ip++)
00362 {
00363 p = dynamic_cast<MCParticleImpl*>
00364 (d->getParents()[ip]);
00365 if(p == mcp)gotit = true;
00366 }
00367 if(!gotit)d->addParent(mcp);
00368 }
00369 }
00370 else if(fd > -1 )
00371 {
00372
00373 if( fd < NHEP ) {
00374 d = dynamic_cast<MCParticleImpl*>
00375 (mcVec->getElementAt(fd));
00376 int np = d->getParents().size();
00377 bool gotit = false;
00378 for(int ip=0;ip < np;ip++)
00379 {
00380 p = dynamic_cast<MCParticleImpl*>
00381 (d->getParents()[ip]);
00382 if(p == mcp)gotit = true;
00383 }
00384 if(!gotit)d->addParent(mcp);
00385
00386 } else {
00387
00388
00389
00390 }
00391
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420 }
00421
00422
00423
00424 return mcVec;
00425 }
00426
00427
00428 int LCStdHepRdr::threeCharge( int pdgID ) const {
00429
00430
00431
00432
00433
00434
00435 enum location { nj=1, nq3, nq2, nq1, nl, nr, n, n8, n9, n10 };
00436
00437 int charge=0;
00438 int ida, sid;
00439 unsigned short q1, q2, q3;
00440 static int ch100[100] = { -1, 2,-1, 2,-1, 2,-1, 2, 0, 0,
00441 -3, 0,-3, 0,-3, 0,-3, 0, 0, 0,
00442 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
00443 0, 0, 0, 3, 0, 0, 3, 0, 0, 0,
00444 0, -1, 0, 0, 0, 0, 0, 0, 0, 0,
00445 0, 6, 3, 6, 0, 0, 0, 0, 0, 0,
00446 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00447 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00448 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00449 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
00450
00451 ida = (pdgID < 0) ? -pdgID : pdgID ;
00452
00453
00454
00455
00456
00457 q1 = ( ida / ( (int) std::pow( 10.0, (nq1 -1) ) ) ) % 10 ;
00458 q2 = ( ida / ( (int) std::pow( 10.0, (nq2 -1) ) ) ) % 10 ;
00459 q3 = ( ida / ( (int) std::pow( 10.0, (nq3 -1) ) ) ) % 10 ;
00460
00461
00462
00463 short dig_n9 = ( ida / ( (int) std::pow( 10.0, (n9 -1) ) ) ) % 10 ;
00464 short dig_n10 = ( ida / ( (int) std::pow( 10.0, (n10 -1) ) ) ) % 10 ;
00465
00466 if( ( dig_n10 == 1 ) && ( dig_n9 == 0 ) ) {
00467
00468 sid = 0 ;
00469 }
00470 else if( q2 == 0 && q1 == 0) {
00471
00472 sid = ida % 10000;
00473 }
00474 else if( ida <= 102 ) {
00475
00476 sid = ida ;
00477 }
00478 else {
00479
00480 sid = 0;
00481 }
00482
00483
00484 int extraBits = ida / 10000000 ;
00485
00486
00487 short dig_nj = ( ida / ( (int) std::pow( 10.0, (nj -1) ) ) ) % 10 ;
00488
00489 if( ida == 0 || extraBits > 0 ) {
00490 return 0;
00491 } else if( sid > 0 && sid <= 100 ) {
00492 charge = ch100[sid-1];
00493 if(ida==1000017 || ida==1000018) { charge = 0; }
00494 if(ida==1000034 || ida==1000052) { charge = 0; }
00495 if(ida==1000053 || ida==1000054) { charge = 0; }
00496 if(ida==5100061 || ida==5100062) { charge = 6; }
00497 } else if( dig_nj == 0 ) {
00498 return 0;
00499 } else if( q1 == 0 ) {
00500 if( q2 == 3 || q2 == 5 ) {
00501 charge = ch100[q3-1] - ch100[q2-1];
00502 } else {
00503 charge = ch100[q2-1] - ch100[q3-1];
00504 }
00505 } else if( q3 == 0 ) {
00506 charge = ch100[q2-1] + ch100[q1-1];
00507 } else {
00508 charge = ch100[q3-1] + ch100[q2-1] + ch100[q1-1];
00509 }
00510 if( charge == 0 ) {
00511 return 0;
00512 } else if( pdgID < 0 ) {
00513 charge = -charge;
00514 }
00515 return charge;
00516 }
00517
00518 }
00519