Skip to content

Commit

Permalink
Update with some comments and name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarcell committed Jul 9, 2024
1 parent ea15114 commit db966bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 42 deletions.
44 changes: 25 additions & 19 deletions k4Reco/Overlay/components/OverlayTiming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,25 @@ StatusCode OverlayTiming::initialize() {
}
}

if (std::any_of(m_bkgEvents->m_totalNumberOfEvents.begin(), m_bkgEvents->m_totalNumberOfEvents.end(),
[this](const int& val) { return this->m_startWithBackgroundEvent >= val; })) {
throw GaudiException("StartWithBackgroundEvent is larger than the number of events in the background files", name(),
StatusCode::FAILURE);
return StatusCode::FAILURE;
}

if (m_Noverlay.empty()) {
info() << "Using the default number of overlay events (1) for each group, since none was specified with "
"NumberBackground "
<< endmsg;
m_Noverlay.value() = std::vector<double>(m_bkgEvents->size(), 1);
m_Noverlay = std::vector<double>(m_bkgEvents->size(), 1);
}

if (m_Poisson.empty()) {
info() << "Using the default overlay mode (no Poission distribution) for each group, since none was specified with "
"Poisson_random_Noverlay"
<< endmsg;
m_Poisson.value() = std::vector<bool>(m_bkgEvents->size(), false);
m_Poisson = std::vector<bool>(m_bkgEvents->size(), false);
}

return StatusCode::SUCCESS;
Expand All @@ -87,7 +94,6 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&
for (size_t i = 0; i < simCaloHits.size(); ++i) {
ocaloHitContribs.emplace_back(edm4hep::CaloHitContributionCollection());
}


