ATLAS Offline Software
simple_lhe_plotter.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 
4 import sys
5 import argparse
6 import glob
7 import math
8 
9 def main():
10 
11  # Grab and parse the arguments for the script
12  parser = argparse.ArgumentParser(description="Make simple plots of an LHE file")
13  parser.add_argument('inputFiles', help='Comma-separated list of input files to sum in plots')
14  args = vars(parser.parse_args(sys.argv[1:]))
15 
16  # Allow people to either separate by commas or with spaces
17  inputfilenames = []
18  for a in args['inputFiles'].split(','):
19  inputfilenames+=glob.glob(a)
20  if len(inputfilenames)==0:
21  print('You must specify at least one input file')
22  return 1
23 
24  # Basic ROOT setup; leave import to here so we've dealt with inputs etc
25  import ROOT
26  ROOT.gROOT.SetBatch(True)
27  ROOT.gErrorIgnoreLevel = ROOT.kError
28 
29  # Set up histograms
30  # Energy and PDG ID of incoming partons
31  e_init_0 = ROOT.TH1D('e_init_0','',100,0,2000)
32  e_init_1 = ROOT.TH1D('e_init_1','',100,0,2000)
33  pdg_init = ROOT.TH1D('pdg_init','',25,0,25)
34  # Hard outgoing partons, just plot pTs
35  pt_hard_0 = ROOT.TH1D('pt_hard_0','',100,0,1000)
36  pt_hard_1 = ROOT.TH1D('pt_hard_1','',100,0,1000)
37  # Generally after the first two, we're into extra partons
38  pt_extra_0 = ROOT.TH1D('pt_extra_0','',100,0,1000)
39  pt_extra_1 = ROOT.TH1D('pt_extra_1','',100,0,1000)
40  pt_extra_2 = ROOT.TH1D('pt_extra_2','',100,0,1000)
41  pt_extra_3 = ROOT.TH1D('pt_extra_3','',100,0,1000)
42  pt_extra_4 = ROOT.TH1D('pt_extra_4','',100,0,1000)
43  pt_extra_5 = ROOT.TH1D('pt_extra_5','',100,0,1000)
44  pdg_extras = ROOT.TH1D('pdg_extras','',100,0,1000)
45  # And LHE multiplicity
46  multip = ROOT.TH1D('mult','',20,0,20)
47  weights = ROOT.TH1D('weights','',100,0,1000)
48  # Array of all the histograms for later writing
49  histograms = [e_init_0,e_init_1,pdg_init,pt_hard_0,pt_hard_1,pt_extra_0,pt_extra_1,pt_extra_2,pt_extra_3,pt_extra_4,pt_extra_5,pdg_extras,multip,weights]
50 
51  # Now loop through input file
52  for inputfilename in inputfilenames:
53  # LHE format documented in https://arxiv.org/pdf/1405.1067.pdf
54  with open(inputfilename,'r') as inputfile:
55  event = False
56  npartons = -1
57  extras = 0
58  for line in inputfile:
59  # Check for the ktdurham cut
60  if 'ktdurham' in line and '=' in line:
61  # Print the matching cut for info
62  print ('Matching cut:',float(line.split()[0]))
63  continue
64  # Check for a comment
65  if len(line.split('#')[0].strip())==0:
66  continue
67  # Check if we have entered an event
68  if not event and '<event>' not in line:
69  continue
70  # Check if we are just starting an event
71  if not event and '<event>' in line:
72  event = True
73  continue
74  # Check if we have finished and are doing something else
75  if '<' in line or '>' in line:
76  event = False
77  npartons = -1
78  continue
79  # Check to parse the first line of the event
80  if npartons<0:
81  npartons = int(line.split()[0])
82  multip.Fill(npartons-2) # Final state multiplicity
83  weight = float(line.split()[2])
84  weights.Fill(weight)
85  extras = npartons-4
86  continue
87  # Deal with the inital state partons first
88  if npartons>extras+2:
89  if npartons>extras+3:
90  e_init_0.Fill( float(line.split()[9]) )
91  else:
92  e_init_1.Fill( float(line.split()[9]) )
93  pdg_init.Fill( abs(int(line.split()[0])) )
94  npartons -= 1
95  continue
96  momentum_x = float(line.split()[6])
97  momentum_y = float(line.split()[7])
98  pt = math.sqrt(momentum_x*momentum_x+momentum_y*momentum_y)
99  # Now deal with hard scatter partons
100  if npartons>extras:
101  if npartons>extras+1:
102  pt_hard_0.Fill(pt)
103  else:
104  pt_hard_1.Fill(pt)
105  npartons -= 1
106  continue
107  # Now we are into the extras
108  if extras==npartons:
109  pt_extra_0.Fill(pt)
110  elif extras-1==npartons:
111  pt_extra_1.Fill(pt)
112  elif extras-2==npartons:
113  pt_extra_2.Fill(pt)
114  elif extras-3==npartons:
115  pt_extra_3.Fill(pt)
116  elif extras-4==npartons:
117  pt_extra_4.Fill(pt)
118  elif extras-5==npartons:
119  pt_extra_5.Fill(pt)
120  pdg_extras.Fill( abs(int(line.split()[0])) )
121  npartons-=1
122  # End of loop over input file lines
123  # Done holding open LHE file
124 
125  # Write all the histograms for future use
126  output_hists = ROOT.TFile.Open('output_hists.root','RECREATE')
127  output_hists.cd()
128  for h in histograms:
129  h.Write(h.GetName())
130  output_hists.Close()
131 
132  return 0
133 
134 if __name__ == "__main__":
135  sys.exit(main())
136 
simple_lhe_plotter.main
def main()
Definition: simple_lhe_plotter.py:9
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
Trk::open
@ open
Definition: BinningType.h:40
Muon::print
std::string print(const MuPatSegment &)
Definition: MuonTrackSteering.cxx:28
readCCLHist.float
float
Definition: readCCLHist.py:83
Trk::split
@ split
Definition: LayerMaterialProperties.h:38