ATLAS Offline Software
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
9 int 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"
17 #include "xAODRootAccess/TEvent.h"
18 #include "xAODRootAccess/TStore.h"
19 #else
20 #include "POOLRootAccess/TEvent.h"
21 #include "StoreGate/StoreGateSvc.h"
22 #endif
23 
24 // FrameWork includes
27 
32 
33 #include "xAODCore/ShallowCopy.h"
34 #include "xAODJet/JetContainer.h"
37 #include "xAODMuon/MuonContainer.h"
40 
41 #include <memory>
42 #undef NDEBUG
43 #include <cassert>
44 #include "TFile.h"
45 #include "TSystem.h"
46 
48 
49 #include "METInterface/IMETMaker.h"
52 
53 #include "xAODCore/tools/IOStats.h"
55 
56 using namespace asg::msgUserCode;
57 
58 int 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();
160  xAOD::MissingETAuxContainer* newMetAuxContainer = new xAOD::MissingETAuxContainer();
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) {
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
ShallowCopy.h
SGTest::store
TestStore store
Definition: TestStore.cxx:23
xAOD::MissingETAuxContainer
MissingETAuxContainer_v1 MissingETAuxContainer
Definition: MissingETAuxContainer.h:16
xAOD::IOStats::stats
ReadStats & stats()
Access the object belonging to the current thread.
Definition: IOStats.cxx:17
IMETMaker::rebuildJetMET
virtual StatusCode rebuildJetMET(const std::string &metJetKey, const std::string &metSoftKey, xAOD::MissingETContainer *metCont, const xAOD::JetContainer *jets, const xAOD::MissingETContainer *metCoreCont, xAOD::MissingETAssociationHelper &helper, bool doJetJVT)=0
IMETMaker::markInvisible
virtual StatusCode markInvisible(const xAOD::IParticleContainer *collection, xAOD::MissingETAssociationHelper &helper, xAOD::MissingETContainer *metCont)=0
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
min
constexpr double min()
Definition: ap_fixedTest.cxx:26
CutsMETMaker::accept
StatusCode accept(const xAOD::Muon *mu)
Definition: CutsMETMaker.cxx:18
IMETMaker.h
MissingETBase::Source::Signal::LCTopo
@ LCTopo
Indicator for MET contribution from TopoClusters with LCW calibration applied.
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
xAOD::MissingETContainer
MissingETContainer_v1 MissingETContainer
Definition: Event/xAOD/xAODMissingET/xAODMissingET/MissingETContainer.h:16
IOStats.h
xAOD::TEvent::kClassAccess
@ kClassAccess
Access auxiliary data using the aux containers.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:100
MissingETBase::Types::bitmask_t
uint64_t bitmask_t
Type for status word bit mask.
Definition: MissingETBase.h:39
ASG_MAKE_ANA_TOOL
#define ASG_MAKE_ANA_TOOL(handle, type)
create the tool in the given tool handle
Definition: AnaToolHandle.h:690
IMETMaker::rebuildMET
virtual StatusCode rebuildMET(const std::string &metKey, xAOD::Type::ObjectType metType, xAOD::MissingETContainer *metCont, const xAOD::IParticleContainer *collection, xAOD::MissingETAssociationHelper &helper, MissingETBase::UsageHandler::Policy objScale=MissingETBase::UsageHandler::PhysicsObject)=0
xAOD::MissingETAssociationMap_v1
Definition: MissingETAssociationMap_v1.h:29
POOL::Init
IAppMgrUI * Init(const char *options="POOLRootAccess/basic.opts")
Bootstraps (creates and configures) the Gaudi Application with the provided options file.
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:29
POOL::TEvent::kClassAccess
@ kClassAccess
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:46
met::buildMETSum
StatusCode buildMETSum(const std::string &totalName, xAOD::MissingETContainer *metCont)
Definition: METHelpers.cxx:64
METHelpers.h
met::addGhostMuonsToJets
void addGhostMuonsToJets(const xAOD::MuonContainer &muons, xAOD::JetContainer &jets)
Definition: METHelpers.cxx:34
POOL::TEvent::readFrom
StatusCode readFrom(TFile *file)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:133
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
JetCalibrationTool.h
ReadStats.h
CheckAppliedSFs.e3
e3
Definition: CheckAppliedSFs.py:264
event
POOL::TEvent event(POOL::TEvent::kClassAccess)
ElectronContainer.h
POOL::TEvent::getEntry
int getEntry(long entry)
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:185
asg::StandaloneToolHandle::setProperty
StatusCode setProperty(const std::string &name, T2 &&value)
Definition: StandaloneToolHandle.h:105
lumiFormat.i
int i
Definition: lumiFormat.py:85
POOL::TEvent::getEntries
long getEntries()
Definition: PhysicsAnalysis/POOLRootAccess/src/TEvent.cxx:124
asg::StandaloneToolHandle
an "initializing" ToolHandle for stand-alone applications
Definition: StandaloneToolHandle.h:44
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
MissingETAuxContainer.h
MessageCheck.h
macros for messaging and checking status codes
JetCalibrationTool
Definition: JetCalibrationTool.h:34
TEvent.h
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
xAOD::MissingETAuxContainer_v1
Auxiliary data store for xAOD::MissingETContainer.
Definition: MissingETAuxContainer_v1.h:20
Init.h
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
TauJetContainer.h
xAOD::IOStats::instance
static IOStats & instance()
Singleton object accessor.
Definition: IOStats.cxx:11
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
asg::StandaloneToolHandle::retrieve
StatusCode retrieve()
initialize the tool, will succeed if the tool was already initialized
Definition: StandaloneToolHandle.h:147
xAOD::ReadStats::printSmartSlimmingBranchList
void printSmartSlimmingBranchList(bool autoIncludeLinks=false) const
Print the accessed variables, formatted for smart slimming.
Trk::Combined
@ Combined
Definition: TrackSummaryTool.h:32
main
int main()
Definition: example_METMaker_advanced.cxx:9
xAOD::MissingETContainer_v1
Container for xAOD::MissingET_v1 objects.
Definition: MissingETContainer_v1.h:21
POOL::TEvent
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:40
TEvent.h
debug
const bool debug
Definition: MakeUncertaintyPlots.cxx:53
CutsMETMaker.h
SGTest::TestStore::record
void record(const T *p, const std::string &key)
Definition: TestStore.h:81
xAOD::MissingETAssociationHelper
Definition: MissingETAssociationHelper.h:26
xAOD::shallowCopyContainer
std::pair< std::unique_ptr< T >, std::unique_ptr< ShallowAuxContainer > > shallowCopyContainer(const T &cont, [[maybe_unused]] const EventContext &ctx)
Function making a shallow copy of a constant container.
Definition: ShallowCopy.h:110
MuonContainer.h
xAOD::Photon
Photon_v1 Photon
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Photon.h:17
Muon
struct TBPatternUnitContext Muon
asg::StandaloneToolHandle::setTypeAndName
void setTypeAndName(const std::string &typeAndName)
Definition: StandaloneToolHandle.h:101
MissingETBase::Source::Signal::Track
@ Track
Indicator for MET contribution from reconstructed charged particle tracks.
ConvertOldUJHistosToNewHistos.jetType
string jetType
Definition: ConvertOldUJHistosToNewHistos.py:121
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
JetContainer.h
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
POOL::TEvent::retrieve
StatusCode retrieve(const T *&obj)
Definition: PhysicsAnalysis/POOLRootAccess/POOLRootAccess/TEvent.h:74
xAOD::setOriginalObjectLink
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...
Definition: IParticleHelpers.cxx:30
IJetCalibrationTool::applyCalibration
virtual StatusCode applyCalibration(xAOD::JetContainer &jets) const =0
Apply calibration to a jet container.
xAODType::Tau
@ Tau
The object is a tau (jet)
Definition: ObjectType.h:49
IMETMaker::rebuildTrackMET
virtual StatusCode rebuildTrackMET(const std::string &metJetKey, const std::string &metSoftKey, xAOD::MissingETContainer *metCont, const xAOD::JetContainer *jets, const xAOD::MissingETContainer *metCoreCont, xAOD::MissingETAssociationHelper &helper, bool doJetJVT)=0
CxxUtils::atoi
int atoi(std::string_view str)
Helper functions to unpack numbers decoded in string into integers and doubles The strings are requir...
Definition: Control/CxxUtils/Root/StringUtils.cxx:85
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
IParticleHelpers.h
MissingETAssociationMap.h
LArCellNtuple.ifile
string ifile
Definition: LArCellNtuple.py:133
StandaloneToolHandle.h
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
MissingETAssociationHelper.h
StoreGateSvc.h
PhotonContainer.h
MissingETContainer.h
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
TStore.h
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84
xAOD::Init
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
Definition: Init.cxx:31
ServiceHandle< StoreGateSvc >