// Copy MCParticles for physics event into a new collection
for (auto&& part : particles) {
Expand Down Expand Up @@ -154,17 +160,17 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&

// Iterate over each group of files and parameters
for (size_t groupIndex = 0; groupIndex < m_bkgEvents->size(); groupIndex++) {
if (_randomBX) {
_BX_phys = std::uniform_int_distribution<int>(0, _nBunchTrain - 1)(m_engine);
debug() << "Physics Event was placed in the " << _BX_phys << " bunch crossing!" << endmsg;
if (m_randomBX) {
m_physBX = std::uniform_int_distribution<int>(0, _nBunchTrain - 1)(m_engine);
debug() << "Physics Event was placed in the " << m_physBX << " bunch crossing!" << endmsg;
}

// define a permutation for the events to overlay -- the physics event is per definition at position 0
std::vector<int> permutation;

// Permutation has negative values and the last one is 0
// if (!_randomBX) then _BX_phys (default = 1)
for (int i = -(_BX_phys - 1); i < _nBunchTrain - (_BX_phys - 1); ++i) {
// if (!m_randomBX) then m_physBX (default = 1)
for (int i = -(m_physBX - 1); i < _nBunchTrain - (m_physBX - 1); ++i) {
permutation.push_back(i);
}
std::shuffle(permutation.begin(), permutation.end(), m_engine);
Expand All @@ -190,19 +196,19 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&
NOverlay_to_this_BX = m_Noverlay[groupIndex];
}

debug() << "Will overlay " << NOverlay_to_this_BX << " events to BX number " << BX_number_in_train + _BX_phys
debug() << "Will overlay " << NOverlay_to_this_BX << " events to BX number " << BX_number_in_train + m_physBX
<< endmsg;

for (int k = 0; k < NOverlay_to_this_BX; ++k) {
info() << "Overlaying event " << m_bkgEvents->m_nextEntry << " to BX number " << BX_number_in_train + _BX_phys
info() << "Overlaying event " << m_bkgEvents->m_nextEntry << " to BX number " << BX_number_in_train + m_physBX
<< endmsg;
auto backgroundEvent =
m_bkgEvents->m_rootFileReaders[groupIndex].readEvent(m_bkgEvents->m_nextEntry);
// m_bkgEvents->m_nextEntry++;
// if(m_bkgEvents->m_nextEntry >= m_bkgEvents->m_totalNumberOfEvents[groupIndex] && !m_allowReusingBackgroundFiles) {
// throw GaudiException("No more events in background file", name(), StatusCode::FAILURE);
// }
// m_bkgEvents->m_nextEntry %= m_bkgEvents->m_totalNumberOfEvents[groupIndex];
auto backgroundEvent = m_bkgEvents->m_rootFileReaders[groupIndex].readEvent(m_bkgEvents->m_nextEntry);
m_bkgEvents->m_nextEntry++;
if (m_bkgEvents->m_nextEntry >= m_bkgEvents->m_totalNumberOfEvents[groupIndex] &&
!m_allowReusingBackgroundFiles) {
throw GaudiException("No more events in background file", name(), StatusCode::FAILURE);
}
m_bkgEvents->m_nextEntry %= m_bkgEvents->m_totalNumberOfEvents[groupIndex];
auto availableCollections = backgroundEvent.getAvailableCollections();

// Either 0 or negative
Expand Down Expand Up @@ -262,7 +268,7 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&
}
auto [this_start, this_stop] = define_time_windows(name);
// There are only contributions to the readout if the hits are in the integration window
if (this_stop <= (BX_number_in_train - _BX_phys) * _T_diff) {
if (this_stop <= (BX_number_in_train - m_physBX) * _T_diff) {
info() << "Skipping collection " << name << " as it is not in the integration window" << endmsg;
continue;
}
Expand Down Expand Up @@ -291,7 +297,7 @@ retType OverlayTiming::operator()(const edm4hep::EventHeaderCollection&
}
auto [this_start, this_stop] = define_time_windows(name);
// There are only contributions to the readout if the hits are in the integration window
if (this_stop <= (BX_number_in_train - _BX_phys) * _T_diff) {
if (this_stop <= (BX_number_in_train - m_physBX) * _T_diff) {
info() << "Skipping collection " << name << " as it is not in the integration window" << endmsg;
continue;
}
Expand Down
62 changes: 39 additions & 23 deletions k4Reco/Overlay/components/OverlayTiming.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/** Background overlay algorithm
This algorithm overlays background events on top of the signal events. The
background events are read from a set of input files, and the signal events
are the input in the main event loop.
The MCParticleCollection in signal are background are overlaid into one
collection. The SimTrackerHit collections are cropped and overlayed if they
are in the time window. The SimCalorimeterHit collections are overlayed
based on the cellID. If a signal hit has the same cellID as a background
hit, they are combined into a single hit. Only hits that have
CaloHitContributions in the time range are considered.
**/

#include "podio/Frame.h"
#include "podio/ROOTReader.h"
#include "podio/Reader.h"
Expand All @@ -18,10 +33,10 @@
#include <random>

struct EventHolder {
std::vector<std::vector<std::string>> m_fileNames;
std::vector<podio::Reader> m_rootFileReaders;
std::vector<int> m_totalNumberOfEvents;
std::map<int, podio::Frame> m_events;
std::vector<std::vector<std::string>> m_fileNames;
std::vector<podio::Reader> m_rootFileReaders;
std::vector<int> m_totalNumberOfEvents;
std::map<int, podio::Frame> m_events;

int m_nextEntry = 0;

Expand All @@ -33,26 +48,28 @@ struct EventHolder {
}
EventHolder() = default;

// TODO: Cache functionality
// podio::Frame& read

size_t size() const { return m_fileNames.size(); }
};

using retType = std::tuple<edm4hep::MCParticleCollection, std::vector<edm4hep::SimTrackerHitCollection>,
std::vector<edm4hep::SimCalorimeterHitCollection>,
std::vector<edm4hep::CaloHitContributionCollection>>;
using retType =
std::tuple<edm4hep::MCParticleCollection, std::vector<edm4hep::SimTrackerHitCollection>,
std::vector<edm4hep::SimCalorimeterHitCollection>, std::vector<edm4hep::CaloHitContributionCollection>>;

struct OverlayTiming : public k4FWCore::MultiTransformer<retType(
const edm4hep::EventHeaderCollection& headers, const edm4hep::MCParticleCollection&,
const std::vector<const edm4hep::SimTrackerHitCollection*>&,
const std::vector<const edm4hep::SimCalorimeterHitCollection*>&
// const std::map<std::string, const edm4hep::CaloHitContributionCollection&>&
)> {
)> {
OverlayTiming(const std::string& name, ISvcLocator* svcLoc)
: MultiTransformer(
name, svcLoc,
{KeyValues("EventHeader", {"EventHeader"}), KeyValues("MCParticles", {"DefaultMCParticles"}),
KeyValues("SimTrackerHits", {"DefaultSimTrackerHits"}),
KeyValues("SimCalorimeterHits", {"DefaultSimCalorimeterHits"})
},
KeyValues("SimCalorimeterHits", {"DefaultSimCalorimeterHits"})},
{KeyValues("OutputMCParticles", {"NewMCParticles"}), KeyValues("OutputSimTrackerHits", {"MCParticles1"}),
KeyValues("OutputSimCalorimeterHits", {"MCParticles2"}),
KeyValues("OutputCaloHitContributions", {"OverlayCaloHitContributions"})}) {}
Expand All @@ -63,27 +80,23 @@ struct OverlayTiming : public k4FWCore::MultiTransformer<retType(

retType virtual operator()(
const edm4hep::EventHeaderCollection& headers, const edm4hep::MCParticleCollection& mcParticles,
const std::vector<const edm4hep::SimTrackerHitCollection*>& simTrackerHits,
const std::vector<const edm4hep::SimCalorimeterHitCollection*>& simCalorimeterHits
) const final;
const std::vector<const edm4hep::SimTrackerHitCollection*>& simTrackerHits,
const std::vector<const edm4hep::SimCalorimeterHitCollection*>& simCalorimeterHits) const final;

std::pair<float, float> define_time_windows(const std::string& Collection_name) const;

private:

// These correspond to the index position in the argument list
constexpr static int TRACKERHIT_INDEX_POSITION = 2;
constexpr static int SIMCALOHIT_INDEX_POSITION = 3;

Gaudi::Property<bool> _randomBX{this, "RandomBx", false,
Gaudi::Property<bool> m_randomBX{this, "RandomBx", false,
"Place the physics event at an random position in the train: overrides PhysicsBX"};
mutable Gaudi::Property<int> _BX_phys{this, "PhysicsBX", 1, "Number of the Bunch crossing of the physics event"};
Gaudi::Property<float> _tpcVdrift_mm_ns{this, "TPCDriftvelocity", float(5.0e-2),
"Drift velocity in TPC [mm/ns] - default 5.0e-2 (5cm/us)"};
mutable Gaudi::Property<int> m_physBX{this, "PhysicsBX", 1, "Number of the Bunch crossing of the physics event"};
Gaudi::Property<int> _nBunchTrain{this, "NBunchtrain", 1, "Number of bunches in a bunch train"};
Gaudi::Property<int> m_startWithBackgroundFile{this, "StartBackgroundFileIndex", -1,
"Which background file to startWith"};
Gaudi::Property<int> m_startWithBackgroundEvent{this, "StartBackgroundEventIndex", -1,
// Gaudi::Property<int> m_startWithBackgroundFile{this, "StartBackgroundFileIndex", -1,
// "Which background file to startWith"};
Gaudi::Property<int> m_startWithBackgroundEvent{this, "StartBackgroundEventIndex", -1,
"Which background event to startWith"};

Gaudi::Property<std::vector<std::vector<std::string>>> m_inputFileNames{
Expand Down Expand Up @@ -116,7 +129,10 @@ struct OverlayTiming : public k4FWCore::MultiTransformer<retType(
Gaudi::Property<bool> m_allowReusingBackgroundFiles{
this, "AllowReusingBackgroundFiles", false, "If true the same background file can be used for the same event"};

// Gaudi::Property<int> m_maxCachedFrames{
// this, "MaxCachedFrames", 0, "Maximum number of frames cached from background files"};

private:
mutable std::mt19937 m_engine;
SmartIF<IUniqueIDGenSvc> m_uidSvc;
inline static thread_local std::mt19937 m_engine;
SmartIF<IUniqueIDGenSvc> m_uidSvc;
};

0 comments on commit db966bf

Please sign in to comment.