ATLAS Offline Software
Loading...
Searching...
No Matches
PileUpToolsAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Class header
6#include "PileUpToolsAlg.h"
7#include <algorithm>
8
10
11PileUpToolsAlg::PileUpToolsAlg(const std::string& name, ISvcLocator* pSvcLocator)
12 : AthAlgorithm(name, pSvcLocator)
13{
14}
15
16// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
17
19{
20 ATH_MSG_DEBUG ("Initializing " << name());
21 //locate the pu tools and initialize them
22 ATH_CHECK(m_puTools.retrieve());
23 // initialise read handle keys
24 ATH_CHECK(m_eventInfoKey.initialize());
25 return StatusCode::SUCCESS;
26}
27
28// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
29
31{
32 return (i.time()!=j.time());
33}
34
35
37{
38 ATH_MSG_DEBUG ("in execute()");
39
41 // Get the overlaid event header, print out event and run number
42
44 if (!evt.isValid()) {
45 ATH_MSG_ERROR("Could not get xAOD::EventInfo " << evt.name() << " from store " << evt.store());
46 return StatusCode::FAILURE;
47 }
48
49 ATH_MSG_INFO ("Hard-scatter xAOD::EventInfo : " << " event: " << evt->eventNumber() << " run: " << evt->runNumber());
50
51 // access the sub events...
52 SubEventIterator iEvt(evt->subEvents().begin());
53 const SubEventIterator endEvt(evt->subEvents().end());
54
55 ATH_MSG_DEBUG( "There are " << std::distance(iEvt,endEvt) << " subevents in this Event." );
56
57 // Group subevents by bunch-crossing and work out the number of
58 // subevents that each PileUpTools will need to process.
59 std::map<const IPileUpTool*, unsigned int> eventsToProcessByTool;
60 std::map<SubEventIterator, SubEventIterator> bunchCrossingRanges;
61 while (iEvt != endEvt)
62 {
63 const SubEventIterator fEvt(iEvt);
64 // find the next SubEvent where the SubEvent after it has a
65 // different bunch crossing time.
66 iEvt = std::adjacent_find(fEvt, endEvt, differentBunchXing);
67 const SubEventIterator lEvt = (iEvt==endEvt) ? iEvt : ++iEvt;
68 const unsigned int nEventsInXing = std::distance(fEvt,lEvt);
69 ATH_MSG_VERBOSE( "Found " << nEventsInXing << " consecutive subevents with a bunch crossing time of " << fEvt->time() << " ns." );
70 bunchCrossingRanges[lEvt]=fEvt; //FIXME storing range "backwards" as the first part of the pair is const when accessed later
71 // loop over PileUpTools to check if they will need to process
72 // the current bunch crossing
73 for(auto& puToolHandle : m_puTools)
74 {
75 ATH_MSG_VERBOSE(__FILE__<<":"<<__LINE__<<" --- Call toProcess on "<<puToolHandle->name());
76 if (puToolHandle->toProcess(fEvt->time()))
77 {
78 eventsToProcessByTool[&(*puToolHandle)] += nEventsInXing;
79 }
80 }
81 }
82
83 // call prepareEvent for all PileUpTools to set nInputEvents
84 for(auto& puToolHandle : m_puTools)
85 {
86 // Reset the filters
87 puToolHandle->resetFilter();
88 ATH_MSG_VERBOSE ( puToolHandle->name() << " will get " << eventsToProcessByTool[&(*puToolHandle)] << " subevents to process." );
89 ATH_CHECK(puToolHandle->prepareEvent(Gaudi::Hive::currentContext(), eventsToProcessByTool[&(*puToolHandle)]));
90 }
91
92 // Loop over bunch-crossings and call processBunchXing for each
93 // PileUpTool
94 for(auto& bunch : bunchCrossingRanges)
95 {
96 SubEventIterator fEvt(bunch.second);
97 const SubEventIterator lEvt(bunch.first);
98 const int bunchXing(fEvt->time());
99 ATH_MSG_DEBUG("Processing bunch-crossing at " << bunchXing << " ns.");
100 for(auto& puToolHandle : m_puTools)
101 {
102
103 ATH_MSG_VERBOSE(__FILE__<<":"<<__LINE__<<" --- call toProcess on "<<puToolHandle->name());
104 if(puToolHandle->toProcess(bunchXing))
105 {
106 ATH_CHECK(puToolHandle->processBunchXing(bunchXing, fEvt, lEvt));
107 }
108 else
109 {
110 ATH_MSG_VERBOSE( "Skipping " << puToolHandle->name() << " for bunch crossing at " << bunchXing << " ns.");
111 }
112 }
113 // clear the bkg events from the bunch xing we have just processed.
114 ATH_CHECK(this->clearXing(fEvt, lEvt));
115 }
116
117 // call mergeEvent for all PileUpTools
118 for(auto& puToolHandle : m_puTools)
119 {
120 ATH_MSG_VERBOSE(__FILE__<<":"<<__LINE__<<" --- Call to mergeEvent on "<<puToolHandle->name());
121 ATH_CHECK(puToolHandle->mergeEvent(Gaudi::Hive::currentContext()));
122 // Check if the event was filtered out by the current PileUpTool.
123 if (!puToolHandle->filterPassed())
124 {
125 ATH_MSG_VERBOSE( "Filter " << puToolHandle->name() << " failed - will stop the event" );
126 this->setFilterPassed(false);
127 }
128 }
129
130 return StatusCode::SUCCESS;
131}
132
133// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
134
136{
137 ATH_MSG_DEBUG ("in finalize()");
138 return StatusCode::SUCCESS;
139}
140
141
143{
144 SubEventIterator iClearEvt=fEvt;
145 while (iClearEvt != lEvt)
146 {
147 ATH_CHECK(iClearEvt->ptr()->evtStore()->clearStore());
148#ifndef NDEBUG
149 ATH_MSG_VERBOSE("Cleared store " << iClearEvt->ptr()->evtStore()->name());
150#endif
151 ++iClearEvt;
152 }
153 return StatusCode::SUCCESS;
154}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
std::vector< xAOD::EventInfo::SubEvent >::const_iterator SubEventIterator
Definition IPileUpTool.h:22
bool differentBunchXing(xAOD::EventInfo::SubEvent i, xAOD::EventInfo::SubEvent j)
an algorithm to steer the IPileUpTools
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
ToolHandleArray< IPileUpTool > m_puTools
virtual StatusCode initialize() override
PileUpToolsAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
StatusCode clearXing(SubEventIterator &fEvt, const SubEventIterator &lEvt)
virtual StatusCode finalize() override
virtual StatusCode execute() override
Class describing the properties of one pileup sub-event.
int16_t time() const
Get the time wrt. the signal event (which has time() == 0)