ATLAS Offline Software
MuonValidation_AddGaussianFitToPulls.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 #author: Dan Mori
4 #Add gaussian curve to pull plots
5 #if plotdir argument given, will output pdfs in that directory of all pull plots
6 #usage: python PullFitMacro.py physval_file.root [plotdir]
7 
8 import ROOT
9 import sys
10 import os
11 import itertools
12 
13 #---------------------------------------------------------------------------
14 
15 def AddGaussian( infile, HistDir, HistName ):
16  hist = infile.GetDirectory(HistDir).Get(HistName)
17  if hist.GetEntries() < 10:
18  print( 'INFO Skipping Fit. NumEntries = {0}: '.format(hist.GetEntries()) + HistName )
19  return
20  gaus2 = ROOT.TF1( "gaus2", "gaus", -2, 2 )
21  gaus5 = ROOT.TF1( "gaus", "gaus", -5, 5 )
22  if int(hist.Fit(gaus2,"RQ")) != 0:
23  print( 'WARNING fit failed for ' + HistName )
24  return
25  hist.GetListOfFunctions().Add(gaus5)
26  f1 = hist.GetFunction("gaus")
27  f1.SetParameters( hist.GetFunction("gaus2").GetParameters() )
28  hist.GetListOfFunctions().Remove( gaus2 )
29  f1.SetLineColor(ROOT.kRed)
30  print('INFO Overwriting histogram: ' + HistDir + '/' + HistName )
31  hist.GetDirectory().WriteTObject( hist, HistName, "Overwrite" )
32 
33 #---------------------------------------------------------------------------
34 def main( args ):
35  if len( args ) > 1:
36  filename = args[1]
37  else:
38  print( 'Usage: python {0} filename'.format( args[0] ) )
39  return
40 
41  if not os.path.exists( filename ):
42  print ( 'File not found: ' + filename )
43  return
44 
45  infile = ROOT.TFile.Open( filename, 'update' )
46 
47  MuonTypes = [ 'All', 'Prompt', 'InFlight', 'NonIsolated' ]
48  PullTypes = [ '', 'ID', 'MS' ]
49  Variables = [ 'phi', 'theta', 'qOverP', 'd0', 'z0' ]
50 
51  for MuonType in MuonTypes:
52  if not infile.Get( 'Muons/' + MuonType ):
53  print( 'INFO TDirectory not found: Muons/' + MuonType )
54  continue
55  AuthDir = infile.Get( 'Muons/{0}/matched'.format( MuonType ) )
56  Authors = [ i.GetName() for i in AuthDir.GetListOfKeys() if AuthDir.Get( i.GetName() ).InheritsFrom( 'TDirectory' ) ]
57  for Author in Authors:
58  DirName = 'Muons/{0}/matched/{1}/Pulls'.format( MuonType, Author )
59  Dir = infile.Get( DirName )
60  if not Dir:
61  print( 'INFO TDirectory not found: ' + DirName )
62  continue
63  for PullType, var in itertools.product( PullTypes, Variables ):
64  HistName = '_'.join( DirName.split('/') ) + '_Pull{0}_'.format(PullType) + var
65  if not Dir.Get( HistName ):
66  print( 'INFO Histogram not found: ' + HistName )
67  continue
68  AddGaussian( infile, DirName, HistName )
69  infile.Close()
70 
71 #---------------------------------------------------------------------------
72 
73 if __name__ == "__main__":
74  ROOT.gROOT.SetBatch()
75  main( sys.argv )
vtune_athena.format
format
Definition: vtune_athena.py:14
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
MuonValidation_AddGaussianFitToPulls.AddGaussian
def AddGaussian(infile, HistDir, HistName)
Definition: MuonValidation_AddGaussianFitToPulls.py:15
Get
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
Definition: comparitor.cxx:179
MuonValidation_AddGaussianFitToPulls.main
def main(args)
Definition: MuonValidation_AddGaussianFitToPulls.py:34
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10