ATLAS Offline Software
Loading...
Searching...
No Matches
example_METMaker_advanced.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5//Author: Russell Smith
6//Email : rsmith@cern.ch
7//Date : May 2015
8#ifndef XAOD_ANALYSIS
9int main() {
10 std::cout << "Examples are only defined in the analysis release." << std::endl;
11 return 0;
12}
13#else
14
15#ifdef XAOD_STANDALONE
16#include "xAODRootAccess/Init.h"
19#else
22#endif
23
24// FrameWork includes
27
32
40
41#include <memory>
42#undef NDEBUG
43#include <cassert>
44#include "TFile.h"
45#include "TSystem.h"
46
48
52
55
56using namespace asg::msgUserCode;
57
58int main( int argc, char* argv[] ){std::cout << __PRETTY_FUNCTION__ << std::endl;
59#ifdef XAOD_STANDALONE
60 //enable status code failures
61 // CP::CorrectionCode::enableFailure();
62 StatusCode::enableFailure();
63 xAOD::Init() ;
64#else
65 IAppMgrUI* app = POOL::Init(); //important to do this first!
66#endif
67
68 std::string jetType = "AntiKt4EMTopo";
69 TString fileName = gSystem->Getenv("ASG_TEST_FILE_MC");
70 size_t evtmax = 100;
71 bool debug(false);
72 for (int i=0; i<argc; ++i) {
73 if (std::string(argv[i]) == "-filen" && i+1<argc) {
74 fileName = argv[i+1];
75 } else if (std::string(argv[i]) == "-jetcoll" && i+1<argc) {
76 jetType = argv[i+1];
77 } else if (std::string(argv[i]) == "-evtmax" && i+1<argc) {
78 evtmax = atoi(argv[i+1]);
79 } else if (std::string(argv[i]) == "-debug") {
80 debug = true;
81 }
82 }
83
85 ANA_CHECK( ASG_MAKE_ANA_TOOL( jetCalibrationTool, JetCalibrationTool ) );
86 jetCalibrationTool.setName("jetCalibTool");
87 ANA_CHECK( jetCalibrationTool.setProperty("JetCollection", jetType) );
88 ANA_CHECK( jetCalibrationTool.setProperty("ConfigFile", "JES_MC15cRecommendation_May2016_rel21.config") );
89 ANA_CHECK( jetCalibrationTool.setProperty("CalibSequence", "JetArea_Residual_EtaJES_GSC") );
90 ANA_CHECK( jetCalibrationTool.setProperty("IsData", false) );
91 ANA_CHECK( jetCalibrationTool.retrieve() );
92
93 //this test file should work. Feel free to contact me if there is a problem with the file.
94 std::unique_ptr< TFile > ifile( TFile::Open( fileName, "READ" ) );
95 assert( ifile.get() );
96
97 // Create a TEvent object to read from file and a transient store in which to place items
98#ifdef XAOD_STANDALONE
99 std::unique_ptr<xAOD::TEvent> event(new xAOD::TEvent( xAOD::TEvent::kClassAccess ) );
100 std::unique_ptr<xAOD::TStore> store(new xAOD::TStore());
101#else // Athena "Store" is the same StoreGate used by the TEvent
102 std::unique_ptr<POOL::TEvent> event(new POOL::TEvent( POOL::TEvent::kClassAccess ));
103 ServiceHandle<StoreGateSvc>& store = event->evtStore();
104#endif
105 ANA_CHECK( event->readFrom( ifile.get() ) );
106
108 metMaker.setTypeAndName("met::METMaker/metMaker");
109 ANA_CHECK( metMaker.setProperty("DoMuonEloss", true) );
110 ANA_CHECK( metMaker.setProperty("DoRemoveMuonJets", true) );
111 ANA_CHECK( metMaker.setProperty("DoSetMuonJetEMScale", true) );
112 ANA_CHECK( metMaker.retrieve() );
113
114 for(size_t ievent = 0; ievent < std::min(size_t(event->getEntries()), evtmax); ++ievent){
115 if(ievent % 10 == 0) std::cout << "event number: " << ievent << std::endl;
116 ANA_CHECK( event->getEntry(ievent) >= 0 );
117
118 //retrieve the original containers
119 const xAOD::MissingETContainer* coreMet = nullptr;
120 std::string coreMetKey = "MET_Core_" + jetType;
121 ANA_CHECK( event->retrieve(coreMet, coreMetKey) );
122 if(debug) std::cout << "Using core MET " << coreMet << std::endl;
123
124 //if you wanted to make a particle invisible to MET, i.e., remove the particle and
125 //its clusters/tracks from the MET calculation, you can make a container of invisible particles
126 //and then use selectIfNoOverlaps (below)
127 //in this example, we will treat electrons as invisible
128 const xAOD::ElectronContainer* invisibleElectrons = nullptr;
129 ANA_CHECK( event->retrieve(invisibleElectrons, "Electrons") );
130
131 const xAOD::MuonContainer* muons = nullptr;
132 ANA_CHECK( event->retrieve(muons, "Muons") );
133
134 const xAOD::PhotonContainer* photons = nullptr;
135 ANA_CHECK( event->retrieve(photons, "Photons"));
136
137 const xAOD::TauJetContainer* taus = nullptr;
138 ANA_CHECK( event->retrieve(taus, "TauJets"));
139
140 //this should probably be a calibrated jet container. See the METUtilities twiki for more info
141 const xAOD::JetContainer* jets = nullptr;
142 ANA_CHECK( event->retrieve(jets, jetType+"Jets"));
143
144 std::pair< xAOD::JetContainer*, xAOD::ShallowAuxContainer* > jets_shallowCopy = xAOD::shallowCopyContainer( *jets );
145 ANA_CHECK(store->record( jets_shallowCopy.first, "CalibJets" ));
146 ANA_CHECK(store->record( jets_shallowCopy.second, "CalibJetsAux."));
147 //this is a non-const copy of the jet collection that you can calibrate.
148 xAOD::JetContainer* calibJets = jets_shallowCopy.first;
149 xAOD::setOriginalObjectLink(*jets,*calibJets);
150 //Shallow copy is needed (see links below)
151 if(jetCalibrationTool->applyCalibration(*calibJets).isFailure())//apply the calibration
152 return 1;
153
154 //retrieve the MET association map
155 const xAOD::MissingETAssociationMap* metMap = nullptr;
156 std::string metAssocKey = "METAssoc_" + jetType;
157 ANA_CHECK( event->retrieve(metMap, metAssocKey) );
158
159 xAOD::MissingETContainer* newMetContainer = new xAOD::MissingETContainer();
161 newMetContainer->setStore(newMetAuxContainer);
162
163 xAOD::MissingETAssociationHelper metHelper(metMap);
164
165 //here we apply some basic cuts and rebuild the met at each step
166 //InvisibleElectrons
167 if(!invisibleElectrons->empty()){
169 for(const auto& el : *invisibleElectrons) {
170 if(CutsMETMaker::accept(el)){
171 metInvisibleElectrons.push_back(el);
172 }
173 }
174 //this line will mark the electron as invisible if it passes the (inv) electron selection cut
175 //this removes the particle and associated clusters from the jet and soft term calculations
176 ANA_CHECK( metMaker->markInvisible(metInvisibleElectrons.asDataVector(),metHelper,newMetContainer) );
177 // NOTE: Objects marked as invisible should not also be added as part
178 // of another term! However, you can e.g. mark some electrons invisible
179 // and compute RefEle with others.
180 }
181
182 //Photons
184 for(const auto& ph : *photons) {
185 if(CutsMETMaker::accept(ph)) metPhotons.push_back(ph);
186 }
187 ANA_CHECK(metMaker->rebuildMET("RefPhoton",
189 newMetContainer,
190 metPhotons.asDataVector(),
191 metHelper)
192 );
193 //Taus
195 for(const auto& tau : *taus) {
196 if(CutsMETMaker::accept(tau)) metTaus.push_back(tau);
197 }
198 ANA_CHECK(metMaker->rebuildMET("RefTau",
200 newMetContainer,
201 metTaus.asDataVector(),
202 metHelper)
203 );
204
205 //Muons
207 for(const auto& mu : *muons) {
208 // if(CutsMETMaker::accept(mu)) metMuons.push_back(mu);
209 if(mu->muonType()==xAOD::Muon::Combined && mu->pt()>10e3) metMuons.push_back(mu);
210 }
211 ANA_CHECK(metMaker->rebuildMET("Muons",
213 newMetContainer,
214 metMuons.asDataVector(),
215 metHelper)
216 );
217
218 met::addGhostMuonsToJets(*muons, *calibJets);
219
220 //Now time to rebuild jetMet and get the soft term
221 //these functions create an xAODMissingET object with the given names inside the container
222 ANA_CHECK( metMaker->rebuildJetMET("RefJet", //name of jet met
223 "SoftClus", //name of soft cluster term met
224 "PVSoftTrk", //name of soft track term met
225 newMetContainer, //adding to this new met container
226 calibJets, //using this jet collection to calculate jet met
227 coreMet, //core met container
228 metHelper, //with this association map
229 false //don't apply jet jvt cut
230 )
231 );
232
233 ANA_CHECK( metMaker->rebuildTrackMET("RefJetTrk", //name of jet track met
234 "PVSoftTrk", //name of soft track term met
235 newMetContainer,//adding to this new met container
236 calibJets, //using this jet collection to calculate jet track met
237 coreMet, //core met container
238 metHelper, //with this association map
239 false //don't apply jet jvt cut
240 )
241 );
242
243
244 //this builds the final track and cluster met sums, using systematic varied container
247
248 ANA_CHECK(store->record( newMetContainer, "FinalMETContainer" ));
249 ANA_CHECK(store->record( newMetAuxContainer, "FinalMETContainerAux."));
250
251#ifdef XAOD_STANDALONE // POOL::TEvent should handle this when changing events
252 //fill the containers stored in the event
253 //to the output file and clear the transient store
254 assert(event->fill());
255 store->clear();
256#endif
257 }
258
259#ifndef XAOD_STANDALONE
260 ANA_CHECK(app->finalize());
261#endif
262
264
265 return 0;
266 }
267
268#endif
#define ASG_MAKE_ANA_TOOL(handle, type)
create the tool in the given tool handle
macros for messaging and checking status codes
#define ANA_CHECK(EXP)
check whether the given expression was successful
static Double_t taus
const bool debug
DataVector adapter that acts like it holds const pointers.
bool empty() const noexcept
Returns true if the collection is empty.
void record(const T *p, const std::string &key)
Definition TestStore.h:81
an "initializing" ToolHandle for stand-alone applications
StatusCode setProperty(const std::string &name, T2 &&value)
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
void setTypeAndName(const std::string &typeAndName)
ReadStats & stats()
Access the object belonging to the current thread.
Definition IOStats.cxx:17
static IOStats & instance()
Singleton object accessor.
Definition IOStats.cxx:11
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
Tool for accessing xAOD files outside of Athena.
@ kClassAccess
Access auxiliary data using the aux containers.
A relatively simple transient store for objects created in analysis.
Definition TStore.h:45
StatusCode accept(const xAOD::Muon *mu)
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
uint64_t bitmask_t
Type for status word bit mask.
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
TestStore store
Definition TestStore.cxx:23
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
StatusCode buildMETSum(const std::string &totalName, xAOD::MissingETContainer *metCont)
void addGhostMuonsToJets(const xAOD::MuonContainer &muons, xAOD::JetContainer &jets)
@ Photon
The object is a photon.
Definition ObjectType.h:47
@ Muon
The object is a muon.
Definition ObjectType.h:48
@ Tau
The object is a tau (jet)
Definition ObjectType.h:49
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition Init.cxx:31
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, const EventContext &ctx)
Function making a shallow copy of a constant container.
bool setOriginalObjectLink(const IParticle &original, IParticle &copy)
This function should be used by CP tools when they make a deep copy of an object in their correctedCo...
MissingETAuxContainer_v1 MissingETAuxContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
TauJetContainer_v3 TauJetContainer
Definition of the current "taujet container version".
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
MissingETAssociationMap_v1 MissingETAssociationMap
Version control by type defintion.
@ LCTopo
Indicator for MET contribution from TopoClusters with LCW calibration applied.
@ Track
Indicator for MET contribution from reconstructed charged particle tracks.