ATLAS Offline Software
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 
11 PileUpToolsAlg::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
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 }
PileUpToolsAlg::finalize
virtual StatusCode finalize() override
Definition: PileUpToolsAlg.cxx:135
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
PileUpToolsAlg::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: PileUpToolsAlg.h:38
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
PileUpToolsAlg::initialize
virtual StatusCode initialize() override
Definition: PileUpToolsAlg.cxx:18
xAOD::EventInfo_v1::SubEvent::time
int16_t time() const
Get the time wrt. the signal event (which has time() == 0)
Definition: EventInfo_v1.cxx:450
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
AthAlgorithm
Definition: AthAlgorithm.h:47
PileUpToolsAlg::clearXing
StatusCode clearXing(SubEventIterator &fEvt, const SubEventIterator &lEvt)
Definition: PileUpToolsAlg.cxx:142
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
PileUpToolsAlg::m_puTools
ToolHandleArray< IPileUpTool > m_puTools
Definition: PileUpToolsAlg.h:36
SubEventIterator
std::vector< xAOD::EventInfo::SubEvent >::const_iterator SubEventIterator
Definition: IPileUpTool.h:22
PileUpToolsAlg::PileUpToolsAlg
PileUpToolsAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: PileUpToolsAlg.cxx:11
PileUpToolsAlg::execute
virtual StatusCode execute() override
Definition: PileUpToolsAlg.cxx:36
PileUpToolsAlg.h
an algorithm to steer the IPileUpTools
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
xAOD::EventInfo_v1::SubEvent
Class describing the properties of one pileup sub-event.
Definition: EventInfo_v1.h:286
differentBunchXing
bool differentBunchXing(xAOD::EventInfo::SubEvent i, xAOD::EventInfo::SubEvent j)
Definition: PileUpToolsAlg.cxx:30