00001
00007 #include "ValBase.h"
00008
00009 #include "GaudiKernel/MsgStream.h"
00010 #include "GaudiKernel/IDataProviderSvc.h"
00011 #include "GaudiKernel/SmartDataPtr.h"
00012 #include "GaudiKernel/AlgTool.h"
00013 #include "GaudiKernel/ToolFactory.h"
00014 #include "GaudiKernel/IToolSvc.h"
00015 #include "GaudiKernel/IParticlePropertySvc.h"
00016 #include "GaudiKernel/ParticleProperty.h"
00017
00018 #include "Event/TopLevel/EventModel.h"
00019 #include "Event/TopLevel/Event.h"
00020 #include "Event/TopLevel/MCEvent.h"
00021
00022
00023 #include "Event/MonteCarlo/McParticle.h"
00024 #include "Event/MonteCarlo/McTrajectory.h"
00025 #include "Event/MonteCarlo/McPositionHit.h"
00026 #include "Event/MonteCarlo/McRelTableDefs.h"
00027
00028 #include "Event/Digi/TkrDigi.h"
00029
00030
00031 #include "idents/VolumeIdentifier.h"
00032
00033 #include <algorithm>
00034 #include <set>
00035
00042 class McTkrHitValsTool : public ValBase
00043 {
00044 public:
00045
00046 McTkrHitValsTool( const std::string& type,
00047 const std::string& name,
00048 const IInterface* parent);
00049
00050 virtual ~McTkrHitValsTool() { }
00051
00052 StatusCode initialize();
00053
00054 StatusCode calculate();
00055
00056 private:
00057
00058 int GetTrackHits(const Event::McParticle* paricle);
00059 void CntTotalHits(const Event::McParticle* primary);
00060 int GetSharedHits(const Event::McParticle* daught1, const Event::McParticle* daught2);
00061 void CntMcPosHits(const Event::McParticle* primary);
00062
00063
00064 int m_primType;
00065 int m_primNumHits;
00066
00067
00068
00069 int m_dght1Type;
00070 int m_dght1NumHits;
00071
00072
00073 int m_dght2Type;
00074 int m_dght2NumHits;
00075
00076
00077 int m_posHitPrimary;
00078 int m_posHitGammas;
00079 int m_posHitElectrons;
00080 int m_posHitPositrons;
00081 int m_posHitOthers;
00082
00083
00084 int m_process;
00085 int m_totalHits;
00086 int m_totalPrimHits;
00087 int m_totalPrimClusHits;
00088 int m_dghtSharedHits;
00089
00090
00091 IParticlePropertySvc* m_ppsvc;
00092
00093
00094 Event::McPartToTrajectoryTab* m_partToTrajTab;
00095 Event::McPointToPosHitTab* m_pointToPosHitTab;
00096
00097 Event::ClusMcPosHitTab* m_clusToPosHitTab;
00098 };
00099
00100
00101 static ToolFactory<McTkrHitValsTool> s_factory;
00102 const IToolFactory& McTkrHitValsToolFactory = s_factory;
00103
00104
00105 McTkrHitValsTool::McTkrHitValsTool(const std::string& type,
00106 const std::string& name,
00107 const IInterface* parent)
00108 : ValBase( type, name, parent ),
00109 m_partToTrajTab(0),
00110 m_pointToPosHitTab(0),
00111 m_clusToPosHitTab(0)
00112 {
00113
00114 declareInterface<IValsTool>(this);
00115 }
00116
00117
00140 StatusCode McTkrHitValsTool::initialize()
00141 {
00142 StatusCode sc = StatusCode::SUCCESS;
00143
00144 MsgStream log(msgSvc(), name());
00145
00146 if( ValBase::initialize().isFailure()) return StatusCode::FAILURE;
00147
00148
00149 if( serviceLocator() ) {
00150 if( service("ParticlePropertySvc", m_ppsvc, true).isFailure() ) {
00151 log << MSG::ERROR << "Service [ParticlePropertySvc] not found" << endreq;
00152 }
00153 } else {
00154 return StatusCode::FAILURE;
00155 }
00156
00157 addItem("McTHPrimType", &m_primType);
00158 addItem("McTHProcess", &m_process);
00159 addItem("McTHPrimNumHits", &m_primNumHits);
00160 addItem("McTHDght1Type", &m_dght1Type);
00161 addItem("McTHDght1NumHits", &m_dght1NumHits);
00162 addItem("McTHDght2Type", &m_dght2Type);
00163 addItem("McTHDght2NumHits", &m_dght2NumHits);
00164 addItem("McTHPosHitPrimary", &m_posHitPrimary);
00165 addItem("McTHPosHitGamma", &m_posHitGammas);
00166 addItem("McTHPosHitElectron", &m_posHitElectrons);
00167 addItem("McTHPosHitPositron", &m_posHitPositrons);
00168 addItem("McTHPosHitOthers", &m_posHitOthers);
00169 addItem("McTHTotalHits", &m_totalHits);
00170 addItem("McTHTotPrmHits", &m_totalPrimHits);
00171 addItem("McTHTotPrmClusHits", &m_totalPrimClusHits);
00172 addItem("McTHDghtSharedHits", &m_dghtSharedHits);
00173
00174 zeroVals();
00175
00176 return sc;
00177 }
00178
00179 StatusCode McTkrHitValsTool::calculate()
00180 {
00181 StatusCode sc = StatusCode::SUCCESS;
00182 MsgStream log( msgSvc(), name() );
00183
00184
00185 SmartDataPtr<Event::EventHeader> header(m_pEventSvc, EventModel::EventHeader);
00186 double t = header->time();
00187 log << MSG::DEBUG << "Event time: " << t << endreq;;
00188
00189
00190 SmartDataPtr<Event::McParticleCol> particleCol(m_pEventSvc, EventModel::MC::McParticleCol);
00191
00192
00193 if (particleCol == 0) return sc;
00194
00195
00196 SmartDataPtr<Event::McPartToTrajectoryTabList>
00197 mcPartToTraj(m_pEventSvc, "/Event/MC/McPartToTrajectory");
00198 m_partToTrajTab = new Event::McPartToTrajectoryTab(mcPartToTraj);
00199
00200
00201 SmartDataPtr<Event::McPointToPosHitTabList>
00202 mcPointToPosHitList(m_pEventSvc, "/Event/MC/McPointToPosHit");
00203 m_pointToPosHitTab = new Event::McPointToPosHitTab(mcPointToPosHitList);
00204
00205
00206
00207
00208
00209
00210
00211 SmartDataPtr<Event::ClusMcPosHitTabList>
00212 clusMcPosHitList(m_pEventSvc, EventModel::Digi::TkrClusterHitTab);
00213
00214
00215 if (clusMcPosHitList != 0) m_clusToPosHitTab = new Event::ClusMcPosHitTab(clusMcPosHitList);
00216
00217
00218 Event::McParticle* primary = 0;
00219 std::string primString = "primary";
00220
00221 Event::McParticleCol::iterator mcPartIter = particleCol->begin();
00222
00223 for(; mcPartIter != particleCol->end(); mcPartIter++)
00224 {
00225 Event::McParticle* mcPart = *mcPartIter;
00226
00227
00228 if (mcPart->getProcess() == primString)
00229 {
00230 primary = mcPart;
00231 break;
00232 }
00233 }
00234
00235
00236 if (primary)
00237 {
00238
00239
00240 CntTotalHits(primary);
00241
00242
00243 CntMcPosHits(primary);
00244
00245
00246
00247 std::map<const Event::McParticle*,int> mcPartHitMap;
00248 mcPartHitMap.clear();
00249
00250
00251 mcPartIter = particleCol->begin();
00252
00253 for(++mcPartIter; mcPartIter != particleCol->end(); mcPartIter++)
00254 {
00255 Event::McParticle* mcPart = *mcPartIter;
00256
00257
00258 int numHits = GetTrackHits(mcPart);
00259
00260 mcPartHitMap[mcPart] = numHits;
00261 }
00262
00263
00264 m_primType = primary->particleProperty();
00265 m_primNumHits = mcPartHitMap[primary];
00266
00267
00268
00269
00270
00271
00272 const Event::McParticle* daughter1 = 0;
00273 const Event::McParticle* daughter2 = 0;
00274
00275
00276 const SmartRefVector<Event::McParticle>& daughterList = primary->daughterList();
00277
00278 for(SmartRefVector<Event::McParticle>::const_iterator daughterIter = daughterList.begin();
00279 daughterIter != daughterList.end(); daughterIter++)
00280 {
00281 const Event::McParticle* daughter = *daughterIter;
00282
00283 int numHits = mcPartHitMap[daughter];
00284
00285
00286 if (numHits > m_dght1NumHits || m_dght1Type == 0)
00287 {
00288
00289 if (m_dght1NumHits > m_dght2NumHits || m_dght2Type == 0)
00290 {
00291 m_dght2NumHits = m_dght1NumHits;
00292 m_dght2Type = m_dght2Type;
00293 daughter2 = daughter1;
00294 }
00295
00296 m_dght1NumHits = numHits;
00297 m_dght1Type = daughter->particleProperty();
00298 daughter1 = daughter;
00299 }
00300
00301 else if (numHits > m_dght2NumHits || m_dght2Type == 0)
00302 {
00303 m_dght2NumHits = numHits;
00304 m_dght2Type = daughter->particleProperty();
00305 daughter2 = daughter;
00306 }
00307 }
00308
00309
00310 m_dghtSharedHits = GetSharedHits(daughter1, daughter2);
00311 }
00312
00313
00314 if (m_partToTrajTab) {delete m_partToTrajTab; m_partToTrajTab = 0;}
00315 if (m_pointToPosHitTab) {delete m_pointToPosHitTab; m_pointToPosHitTab = 0;}
00316
00317 if (m_clusToPosHitTab) {delete m_clusToPosHitTab; m_clusToPosHitTab = 0;}
00318
00319 log << MSG::DEBUG << " returning. " << endreq;
00320
00321 return sc;
00322 }
00323
00324
00325 void McTkrHitValsTool::CntTotalHits(const Event::McParticle* primary)
00326 {
00327 m_totalHits = 0;
00328 m_totalPrimHits = 0;
00329
00330
00331 if (m_clusToPosHitTab)
00332 {
00333
00334 SmartDataPtr<Event::McPositionHitCol> posHitCol(m_pEventSvc, EventModel::MC::McPositionHitCol);
00335
00336
00337 std::map<int, std::set<Event::TkrCluster*> > idToHitMap;
00338 std::set<int> primTrkIdSet;
00339 std::set<Event::TkrCluster*> primClusSet;
00340
00341 idToHitMap.clear();
00342 primTrkIdSet.clear();
00343 primClusSet.clear();
00344
00345
00346 for (Event::McPositionHitCol::iterator posHitIter = posHitCol->begin(); posHitIter != posHitCol->end(); posHitIter++)
00347 {
00348 Event::McPositionHit* posHit = *posHitIter;
00349
00350
00351 Event::ClusMcPosHitVec clusHitVec = m_clusToPosHitTab->getRelBySecond(posHit);
00352
00353
00354
00355 if (!clusHitVec.empty())
00356 {
00357
00358
00359 Event::TkrCluster* cluster = (*(clusHitVec.begin()))->getFirst();
00360
00361
00362 int trackId = posHit->getPackedFlags();
00363
00364 idToHitMap[trackId].insert(cluster);
00365
00366
00367
00368 if (posHit->originMcParticle() != 0)
00369 {
00370 primClusSet.insert(cluster);
00371 primTrkIdSet.insert(trackId);
00372 }
00373 }
00374 }
00375
00376
00377 for (std::map<int,std::set<Event::TkrCluster*> >::iterator idToHitIter = idToHitMap.begin();
00378 idToHitIter != idToHitMap.end(); idToHitIter++)
00379 {
00380 m_totalHits += idToHitIter->second.size();
00381 }
00382
00383
00384 for (std::set<int>::iterator trkIdIter = primTrkIdSet.begin(); trkIdIter != primTrkIdSet.end(); trkIdIter++)
00385 {
00386 m_totalPrimHits += idToHitMap[*trkIdIter].size();
00387 }
00388
00389
00390 m_totalPrimClusHits = primClusSet.size();
00391 }
00392
00393 return;
00394 }
00395
00396 int McTkrHitValsTool::GetTrackHits(const Event::McParticle* particle)
00397 {
00398 int nTrackHits = 0;
00399
00400
00401 if (m_clusToPosHitTab)
00402 {
00403
00404 Event::McPartToTrajectoryVec trajVec = m_partToTrajTab->getRelByFirst(particle);
00405 Event::McTrajectory* mcTraj = (*(trajVec.begin()))->getSecond();
00406
00407
00408 std::vector<Event::McTrajectoryPoint*> points = mcTraj->getPoints();
00409 std::vector<Event::McTrajectoryPoint*>::const_iterator pointIter;
00410
00411
00412
00413 std::set<Event::TkrCluster*> trackClusters;
00414 trackClusters.clear();
00415
00416
00417 for(pointIter = points.begin(); pointIter != points.end(); pointIter++)
00418 {
00419
00420 Event::McTrajectoryPoint* mcPoint = *pointIter;
00421
00422
00423 Event::McPointToPosHitVec posRelVec = m_pointToPosHitTab->getRelByFirst(mcPoint);
00424
00425
00426 if (!posRelVec.empty())
00427 {
00428
00429 Event::McPointToPosHitRel* hitRel = *(posRelVec.begin());
00430
00431 Event::McPositionHit* mcPosHit = hitRel->getSecond();
00432
00433
00434 Event::ClusMcPosHitVec clusHitVec = m_clusToPosHitTab->getRelBySecond(mcPosHit);
00435
00436
00437 if (!clusHitVec.empty())
00438 {
00439
00440
00441 Event::TkrCluster* cluster = (*(clusHitVec.begin()))->getFirst();
00442
00443
00444 trackClusters.insert(cluster);
00445 }
00446 }
00447 }
00448
00449
00450 nTrackHits = trackClusters.size();
00451 }
00452
00453 return nTrackHits;
00454 }
00455
00456 int McTkrHitValsTool::GetSharedHits(const Event::McParticle* daughter1, const Event::McParticle* daughter2)
00457 {
00458 int numShared = 0;
00459
00460
00461
00462
00463
00464
00465
00466 if (m_clusToPosHitTab && daughter1 && daughter2)
00467 {
00468
00469
00470 Event::McPartToTrajectoryVec trajVec = m_partToTrajTab->getRelByFirst(daughter2);
00471 Event::McTrajectory* mcTraj = (*(trajVec.begin()))->getSecond();
00472
00473
00474 std::vector<Event::McTrajectoryPoint*> points = mcTraj->getPoints();
00475 std::vector<Event::McTrajectoryPoint*>::const_iterator pointIter;
00476
00477
00478
00479 std::set<Event::TkrCluster*> trackClusters;
00480 trackClusters.clear();
00481
00482
00483 for(pointIter = points.begin(); pointIter != points.end(); pointIter++)
00484 {
00485
00486 Event::McTrajectoryPoint* mcPoint = *pointIter;
00487
00488
00489 Event::McPointToPosHitVec posRelVec = m_pointToPosHitTab->getRelByFirst(mcPoint);
00490
00491
00492 if (!posRelVec.empty())
00493 {
00494
00495 Event::McPointToPosHitRel* hitRel = *(posRelVec.begin());
00496
00497 Event::McPositionHit* mcPosHit = hitRel->getSecond();
00498
00499
00500 Event::ClusMcPosHitVec clusHitVec = m_clusToPosHitTab->getRelBySecond(mcPosHit);
00501
00502
00503 if (!clusHitVec.empty())
00504 {
00505
00506
00507 Event::TkrCluster* cluster = (*(clusHitVec.begin()))->getFirst();
00508
00509
00510 trackClusters.insert(cluster);
00511 }
00512 }
00513 }
00514
00515
00516 for (std::set<Event::TkrCluster*>::iterator clusIter = trackClusters.begin(); clusIter != trackClusters.end(); clusIter++)
00517 {
00518 Event::TkrCluster* cluster = *clusIter;
00519
00520
00521 Event::ClusMcPosHitVec clusHitVec = m_clusToPosHitTab->getRelByFirst(cluster);
00522
00523
00524 if (clusHitVec.size() > 1)
00525 {
00526 for(Event::ClusMcPosHitVec::iterator relHitIter = clusHitVec.begin(); relHitIter != clusHitVec.end(); relHitIter++)
00527 {
00528 Event::McPositionHit* relPosHit = (*relHitIter)->getSecond();
00529
00530
00531 if (relPosHit && relPosHit->mcParticle() == daughter1)
00532 {
00533 numShared++;
00534 break;
00535 }
00536 }
00537 }
00538 }
00539 }
00540
00541 return numShared;
00542 }
00543
00544 void McTkrHitValsTool::CntMcPosHits(const Event::McParticle* primary)
00545 {
00546
00547 static Event::McParticle::StdHepId gamma = 22;
00548 static Event::McParticle::StdHepId electron = 11;
00549 static Event::McParticle::StdHepId positron = -11;
00550
00551
00552 m_posHitPrimary = 0;
00553 m_posHitGammas = 0;
00554 m_posHitElectrons = 0;
00555 m_posHitPositrons = 0;
00556 m_posHitOthers = 0;
00557
00558
00559 SmartDataPtr<Event::McPositionHitCol> positionHitCol(m_pEventSvc, EventModel::MC::McPositionHitCol);
00560
00561
00562 if (positionHitCol)
00563 {
00564
00565 for(Event::McPositionHitCol::iterator posHitIter = positionHitCol->begin();
00566 posHitIter != positionHitCol->end(); posHitIter++)
00567 {
00568 Event::McPositionHit* posHit = *posHitIter;
00569
00570
00571 idents::VolumeIdentifier volId = posHit->volumeID();
00572
00573
00574 if (volId[0] != 0) continue;
00575
00576
00577 const Event::McParticle* particle = posHit->mcParticle();
00578 const Event::McParticle* mother = 0;
00579
00580 if (particle) mother = particle->getMother();
00581
00582
00583 if (particle == primary) m_posHitPrimary++;
00584 else if (mother == primary) m_posHitPrimary++;
00585
00586
00587 if (posHit->getMcParticleId() == gamma) m_posHitGammas++;
00588 else if (posHit->getMcParticleId() == electron) m_posHitElectrons++;
00589 else if (posHit->getMcParticleId() == positron) m_posHitPositrons++;
00590 else m_posHitOthers++;
00591 }
00592 }
00593
00594
00595 return;
00596 }