From f37b282e9b7123cfdbf63d4d63057d82824adeeb Mon Sep 17 00:00:00 2001 From: Thomas Madlener Date: Fri, 16 Feb 2024 16:36:46 +0100 Subject: [PATCH] Make sure to set the necessary type information in relation collections that are created (#69) * Make the SpacePoint relations carry type information Necessary for automatic conversion to EDM4hep * Use unique_ptr to manage memory * Make sure to propagate type information into the relation * Make sure to keep necessary type information in output relations --- .../Digitisers/include/DDTPCDigiProcessor.h | 9 +++-- source/Digitisers/src/DDSpacePointBuilder.cc | 33 ++++------------ source/Digitisers/src/DDTPCDigiProcessor.cc | 39 +++++++------------ source/Refitting/include/RefitProcessor.h | 4 +- source/Refitting/src/RefitProcessor.cc | 39 +++++++------------ 5 files changed, 44 insertions(+), 80 deletions(-) diff --git a/source/Digitisers/include/DDTPCDigiProcessor.h b/source/Digitisers/include/DDTPCDigiProcessor.h index dfee072..88974a8 100644 --- a/source/Digitisers/include/DDTPCDigiProcessor.h +++ b/source/Digitisers/include/DDTPCDigiProcessor.h @@ -58,6 +58,10 @@ class Voxel_tpc; class TPCModularEndplate ; +namespace UTIL { + class LCRelationNavigator; +} + /** ====== DDTPCDigiProcessor ======
* * This Processor depends on Circle.h from MarlinUtil @@ -138,8 +142,8 @@ class DDTPCDigiProcessor : public marlin::Processor { */ virtual void end() ; - void writeVoxelToHit( Voxel_tpc* aVoxel) ; - void writeMergedVoxelsToHit( std::vector * hitList ) ; + void writeVoxelToHit( Voxel_tpc* aVoxel, UTIL::LCRelationNavigator& hitSimHitNav) ; + void writeMergedVoxelsToHit( std::vector * hitList, UTIL::LCRelationNavigator& hitSimHitNav ) ; void plotHelixHitResidual(MCParticle *mcp, CLHEP::Hep3Vector *thisPointRPhi); double getPadPhi( CLHEP::Hep3Vector* thisPointRPhi, CLHEP::Hep3Vector* firstPointRPhi, CLHEP::Hep3Vector* middlePointRPhi, CLHEP::Hep3Vector* lastPointRPhi); double getPadTheta( CLHEP::Hep3Vector* firstPointRPhi, CLHEP::Hep3Vector* middlePointRPhi, CLHEP::Hep3Vector* lastPointRPhi ); @@ -204,7 +208,6 @@ class DDTPCDigiProcessor : public marlin::Processor { int lenpos{}; LCCollectionVec* _trkhitVec{}; - LCCollectionVec* _relCol{}; CellIDEncoder* _cellid_encoder {}; int _NSimTPCHits{}; diff --git a/source/Digitisers/src/DDSpacePointBuilder.cc b/source/Digitisers/src/DDSpacePointBuilder.cc index 8e734b6..5bcb3b9 100644 --- a/source/Digitisers/src/DDSpacePointBuilder.cc +++ b/source/Digitisers/src/DDSpacePointBuilder.cc @@ -176,14 +176,10 @@ void DDSpacePointBuilder::processEvent( LCEvent * evt ) { LCCollectionVec * spCol = new LCCollectionVec(LCIO::TRACKERHIT); // output spacepoint collection - LCCollectionVec* relCol = new LCCollectionVec(LCIO::LCRELATION); // outpur relation collection - - // to store the weights - LCFlagImpl lcFlag(0) ; - lcFlag.setBit( LCIO::LCREL_WEIGHTED ) ; - relCol->setFlag( lcFlag.getFlag() ) ; - - + + // Relation navigator for creating SpacePoint - SimTrackerHit relations + auto spSimHitNav = UTIL::LCRelationNavigator(LCIO::TRACKERHIT, LCIO::SIMTRACKERHIT); + unsigned nHits = col->getNumberOfElements() ; streamlog_out(DEBUG3) << "Number of hits: " << nHits <<"\n"; @@ -304,38 +300,22 @@ void DDSpacePointBuilder::processEvent( LCEvent * evt ) { /////////////////////////////// // make the relations - if( simHitsFront.size() == 1 ){ SimTrackerHit* simHit = dynamic_cast< SimTrackerHit* >( simHitsFront[0] ); if( simHit != NULL ){ - LCRelationImpl* rel = new LCRelationImpl; - rel->setFrom (spacePoint); - rel->setTo (simHit); - rel->setWeight( 0.5 ); - relCol->addElement(rel); + spSimHitNav.addRelation(spacePoint, simHit, 0.5); } } - - - if( simHitsBack.size() == 1 ){ SimTrackerHit* simHit = dynamic_cast< SimTrackerHit* >( simHitsBack[0] ); if( simHit != NULL ){ - LCRelationImpl* rel = new LCRelationImpl; - rel->setFrom (spacePoint); - rel->setTo (simHit); - rel->setWeight( 0.5 ); - relCol->addElement(rel); + spSimHitNav.addRelation(spacePoint, simHit, 0.5); } } - - - - } else { if ( ghost_hit == true ) { @@ -356,6 +336,7 @@ void DDSpacePointBuilder::processEvent( LCEvent * evt ) { } evt->addCollection( spCol, _SpacePointsCollection); + auto* relCol = spSimHitNav.createLCCollection(); evt->addCollection( relCol , _relColName ) ; streamlog_out(DEBUG3)<< "\nCreated " << createdSpacePoints diff --git a/source/Digitisers/src/DDTPCDigiProcessor.cc b/source/Digitisers/src/DDTPCDigiProcessor.cc index db91de6..e9157d9 100644 --- a/source/Digitisers/src/DDTPCDigiProcessor.cc +++ b/source/Digitisers/src/DDTPCDigiProcessor.cc @@ -32,6 +32,7 @@ // #include "UTIL/LCTrackerConf.h" #include +#include // --- DD4hep --- #include "DD4hep/Detector.h" @@ -500,12 +501,8 @@ void DDTPCDigiProcessor::processEvent( LCEvent * evt ) // created the collection which will be written out _trkhitVec = new LCCollectionVec( LCIO::TRACKERHIT ) ; - _relCol = new LCCollectionVec(LCIO::LCRELATION); - - // to store the weights - LCFlagImpl lcFlag(0) ; - lcFlag.setBit( LCIO::LCREL_WEIGHTED ) ; - _relCol->setFlag( lcFlag.getFlag() ) ; + // relations from created trackerhits to the SimTrackerHits that caused them + auto hitSimHitNav = UTIL::LCRelationNavigator(LCIO::TRACKERHIT, LCIO::SIMTRACKERHIT); _cellid_encoder = new CellIDEncoder( lcio::LCTrackerCellID::encoding_string() , _trkhitVec ) ; @@ -1017,7 +1014,7 @@ void DDTPCDigiProcessor::processEvent( LCEvent * evt ) } if(seed_hit->getNumberOfAdjacent()==0){ // no adjacent hits so smear and write to hit collection - writeVoxelToHit(seed_hit); + writeVoxelToHit(seed_hit, hitSimHitNav); } else if(seed_hit->getNumberOfAdjacent() < (_maxMerge)){ // potential 3-hit cluster, can use simple average merge. @@ -1028,7 +1025,7 @@ void DDTPCDigiProcessor::processEvent( LCEvent * evt ) if( clusterSize <= _maxMerge ){ // merge cluster seed_hit->setIsMerged(); - writeMergedVoxelsToHit(hitsToMerge); + writeMergedVoxelsToHit(hitsToMerge, hitSimHitNav); } delete hitsToMerge; } @@ -1074,7 +1071,8 @@ void DDTPCDigiProcessor::processEvent( LCEvent * evt ) // add the collection to the event evt->addCollection( _trkhitVec , _TPCTrackerHitsCol ) ; - evt->addCollection( _relCol , _outRelColName ) ; + auto relCol = hitSimHitNav.createLCCollection(); + evt->addCollection( relCol , _outRelColName ) ; // delete voxels for (unsigned int i = 0; i<_tpcRowHits.size(); ++i){ @@ -1149,7 +1147,7 @@ void DDTPCDigiProcessor::end() // } -void DDTPCDigiProcessor::writeVoxelToHit( Voxel_tpc* aVoxel){ +void DDTPCDigiProcessor::writeVoxelToHit( Voxel_tpc* aVoxel, UTIL::LCRelationNavigator& hitSimHitNav){ Voxel_tpc* seed_hit = aVoxel; @@ -1261,13 +1259,8 @@ void DDTPCDigiProcessor::writeVoxelToHit( Voxel_tpc* aVoxel){ trkHit->rawHits().push_back( _tpcHitMap[seed_hit] ); } - LCRelationImpl* rel = new LCRelationImpl; - - rel->setFrom (trkHit); - rel->setTo (_tpcHitMap[seed_hit]); - rel->setWeight( 1.0 ); - _relCol->addElement(rel); - + hitSimHitNav.addRelation(trkHit, _tpcHitMap[seed_hit], 1.0); + _trkhitVec->addElement( trkHit ); _NRecTPCHits++; } @@ -1298,7 +1291,7 @@ void DDTPCDigiProcessor::writeVoxelToHit( Voxel_tpc* aVoxel){ #endif } -void DDTPCDigiProcessor::writeMergedVoxelsToHit( vector * hitsToMerge){ +void DDTPCDigiProcessor::writeMergedVoxelsToHit( vector * hitsToMerge, UTIL::LCRelationNavigator& hitSimHitNav){ TrackerHitImpl* trkHit = new TrackerHitImpl ; @@ -1324,13 +1317,9 @@ void DDTPCDigiProcessor::writeMergedVoxelsToHit( vector * hitsToMerg trkHit->rawHits().push_back( _tpcHitMap[hitsToMerge->at(ihitCluster)] ); } - LCRelationImpl* rel = new LCRelationImpl; - - rel->setFrom (trkHit); - rel->setTo (_tpcHitMap[ hitsToMerge->at(ihitCluster) ]); - rel->setWeight( float(1.0/number_of_hits_to_merge) ); - _relCol->addElement(rel); - + hitSimHitNav.addRelation(trkHit, + _tpcHitMap[hitsToMerge->at(ihitCluster)], + float(1.0/number_of_hits_to_merge)); } double avgZ = sumZ/(hitsToMerge->size()); diff --git a/source/Refitting/include/RefitProcessor.h b/source/Refitting/include/RefitProcessor.h index 541bed1..4be228e 100644 --- a/source/Refitting/include/RefitProcessor.h +++ b/source/Refitting/include/RefitProcessor.h @@ -9,6 +9,8 @@ #include +#include + namespace MarlinTrk{ class IMarlinTrkSystem ; } @@ -79,7 +81,7 @@ class RefitProcessor : public marlin::Processor { lcio::LCCollection* GetCollection( lcio::LCEvent * evt, std::string colName ) ; /* helper function to get relations using try catch block */ - lcio::LCRelationNavigator* GetRelations(lcio::LCEvent * evt, std::string RelName ) ; + std::unique_ptr GetRelations(lcio::LCEvent * evt, std::string RelName ) ; /** Input track collection name for refitting. */ diff --git a/source/Refitting/src/RefitProcessor.cc b/source/Refitting/src/RefitProcessor.cc index e6e92bd..3ef22b9 100644 --- a/source/Refitting/src/RefitProcessor.cc +++ b/source/Refitting/src/RefitProcessor.cc @@ -195,7 +195,7 @@ void RefitProcessor::processEvent( LCEvent * evt ) { // get input collection and relations LCCollection* input_track_col = this->GetCollection( evt, _input_track_col_name ) ; - LCRelationNavigator* input_track_rels = this->GetRelations( evt, _input_track_rel_name ) ; + auto input_track_rels = this->GetRelations( evt, _input_track_rel_name ) ; if( input_track_col != 0 ){ @@ -207,12 +207,11 @@ void RefitProcessor::processEvent( LCEvent * evt ) { LCFlagImpl trkFlag(0) ; trkFlag.setBit( LCIO::TRBIT_HITS ) ; trackVec->setFlag( trkFlag.getFlag() ) ; - - // establish the track relations collection that will be created - LCCollectionVec* trackRelVec = new LCCollectionVec( LCIO::LCRELATION ) ; - + + // establish the track relations collection that will be created + auto trackRelNav = UTIL::LCRelationNavigator(LCIO::TRACK, LCIO::MCPARTICLE); + int nTracks = input_track_col->getNumberOfElements() ; - streamlog_out(DEBUG4) << "Processing input collection " << _input_track_col_name << " with " << nTracks << " tracks\n"; // loop over the input tacks and refit using KalTest @@ -394,28 +393,18 @@ void RefitProcessor::processEvent( LCEvent * evt ) { // assign the relations previously assigned to the input tracks if(input_track_rels){ - LCObjectVec objVec = input_track_rels->getRelatedToObjects( track_to_refit ); - FloatVec weights = input_track_rels->getRelatedToWeights( track_to_refit ); + const auto& objVec = input_track_rels->getRelatedToObjects( track_to_refit ); + const auto& weights = input_track_rels->getRelatedToWeights( track_to_refit ); for( unsigned int irel=0 ; irel < objVec.size() ; ++irel ){ - - LCRelationImpl* rel = new LCRelationImpl ; - rel->setFrom (refittedTrack) ; - rel->setTo ( objVec[irel] ) ; - rel->setWeight(weights[irel]) ; - trackRelVec->addElement( rel ); - + trackRelNav.addRelation(refittedTrack, objVec[irel], weights[irel]); } } - - - - } + } evt->addCollection( trackVec , _output_track_col_name) ; + auto trackRelVec = trackRelNav.createLCCollection(); evt->addCollection( trackRelVec , _output_track_rel_name) ; - delete input_track_rels; input_track_rels = 0; - } ++_n_evt ; } @@ -452,13 +441,13 @@ LCCollection* RefitProcessor::GetCollection( LCEvent * evt, std::string colName } -LCRelationNavigator* RefitProcessor::GetRelations(LCEvent * evt , std::string RelName ) { +std::unique_ptr RefitProcessor::GetRelations(LCEvent * evt , std::string RelName ) { - LCRelationNavigator* nav = 0 ; + std::unique_ptr nav = nullptr; try{ - nav = new LCRelationNavigator(evt->getCollection( RelName.c_str() )); - streamlog_out( DEBUG2 ) << "RefitProcessor --> " << RelName << " track relation collection in event = " << nav << std::endl; + nav.reset(new LCRelationNavigator(evt->getCollection( RelName.c_str() ))); + streamlog_out( DEBUG2 ) << "RefitProcessor --> " << RelName << " track relation collection in event = " << nav.get() << std::endl; } catch(DataNotAvailableException &e){ streamlog_out( DEBUG2 ) << "RefitProcessor --> " << RelName.c_str() << " track relation collection absent in event" << std::endl;