ATLAS Offline Software
Loading...
Searching...
No Matches
TrackCollHandle_TruthTracks.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5
7// //
8// Implementation of class TrackCollHandle_TruthTracks //
9// //
10// Author: Thomas H. Kittelmann (Thomas.Kittelmann@cern.ch) //
11// Initial version: March 2008 //
12// //
14
23#include "VP1Base/IVP1System.h"
24#include "VP1Base/VP1Msg.h"
27
28#include "AtlasHepMC/GenEvent.h"
32
34
37
44#include "CLHEP/Units/PhysicalConstants.h"
45
46#include "CLHEP/Vector/LorentzVector.h"
47
48#include <QStringList>
49#include <QTime>
50#include <QElapsedTimer>
51
52
53//____________________________________________________________________
55public:
56 static double mag(const HepMC::FourVector& v) {
57 return std::sqrt( v.x()*v.x() + v.y()*v.y() + v.z()*v.z() );
58 }
59
61 bool loadHitLists(std::map<SimBarCode,SimHitList> & hitLists);
62 void loadGenParticles( std::map<SimBarCode,HepMC::ConstGenParticlePtr> & genParticles,
63 const HepMC::ConstGenVertexPtr& vtx );
64 bool loadGenParticles( std::map<SimBarCode,HepMC::ConstGenParticlePtr> & genParticles,
65 const QString& hepMcCollKey );
66
67 template <class collT>
68 void addHitCollections(std::map<SimBarCode,SimHitList> & hitLists);
69
70// static SimHitHandleBase * createHitHandle( const TrackRecord * h ) { return new SimHitHandle_TrackRecord(h); }
73 static SimHitHandleBase * createHitHandle( const SiHit& h ) { return new SimHitHandle_SiHit(&h); }
74
75 static QString nameHepMCAugmentedEnd;
76 static QString nameAugmentedOnly;
77
78 void createSecondaryHitLists(const SimBarCode& origSimBarCode,
79 const SimHitList& origHitList,
80 std::map<SimBarCode,SimHitList> & outlists,
81 int& newBarCode );
82 std::list<SimHitHandleBase*>::iterator closestCompatibleHandleItr(SimHitHandleBase* handle,
83 const std::list<SimHitHandleBase*>::iterator& itFirst,
84 std::list<SimHitHandleBase*>& handleList,
85 const double& massSquared) const;
86
87 std::map<SimBarCode::ExtBarCode,int> extBarCode2pdg;
88
91 if (!((updateGUICounter++)%750)) {
92 theclass->systemBase()->updateGUI();
93 }
94 }
95
96 bool cut_fromIROnly = false;
98 bool cut_excludeNeutrals = false;
99
100 bool displayAscObjs = false;
102
104
105 static const int maxPdgCode = 1000000000;
106
107};
108
110QString TrackCollHandle_TruthTracks::Imp::nameAugmentedOnly = "Sim hits/trk rec.";
111
112//____________________________________________________________________
114{
115 QStringList l;
116 VP1SGContentsHelper sgcont(sys);
117
118 QStringList mcevent_keys = sgcont.getKeys<McEventCollection>();
119 QStringList trackrecord_keys = sgcont.getKeys<TrackRecordCollection>();
120
121 QStringList keys_siliconhits, keys_trthits, keys_mdthits,
122 keys_rpchits, keys_tgchits, keys_cschits;
123
125 keys_siliconhits = sgcont.getKeys<SiHitCollection>();//"PixelHits" and "SCT_Hits"
127 keys_trthits = sgcont.getKeys<TRTUncompressedHitCollection>();//"TRTUncompressedHits"
128 if (false&&/*fixme!!*/VP1JobConfigInfo::hasMuonGeometry()) {
129 keys_mdthits = sgcont.getKeys<MDTSimHitCollection>();
130 keys_rpchits = sgcont.getKeys<RPCSimHitCollection>();
131 keys_tgchits = sgcont.getKeys<TGCSimHitCollection>();
132 keys_cschits = sgcont.getKeys<CSCSimHitCollection>();
133 }
134
135 bool extrainfo = ! ( keys_siliconhits.empty() && keys_trthits.empty()
136 && keys_mdthits.empty() && keys_rpchits.empty()
137 && keys_tgchits.empty() && keys_cschits.empty()
138 && trackrecord_keys.empty() );
139
140 if (extrainfo&&mcevent_keys.empty()) {
142 return l;
143 }
144
145 for (const QString& mcevent_key : mcevent_keys) {
146 l << mcevent_key;
147 if (extrainfo)
148 l << mcevent_key + Imp::nameHepMCAugmentedEnd;
149 }
150
151 //Fixme: REMOVE THIS (only for testing!!):
152 if (extrainfo)
154
155 return l;
156}
157
158//____________________________________________________________________
160 const QString& name)
161 : TrackCollHandleBase(cd,name,TrackType::TruthTrack), m_d(new Imp)
162{
163 setHelperClassName("TrackCollHandle_TruthTracks");
164 m_d->theclass = this;
165 m_d->updateGUICounter = 0;
166 m_d->cut_fromIROnly = true;
167 m_d->cut_excludeBarcodeZero = true;
168 m_d->cut_excludeNeutrals = true;
169 m_d->displayAscObjs = false;
170}
171
172//____________________________________________________________________
177
178//____________________________________________________________________
180{
181 connect(controller,SIGNAL(cutTruthFromIROnlyChanged(bool)),this,SLOT(setCutFromIROnly(bool)));
183
184 connect(controller,SIGNAL(cutExcludeBarcodeZeroChanged(bool)),this,SLOT(setCutExcludeBarcodeZero(bool)));
186
187 connect(controller,SIGNAL(cutTruthExcludeNeutralsChanged(bool)),this,SLOT(setCutExcludeNeutrals(bool)));
189
190 connect(controller,SIGNAL(showTruthAscObjsChanged(bool)),this,SLOT(setShowAscObjs(bool)));
191 setShowAscObjs(controller->showTruthAscObjs());
192}
193
194//____________________________________________________________________
196{
197 if (m_d->displayAscObjs==b)
198 return;
199 m_d->displayAscObjs=b;
200 messageVerbose("Associated objects shown flag changed to " + str(b));
201 m_d->updateVisibleAssociatedObjects();
202}
203
204//____________________________________________________________________
205template <class collT>
206void TrackCollHandle_TruthTracks::Imp::addHitCollections(std::map<SimBarCode,SimHitList> & hitLists)
207{
208 std::map<SimBarCode,SimHitList>::iterator itHitList;
209
210 VP1SGAccessHelper sgaccess(theclass->systemBase());
211
212 for (const QString& key : VP1SGContentsHelper(theclass->systemBase()).getKeys<collT>()) {
213 const collT * hitColl;
214 if (!sgaccess.retrieve(hitColl, key)) {
215 theclass->message("Error: Could not retrieve "+QString(typeid(collT).name())+" collection with key = "+key);
216 continue;
217 }
218 theclass->messageVerbose("Retrieved hit collection of type "+QString(typeid(collT).name())+" with key = "+key);
219 typename collT::const_iterator it, itE(hitColl->end());
220 int itot(0), iadded(0);
221 for (it=hitColl->begin();it!=itE;++it) {
222 ++itot;
224 SimHitHandleBase * handle = createHitHandle(*it);
225 handle->cacheMomentum();
226 theclass->fixPDGCode(handle);
227 SimBarCode trackID = handle->simBarCode();
228 if (trackID.pdgCode()>maxPdgCode) {
229 continue;
230 }
231 if (trackID.isNonUniqueSecondary()) {
232 double absmom = handle->momentum();
233 if (absmom>=0&&absmom<1.0*CLHEP::MeV) {//Fixme: Useful? Put it higher??
234 // if (VP1Msg::verbose())
235 // theclass->messageVerbose("Ignoring low momentum sim hit for non. unique secondary particle");
236 delete handle;
237 continue;
238 }
239 }
240 itHitList = hitLists.find(trackID);
241 if ( itHitList == hitLists.end() ) {
242 SimHitList l;
243 l.push_back(std::pair<double,SimHitHandleBase*>(handle->hitTime(),handle));
244 hitLists[trackID] = l;
245 } else {
246 itHitList->second.emplace_back(handle->hitTime(),handle);
247 }
248 ++iadded;
249 }
250 theclass->messageVerbose(" => used "+str(iadded)+" of "+str(itot)+" hits");
251 }
252
253}
254
255//____________________________________________________________________
256bool TrackCollHandle_TruthTracks::Imp::loadHitLists(std::map<SimBarCode,SimHitList> & hitLists)
257{
258 //Fixme: Return false if we do not find at least one collection
260
261 //Important that collections which inherently contains pdg codes (TRT) are loaded first!
264
267
268 if (VP1Msg::verbose())
269 theclass->messageVerbose( "Found " + str( hitLists.size() ) + " lists of sim. hits.");
270
271 //Time to assign all simhits with known pdg code a charge:
272 std::map<SimBarCode,SimHitList>::iterator it, itE(hitLists.end());
273 for (it = hitLists.begin(); it!=itE; ++it) {
274 if (it->first.unknownPdgCode())
275 continue;
276 bool ok;
277 double charge = VP1ParticleData::particleCharge(it->first.pdgCode(),ok);
278 if (!ok)
279 continue;
280 SimHitList::iterator itHit(it->second.begin()), itHitE(it->second.end());
281 for (;itHit!=itHitE;++itHit)
282 itHit->second->setCharge(charge);
284 }
285
286 //Fixme: Add hits from muon subsystems.
287
288 //Sort hitLists:
289 for (it = hitLists.begin(); it!=itE; ++it) {
290 sort(it->second.begin(),it->second.end());
292 }
293 return true;
294}
295
296
297//____________________________________________________________________
299{
300 int pdgfromsimhit =handle->actualPDGCodeFromSimHit();
301 bool isNonUniqueSecondary = handle->simBarCode().isNonUniqueSecondary();
302 SimBarCode::ExtBarCode extBarCode = handle->simBarCode().extBarCode();
303
304 if (pdgfromsimhit!=SimBarCode::unknownPDG) {
305 handle->setPDG(handle->actualPDGCodeFromSimHit());
306 std::map<SimBarCode::ExtBarCode,int>::const_iterator it = m_d->extBarCode2pdg.find(extBarCode);
307 if ( !isNonUniqueSecondary && it==m_d->extBarCode2pdg.end())
308 m_d->extBarCode2pdg[extBarCode] = pdgfromsimhit;
309 return;
310 }
311 if (isNonUniqueSecondary)
312 return;
313 std::map<SimBarCode::ExtBarCode,int>::const_iterator it = m_d->extBarCode2pdg.find(extBarCode);
314 if (it!=m_d->extBarCode2pdg.end()) {
315 handle->setPDG(it->second);
316 }
317}
318
319//____________________________________________________________________
320void TrackCollHandle_TruthTracks::Imp::loadGenParticles( std::map<SimBarCode,HepMC::ConstGenParticlePtr> & genParticles,
321 const HepMC::ConstGenVertexPtr& vtx )
322{
323 if (!vtx)
324 return;
325 for (const auto& p: *vtx){
326
327 if (!p)//fixme: message.
328 continue;
329 const HepMC::GenEvent* evt = p->parent_event();
330 if (!evt)
331 continue;//fixme: message.
332 //Fixme: If verbose: check barcode does not already exists!
333 SimBarCode simBarCode(HepMC::barcode(p),0/*evt->event_number()...fixme: correct??*/,p->pdg_id());
334 genParticles[simBarCode] = p;
335 if (!simBarCode.isNonUniqueSecondary())
336 extBarCode2pdg[simBarCode.extBarCode()] = p->pdg_id();
337
338 loadGenParticles(genParticles,p->end_vertex());
339 }
340}
341
342//____________________________________________________________________
343bool TrackCollHandle_TruthTracks::Imp::loadGenParticles( std::map<SimBarCode,HepMC::ConstGenParticlePtr> & genParticles,
344 const QString& hepMcCollKey )
345{
347 const McEventCollection* mcColl;
348 if (!VP1SGAccessHelper(theclass->systemBase()).retrieve(mcColl,hepMcCollKey)) {
349 theclass->message("Error: Could not retrieve "+QString(typeid(McEventCollection).name())+" collection with key = "+hepMcCollKey);
350 return false;//fixme message
351 }
352
353 McEventCollection::const_iterator itEvt(mcColl->begin()), itEvtEnd(mcColl->end());
354 if (itEvt==itEvtEnd)
355 return false;//fixme message
356
357 for (;itEvt!=itEvtEnd;++itEvt) {
358 //NB: Signal is always the first event in collection!
359 const HepMC::GenEvent * evt = *itEvt;
360 if (!evt)
361 continue;
362 for(auto itVtx: evt->vertices()) loadGenParticles(genParticles,itVtx);
363 }
364
365 return true;
366}
367
368//____________________________________________________________________
370{
371 //Decode name to figure out if we need to load simhits/track records and which HepMC key to use.
372 bool augmentedonly = name()==Imp::nameAugmentedOnly;
373 bool augmented = augmentedonly || name().endsWith(Imp::nameHepMCAugmentedEnd);
374 QString hepmckey;
375 if (!augmentedonly) {
376 hepmckey = name();
377 if (augmented)
378 hepmckey.chop(Imp::nameHepMCAugmentedEnd.count());
379 }
380
381 //get genparticles (should be done BEFORE we load sim. hits., so the barCode2pdg map gets filled):
382 std::map<SimBarCode,HepMC::ConstGenParticlePtr> genParticles;
383 if (!hepmckey.isEmpty())
384 if (!m_d->loadGenParticles(genParticles,hepmckey))
385 return false;
386
387 //get sim hits and track records:
388 std::map<SimBarCode,SimHitList> hitLists;
389 if (augmented) {
390 if (!m_d->loadHitLists(hitLists))
391 return false;
392 messageVerbose("TrackCollHandle_TruthTracks "+name()
393 +": Found "+str(hitLists.size())+" truth particles from simhits");
394 }
395
396 //Finally we need to combine the info we thus found, and construct
397 //actual track handles:
398
399 //We do this by looping through the simhit list, and checking for
400 //genparticles with the same SimBarCode. Those gen particles we use
401 //in this way, we take out of the map (put to null), to indicate we
402 //already used them. In the final step we add genparticle-only
403 //handles for the remaining genparticle (unless they have production
404 //and decay vertices ultra-close to each other):
405
406 std::map<SimBarCode,HepMC::ConstGenParticlePtr>::iterator itGenPart, itGenPartEnd(genParticles.end());
407 std::map<SimBarCode,SimHitList>::iterator itHitList, itHitListEnd(hitLists.end()), itHitListTemp;
408
409 //First we attempt to sort secondaries with barcode=0 into new lists
410 //of hits that are likely to have come from the same particle.
411
412 int newBarCode(-1);
413 std::map<SimBarCode,SimHitList> secondaryHitLists;
414 messageVerbose("Sorting non-unique secondaries into lists of hits likely to originate from the same track.");
415 QElapsedTimer timer;timer.start();
416 for (itHitList = hitLists.begin();itHitList!=itHitListEnd;) {
417 if (itHitList->first.isNonUniqueSecondary()) {
418 m_d->createSecondaryHitLists(itHitList->first,itHitList->second,secondaryHitLists,newBarCode);
419 itHitListTemp = itHitList;
420 ++itHitList;
421 hitLists.erase(itHitListTemp);
422 } else {
423 ++itHitList;
424 }
425 }
426 messageVerbose("Finished sorting non-unique secondaries into lists. Time spent: "+str(timer.elapsed()*0.001)+" seconds");
427 std::map<SimBarCode,SimHitList>::iterator itSecondaryList,itSecondaryListEnd(secondaryHitLists.end());
428 for (itSecondaryList = secondaryHitLists.begin();itSecondaryList!=itSecondaryListEnd;++itSecondaryList)
429 hitLists[itSecondaryList->first] = itSecondaryList->second;//Fixme: Check that it does not already exist!
430
431 for (itHitList = hitLists.begin();itHitList!=itHitListEnd;++itHitList) {
432 if (itHitList->second.empty()) {
433 message("load WARNING: Ignoring empty hit list.");
434 continue;
435 }
436 itGenPart = genParticles.find(itHitList->first);
438 if (itGenPart!=itGenPartEnd) {
439 p = itGenPart->second;
440 itGenPart->second = 0;
441 }
442
443 m_d->possiblyUpdateGUI();
444
445 if (m_d->fixMomentumInfoInSimHits(p,itHitList->second))//Provide guesses for momentum in simhits that needs them (and deletes the rest).
446 addTrackHandle( new TrackHandle_TruthTrack( this, itHitList->first, itHitList->second, p ) );
447 }
448
449 const double minSpacialSeparation = 1.0e-3*CLHEP::mm;
450 const double minSepSq = minSpacialSeparation*minSpacialSeparation;
451 for (itGenPart=genParticles.begin();itGenPart!=itGenPartEnd;++itGenPart) {
452 HepMC::ConstGenParticlePtr p = itGenPart->second;
453 if (!p)
454 continue;
455 if (abs(p->pdg_id())>=Imp::maxPdgCode)//Internal particle... (fixme: find proper limit!!)
456 continue;
457 if (!p->production_vertex())
458 continue;
459 if (p->end_vertex()) {
460 double dx(p->end_vertex()->position().x()-p->production_vertex()->position().x());
461 double dy(p->end_vertex()->position().y()-p->production_vertex()->position().y());
462 double dz(p->end_vertex()->position().z()-p->production_vertex()->position().z());
463 if ( dx*dx+dy*dy+dz*dz < minSepSq )
464 continue;
465 }
466 m_d->possiblyUpdateGUI();
467 addTrackHandle( new TrackHandle_TruthTrack( this, itGenPart->first, SimHitList(), p ) );
468 }
469
470 //Maybe we need to show measurements, etc.:
471 m_d->updateVisibleAssociatedObjects();
472
473 return true;
474}
475
476//____________________________________________________________________
478{
479 if (!TrackCollHandleBase::cut(handle))
480 return false;
481
482 if (m_d->cut_excludeNeutrals && handle->hasCharge() && handle->charge()==0.0)
483 return false;
484
485 TrackHandle_TruthTrack * truthhandle = static_cast<TrackHandle_TruthTrack *>(handle);
486 if (m_d->cut_excludeBarcodeZero && truthhandle->hasBarCodeZero())
487 return false;
488
489 if (m_d->cut_fromIROnly && ! truthhandle->hasVertexAtIR(2.8*CLHEP::cm*2.8*CLHEP::cm,50*CLHEP::cm))
490 return false;
491
492 return true;
493}
494
495//____________________________________________________________________
497{
498 if (m_d->cut_fromIROnly == b)
499 return;
500 m_d->cut_fromIROnly = b;
501 if (b)
503 else
505}
506
507//____________________________________________________________________
509{
510 if (m_d->cut_excludeBarcodeZero==b)
511 return;
512 m_d->cut_excludeBarcodeZero=b;
513 if (b)
515 else
517}
518
519//____________________________________________________________________
521{
522 if (m_d->cut_excludeNeutrals==b)
523 return;
524 m_d->cut_excludeNeutrals=b;
525 if (b)
527 else
529}
530
531//____________________________________________________________________
533 const SimHitList& origHitList,
534 std::map<SimBarCode,SimHitList> & outlists,
535 int& newBarCode )
536{
537 if (!origSimBarCode.isNonUniqueSecondary()||newBarCode>=0) {
538 theclass->message("createSecondaryHitLists"
539 " ERROR: Unexpected input");
540 return;
541 }
542
543 unsigned ntothitinput = origHitList.size();
544 HepMcParticleLink::index_type evtIndex = origSimBarCode.evtIndex();
545 int pdgCode = origSimBarCode.pdgCode();
546
548 // Temporarily put the (time,handle) pairs from the vector into a list. //
550
551 SimHitList::const_iterator itOrig(origHitList.begin()),itOrigE(origHitList.end());
552 std::list<SimHitHandleBase*> handleList;
553 for(;itOrig!=itOrigE;++itOrig)
554 handleList.push_back(itOrig->second);
555
557 // Produce handle lists based on requirements of proximity, //
558 // decreasing momenta and causality. //
560 std::set<std::list<SimHitHandleBase*> > outHandleLists;
561
562 bool ok;
563 double mass = VP1ParticleData::particleMass(pdgCode,ok);
564 double massSquared( (ok&&mass>=0) ? mass*mass : -1);
565 while (!handleList.empty()) {
566 std::list<SimHitHandleBase*> list;
567 std::list<SimHitHandleBase*>::iterator it(handleList.begin()), itNext, itTemp;
568 //Take this handle from the list, and increment to next one:
569 SimHitHandleBase * handle = *it;
570 list.push_back(handle);
571 itTemp = it++;
572 handleList.erase(itTemp);
573 while ( true ) {
574 if (it==handleList.end())
575 break;//TEST
576 itNext = closestCompatibleHandleItr(handle,it,handleList,massSquared);
578 if (itNext == handleList.end())//Fixme: cache handleList.end()? Check erase invalidation!
579 break;
580 handle = *itNext;
581 list.push_back(handle);
582 it = itNext; ++it;
583 handleList.erase(itNext);
584 if (it == handleList.end())//Fixme: cache handleList.end()? Check erase invalidation!
585 break;
586 }
587 if (list.size()==1) {//5 is ok for trt, but probably not for silicon!
588 //We need at least two sim hits//FIXME: 5?
589 //Fixme: Make minimum number of hits depend on energy?
590// theclass->messageVerbose("Ignoring secondary with barCode 0, since it seems to be the only sim. hit. by this track.");//Fixme: We could do something with posStart/posEnd?//fixme: update text to reflect 5
591 //FIXME: use this!: delete *(list.begin());
592 } else {
593 outHandleLists.insert(list);
594 }
595 }
596
598 // Put handle lists in output handle lists, and assign unique new fake barcodes //
600
601 //We assign fake negative barcodes to
602 //secondaries, since negative barcodes are
603 //by convention reserved for vertices in
604 //the HepMC collection.
605
606 std::set<std::list<SimHitHandleBase*> >::iterator itOutList(outHandleLists.begin()), itOutListE(outHandleLists.end());
607 unsigned totused(0);
608 for (;itOutList!=itOutListE;++itOutList) {
609 const SimBarCode fakeBarCode(newBarCode--,evtIndex,pdgCode);
610 //Fixme: Update barcodes contained in simhithandles!!
611 const unsigned n = itOutList->size();
612 totused += n;
613 outlists[fakeBarCode] = SimHitList();
614 std::map<SimBarCode,SimHitList>::iterator itActualOutList = outlists.find(fakeBarCode);
615 itActualOutList->second.reserve(n);
616
617 std::list<SimHitHandleBase*>::const_iterator itHandle(itOutList->begin()),itHandleE(itOutList->end());
618 for (;itHandle!=itHandleE;++itHandle)
619 itActualOutList->second.emplace_back((*itHandle)->hitTime(),*itHandle);
620
621 //Should be ok already, but just to be safe: (fixme: don't do this?)
622 sort(itActualOutList->second.begin(),itActualOutList->second.end());
623 }
624
625 if (VP1Msg::verbose())
626 theclass->messageVerbose("Grouped "+str(ntothitinput)+" secondaries with pdgCode = "
627 +str(pdgCode)+" into "+str(outHandleLists.size())
628 +" tracks ("+str(ntothitinput-totused)+" went unused).");
629}
630
631
632//____________________________________________________________________
634 const std::list<SimHitHandleBase*>::iterator& itFirst,
635 std::list<SimHitHandleBase*>& handleList,
636 const double& massSquared) const {
637// if (itFirst==handleList.end())
638// return handleList.end();
639 const double mom = handle->momentum();
640 const double momSq = mom*mom;
641 const double betaSqMax = ( (mom < 0 || massSquared<=0 ? 1 : (momSq/(momSq+massSquared)) ));
642 const double speedSqMax = 4.0* CLHEP::c_squared * betaSqMax;//NB: The factor of 4 is a fudge factor, not really clear why needed!!! VP1 or GEANT4 bug!
643 // const double speedSqMax = 1.0001 * c_squared;/*fixme test*/
644// const double speedSqMax = 1.1*betaSqMax*300*300;/*fixme test*/
645// double testmsq=ParticleConstants::electronMassInMeV*MeV*ParticleConstants::electronMassInMeV;
646// const double speedSqMax = 1.0001 * c_squared * ( (mom < 0 || testmsq<=0 ? 1 : (momSq/(momSq+testmsq)) ));
647
648 unsigned ichecked(0);
649 unsigned maxchecked(50);
650
651 const double hitTime = handle->hitTime();
652 const Amg::Vector3D pos = handle->posStart();
653 double mom2, flightTime;
654 std::list<SimHitHandleBase*>::iterator it(itFirst), itE(handleList.end());
655 std::list<SimHitHandleBase*>::iterator itMinDist(itE);
656 double minDistSq(100*CLHEP::cm*100*CLHEP::cm);//Put to 1.0e99 for no hard limit.
657 if (mom>0)
658 if (mom<500*CLHEP::MeV) {
659 minDistSq = 20*CLHEP::cm*20*CLHEP::cm;
660 if (mom<100*CLHEP::MeV) {
661 minDistSq = 10*CLHEP::cm*10*CLHEP::cm;
662 if (mom<10*CLHEP::MeV)
663 minDistSq = 5*CLHEP::cm*5*CLHEP::cm;
664 }
665 }
666
667 for (;it!=itE;++it) {
668 ++ichecked;
669 mom2 = (*it)->momentum();
670 //Possible decreasing momentum requirement:
671 if (mom>=0&&mom2>=0) {
672 if (mom2>mom)
673 continue;
674 if (mom2<0.5*mom)//Guard against delta-rays-ish.
675 continue;
676 }
677
678 //Maximal separation requirement:
679 const double distSquared = ((*it)->posStart()-pos).mag2();
680
681 //Only investigate if it is the closest one:
682 if (distSquared>=minDistSq)
683 continue;
684
685 flightTime = (*it)->hitTime() - hitTime;
686 if (flightTime<=0||flightTime>100*CLHEP::ns) {
687 //Second hit comes before (shouldn't happen), or waaaay after the first hit.
688 if (flightTime<0)
689 theclass->message("closestCompatibleHandleItr WARNING: Should never happen. T1="+str(hitTime)+", T2="+str((*it)->hitTime()));
690 continue;
691 } else {
692 if (distSquared>flightTime*flightTime*speedSqMax)
693 continue;
694 }
695
696
697 //Check for coalignment of momenta:
698 double mindotproduct = -0.5;
699 if (mom>10.0*CLHEP::MeV) {
700 mindotproduct = -0.1;
701 if (mom>1000.0*CLHEP::MeV) {
702 mindotproduct = 0.5;
703 if (mom>10000.0*CLHEP::MeV) {
704 mindotproduct = 0.80;
705 }
706 }
707 }
708 if (mindotproduct>-1.0)
709 if (handle->momentumDirection().dot((*it)->momentumDirection())<mindotproduct)
710 continue;
711
712 // theclass->messageDebug("Test. Hit passed mom and causality.");
713
714 // //Possible minimal separation requirement: //FIXME: Turn this on if momentum is greater than...?
715 // if (mom2>=0&&massSquared>=0) {
716 // const double speedSqMin = 0.9999 * c_squared * momSq/(momSq+massSquared);
717 // const double minDistSquared = flightTimeSq*speedSqMin;
718 // if (distSquared<minDistSquared)
719 // continue;
720 // }
721 //Fixme: We might also make some requirement that distance should be less than e.g. 20cm??
722
723 //Hits are in principle compatible. Store it if it is also the closest:
724 // if (distSquared<minDistSq) {
725 minDistSq = distSquared;
726 itMinDist = it;
727
728 if (distSquared<15*15*CLHEP::cm*CLHEP::cm) {
729 //We already found a very good hit - should not look much further.
730 if (distSquared<5*5*CLHEP::cm*CLHEP::cm)
731 maxchecked = ichecked + 5;
732 else
733 maxchecked = ichecked + 15;
734 }
735
736 if (ichecked>maxchecked)//For performance reasons
737 break;
738 }
739 return itMinDist;
740}
741
742
743//____________________________________________________________________
745 //Returns false only if we prune down to zero information!
746
747 if (hitlist.empty())
748 return true;
749
750
751 static double unknown = -1.0e99;
752 double mom(unknown), time(unknown);
753 if (p) {
754 HepMC::ConstGenVertexPtr v = p->production_vertex();
755 if (v) {
756 mom = mag(p->momentum());
757 time = v->position().t()/CLHEP::c_light;
758 // theclass->messageDebug("fixMomentumInfoInSimHits genparticle "+str(mom/GeV)+" GeV, time = "+str(time/ns)+" ns");
759 }
760 }
761
762 //First thing we do is to quickly determine if we are in the special
763 //situation of all hits+genparticle missing momentum information, or
764 //all hits already including momentum information.
765
766 bool sawhitwithoutmominfo(false);
767 bool sawhitwithmominfo(mom!=unknown);
768 SimHitList::iterator it(hitlist.begin()), itE(hitlist.end());
769 for (;it!=itE;++it) {
770 const bool hasinfo = it->second->momentum()>=0.0;
771 if (hasinfo)
772 sawhitwithmominfo = true;
773 else
774 sawhitwithoutmominfo = true;
775 if (sawhitwithoutmominfo&&sawhitwithmominfo)
776 break;
777 }
778
779 if (!sawhitwithoutmominfo) {
780 //Already perfectly fine
781 return true;
782 }
783 if (!sawhitwithmominfo) {
784 //Worst case scenario. Discard all for now.
785 theclass->messageDebug("Discarding hitlist." );
786 SimHitList::iterator it(hitlist.begin()), itE(hitlist.end());
787 for (;it!=itE;++it)
788 delete it->second;
789 hitlist.clear();
790 return false;
791 }
792
793// {
794// if (time!=unknown)
795// theclass->messageDebug("BEFORE STARTS WITH GP time = "+str(time/ns)+" ns, mom = "+str(mom/GeV)+" GeV" );
796// SimHitList::iterator it(hitlist.begin()), itE(hitlist.end());
797// for (;it!=itE;++it) {
798// theclass->messageDebug("BEFORE time = "+str(it->second->hitTime()/ns)+" ns, mom = "+str(it->second->momentum()/GeV)+" GeV" );
799// }
800// }
801
802 //OK, we have some mom info, but not in all hits. Time to do some dirty work!
803
804 //First (if no genparticle), we check if the hitlist begins with
805 //hits without momentum information. If it does, we simply supply
806 //the first hit with an *extrapolated* guess!
807 if (mom==unknown) {
808 if (hitlist.at(0).second->momentum()<0.0) {
809 SimHitList::iterator it(hitlist.begin()), itE(hitlist.end());
810 for (;it!=itE;++it) {
811 if (it->second->momentum()>=0.0) {
812 hitlist.at(0).second->setFakeMomentum(it->second->momentum()*1.00001);//We make it slightly bigger. Just because.
813 break;
814 }
815 }
816 if (hitlist.at(0).second->momentum()<0.0) {
817 theclass->messageDebug("fixMomentumInfoInSimHits ERROR: Should not happen! (1)" );
818 //Fixme: clear hitlist.
819 return false;
820 }
821 }
822 mom = hitlist.at(0).second->momentum();
823 time = hitlist.at(0).second->hitTime();
824 }
825
826 //Then we check if the hitlist ends with hits without momentum
827 //information. If it does we simply supply the very last of the hits
828 //with an *extrapolated* guess! (FIXME: ASSUME 0.01% loss every CLHEP::ns or
829 //something else that is simple? Or even use the extrapolator to the
830 //last of those hits?)
831 unsigned ilast = hitlist.size()-1;
832 if (hitlist.at(ilast).second->momentum()<0.0) {
833 for (int iLastWithMom = ilast-1;iLastWithMom>=0;--iLastWithMom) {
834 if (hitlist.at(iLastWithMom).second->momentum()>0.0) {
835 hitlist.at(ilast).second->setFakeMomentum(hitlist.at(iLastWithMom).second->momentum()*0.99999);//Slight smaller. Just because.
836 break;
837 }
838 }
839 if (hitlist.at(ilast).second->momentum()<0.0) {
840 //Get it from the genparticle:
841 if (mom==unknown) {
842 theclass->messageDebug("fixMomentumInfoInSimHits ERROR: Should not happen! (2)" );
843 //Fixme: clear hitlist.
844 return false;
845 }
846 hitlist.at(ilast).second->setFakeMomentum(mom*0.99999);
847 }
848 }
849
850 //Every unknown momentum is now surrounded in time with momentum information. Time to interpolate!!
851 if (mom==unknown||time==unknown) {
852 //No genparticle. Initialise from first hit.
853 mom = hitlist.at(0).second->momentum();
854 time = hitlist.at(0).second->hitTime();
855 }
856
857 unsigned iNextWithMom(0);
858 for (unsigned i = 0; i < hitlist.size(); ++i) {
859 if (hitlist.at(i).second->momentum()>=0.0) {
860 mom = hitlist.at(i).second->momentum();
861 time = hitlist.at(i).second->hitTime();
862 continue;
863 }
864 if (iNextWithMom<=i) {
865 for (unsigned j = i+1;j<hitlist.size();++j) {
866 if (hitlist.at(j).second->momentum()>=0.0) {
867 iNextWithMom = j;
868 break;
869 }
870 }
871 if (iNextWithMom<=i) {
872 theclass->messageDebug("fixMomentumInfoInSimHits ERROR: Should not happen! (3)" );
873 //Fixme: clear hitlist.
874 return false;
875 }
876 }
877 //
878 double time2 = hitlist.at(iNextWithMom).second->hitTime();
879 double mom2 = hitlist.at(iNextWithMom).second->momentum();
880 double t = hitlist.at(i).second->hitTime();
881// theclass->message("ABOUT TO INTERPOLATE time="+str(time/ns)+", t="+str(t/ns)+", time2="+str(time2/ns)+", mom="+str(mom/GeV)+", mom2="+str(mom2/GeV));//fixme
882
883 if (t<=time||t>=time2||time2<=time)
884 theclass->message("SUSPICIOUS TIME");//fixme
885 if (mom2>=mom)
886 theclass->message("SUSPICIOUS MOM mom="+str(mom)+", mom2="+str(mom2));//fixme
887 mom += (mom2-mom)*(t-time)/(time2-time);
888 time = t;
889 hitlist.at(i).second->setFakeMomentum(mom);
890 }
891
892
893
894
895// //Now we loop through the list and see what we need to fix, if anything:
896
897// unsigned iNextWithMom(0);
898// for (unsigned i = 1/*already dealt with first*/; i < hitlist.size(); ++i) {
899// if (hitlist.at(i).second->momentum()>=0.0) {
900// mom = hitlist.at(i).second->momentum();
901// time = hitlist.at(i).second->hitTime();
902// } else {
903// if (iNextWithMom<=i) {
904// for (unsigned j = i+1;j<hitlist.size();++j) {
905// if (hitlist.at(j).second->momentum()>=0.0) {
906// iNextWithMom = j;
907// break;
908// }
909// }
910// if (iNextWithMom<=i) {
911// //Discard end of list!!
912// unsigned j = i;
913// for (;j<hitlist.size();++j) {
914// //TESTdelete hitlist.at(j).second;
915// }
916// hitlist.resize(j);
917// theclass->messageDebug("Discarded "+str(hitlist.size()-i)+" simhits due to missing momentum information at the start of the list");
918// return !hitlist.empty() || !p;
919// }
920// //Interpolate mom(time) values:
921// double time2 = hitlist.at(iNextWithMom).second->hitTime();
922// double mom2 = hitlist.at(iNextWithMom).second->momentum();
923// double t = hitlist.at(i).second->hitTime();
924// if (t<=time||t>=time2||time2<=time)
925// theclass->message("SUSPICIOUS TIME");//fixme
926// if (mom2>=mom)
927// theclass->message("SUSPICIOUS MOM");//fixme
928// mom = (t-time)/(time2-time)*(mom2-mom);
929// time = t;
930// hitlist.at(i).second->setFakeMomentum(mom);
931// }
932// }
933// }
934
935
936// // typedef std::vector<std::pair<double,SimHitHandleBase*> > SimHitList;//hitTime() to SimHitHandle's
937
938
939// {
940// SimHitList::iterator it(hitlist.begin()), itE(hitlist.end());
941// for (;it!=itE;++it) {
942// theclass->messageDebug("AFTER time = "+str(it->second->hitTime()/ns)+" ns, mom = "+str(it->second->momentum()/GeV)+" GeV" );
943// }
944// }
945
946 return true;
947
948}
949
950
951//If has momentum - Check light-speed consistency given positions and time.
952//mom should be decreasing
953
954//____________________________________________________________________
956{
957
958 theclass->message("updateVisibleAssociatedObjects");//fixme
959 theclass->largeChangesBegin();
960 theclass->trackHandleIterationBegin();
962 while ((handle=static_cast<TrackHandle_TruthTrack*>(theclass->getNextTrackHandle()))) {
963 handle->setAscObjsVisible(displayAscObjs);
964 }
965 theclass->largeChangesEnd();
966}
967
float hitTime(const AFP_SIDSimHit &hit)
Scalar mag() const
mag method
double charge(const T &p)
Definition AtlasPID.h:997
AtlasHitsVector< CSCSimHit > CSCSimHitCollection
AtlasHitsVector< MDTSimHit > MDTSimHitCollection
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
AtlasHitsVector< RPCSimHit > RPCSimHitCollection
AtlasHitsVector< SiHit > SiHitCollection
std::vector< std::pair< double, SimHitHandleBase * > > SimHitList
AtlasHitsVector< TGCSimHit > TGCSimHitCollection
AtlasHitsVector< TRTUncompressedHit > TRTUncompressedHitCollection
AtlasHitsVector< TrackRecord > TrackRecordCollection
Header file for AthHistogramAlgorithm.
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
Definition SiHit.h:19
std::pair< int, HepMcParticleLink::index_type > ExtBarCode
Definition SimBarCode.h:42
ExtBarCode extBarCode() const
Definition SimBarCode.h:43
static const int unknownPDG
Definition SimBarCode.h:25
bool isNonUniqueSecondary() const
HepMcParticleLink::index_type evtIndex() const
int pdgCode() const
const QString & name() const
virtual bool cut(TrackHandleBase *)
void addTrackHandle(TrackHandleBase *)
TrackCollHandleBase(TrackSysCommonData *, const QString &name, TrackType::Type)
static SimHitHandleBase * createHitHandle(const TRTUncompressedHit &h)
void addHitCollections(std::map< SimBarCode, SimHitList > &hitLists)
std::map< SimBarCode::ExtBarCode, int > extBarCode2pdg
bool fixMomentumInfoInSimHits(HepMC::ConstGenParticlePtr p, SimHitList &hitlist) const
bool loadHitLists(std::map< SimBarCode, SimHitList > &hitLists)
std::list< SimHitHandleBase * >::iterator closestCompatibleHandleItr(SimHitHandleBase *handle, const std::list< SimHitHandleBase * >::iterator &itFirst, std::list< SimHitHandleBase * > &handleList, const double &massSquared) const
static SimHitHandleBase * createHitHandle(const TrackRecord &h)
void createSecondaryHitLists(const SimBarCode &origSimBarCode, const SimHitList &origHitList, std::map< SimBarCode, SimHitList > &outlists, int &newBarCode)
static double mag(const HepMC::FourVector &v)
static SimHitHandleBase * createHitHandle(const SiHit &h)
void loadGenParticles(std::map< SimBarCode, HepMC::ConstGenParticlePtr > &genParticles, const HepMC::ConstGenVertexPtr &vtx)
static QStringList availableCollections(IVP1System *)
virtual bool cut(TrackHandleBase *)
TrackCollHandle_TruthTracks(TrackSysCommonData *, const QString &name)
virtual void setupSettingsFromControllerSpecific(TrackSystemController *)
void fixPDGCode(SimHitHandleBase *) const
bool hasVertexAtIR(const double &rmaxsq, const double &zmax) const
void messageVerbose(const QString &) const
void message(const QString &) const
void setHelperClassName(const QString &n)
static bool hasTRTGeometry()
static bool hasPixelGeometry()
static bool hasSCTGeometry()
static bool hasMuonGeometry()
static bool verbose()
Definition VP1Msg.h:31
static double particleMass(const int &pdgcode, bool &ok)
static double particleCharge(const int &pdgcode, bool &ok)
bool retrieve(const T *&, const QString &key) const
QStringList getKeys() const
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:148
Eigen::Matrix< double, 3, 1 > Vector3D
int barcode(const T *p)
Definition Barcode.h:15
HepMC3::FourVector FourVector
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
Definition GenParticle.h:20
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
Definition GenVertex.h:24
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39