ATLAS Offline Software
EventSaverxAOD.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3  */
4 
6 
7 #include "TopEvent/Event.h"
8 #include "TopEvent/EventTools.h"
10 
12 
14 
15 #include "xAODRootAccess/TEvent.h"
16 
17 #include "TFile.h"
18 
19 namespace top {
21  asg::AsgTool("top::EventSaverxAOD"),
22  m_prefix("CorrectedSelected"),
23  m_config(nullptr),
24  m_outputFile(nullptr) {
25  }
26 
28  }
29 
30  void EventSaverxAOD::initialize(std::shared_ptr<top::TopConfig> config, TFile* file,
31  const std::vector<std::string>& extraBranches) {
32  m_config = config;
34  top::check(evtStore()->event()->writeTo(m_outputFile), "EventSaverxAOD trying to call writeTo on output file");
35 
36  //EventInfo
37  std::string eventInfoList = "runNumber.eventNumber.eventTypeBitmask.averageInteractionsPerCrossing";
38  if (config->isMC()) eventInfoList += ".mcChannelNumber.mcEventWeights.PileupWeight";
39 
40  //Add extra branches - e.g. if it passed the ee / mumu / emu selection per event
41  for (const auto& branchName : extraBranches)
42  eventInfoList += "." + branchName;
43 
44  evtStore()->event()->setAuxItemList("EventInfoAux.", eventInfoList);
45 
46  //Electrons
47  evtStore()->event()->setAuxItemList(m_prefix + m_config->sgKeyElectrons() + "Aux.", "pt.eta.phi.m.charge");
48 
49  //Muons
50  evtStore()->event()->setAuxItemList(
51  m_prefix + m_config->sgKeyMuons() + "Aux.", "pt.eta.phi.m.charge.muonType.combinedTrackParticleLink");
52 
53  //Once mu has a built-in charge we can get rid of this
54  //Muon charge is a property of the associated track - e.g. d0.z0.phi.theta.qOverP
55  evtStore()->event()->setAuxItemList("InDetTrackParticlesAux.", "qOverP");
56  evtStore()->event()->setAuxItemList("ExtrapolatedMuonTrackParticlesAux.", "qOverP");
57  evtStore()->event()->setAuxItemList("CombinedMuonTrackParticlesAux.", "qOverP");
58 
59  //Jets
60  evtStore()->event()->setAuxItemList(m_prefix + m_config->sgKeyJets() + "Aux.", "pt.eta.phi.m.btaggingLink");
61 
62  }
63 
65  //only save the nominal variation for now - speak to Attila about TEvent.fill not taking a tree name
66  if (event.m_hashValue != m_config->nominalHashValue()) return;
67 
68  // record the event?
69  if (m_config->saveOnlySelectedEvents() && !event.m_saveEvent) return;
70 
71  // Systematic Event
72  const xAOD::SystematicEventContainer* allSystematics(nullptr);
73  top::check(evtStore()->retrieve(allSystematics,
74  m_config->sgKeyTopSystematicEvents()),
75  "Failed to retrieve xAOD::SystematicEventContainer");
76  //cppcheck-suppress uninitvar
77  xAOD::SystematicEventContainer* allSystematics_output = new xAOD::SystematicEventContainer {};
78  SG::IAuxStore* allSystematics_aux = evtStore()->event()->recordAux(m_config->sgKeyTopSystematicEvents() + "Aux.");
79  allSystematics_output->setStore(allSystematics_aux);
80  for (const auto *currentSystematic : *allSystematics) {
81  //cppcheck-suppress uninitvar
83  out->makePrivateStore(*currentSystematic);
84  allSystematics_output->push_back(out);
85  }
86  top::check(evtStore()->event()->record(allSystematics_output,
87  m_config->sgKeyTopSystematicEvents()),
88  "Failed to record xAOD::SystematicEventContainer");
89 
90 
91  // Top Parton History
92  if (m_config->doTopPartonHistory()) {
93  if (evtStore()->contains<xAOD::PartonHistoryContainer>(m_config->sgKeyTopPartonHistory())) {
94  const xAOD::PartonHistoryContainer* partonHistory(nullptr);
95  top::check(evtStore()->retrieve(partonHistory,
96  m_config->sgKeyTopPartonHistory()), "Failed to retrieve Top Parton History");
97  //cppcheck-suppress uninitvar
98  xAOD::PartonHistoryContainer* partonHistory_output = new xAOD::PartonHistoryContainer {};
99  SG::IAuxStore* partonHistory_aux = evtStore()->event()->recordAux(m_config->sgKeyTopPartonHistory() + "Aux.");
100  partonHistory_output->setStore(partonHistory_aux);
101  for (const auto *x : *partonHistory) {
102  //cppcheck-suppress uninitvar
104  out->makePrivateStore(*x);
105  partonHistory_output->push_back(out);
106  }
107  top::check(evtStore()->event()->record(partonHistory_output,
108  m_config->sgKeyTopPartonHistory()), "Failed to record Parton History");
109  }
110  }
111 
112 
113 
114  //Event Info
115  top::check(evtStore()->event()->copy("EventInfo"), "copying event info failed");
116 
117  //Electrons
118  if (m_config->useElectrons()) {
119  xAOD::ElectronContainer* electrons_output = new xAOD::ElectronContainer();
120  SG::IAuxStore* electrons_aux = evtStore()->event()->recordAux(m_prefix + m_config->sgKeyElectrons() + "Aux.");
121  electrons_output->setStore(electrons_aux);
122 
123  for (const auto* const elPtr : event.m_electrons) {
124  xAOD::Electron* outputElPtr = new xAOD::Electron();
125  outputElPtr->makePrivateStore(*elPtr);
126  electrons_output->push_back(outputElPtr);
127  }
128 
129  top::check(evtStore()->event()->record(electrons_output,
130  m_prefix + m_config->sgKeyElectrons()),
131  "xAOD::TEvent record " + m_prefix + m_config->sgKeyElectrons() + " failed");
132  }
133 
134  //Muons
135  if (m_config->useMuons()) {
136  xAOD::MuonContainer* muons_output = new xAOD::MuonContainer();
137  SG::IAuxStore* muons_aux = evtStore()->event()->recordAux(m_prefix + m_config->sgKeyMuons() + "Aux.");
138  muons_output->setStore(muons_aux);
139 
140  for (const auto* const muPtr : event.m_muons) {
141  xAOD::Muon* outputMuPtr = new xAOD::Muon();
142  outputMuPtr->makePrivateStore(*muPtr);
143  muons_output->push_back(outputMuPtr);
144  }
145 
146  top::check(evtStore()->event()->record(muons_output,
147  m_prefix + m_config->sgKeyMuons()),
148  "xAOD::TEvent record " + m_prefix + m_config->sgKeyMuons() + " failed");
149 
150  //for now...
151  //Muon charge is a property of the associated track
152  //Copy tracks (for muon charge)
153  check(evtStore()->event()->copy("InDetTrackParticles"), "xAOD::TEvent copy InDetTrackParticles failed");
154  check(evtStore()->event()->copy(
155  "ExtrapolatedMuonTrackParticles"),
156  "xAOD::TEvent copy InDetTrackParticles ExtrapolatedMuonTrackParticles");
157  check(evtStore()->event()->copy(
158  "CombinedMuonTrackParticles"), "xAOD::TEvent copy InDetTrackParticles CombinedMuonTrackParticles");
159  }
160 
161  //Jets
162  if (m_config->useJets()) {
163  xAOD::JetContainer* jets_output = new xAOD::JetContainer();
164  SG::IAuxStore* jets_aux = evtStore()->event()->recordAux(m_prefix + m_config->sgKeyJets() + "Aux.");
165  jets_output->setStore(jets_aux);
166 
167  for (const auto* const jetPtr : event.m_jets) {
168  xAOD::Jet* outputJetPtr = new xAOD::Jet();
169  outputJetPtr->makePrivateStore(*jetPtr);
170  jets_output->push_back(outputJetPtr);
171  }
172 
173  top::check(evtStore()->event()->record(jets_output,
174  m_prefix + m_config->sgKeyJets()),
175  "xAOD::TEvent record " + m_prefix + m_config->sgKeyJets() + " failed");
176 
177  //b-tagging variables need the BTagging_JetNameWithoutJetsOnTheEnd container
178  //remove Jets from the end of the container name and add BTagging_
179  const std::string btagName = "BTagging_" + m_config->sgKeyJets().substr(0, m_config->sgKeyJets().size() - 4);
180  check(evtStore()->event()->copy(btagName), "xAOD::TEvent copy " + btagName + " failed");
181  }
182 
183  //Large Jets
184  if (m_config->useLargeRJets()) {
185  xAOD::JetContainer* largeJets_output = new xAOD::JetContainer();
186  SG::IAuxStore* largeJets_aux = evtStore()->event()->recordAux(m_prefix + m_config->sgKeyLargeRJets() + "Aux.");
187  largeJets_output->setStore(largeJets_aux);
188 
189  for (const auto* const jetPtr : event.m_largeJets) {
190  xAOD::Jet* outputJetPtr = new xAOD::Jet();
191  outputJetPtr->makePrivateStore(*jetPtr);
192  largeJets_output->push_back(outputJetPtr);
193  }
194 
195  check(evtStore()->event()->record(largeJets_output,
196  m_prefix + m_config->sgKeyLargeRJets()),
197  "xAOD::TEvent record " + m_prefix + m_config->sgKeyLargeRJets() + " failed");
198  }
199 
200  //MET
201  top::check(evtStore()->event()->copy(m_config->sgKeyMissingEt()),
202  "xAOD::TEvent copy " + m_config->sgKeyMissingEt() + " failed");
203 
204  //do it!
205  top::check(evtStore()->event()->fill(), "xAOD::TEvent fill failed");
206  }
207 
209  m_outputFile->Write();
210  top::check(evtStore()->event()->finishWritingTo(
211  m_outputFile), "EventSaverxAOD::finalise trying to call finishWritingTo");
212  }
213 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
xAOD::PartonHistory
Interface class.
Definition: PartonHistory.h:48
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
top
TopConfig A simple configuration that is NOT a singleton.
Definition: AnalysisTrackingHelper.cxx:58
xAOD::MuonContainer
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
Definition: Event/xAOD/xAODMuon/xAODMuon/MuonContainer.h:14
top::EventSaverxAOD::m_outputFile
TFile * m_outputFile
We need to hold on to the output file.
Definition: EventSaverxAOD.h:83
asg
Definition: DataHandleTestTool.h:28
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
top::EventSaverxAOD::EventSaverxAOD
EventSaverxAOD()
Kept simple, set the output container prefix (so they have a different name to the input containers).
Definition: EventSaverxAOD.cxx:20
x
#define x
top::EventSaverxAOD::finalize
virtual void finalize()
xAOD needs to write some more stuff to the file at the end of a job
Definition: EventSaverxAOD.cxx:208
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
EventTools.h
A few functions for doing operations on particles / events. Currently holds code for dR,...
EventSaverxAOD.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
PartonHistory.h
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
TEvent.h
file
TFile * file
Definition: tile_monitor.h:29
top::check
void check(bool thingToCheck, const std::string &usefulFailureMessage)
Print an error message and terminate if thingToCheck is false.
Definition: EventTools.cxx:15
Event.h
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
top::EventSaverxAOD::saveEvent
virtual void saveEvent(const top::Event &event)
Save an event.
Definition: EventSaverxAOD.cxx:64
top::EventSaverxAOD::m_config
std::shared_ptr< top::TopConfig > m_config
We need access to the configuration file to get the container names.
Definition: EventSaverxAOD.h:80
xAOD::ElectronContainer
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/ElectronContainer.h:17
top::EventSaverxAOD::~EventSaverxAOD
virtual ~EventSaverxAOD()
Definition: EventSaverxAOD.cxx:27
xAOD::Muon
Muon_v1 Muon
Reference the current persistent version:
Definition: Event/xAOD/xAODMuon/xAODMuon/Muon.h:13
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
SG::AuxElement::makePrivateStore
void makePrivateStore()
Create a new (empty) private store for this object.
Definition: AuxElement.cxx:172
TopConfig.h
xAOD::Electron_v1
Definition: Electron_v1.h:34
SG::IAuxStore
Interface for non-const operations on an auxiliary store.
Definition: IAuxStore.h:48
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
xAOD::SystematicEvent
SystematicEvent A simple xAOD class which we can persist into a mini-xAOD The xAOD EDM is way too com...
Definition: SystematicEvent.h:27
lumiFormat.fill
fill
Definition: lumiFormat.py:111
config
std::vector< std::string > config
Definition: fbtTestBasics.cxx:72
top::Event
Very simple class to hold event data after reading from a file.
Definition: Event.h:49
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
top::EventSaverxAOD::initialize
virtual StatusCode initialize()
Dummy implementation of the initialisation function.
Definition: EventSaverxAOD.h:51
calibdata.copy
bool copy
Definition: calibdata.py:27
top::EventSaverxAOD::m_prefix
const std::string m_prefix
Name to prepend to the output containers.
Definition: EventSaverxAOD.h:77
SystematicEventContainer.h
xAOD::Jet
Jet_v1 Jet
Definition of the current "jet version".
Definition: Event/xAOD/xAODJet/xAODJet/Jet.h:17