Skip to content

Commit

Permalink
Moving to MetaHandle (#44)
Browse files Browse the repository at this point in the history
* Triggering tests

* Update clicRec_e4h_input_clue.py

* New input file in clue_gaudi_wrapper.py

* Add Legacy in clue_gaudi_wrapper.py

* Move away from PodioLegacyDataSvc

* Move to MetaDataHandle

* Move away from Legacy in input file

* Run on reco instead of sim

* By tmadlener: Fix ClueGaudiAlgorithmWrapper for using new handles

* Fix Ntuplizer

* Update input file

* Clean up

* Update input file

* Update input files

* Update config files

* Fix licence

* Clean up

* Triggering tests

* New input file in clue_gaudi_wrapper.py

* Add Legacy in clue_gaudi_wrapper.py

* Move away from PodioLegacyDataSvc

* Move to MetaDataHandle

* Move away from Legacy in input file

* Run on reco instead of sim

* By tmadlener: Fix ClueGaudiAlgorithmWrapper for using new handles

* Fix Ntuplizer

* Update input file

* Clean up

* Update input file

* Update input files

* Update config files

* Fix licence

* Clean up

* Use edm4hep::CellIDEncoding

* Fix pre-commit

* Improvements in printout

---------

Co-authored-by: jmcarcell <[email protected]>
  • Loading branch information
ebrondol and jmcarcell authored Nov 6, 2023
1 parent b8d87d8 commit c856ebc
Show file tree
Hide file tree
Showing 9 changed files with 3,481 additions and 1,226 deletions.
5 changes: 3 additions & 2 deletions docs/clic-recipe.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ cp ../../k4MarlinWrapper/test/gaudi_opts/clicRec_e4h_input.py .
k4run clicRec_e4h_input.py --EventDataSvc.input gamma_10GeV_edm4hep.root

#Run CLUE in CLIC reconstruction
cp ../../k4Clue/gaudi_opts/clicRec_e4h_input_clue.py .
k4run clicRec_e4h_input_clue.py --EventDataSvc.input gamma_10GeV_edm4hep.root
cp ../../k4Clue/gaudi_opts/clicRec_e4h_input_gun_clue.py .
k4run clicRec_e4h_input_gun_clue.py --EventDataSvc.input gamma_10GeV_edm4hep.root

#Run CLUE standalone
cp ../../k4Clue/gaudi_opts/clue_gaudi_wrapper.py .
Expand All @@ -56,6 +56,7 @@ ddsim --steeringFile clic_steer.py --compactFile $LCGEO/CLIC/compact/CLIC_o3_v14
```
where `myGPS.mac` can be found in this folder.

In the case of simulation from the surface, the config file needs to be adapted: `clicRec_e4h_input_gps.py`

## Visualization

Expand Down
1,849 changes: 1,849 additions & 0 deletions gaudi_opts/clicRec_e4h_input_gps.py

Large diffs are not rendered by default.

2,506 changes: 1,458 additions & 1,048 deletions gaudi_opts/clicReconstruction_noClue.py → gaudi_opts/clicRec_e4h_input_gun.py

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions gaudi_opts/clue_gaudi_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

from Configurables import PodioInput
evtsvc = k4DataSvc('EventDataSvc')
evtsvc.input = 'https://key4hep.web.cern.ch/testFiles/k4clue/inputData/cld/20230109_gammaFromSurface_10GeV_uniform_500events_edm4hep.root'
evtsvc.input = 'https://key4hep.web.cern.ch/testFiles/k4clue/inputData/clic/20230825_gammaFromSurface_10GeV_uniform_500events_reco_edm4hep.root'

inp = PodioInput('InputReader')
inp.collections = [
Expand All @@ -33,7 +33,7 @@
'ECALBarrel',
'ECALEndcap',
]
inp.OutputLevel = DEBUG
inp.OutputLevel = WARNING

MyAIDAProcessor = MarlinProcessorWrapper("MyAIDAProcessor")
MyAIDAProcessor.OutputLevel = WARNING
Expand Down
11 changes: 5 additions & 6 deletions include/CLUENtuplizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
#define CLUE_HISTOGRAMS_H

#include "k4FWCore/DataHandle.h"
#include "k4FWCore/MetaDataHandle.h"
#include "GaudiAlg/GaudiAlgorithm.h"
#include "GaudiKernel/ITHistSvc.h"

#include <edm4hep/CalorimeterHitCollection.h>
#include <edm4hep/ClusterCollection.h>
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/EventHeaderCollection.h>
#include <edm4hep/Constants.h>
#include "CLUECalorimeterHit.h"

#include "TH1F.h"
Expand Down Expand Up @@ -89,17 +91,14 @@ class CLUENtuplizer : public GaudiAlgorithm {
const clue::CLUECalorimeterHitCollection* clue_calo_coll;
std::string ClusterCollectionName;
const edm4hep::ClusterCollection* cluster_coll;
std::string EBCaloCollectionName = "ECALBarrel";
std::string EECaloCollectionName = "ECALEndcap";
const edm4hep::CalorimeterHitCollection* EB_calo_coll;
const edm4hep::CalorimeterHitCollection* EE_calo_coll;
DataHandle<edm4hep::CalorimeterHitCollection> EB_calo_handle {"BarrelInputHits", Gaudi::DataHandle::Reader, this};
DataHandle<edm4hep::CalorimeterHitCollection> EE_calo_handle {"EndcapInputHits", Gaudi::DataHandle::Reader, this};
MetaDataHandle<std::string> cellIDHandle {EB_calo_handle, edm4hep::CellIDEncoding, Gaudi::DataHandle::Reader};

bool singleMCParticle = false;

// PODIO data service
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
PodioLegacyDataSvc* m_podioDataSvc;

ITHistSvc* m_ths{nullptr}; ///< THistogram service

TTree* t_hits{nullptr};
Expand Down
30 changes: 9 additions & 21 deletions src/CLUENtuplizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,16 @@ using namespace DDSegmentation ;

DECLARE_COMPONENT(CLUENtuplizer)

CLUENtuplizer::CLUENtuplizer(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc), m_eventDataSvc("EventDataSvc", "CLUENtuplizer") {
CLUENtuplizer::CLUENtuplizer(const std::string& name, ISvcLocator* svcLoc) : GaudiAlgorithm(name, svcLoc) {
declareProperty("ClusterCollection", ClusterCollectionName, "Collection of clusters in input");
declareProperty("BarrelCaloHitsCollection", EBCaloCollectionName, "Collection for Barrel Calo Hits used in input");
declareProperty("EndcapCaloHitsCollection", EECaloCollectionName, "Collection for Endcap Calo Hits used in input");
declareProperty("BarrelCaloHitsCollection", EB_calo_handle, "Collection for Barrel Calo Hits used in input");
declareProperty("EndcapCaloHitsCollection", EE_calo_handle, "Collection for Endcap Calo Hits used in input");
declareProperty("SingleMCParticle", singleMCParticle, "If this is True, the analysis is run only if one MCParticle is present in the event");
StatusCode sc = m_eventDataSvc.retrieve();
}

StatusCode CLUENtuplizer::initialize() {
if (GaudiAlgorithm::initialize().isFailure()) return StatusCode::FAILURE;

m_podioDataSvc = dynamic_cast<PodioLegacyDataSvc*>(m_eventDataSvc.get());
if (m_podioDataSvc == nullptr) {
return StatusCode::FAILURE;
}

if (service("THistSvc", m_ths).isFailure()) {
error() << "Couldn't get THistSvc" << endmsg;
return StatusCode::FAILURE;
Expand Down Expand Up @@ -77,7 +71,7 @@ StatusCode CLUENtuplizer::execute() {
"EventHeader", Gaudi::DataHandle::Reader, this};
auto evs = ev_handle.get();
evNum = (*evs)[0].getEventNumber();
info() << "Event number = " << evNum << std::endl;
info() << "Event number = " << evNum << endmsg;

DataHandle<edm4hep::MCParticleCollection> mcp_handle {
"MCParticles", Gaudi::DataHandle::Reader, this};
Expand Down Expand Up @@ -106,26 +100,19 @@ StatusCode CLUENtuplizer::execute() {
throw std::runtime_error("CLUE hits collection not available");
}

// Read EB collection for metadata cellID
DataHandle<edm4hep::CalorimeterHitCollection> EB_calo_handle {
EBCaloCollectionName, Gaudi::DataHandle::Reader, this};
// Read EB and EE collection
EB_calo_coll = EB_calo_handle.get();

// Read EE collection
DataHandle<edm4hep::CalorimeterHitCollection> EE_calo_handle {
EECaloCollectionName, Gaudi::DataHandle::Reader, this};
EE_calo_coll = EE_calo_handle.get();

debug() << "ECAL Calorimeter Hits Size = " << int( EB_calo_coll->size()+EE_calo_coll->size() ) << endmsg;
debug() << "ECAL Calorimeter Hits Size = " << (*EB_calo_coll).size()+(*EE_calo_coll).size() << endmsg;

// Read cluster collection
DataHandle<edm4hep::ClusterCollection> cluster_handle {
ClusterCollectionName, Gaudi::DataHandle::Reader, this};
cluster_coll = cluster_handle.get();

// Get collection metadata cellID which is valid for both EB, EE and Clusters
auto collID = EB_calo_coll->getID();
const auto cellIDstr = EB_calo_handle.getCollMetadataCellID(collID);
const auto cellIDstr = cellIDHandle.get();
const BitFieldCoder bf(cellIDstr);
cleanTrees();

Expand All @@ -136,6 +123,7 @@ StatusCode CLUENtuplizer::execute() {
std::uint64_t totSize = 0;
bool foundInECAL = false;

debug() << "Cluster Collection size = " << cluster_coll->size() << endmsg;
for (const auto& cl : *cluster_coll) {
m_clusters_event->push_back (evNum);
m_clusters_energy->push_back (cl.getEnergy());
Expand Down Expand Up @@ -213,6 +201,7 @@ StatusCode CLUENtuplizer::execute() {
std::uint64_t nFollowers = 0;
std::uint64_t nOutliers = 0;
totEnergy = 0;
debug() << "CLUE Calorimeter Hits Size = " << clue_calo_coll->vect.size() << endmsg;
for (const auto& clue_hit : (clue_calo_coll->vect)) {
m_hits_event->push_back (evNum);
if(clue_hit.inBarrel()){
Expand Down Expand Up @@ -247,7 +236,6 @@ StatusCode CLUENtuplizer::execute() {
nOutliers++;
}
}
debug() << "CLUE Calorimeter Hits Size = " << clue_calo_coll->vect.size() << endmsg;
debug() << "Found: " << nSeeds << " seeds, "
<< nOutliers << " outliers, "
<< nFollowers << " followers."
Expand Down
61 changes: 26 additions & 35 deletions src/ClueGaudiAlgorithmWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,18 @@ using namespace DDSegmentation ;
DECLARE_COMPONENT(ClueGaudiAlgorithmWrapper)

ClueGaudiAlgorithmWrapper::ClueGaudiAlgorithmWrapper(const std::string& name, ISvcLocator* pSL) :
GaudiAlgorithm(name, pSL), m_eventDataSvc("EventDataSvc", "ClueGaudiAlgorithmWrapper") {
declareProperty("BarrelCaloHitsCollection", EBCaloCollectionName, "Collection for Barrel Calo Hits used in input");
declareProperty("EndcapCaloHitsCollection", EECaloCollectionName, "Collection for Endcap Calo Hits used in input");
GaudiAlgorithm(name, pSL) {
declareProperty("BarrelCaloHitsCollection", EB_calo_handle, "Collection for Barrel Calo Hits used in input");
declareProperty("EndcapCaloHitsCollection", EE_calo_handle, "Collection for Endcap Calo Hits used in input");
declareProperty("CriticalDistance", dc, "Used to compute the local density");
declareProperty("MinLocalDensity", rhoc, "Minimum local density for a point to be promoted as a Seed");
declareProperty("OutlierDeltaFactor", outlierDeltaFactor, "Multiplicative constant to be applied to CriticalDistance");
declareProperty("OutClusters", clustersHandle, "Clusters collection (output)");
declareProperty("OutCaloHits", caloHitsHandle, "Calo hits collection created from Clusters (output)");

StatusCode sc = m_eventDataSvc.retrieve();
}

StatusCode ClueGaudiAlgorithmWrapper::initialize() {

m_podioDataSvc = dynamic_cast<PodioLegacyDataSvc*>(m_eventDataSvc.get());
if (m_podioDataSvc == nullptr) {
return StatusCode::FAILURE;
}

return Algorithm::initialize();

}
Expand Down Expand Up @@ -264,28 +257,23 @@ void ClueGaudiAlgorithmWrapper::transformClustersInCaloHits(edm4hep::ClusterColl

StatusCode ClueGaudiAlgorithmWrapper::execute() {

// Read EB collection
DataHandle<edm4hep::CalorimeterHitCollection> EB_calo_handle {
EBCaloCollectionName, Gaudi::DataHandle::Reader, this};
// Read EB and EE collection
EB_calo_coll = EB_calo_handle.get();

// Read EE collection
DataHandle<edm4hep::CalorimeterHitCollection> EE_calo_handle {
EECaloCollectionName, Gaudi::DataHandle::Reader, this};
EE_calo_coll = EE_calo_handle.get();

// Get collection metadata cellID which is valid for both EB and EE
auto collID = EB_calo_coll->getID();
const auto cellIDstr = EB_calo_handle.getCollMetadataCellID(collID);
const auto cellIDstr = cellIDHandle.get();
const BitFieldCoder bf(cellIDstr);

// Output CLUE clusters
edm4hep::ClusterCollection* finalClusters = clustersHandle.createAndPut();
// edm4hep::ClusterCollection* finalClusters = clustersHandle.createAndPut();
auto finalClusters = std::make_unique<edm4hep::ClusterCollection>();

// Output CLUE calo hits
clue::CLUECalorimeterHitCollection clue_hit_coll_barrel;
clue::CLUECalorimeterHitCollection clue_hit_coll_endcap;

info() << EB_calo_coll->size() << " caloHits in ECAL Barrel." << endmsg;
// Fill CLUECaloHits in the barrel
if( EB_calo_coll->isValid() ) {
for(const auto& calo_hit : (*EB_calo_coll) ){
Expand All @@ -294,25 +282,25 @@ StatusCode ClueGaudiAlgorithmWrapper::execute() {
} else {
throw std::runtime_error("Collection not found.");
}
debug() << EB_calo_coll->size() << " caloHits in " << EBCaloCollectionName << "." << endmsg;

// Run CLUE in the barrel
if(!clue_hit_coll_barrel.vect.empty()){

std::map<int, std::vector<int> > clueClustersBarrel = runAlgo(clue_hit_coll_barrel.vect, true);
debug() << "Produced " << clueClustersBarrel.size() << " clusters in " << EBCaloCollectionName << endmsg;
debug() << "Produced " << clueClustersBarrel.size() << " clusters in ECAL Barrel" << endmsg;

clue_hit_coll.vect.insert(clue_hit_coll.vect.end(), clue_hit_coll_barrel.vect.begin(), clue_hit_coll_barrel.vect.end());

fillFinalClusters(clue_hit_coll_barrel.vect, clueClustersBarrel, finalClusters);
debug() << "Saved " << finalClusters->size() << " clusters using " << EBCaloCollectionName << endmsg;
fillFinalClusters(clue_hit_coll_barrel.vect, clueClustersBarrel, finalClusters.get());
debug() << "Saved " << finalClusters->size() << " clusters using ECAL Barrel hits" << endmsg;

}

// Total amount of EE+ and EE- layers (80)
// already described in `include/CLDEndcapLayerTilesConstants.h`
int maxLayerPerSide = 40;

info() << EE_calo_coll->size() << " caloHits in ECAL Endcap." << endmsg;
// Fill CLUECaloHits in the endcap
if( EE_calo_coll->isValid() ) {
for(const auto& calo_hit : (*EE_calo_coll) ){
Expand All @@ -325,23 +313,20 @@ StatusCode ClueGaudiAlgorithmWrapper::execute() {
} else {
throw std::runtime_error("Collection not found.");
}
debug() << EE_calo_coll->size() << " caloHits in " << EECaloCollectionName << "." << endmsg;
debug() << EE_calo_coll->size() << " caloHits in ECAL Endcap" << endmsg;

// Run CLUE in the endcap
if(!clue_hit_coll_endcap.vect.empty()){
std::map<int, std::vector<int> > clueClustersEndcap = runAlgo(clue_hit_coll_endcap.vect, false);
debug() << "Produced " << clueClustersEndcap.size() << " clusters in " << EECaloCollectionName << endmsg;
debug() << "Produced " << clueClustersEndcap.size() << " clusters in ECAL Endcap" << endmsg;

clue_hit_coll.vect.insert(clue_hit_coll.vect.end(), clue_hit_coll_endcap.vect.begin(), clue_hit_coll_endcap.vect.end());

fillFinalClusters(clue_hit_coll_endcap.vect, clueClustersEndcap, finalClusters);
debug() << "Saved " << finalClusters->size() << " clusters using " << EECaloCollectionName << endmsg;
fillFinalClusters(clue_hit_coll_endcap.vect, clueClustersEndcap, finalClusters.get());
debug() << "Saved " << finalClusters->size() << " clusters using ECAL Endcap hits" << endmsg;

}

// Add cellID to CLUE clusters
auto& clusters_md = m_podioDataSvc->getProvider().getCollectionMetaData(finalClusters->getID());
clusters_md.setValue("CellIDEncodingString", cellIDstr);
info() << "Saved " << finalClusters->size() << " CLUE clusters in total." << endmsg;

// Save CLUE calo hits
Expand All @@ -350,12 +335,18 @@ StatusCode ClueGaudiAlgorithmWrapper::execute() {
info() << "Saved " << clue_hit_coll.vect.size() << " CLUE calo hits in total. " << endmsg;

// Save clusters as calo hits and add cellID to them
edm4hep::CalorimeterHitCollection* finalCaloHits = caloHitsHandle.createAndPut();
transformClustersInCaloHits(finalClusters, finalCaloHits);
auto& calohits_md = m_podioDataSvc->getProvider().getCollectionMetaData(finalCaloHits->getID());
calohits_md.setValue("CellIDEncodingString", cellIDstr);
auto finalCaloHits = std::make_unique<edm4hep::CalorimeterHitCollection>();
transformClustersInCaloHits(finalClusters.get(), finalCaloHits.get());
info() << "Saved " << finalCaloHits->size() << " clusters as calo hits" << endmsg;

// Only now can we put the collections into the event store, as nothing needs
// them any longer
caloHitsHandle.put(std::move(finalCaloHits));
clustersHandle.put(std::move(finalClusters));

// To be fixed in the future:
// Add CellIDEncodingString to CLUE clusters and CLUE calo hits

// Cleaning
clue_hit_coll.vect.clear();
cleanCLUEPoints();
Expand Down
13 changes: 7 additions & 6 deletions src/ClueGaudiAlgorithmWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#include <GaudiAlg/GaudiAlgorithm.h>

// FWCore
#include <k4FWCore/DataHandle.h>
#include "k4FWCore/DataHandle.h"
#include "k4FWCore/MetaDataHandle.h"

#include <edm4hep/CalorimeterHitCollection.h>
#include <edm4hep/ClusterCollection.h>
#include <edm4hep/Constants.h>
#include "CLUECalorimeterHit.h"

class ClueGaudiAlgorithmWrapper : public GaudiAlgorithm {
Expand All @@ -49,8 +51,6 @@ class ClueGaudiAlgorithmWrapper : public GaudiAlgorithm {

private:
// Parameters in input
std::string EBCaloCollectionName;
std::string EECaloCollectionName;
const edm4hep::CalorimeterHitCollection* EB_calo_coll;
const edm4hep::CalorimeterHitCollection* EE_calo_coll;
float dc;
Expand All @@ -65,9 +65,10 @@ class ClueGaudiAlgorithmWrapper : public GaudiAlgorithm {
std::vector<int> layer;
std::vector<float> weight;

// PODIO data service
ServiceHandle<IDataProviderSvc> m_eventDataSvc;
PodioLegacyDataSvc* m_podioDataSvc;
// Handle to read the calo cells and their cellID
DataHandle<edm4hep::CalorimeterHitCollection> EB_calo_handle {"BarrelInputHits", Gaudi::DataHandle::Reader, this};
DataHandle<edm4hep::CalorimeterHitCollection> EE_calo_handle {"EndcapInputHits", Gaudi::DataHandle::Reader, this};
MetaDataHandle<std::string> cellIDHandle {EB_calo_handle, edm4hep::CellIDEncoding, Gaudi::DataHandle::Reader};

// Collections in output
DataHandle<edm4hep::CalorimeterHitCollection> caloHitsHandle{"CLUEClustersAsHits", Gaudi::DataHandle::Writer, this};
Expand Down

0 comments on commit c856ebc

Please sign in to comment.