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