From dd80ac252dca505e112abb37bb485384303fc0e5 Mon Sep 17 00:00:00 2001 From: Alvaro Tolosa Delgado Date: Fri, 22 Nov 2024 15:58:05 +0100 Subject: [PATCH] use new geometry functions from data extension, safer way of saving output histograms --- DCHdigi/include/DCHdigi_v01.h | 8 ++++++- DCHdigi/src/DCHdigi_v01.cpp | 39 +++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/DCHdigi/include/DCHdigi_v01.h b/DCHdigi/include/DCHdigi_v01.h index 81bb291..21a9401 100644 --- a/DCHdigi/include/DCHdigi_v01.h +++ b/DCHdigi/include/DCHdigi_v01.h @@ -72,7 +72,6 @@ #include "AlgData.h" /// constant to convert from mm (EDM4hep) to DD4hep (cm) -constexpr double MM_TO_CM = 0.1; struct DCHdigi_v01 final : k4FWCore::MultiTransformer< @@ -87,6 +86,9 @@ struct DCHdigi_v01 final operator()(const edm4hep::SimTrackerHitCollection&, const edm4hep::EventHeaderCollection&) const override; private: + /// conversion factor mm to cm, static to the class to avoid clash with DD4hep + static constexpr double MM_TO_CM = 0.1; + //------------------------------------------------------------------ // machinery for geometry @@ -193,6 +195,10 @@ struct DCHdigi_v01 final /// histogram to store smearing perpendicular the wire TH1D* hSxy; + + /// Create ROOT file for debug histograms + /// Does not change ROOT directory + void Create_outputROOTfile_for_debugHistograms(); }; DECLARE_COMPONENT(DCHdigi_v01); diff --git a/DCHdigi/src/DCHdigi_v01.cpp b/DCHdigi/src/DCHdigi_v01.cpp index ab53870..6b61090 100644 --- a/DCHdigi/src/DCHdigi_v01.cpp +++ b/DCHdigi/src/DCHdigi_v01.cpp @@ -50,6 +50,8 @@ StatusCode DCHdigi_v01::initialize() { /////////////////////////// retrieve data extension ////////////////////////// /////////////////////////////////////////////////////////////////////////////////// this->dch_data = DCH_DE.extension(); + if (not dch_data->IsValid()) + ThrowException("No valid data extension was found for detector <<" + DCH_name + ">>."); /////////////////////////////////////////////////////////////////////////////////// @@ -115,13 +117,13 @@ DCHdigi_v01::operator()(const edm4hep::SimTrackerHitCollection& input_sim_hits, // ------------------------------------------------------------------------- // calculate hit position projection into the wire - TVector3 hit_to_wire_vector = this->Calculate_hitpos_to_wire_vector(ilayer, nphi, hit_position); + TVector3 hit_to_wire_vector = this->dch_data->Calculate_hitpos_to_wire_vector(ilayer, nphi, hit_position); TVector3 hit_projection_on_the_wire = hit_position + hit_to_wire_vector; if (m_create_debug_histos.value()) { double distance_hit_wire = hit_to_wire_vector.Mag(); hDpw->Fill(distance_hit_wire); } - TVector3 wire_direction_ez = this->Calculate_wire_vector_ez(ilayer, nphi); + TVector3 wire_direction_ez = this->dch_data->Calculate_wire_vector_ez(ilayer, nphi); // ------------------------------------------------------------------------- // smear the position @@ -134,7 +136,7 @@ DCHdigi_v01::operator()(const edm4hep::SimTrackerHitCollection& input_sim_hits, hit_projection_on_the_wire += smearing_z * (wire_direction_ez.Unit()); if (m_create_debug_histos.value()) { // the distance from the hit projection and the wire should be zero - TVector3 dummy_vector = this->Calculate_hitpos_to_wire_vector(ilayer, nphi, hit_projection_on_the_wire); + TVector3 dummy_vector = this->dch_data->Calculate_hitpos_to_wire_vector(ilayer, nphi, hit_projection_on_the_wire); hDww->Fill(dummy_vector.Mag()); } @@ -193,9 +195,22 @@ DCHdigi_v01::operator()(const edm4hep::SimTrackerHitCollection& input_sim_hits, /////////////////////////////////////////////////////////////////////////////////////// /////////////////////// finalize ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////// -StatusCode DCHdigi_v01::finalize() { - if (m_create_debug_histos.value()) { - std::unique_ptr ofile{TFile::Open(m_out_debug_filename.value().c_str(), "recreate")}; + +void DCHdigi_v01::Create_outputROOTfile_for_debugHistograms() +{ + // save current ROOT directory + TDirectory* currentDir = gDirectory; + + // save the debug histograms in a file + // file is saved and closed when going out of scope + { + auto filename = m_out_debug_filename.value().c_str(); + std::unique_ptr ofile{TFile::Open( filename, "recreate")}; + if (!ofile || ofile->IsZombie()) + { + error() << "Error: Could not open file " << filename << std::endl; + return; + } ofile->cd(); hDpw->Write(); hDww->Write(); @@ -203,6 +218,18 @@ StatusCode DCHdigi_v01::finalize() { hSz->Write(); } + // Restore previous ROOT directory + if(currentDir && ( not currentDir->IsDestructed() ) ) + currentDir->cd(); + return; +} + +StatusCode DCHdigi_v01::finalize() { + if (m_create_debug_histos.value()) + { + this->Create_outputROOTfile_for_debugHistograms(); + } + return StatusCode::SUCCESS; }