CMS 3D CMS Logo

MCTruth.py
Go to the documentation of this file.
1 from __future__ import print_function
2 from PhysicsTools.PythonAnalysis import *
3 import ROOT
4 from ROOT import gSystem, TFile
5 # prepare the FWLite autoloading mechanism
6 gSystem.Load("libFWCoreFWLite.so")
7 ROOT.FWLiteEnabler.enable()
8 
9 # load the file with the generator output
10 theFile = TFile("generatorOutput.root")
11 
12 events = theFile.Get("Events")
13 
14 # Needed for SetAddress to work right
15 events.GetEntry()
16 
17 # set the buffers for the branches you want to access
18 # 1) create a buffer
19 # 2) open the root branch
20 # 3) connect buffer and branch
21 # example: generator particles
22 source = edm.HepMCProduct()
23 sourceBranch = events.GetBranch(events.GetAlias("source"))
24 sourceBranch.SetAddress(source)
25 
26 # now loop over the events
27 for index in all(events):
28 
29  # update all branches - the buffers are filled automatically
30  # Hint: put all you branches in a list and loop over it
31  sourceBranch.GetEntry(index)
32  events.GetEntry(index,0)
33 
34  # do something with the data
35  genEvent = source.GetEvent();
36  print(genEvent.event_number())
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47