ATLAS Offline Software
Loading...
Searching...
No Matches
normalizator.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4
5#This script normalizes all histograms (except TH2, efficiencies and TProfiles) in a given root file.
6import ROOT
7from ROOT import TFile, TH1
8import sys
9import argparse
10import re
11import os
12import math
13
14
15def browseDir( dir ):
16 #print dir.GetPath().split(':', 1)[-1]
17 path = dir.GetPath().split(':/', 1)[-1]
18 pathdir = dir #.GetDirectory(path)
19 #print pathdir.GetPath()
20 keylist = dir.GetListOfKeys()
21
22 for key in keylist:
23 obj = key.ReadObj()
24
25 #2D histograms cannot be compared, tprofiles and efficiencies should not be normalized; skip
26 if obj.IsA().InheritsFrom(ROOT.TH2.Class()) or obj.IsA().InheritsFrom(ROOT.TProfile.Class()) or "fficiency" in obj.GetName() or 'RecoFraction' in obj.GetName() or 'purity' in obj.GetName() or "PtResol" in obj.GetName() or "PtScale" in obj.GetName() or "Prof" in obj.GetName() or "Fit" in obj.GetName():
27 continue
28
29 if obj.IsA().InheritsFrom(ROOT.TH1.Class()) :
30 #normalize
31 #print '----- ', obj.GetName()
32 ii = 1.*obj.Integral()
33 hname = obj.GetName()
34 if ii>0:
35 obj.Scale(1./ii)
36 #print ' ',hname
37 pathdir.WriteTObject( obj , hname , "WriteDelete" )
38 continue
39
40 if obj.IsA().InheritsFrom(ROOT.TDirectory.Class()):
41 #print obj.GetName()
42 browseDir(obj)
43
44
45
46#======================================================================
47def main( argv ):
48 """
49 Main function to be executed when starting the code.
50 """
51
52 if len( argv ) < 2:
53 print( 'No filename given' )
54 print( 'Usage: python '+argv[0]+' physval_filename' )
55 exit(1)
56
57 filename = argv[1]
58 if not os.path.exists( filename ):
59 print ( 'File not found: ' + filename )
60 exit(1)
61
62 try:
63 infile = ROOT.TFile.Open( filename, 'update' )
64 except:
65 print( 'Cannot open file: {0}'.format( filename ) )
66 exit(1)
67
68 ROOT.gROOT.SetBatch()
69
70 infile.cd("Muons")
71 topDir = infile.GetDirectory("Muons")
72
73 browseDir(topDir)
74 infile.Close()
75
76#======================================================================
77
78if __name__ == "__main__":
79 """
80 Here the code should appear that is executed when running the plotter directly
81 (and not import it in another python file via 'import Plotter')
82 """
83
84 # start main program
85 main( sys.argv )
void print(char *figname, TCanvas *c1)
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
int main()
Definition hello.cxx:18