11#include "GaudiKernel/ITHistSvc.h"
42#include "CLHEP/Vector/LorentzVector.h"
43using CLHEP::HepLorentzVector;
62 m_trackSelector(
"InDet::InDetTrackSelectorTool/InDetTrackSelectorTool"),
63 m_genPartSelector(
"Trk::InDetReconstructableSelector/InDetReconstructableSelector"),
89 declareProperty(
"PrimaryVertexCollection",
m_inputPrimaryVertexCollection,
"SG key of input PrimaryVertex collection (needed for TrackSelector, if no vertex collection is given, the selector will not use the vertex position)");
112 msg(MSG::INFO) <<
"TrackValidationNtupleWriter initialize()" <<
endmsg;
116 msg(MSG::FATAL) <<
"joboptions TrackCollection and TrackTruthCollection have different sizes!" <<
endmsg;
117 msg(MSG::FATAL) <<
"If you like to get truth data (DoTruth=True)," <<
endmsg;
118 msg(MSG::FATAL) <<
" please make sure to set for each TrackCollection the corresponding TrackTruthCollection" <<
endmsg;
119 return StatusCode::FAILURE;
124 if (
sc.isFailure()) {
131 if (
sc.isFailure()) {
140 if (
sc.isFailure()) {
141 msg(MSG::FATAL) <<
"Could not retrieve "<<
m_trackSelector <<
" (to select the tracks which are written to the ntuple) "<<
endmsg;
142 msg(MSG::INFO) <<
"Set the ToolHandle to None if track selection is supposed to be disabled" <<
endmsg;
147 msg(MSG::INFO) <<
"Track Selector retrieved" <<
endmsg;
150 ToolHandleArray< Trk::IEventPropertyNtupleTool >::iterator itTools;
168 if (
sc.isFailure()) {
175 if (
sc.isFailure()) {
182 ToolHandleArray< ITrackTruthClassifier >::iterator itTools;
199 if (
sc.isFailure()) {
201 <<
" (to write jet data)."<<
endmsg <<
"--> Instead configure "
202 <<
"to empty string if jet data not desired." <<
endmsg;
206 if (
sc.isFailure()) {
208 <<
" (to find jets at truth level)."<<
endmsg;
215 if (
sc.isFailure()) {
221 if (
sc.isFailure())
return sc;
229 SmartIF<ITHistSvc> tHistSvc{service(
"THistSvc")};
237 TTree*
tree =
new TTree((*trackColNameIter).c_str(), ((*trackColNameIter)+
" Validation").c_str());
240 sc = tHistSvc->regTree(fullNtupleName,
tree);
241 if (
sc.isFailure()) {
242 msg(MSG::ERROR) <<
"Unable to register TTree : " << fullNtupleName <<
endmsg;
246 ToolHandleArray< Trk::ITrackValidationNtupleTool >::iterator itTools;
249 if (((*itTools)->addNtupleItems(
tree)).isFailure()) {
250 msg(MSG::ERROR) <<
"ValidationNtupleTool could not add its branches"
251 <<
" for tree " << fullNtupleName <<
endmsg;
252 return StatusCode::FAILURE;
264 TTree*
tree =
new TTree((*trackParticleColNameIter).c_str(), ((*trackParticleColNameIter)+
" Validation").c_str());
267 sc = tHistSvc->regTree(fullNtupleName,
tree);
268 if (
sc.isFailure()) {
269 msg(MSG::ERROR) <<
"Unable to register TTree : " << fullNtupleName <<
endmsg;
274 if (
sc.isFailure()) {
275 msg(MSG::ERROR) <<
"ValidationNtupleTool could not add its branches for tree " << fullNtupleName <<
endmsg;
293 m_eventLinkTree =
new TTree(
"EventToTrackLink",
"Event to track entry link");
296 if (
sc.isFailure()) {
297 msg(MSG::ERROR) <<
"Unable to register TTree : " << fullNtupleName <<
endmsg;
308 return StatusCode::SUCCESS;
313 return StatusCode::SUCCESS;
322 ATH_MSG_DEBUG (
"TrackValidationNtupleWriter execute() start");
324 StatusCode
sc = StatusCode::SUCCESS;
if (
sc.isFailure()) { }
331 unsigned int nTruthTreeRecordsAtCurrentEvent = 0;
332 std::vector<Trk::ValidationTrackTruthData> truthData;
333 std::vector<HepMC::ConstGenParticlePtr>* selecParticles =
nullptr;
336 std::vector< Trk::GenParticleJet >* genParticleJets =
nullptr;
343 std::string key =
"G4Truth";
344 if (
evtStore()->retrieve(mcEventColl, key).isFailure())
346 msg(MSG::WARNING) <<
"Could not find the McEventCollection" <<
endmsg;
347 return StatusCode::SUCCESS;
368 for (
const auto& genParticle: *selecParticles)
375 if ( genParticle->production_vertex() )
377 generatedTrackPerigee =
m_truthToTrack->makePerigeeParameters( genParticle );
379 ATH_MSG_DEBUG (
"No perigee available for interacting truth particle."
380 <<
" -> This is OK for particles from the TrackRecord, but probably"
381 <<
" a bug for production vertex particles.");
395 truthData.push_back(partData);
406 else ATH_MSG_DEBUG (
"jets found: " << genParticleJets->size());
417 const VxContainer* primaryVertexContainer =
nullptr;
420 if ( !primaryVertexContainer ||
sc.isFailure() ) {
422 delete genParticleJets;
423 return StatusCode::FAILURE;
427 if (primaryVertexContainer) {
428 if (!primaryVertexContainer->
empty())
vertex = &((*primaryVertexContainer)[0]->recVertex());
435 if (
sc.isFailure()) {
437 delete genParticleJets;
447 if (
sc.isFailure()) {
449 delete genParticleJets;
458 (nTruthTreeRecordsAtCurrentEvent,
459 selecParticles ? (
int)selecParticles->size() : 0);
466 if (
sc.isFailure()) {
467 msg(MSG::ERROR) <<
"Failure when filling event data." <<
endmsg;
468 delete genParticleJets;
476 std::vector<unsigned int> truthToJetIndices(selecParticles ? selecParticles->size() : 0);
479 const unsigned int nJetTruthTreeRecordsAtCurrentEvent
482 for( std::vector<Trk::GenParticleJet>::iterator itrMcJet = genParticleJets->begin();
483 itrMcJet < genParticleJets->end() ; ++itrMcJet) {
485 std::vector<int>
indices = (*itrMcJet).getIndicesInEvent();
487 for (std::vector<int>::const_iterator k =
indices.begin(); k !=
indices.end(); ++k) {
488 if (*k >= 0 && *k <= (
int)selecParticles->size() ) {
489 truthData[*k].truthToJetIndex = nJetTruthTreeRecordsAtCurrentEvent
490 + int(itrMcJet - genParticleJets->begin()) + 1;
492 msg(MSG::WARNING) <<
" mistake with jet::indexInEvent !! " << *k <<
endmsg;
497 nTruthTreeRecordsAtCurrentEvent);
498 if (
sc.isFailure() ){
499 msg(MSG::ERROR) <<
"Jet Truth Ntuple Tool could not fill data" <<
endmsg;
500 delete genParticleJets;
501 return StatusCode::FAILURE;
508 if (
sc.isFailure() ){
509 msg(MSG::ERROR) <<
"Truth Ntuple Tool could not fill data" <<
endmsg;
510 delete genParticleJets;
511 return StatusCode::FAILURE;
515 delete selecParticles;
516 delete genParticleJets;
517 std::vector<Trk::ValidationTrackTruthData>::iterator truthDataIter = truthData.begin();
518 for (; truthDataIter != truthData.end(); ++truthDataIter) {
519 delete (*truthDataIter).truthPerigee;
520 (*truthDataIter).truthPerigee =
nullptr;
522 return StatusCode::SUCCESS;
526 std::vector<Trk::ValidationTrackTruthData>& truthData,
528 StatusCode
sc = StatusCode::SUCCESS;
if (
sc.isFailure()) { }
533 if (
sc.isFailure()) {
535 return StatusCode::SUCCESS;
541 return StatusCode::SUCCESS;
549 if (
sc.isFailure()) {
552 return StatusCode::SUCCESS;
560 return StatusCode::SUCCESS;
564 const unsigned int nTruthTreeRecordsAtCurrentEvent =
567 int trackTreeIndexBegin =
m_trees[trackColIndex]->GetEntries();
568 int nTracksPerEvent = 0;
572 for ( ; trackIterator < (*tracks).end(); ++trackIterator) {
573 if (!((*trackIterator))) {
579 ToolHandleArray< Trk::ITrackValidationNtupleTool >::iterator itTools;
582 if (((*itTools)->fillTrackData( *(*trackIterator), 0 )).isFailure()) {
583 ATH_MSG_ERROR (
"Validation Ntuple Tool could not fill track data.");
584 return StatusCode::FAILURE;
587 nTracksPerEvent += 1;
593 TrackTruthCollection::const_iterator truthIterator = trackTruthCollection->find( trackIterator - (*tracks).begin() );
594 if ( truthIterator == trackTruthCollection->end() ){
595 ATH_MSG_DEBUG (
"No matching truth particle found for track");
597 trackTruth = &((*truthIterator).second);
599 if (
msgLvl(MSG::DEBUG))
msg(MSG::DEBUG) <<
"Link to generated particle information is not there - assuming a lost G4 particle ('fake fake')." <<
endmsg;
607 if ( genParticle!=
nullptr && genParticle->pdg_id() == 0 ) {
608 if (
msgLvl(MSG::DEBUG))
msg(MSG::DEBUG) <<
"Associated Particle ID " << genParticle->pdg_id()
609 <<
" does not conform to PDG requirements... ignore it!" <<
endmsg;
610 genParticle =
nullptr;
622 std::vector<Trk::ValidationTrackTruthData>::iterator matchedPartIter = truthData.begin();
623 for (; matchedPartIter != truthData.end(); ++matchedPartIter) {
625 if ((*matchedPartIter).genParticle == genParticle)
break;
627 if (matchedPartIter == truthData.end()) {
630 if (
msgLvl(MSG::VERBOSE))
msg(MSG::VERBOSE) <<
"Matched particle " << genParticle <<
" is not in list of selected particles" <<
endmsg;
631 if ( genParticle->production_vertex() ) {
632 newTrackPerigee =
m_truthToTrack->makePerigeeParameters( genParticle );
633 generatedTrackPerigee = newTrackPerigee;
637 (*matchedPartIter).truthToTrackIndices[trackColIndex].push_back(
m_nTrackTreeRecords[trackColIndex]);
638 (*matchedPartIter).truthToTrackMatchingProbabilities[trackColIndex].push_back(trackTruth->
probability());
639 generatedTrackPerigee = (*matchedPartIter).truthPerigee;
641 truthIndex += nTruthTreeRecordsAtCurrentEvent;
643 ToolHandleArray< Trk::ITrackValidationNtupleTool >::iterator itTools;
646 if (((*itTools)->fillTrackTruthData( generatedTrackPerigee, *trackTruth, truthIndex )).isFailure()) {
647 ATH_MSG_ERROR (
"Validation Ntuple Tool could not fill track truth data.");
648 delete newTrackPerigee;
649 return StatusCode::FAILURE;
653 delete newTrackPerigee;
658 m_trees[trackColIndex]->Fill();
661 (*itTools)->resetVariables();
666 if (
msgLvl(MSG::VERBOSE))
msg(MSG::VERBOSE) <<
"track not selected!" <<
endmsg;
672 (trackColIndex, trackTreeIndexBegin, nTracksPerEvent );
674 return StatusCode::SUCCESS;
679 msg(MSG::DEBUG) <<
"writeTrackParticleData method started" <<
endmsg;
688 return StatusCode::SUCCESS;
694 return StatusCode::SUCCESS;
699 int nTrkParticlesPerEvent = 0;
703 for ( ; trackParticleIterator < (*trackParticles).end(); ++trackParticleIterator) {
704 if (!((*trackParticleIterator))) {
710 msg(MSG::ERROR) <<
"Validation Ntuple Tool could not fill track data." <<
endmsg;
711 return StatusCode::FAILURE;
713 nTrkParticlesPerEvent += 1;
716 msg(MSG::ERROR) <<
"Validation Ntuple Tool could not write track data." <<
endmsg;
717 return StatusCode::FAILURE;
724 m_eventPropertyNtupleTools[toolIndex]->setTrackTreeIndices(trackParticleColIndex, trackTreeIndexBegin, nTrkParticlesPerEvent );
727 msg(MSG::DEBUG) <<
"writeTrackParticleData successfully finished " <<
endmsg;
728 return StatusCode::SUCCESS;
734 msg(MSG::INFO) <<
"TrackValidationNtupleWriter finalize()" <<
endmsg;
745 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
Extrapolation for HepMC particles.
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
static const Attributes_t empty
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
bool msgLvl(const MSG::Level lvl) const
DataModel_detail::const_iterator< DataVector > const_iterator
bool empty() const noexcept
Returns true if the collection is empty.
bool isValid() const
Validity check.
HepMC::ConstGenParticlePtr cptr() const
Dereference.
HepMC::ConstGenParticlePtr scptr() const
Dereference/smart pointer.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
MC particle associated with a reco track + the quality of match.
float probability() const
const HepMcParticleLink & particleLink() const
std::vector< std::string > m_inputTrackCollection
name of the TrackCollection
bool m_useExternalEventLinkTree
if TVNW should make event tree itself or assume CBNT does
StatusCode writeTrackParticleData(unsigned int trackParticleColIndex)
method to write track particle data to Ntuple.
std::string m_ntupleDirName
jobOption: Ntuple directory name
StatusCode initialize()
standard Athena-Algorithm method
std::vector< unsigned int > m_nTrackTreeRecords
ToolHandle< Trk::IGenParticleSelector > m_genPartSelector
Tool handle to the Trk::IGenParticleSelector.
StatusCode finalize()
standard Athena-Algorithm method
std::vector< TTree * > m_trees
Pointer to the NTuple trees (one for each input track collection)
ToolHandleArray< Trk::ITrackValidationNtupleTool > m_ValidationNtupleTools
set of tools for writing Trk::Track into the Ntuple
std::string m_inputPrimaryVertexCollection
SG key of the input primary vertex collection (used by the track selector)
StatusCode execute()
standard Athena-Algorithm method
TTree * m_eventLinkTree
pointer to event-wise ntuple trees (one for all input track collections + truth)
bool m_doTruth
Switch to turn on / off truth.
StatusCode writeTrackData(unsigned int trackColIndex, std::vector< Trk::ValidationTrackTruthData > &truthData, const Trk::Vertex *primaryVertex=NULL)
< following methods and atributes are used in derived class TrigTrackValidationNtupleWriter Private m...
ToolHandleArray< Trk::ITrackTruthClassifier > m_trackTruthClassifierHandles
jobO: list of truth classifiers
ToolHandle< Trk::ITrackSelectorTool > m_trackSelector
Tool handle to the Trk::ITrackSelectorTool.
ToolHandle< Trk::IValidationNtupleTool > m_ValTrkParticleNtupleTool
Tool for writting Rec::TrackParticle into the Ntuple – OBSOLETE.
std::string m_McEventCollectionName
name of the McEventCollection
std::vector< const Trk::ITrackTruthClassifier * > m_trackTruthClassifiers
the truth classifiers
ToolHandle< Trk::ITruthNtupleTool > m_truthNtupleTool
ToolHandle< Trk::IJetTruthNtupleTool > m_jetTruthNtupleTool
std::vector< Trk::IEventPropertyNtupleTool * > m_eventPropertyNtupleTools
list of event property tools
ToolHandleArray< Trk::IEventPropertyNtupleTool > m_eventPropertyNtupleHandles
jobO: list of event property tools
ToolHandle< Trk::ITruthToTrack > m_truthToTrack
Tool handle to the Trk::TruthToTrack tool.
std::string m_ntupleFileName
jobOption: Ntuple file name
std::vector< std::string > m_trackTruthCollectionName
name of the TrackTruthCollection
~TrackValidationNtupleWriter()
Default Destructor.
HepMC::GenParticlePtr m_visibleParticleWithoutTruth
Switch to turn on/pff recording track particle trees into Ntuple.
std::vector< std::string > m_inputTrackParticleCollection
name of the TrackParticleCollection
ToolHandle< Trk::IGenParticleJetFinder > m_genJetFinder
Tool to find jets.
TrackValidationNtupleWriter(const std::string &name, ISvcLocator *pSvcLocator)
Standard Athena-Algorithm Constructor.
const Trk::TrackParameters * truthPerigee
std::vector< std::vector< unsigned int > > truthToTrackIndices
std::vector< unsigned int > classifications
HepMC::ConstGenParticlePtr genParticle
std::vector< std::vector< float > > truthToTrackMatchingProbabilities
This class is a simplest representation of a vertex candidate.
bool contains(const std::string &s, const std::string ®x)
does a string contain the substring
GenParticlePtr newGenParticlePtr(const HepMC::FourVector &mom=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), int pid=0, int status=0)
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation (TODO migrate to be...
const GenParticle * ConstGenParticlePtr
DataVector< TrackParticleBase > TrackParticleBaseCollection
std::pair< long int, long int > indices
ParametersBase< TrackParametersDim, Charged > TrackParameters