ATLAS Offline Software
Macros | Functions
fbtNtupleTester.cxx File Reference
#include <AsgMessaging/MessageCheck.h>
#include <AsgTools/StandaloneToolHandle.h>
#include <iostream>
#include <sstream>
#include <memory>
#include <vector>
#include <string>
#include <cmath>
#include <numeric>
#include <TFile.h>
#include <TChain.h>
#include <TString.h>
#include <TSystem.h>
#include "AsgTools/AnaToolHandle.h"
#include "xAODEventInfo/EventInfo.h"
#include "xAODEventInfo/EventAuxInfo.h"
#include "xAODBase/IParticle.h"
#include "xAODEgamma/Electron.h"
#include "xAODMuon/Muon.h"
#include "FakeBkgTools/ApplyFakeFactor.h"

Go to the source code of this file.

Macros

#define READ_TREE_ADDRESSES(typeName, name)   typeName name = 0; chain->SetBranchAddress(#name, &name)
 
#define READ_TREE_ADDRESSES_VEC(typeName, name)   typeName* name = 0; chain->SetBranchAddress(#name, &name)
 

Functions

int main (int argc, char *argv[])
 

Macro Definition Documentation

◆ READ_TREE_ADDRESSES

#define READ_TREE_ADDRESSES (   typeName,
  name 
)    typeName name = 0; chain->SetBranchAddress(#name, &name)

Definition at line 38 of file fbtNtupleTester.cxx.

◆ READ_TREE_ADDRESSES_VEC

#define READ_TREE_ADDRESSES_VEC (   typeName,
  name 
)    typeName* name = 0; chain->SetBranchAddress(#name, &name)

Definition at line 43 of file fbtNtupleTester.cxx.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 54 of file fbtNtupleTester.cxx.

55 {
56  if(argc < 2){
57  std::cerr << "No input file specified! Exiting." << std::endl;
58  return EXIT_FAILURE;
59  }
60 
61  TString fileNameFullPath = argv[1];
62  TString fileName = fileNameFullPath;
63 
64 #ifdef XAOD_STANDALONE
65  ANA_CHECK_SET_TYPE (int); // makes ANA_CHECK return ints if exiting function
66 
67 
68  TFile *inFile = TFile::Open(fileName, "READ");
69  if(!inFile){
70  std::cerr << "Invalid root file! Exiting." << std::endl;
71  return EXIT_FAILURE;
72  }
73 
74  // Name of the tree in the file
75  TString sample = "tree_NoSys";
76 
77  std::cout << "Running sample: " << sample << std::endl;
78 
79  // Create instance of the tools and set the FF file names + the histogram names
80  CP::ApplyFakeFactor* ffTool = new CP::ApplyFakeFactor("ApplyFF");
81 
82  // Path to the file of FF histograms
83  std::vector<std::string> ff_file = { "FakeBkgTools/testValuesFF.root" };
84 
85  ANA_CHECK( ffTool->setProperty("InputFiles", ff_file) );
86  ANA_CHECK( ffTool->setProperty("EnergyUnit", "GeV") );
87  ANA_CHECK( ffTool->setProperty("SkipUncertainties", true) );
88  ANA_CHECK( ffTool->setProperty("OutputLevel", MSG::FATAL) );
89 
90  // ntuple values are in GeV, but xAOD::IParticles that
91  // we create will be in MeV
92  float convertToMeV = 1000.;
93 
94  // Initialize tools
95  ANA_CHECK( ffTool->initialize() );
96  TChain* chain = new TChain("chain");
97  std::vector<TString> datasets = {};
98 
99  datasets = {sample};
100 
101  for(TString dataset : datasets){
102  chain->Add(fileNameFullPath+"/"+dataset);
103  }
104 
105  READ_TREE_ADDRESSES(unsigned long long, EventNumber);
106  READ_TREE_ADDRESSES(int, DatasetNumber);
107  READ_TREE_ADDRESSES(int, nLep_base);
108  READ_TREE_ADDRESSES(int, nLep_signal);
109  READ_TREE_ADDRESSES(int, nLep_antiID);
110 
111  READ_TREE_ADDRESSES(float, lep1Pt);
112  READ_TREE_ADDRESSES(float, lep1Eta);
113  READ_TREE_ADDRESSES(float, lep1Phi);
114  READ_TREE_ADDRESSES(float, lep1D0Sig);
115  READ_TREE_ADDRESSES(float, lep1Z0SinTheta);
116  READ_TREE_ADDRESSES(int, lep1Flavor);
117  READ_TREE_ADDRESSES(int, lep1Charge);
118  READ_TREE_ADDRESSES(int, lep1PassOR);
119  READ_TREE_ADDRESSES(int, lep1PassBL);
120  READ_TREE_ADDRESSES(bool, lep1Loose);
121  READ_TREE_ADDRESSES(bool, lep1Medium);
122  READ_TREE_ADDRESSES(bool, lep1TruthMatched);
123  READ_TREE_ADDRESSES(bool, lep1Signal);
124  READ_TREE_ADDRESSES(bool, lep1AntiID);
125  READ_TREE_ADDRESSES(int, lep1Type);
126 
127  READ_TREE_ADDRESSES(float, lep2Pt);
128  READ_TREE_ADDRESSES(float, lep2Eta);
129  READ_TREE_ADDRESSES(float, lep2Phi);
130  READ_TREE_ADDRESSES(float, lep2D0Sig);
131  READ_TREE_ADDRESSES(float, lep2Z0SinTheta);
132  READ_TREE_ADDRESSES(int, lep2Flavor);
133  READ_TREE_ADDRESSES(int, lep2Charge);
134  READ_TREE_ADDRESSES(bool, lep2PassOR);
135  READ_TREE_ADDRESSES(bool, lep2PassBL);
136  READ_TREE_ADDRESSES(bool, lep2Loose);
137  READ_TREE_ADDRESSES(bool, lep2Medium);
138  READ_TREE_ADDRESSES(bool, lep2TruthMatched);
139  READ_TREE_ADDRESSES(bool, lep2Signal);
140  READ_TREE_ADDRESSES(bool, lep2AntiID);
141  READ_TREE_ADDRESSES(int, lep2Type);
142 
143  READ_TREE_ADDRESSES(float, lep3Pt);
144  READ_TREE_ADDRESSES(float, lep3Eta);
145  READ_TREE_ADDRESSES(float, lep3Phi);
146  READ_TREE_ADDRESSES(float, lep3D0Sig);
147  READ_TREE_ADDRESSES(float, lep3Z0SinTheta);
148  READ_TREE_ADDRESSES(int, lep3Flavor);
149  READ_TREE_ADDRESSES(int, lep3Charge);
150  READ_TREE_ADDRESSES(bool, lep3PassOR);
151  READ_TREE_ADDRESSES(bool, lep3PassBL);
152  READ_TREE_ADDRESSES(bool, lep3Loose);
153  READ_TREE_ADDRESSES(bool, lep3Medium);
154  READ_TREE_ADDRESSES(bool, lep3TruthMatched);
155  READ_TREE_ADDRESSES(bool, lep3Signal);
156  READ_TREE_ADDRESSES(bool, lep3AntiID);
157  READ_TREE_ADDRESSES(int, lep3Type);
158 
159  READ_TREE_ADDRESSES(double, FFWeight);
160 
161  // Turn on all branches. Our cloned tree will share
162  // the references to the variables which we wish to modify!
163  chain->SetBranchStatus("*",1);
164 
165  TFile* outFile_nominal = new TFile("antiIDTimesFF_"+sample+"_nominal.root","RECREATE");
166  TTree* outTree_nominal = chain->CloneTree(0);
167  outTree_nominal->SetTitle("fakes");
168  outTree_nominal->SetName("fakes");
169 
170  // We need to make a TEvent and TStore object so that the FBT can look
171  // for the evtStore()
172  xAOD::TEvent* tEvent = new xAOD::TEvent();
173  xAOD::TStore* tStore = new xAOD::TStore();
174 
175  // For creating an EventInfo object that the tool will look for
176  std::unique_ptr<xAOD::EventInfo> eventInfo = std::make_unique<xAOD::EventInfo>();
177  std::unique_ptr<xAOD::EventAuxInfo> eventAuxInfo = std::make_unique<xAOD::EventAuxInfo>();
178 
179  eventInfo->setStore(eventAuxInfo.get());
180  ANA_CHECK( ffTool->evtStore()->record(eventInfo.get(), "EventInfo") );
181  ANA_CHECK( ffTool->evtStore()->record(eventAuxInfo.get(), "EventInfoAux.") );;
182 
183  // we can clear this vector in each loop, rather than allocate
184  // the memory for each event
186 
187  static const SG::Decorator<char> tightDecor("Tight");
188  static const SG::Decorator<unsigned int> runNumberDecor("RandomRunNumber");
189 
190  // Loop over events!
191  int nEventsProcessed(0);
192  int nEventsPassed(0);
193 
194  Long64_t nentries = chain->GetEntries();
195  for(Long64_t i(0); i < nentries; ++i){
196  std::cout << "processed " << nEventsProcessed << " events" << std::endl;
197  ++nEventsProcessed;
198 
199  int getEntry = chain->GetEntry(i);
200  if(getEntry < 1){
201  std::cerr << "GetEntry() error!! Either an I/O issue, or an invalid entry. Aborting." << std::endl;
202  abort();
203  }
204 
205  // just as an example for an EventInfo decor that one could set...
206  runNumberDecor(*eventInfo) = 507;
207 
208  std::vector<float> lepPt = {lep1Pt, lep2Pt, lep3Pt};
209  std::vector<float> lepEta = {lep1Eta, lep2Eta, lep3Eta};
210  std::vector<float> lepPhi = {lep1Phi, lep2Phi, lep3Phi};
211  std::vector<float> lepD0Sig = {lep1D0Sig, lep2D0Sig, lep3D0Sig};
212  std::vector<float> lepZ0SinTheta = {lep1Z0SinTheta, lep2Z0SinTheta, lep3Z0SinTheta};
213  std::vector<int> lepFlavor = {lep1Flavor, lep2Flavor, lep3Flavor};
214  std::vector<int> lepCharge = {lep1Charge, lep2Charge, lep3Charge};
215  std::vector<int> lepPassOR = {lep1PassOR, lep2PassOR, lep3PassOR};
216  std::vector<int> lepPassBL = {lep1PassBL, lep2PassBL, lep3PassBL};
217  std::vector<int> lepLoose = {lep1Loose, lep2Loose, lep3Loose};
218  std::vector<int> lepMedium = {lep1Medium, lep2Medium, lep3Medium};
219  std::vector<int> lepSignal = {lep1Signal, lep2Signal, lep3Signal};
220  std::vector<int> lepTruthMatched = {lep1TruthMatched, lep2TruthMatched, lep3TruthMatched};
221 
223  FFWeight = 1;
224  nLep_antiID = 0;
225  lep1AntiID = 0;
226  lep2AntiID = 0;
227  lep3AntiID = 0;
228 
229  // Right now, we have up to 3 leptons saved in our ntuples
230  int nTotalLeps = 3;
231 
232  // Look at ID and isolation to fill whether a given lepton passes
233  // the signal ID or antiID
234  std::vector<int> lepPassSigID(nTotalLeps, 0);
235  std::vector<int> lepPassAntiID(nTotalLeps, 0);
236 
237  // clear the vector of IParticle objects
238  for(auto part : particles){
239  delete part;
240  }
241  particles.clear();
242 
243  // Loop over the events to assign ID vs. antiID leptons
244  for(int i = 0; i < nTotalLeps; ++i){
245 
246  bool passesSignal = false;
247  bool passesAntiID = false;
248  xAOD::Type::ObjectType lepType = xAOD::Type::Other;
249 
250  // Fill ID and SF vectors below:
251  if( (lepFlavor).at(i) == 1 ){ // electrons
252 
253  //double z0sinTheta = fabs( (lepZ0SinTheta).at(i) );
254 
255  passesSignal = (lepSignal).at(i);
256  passesAntiID = !(lepSignal).at(i);
257  //passesAntiID = (lepLoose.at(i) && lepPassBL.at(i) && (z0sinTheta < 0.5) && (lepPassOR).at(i)) && !(lepSignal.at(i));
258 
259  // Now fill the vectors
260  lepPassSigID.at(i) = passesSignal;
261  lepPassAntiID.at(i) = passesAntiID;
262 
263  lepType = xAOD::Type::Electron;
264  }
265  else if( (lepFlavor).at(i) == 2){ // muons
266 
267  //double z0sinTheta = fabs( (lepZ0SinTheta).at(i) );
268 
269  passesSignal = (lepSignal).at(i);
270  passesAntiID = !(lepSignal).at(i);
271  //passesAntiID = (lepMedium.at(i) && (z0sinTheta < 0.5) && !(lepSignal).at(i));
272 
273  // Now fill the vectors
274  lepPassSigID.at(i) = passesSignal;
275  lepPassAntiID.at(i) = passesAntiID;
276 
277  lepType = xAOD::Type::Muon;
278  }
279 
280  // if this lepton isn't relevant, we can skip
281  if(!passesSignal && !passesAntiID) continue;
282 
283  // FIXME need to be sure all pT/eta/phi values are in MeV, and then just need to
284  // unpack the FFs into MeV
285  if (lepType == xAOD::Type::Electron){
287  particle->makePrivateStore();
288  particle->setP4(lepPt.at(i)*convertToMeV,lepEta.at(i),lepPhi.at(i),0.511);
289  particle->setCharge(lepCharge.at(i));
290  tightDecor(*particle) = passesSignal;
291  particles.push_back(static_cast<xAOD::IParticle*>(particle));
292  }
293  else if(lepType == xAOD::Type::Muon){
294  xAOD::Muon* particle = new xAOD::Muon();
295  particle->makePrivateStore();
296  particle->setP4(lepPt.at(i)*convertToMeV,lepEta.at(i),lepPhi.at(i));
297  particle->setCharge(lepCharge.at(i));
298  tightDecor(*particle) = passesSignal;
299  particles.push_back(static_cast<xAOD::IParticle*>(particle));
300  }
301  else{
302  std::cerr << "invalid lepton type!" << std::endl;
303  continue;
304  }
305 
306  // Here we'll redefine "signal" lepton for our outputs, because
307  // we're about to promote our antiID leptons to signal leptons.
308  if(passesAntiID){
309 
310  ++nLep_signal;
311  ++nLep_antiID;
312 
313  if(i == 0){
314  lep1Signal = 1;
315  lep1AntiID = 1;
316  }
317  else if(i == 1){
318  lep2Signal = 1;
319  lep2AntiID = 1;
320  }
321  else if(i == 2){
322  lep3Signal = 1;
323  lep3AntiID = 1;
324  }
325  }
326 
327  } // end loop over nTotalLeps
328 
329  // Event is not relevant for fake estimation!
330  if(nLep_antiID == 0) continue;
331 
332  // Now actually compute the weight!
333  float fbtWeight;
334  ANA_CHECK( ffTool->addEvent(particles) );;
335  ANA_CHECK( ffTool->getEventWeight(fbtWeight, "3T", ">=1F[T]") );
336 
337  // FIXME note that the relevant MC events will need their
338  // FFWeight to be multiplied by -1 here, since these are used
339  // for the prompt MC subtraction
340  FFWeight = fbtWeight;
341  std::cout << "nLep_antiID " << nLep_antiID << std::endl;
342  std::cout << "FFWeight " << FFWeight << std::endl;
343 
344  // Fill the tree
345  outTree_nominal->Fill();
346 
347  // just to keep track
348  ++nEventsPassed;
349 
350  } // end loop over events
351 
352  std::cout << "nEventsProcessed is " << nEventsProcessed << " and nEventsPassed is " << nEventsPassed << std::endl;
353 
354  outFile_nominal->Write();
355 
356  delete chain;
357  delete ffTool;
358  delete outFile_nominal;
359  delete tStore;
360  delete tEvent;
361 
362 #endif
363 
364  return EXIT_SUCCESS;
365 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
READ_TREE_ADDRESSES
#define READ_TREE_ADDRESSES(typeName, name)
Definition: fbtNtupleTester.cxx:37
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
ObjectType
ObjectType
Definition: BaseObject.h:11
makeTOC.inFile
string inFile
Definition: makeTOC.py:5
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
CP::ApplyFakeFactor
Definition: ApplyFakeFactor.h:22
sct_calib_tf.EventNumber
int EventNumber
Definition: sct_calib_tf.py:29
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
CP::BaseLinearFakeBkgTool::getEventWeight
virtual StatusCode getEventWeight(float &weight, const std::string &selection, const std::string &process) override final
returns an event weight addEvent() must have been called before hand.
Definition: BaseLinearFakeBkgTool.cxx:39
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
AthCommonDataStore::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
PlotCalibFromCool.nentries
nentries
Definition: PlotCalibFromCool.py:798
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
SG::Decorator< char >
FullCPAlgorithmsTest_eljob.sample
sample
Definition: FullCPAlgorithmsTest_eljob.py:113
lumiFormat.i
int i
Definition: lumiFormat.py:85
SG::AuxElement::setStore
void setStore(const SG::IConstAuxStore *store)
Set the store associated with this object.
Definition: AuxElement.cxx:241
LArCellNtuple.argv
argv
Definition: LArCellNtuple.py:152
dataset
Definition: dataset.h:27
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
xAOD::Muon
Muon_v1 Muon
Reference the current persistent version:
Definition: Event/xAOD/xAODMuon/xAODMuon/Muon.h:13
xAOD::Electron_v1
Definition: Electron_v1.h:34
Muon
struct TBPatternUnitContext Muon
xAOD::TStore
A relatively simple transient store for objects created in analysis.
Definition: TStore.h:44
ANA_CHECK_SET_TYPE
#define ANA_CHECK_SET_TYPE(TYPE)
set the type for ANA_CHECK to report failures
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:314
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
CP::BaseFakeBkgTool::addEvent
virtual StatusCode addEvent(const xAOD::IParticleContainer &particles, float extraWeight=1.f) override final
supply list of leptons / global variables, internal counters incremented Does not return anything; ev...
Definition: BaseFakeBkgTool.cxx:238
xAOD::TEvent
Tool for accessing xAOD files outside of Athena.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:84
CP::ApplyFakeFactor::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: ApplyFakeFactor.cxx:32