SND@LHC Software
Loading...
Searching...
No Matches
rootUtils Namespace Reference

Classes

class  PyListOfLeaves
 

Functions

 readHists (h, fname, wanted=[], withProj=True)
 
 bookHist (h, key=None, title='', nbinsx=100, xmin=0, xmax=1, nbinsy=0, ymin=0, ymax=1, nbinsz=0, zmin=0, zmax=1)
 
 bookProf (h, key=None, title='', nbinsx=100, xmin=0, xmax=1, ymin=None, ymax=None, option="")
 
 writeHists (h, fname, plusCanvas=False)
 
 bookCanvas (h, key=None, title='', nx=900, ny=600, cx=1, cy=1)
 
 reportError (s)
 
 errorSummary ()
 
 printout (atc, name, Work)
 
 setAttributes (pyl, leaves, printout=False)
 
 container_sizes (sTree, perEvent=False)
 
 stripOffBranches (fout)
 
 checkFileExists (x)
 
 findMaximumAndMinimum (histo)
 
 makeIntegralDistrib (h, key)
 

Function Documentation

◆ bookCanvas()

rootUtils.bookCanvas (   h,
  key = None,
  title = '',
  nx = 900,
  ny = 600,
  cx = 1,
  cy = 1 
)

Definition at line 77 of file rootUtils.py.

77def bookCanvas(h,key=None,title='',nx=900,ny=600,cx=1,cy=1):
78 if key==None :
79 print('missing key')
80 return
81 if key not in h:
82 h[key]=TCanvas(key,title,nx,ny)
83 h[key].Divide(cx,cy)

◆ bookHist()

rootUtils.bookHist (   h,
  key = None,
  title = '',
  nbinsx = 100,
  xmin = 0,
  xmax = 1,
  nbinsy = 0,
  ymin = 0,
  ymax = 1,
  nbinsz = 0,
  zmin = 0,
  zmax = 1 
)

Definition at line 49 of file rootUtils.py.

49def bookHist(h,key=None,title='',nbinsx=100,xmin=0,xmax=1,nbinsy=0,ymin=0,ymax=1,nbinsz=0,zmin=0,zmax=1):
50 if key==None :
51 print('missing key')
52 return
53 rkey = str(key) # in case somebody wants to use integers, or floats as keys
54 if key in h: h[key].Reset()
55 elif nbinsz >0: h[key] = TH3D(rkey,title,nbinsx,xmin,xmax,nbinsy,ymin,ymax,nbinsz,zmin,zmax)
56 elif nbinsy >0: h[key] = TH2D(rkey,title,nbinsx,xmin,xmax,nbinsy,ymin,ymax)
57 else: h[key] = TH1D(rkey,title,nbinsx,xmin,xmax)
58 h[key].SetDirectory(gROOT)

◆ bookProf()

rootUtils.bookProf (   h,
  key = None,
  title = '',
  nbinsx = 100,
  xmin = 0,
  xmax = 1,
  ymin = None,
  ymax = None,
  option = "" 
)

Definition at line 59 of file rootUtils.py.

59def bookProf(h,key=None,title='',nbinsx=100,xmin=0,xmax=1,ymin=None,ymax=None,option=""):
60 if key==None :
61 print('missing key')
62 return
63 rkey = str(key) # in case somebody wants to use integers, or floats as keys
64 if key in h: h[key].Reset()
65 if ymin==None or ymax==None: h[key] = TProfile(key,title,nbinsx,xmin,xmax,option)
66 else: h[key] = TProfile(key,title,nbinsx,xmin,xmax,ymin,ymax,option)
67 h[key].SetDirectory(gROOT)

◆ checkFileExists()

rootUtils.checkFileExists (   x)

Definition at line 185 of file rootUtils.py.

185def checkFileExists(x):
186 if x[0:4] == "/eos": f=gSystem.Getenv("EOSSHIP")+x
187 else: f=x
188 test = TFile.Open(f)
189 if not test:
190 print("input file",f," does not exist. Missing authentication?")
191 os._exit(1)
192 if test.FindObjectAny('cbmsim'):
193 return 'tree'
194 else:
195 return 'ntuple'

◆ container_sizes()

rootUtils.container_sizes (   sTree,
  perEvent = False 
)

Definition at line 116 of file rootUtils.py.

116def container_sizes(sTree,perEvent=False):
117 counter = {}
118 print("name ZipBytes[MB] TotBytes[MB] TotalSize[MB]")
119 counter['total']=[0,0,0]
120 for l in sTree.GetListOfLeaves():
121 b = l.GetBranch()
122 nm = b.GetName()
123 print("%30s :%8.3F %8.3F %8.3F "%(nm,b.GetZipBytes()/1.E6,b.GetTotBytes()/1.E6,b.GetTotalSize()/1.E6))
124 bnm = nm.split('.')[0]
125 if bnm not in counter: counter[bnm]=[0,0,0]
126 counter[bnm][0]+=b.GetZipBytes()/1.E6
127 counter[bnm][1]+=b.GetTotBytes()/1.E6
128 counter[bnm][2]+=b.GetTotalSize()/1.E6
129 counter['total'][0]+=b.GetZipBytes()/1.E6
130 counter['total'][1]+=b.GetTotBytes()/1.E6
131 counter['total'][2]+=b.GetTotalSize()/1.E6
132 print("---> SUMMARY <---------------")
133 N = sTree.GetEntries()/1000.
134 if perEvent:
135 print(" name ZipBytes[kB]/ev TotBytes[kB]/ev TotalSize[kB]/ev")
136 else:
137 print(" name ZipBytes[MB] TotBytes[MB] TotalSize[MB]")
138 sorted_c = sorted(counter.items(), key=operator.itemgetter(1))
139 sorted_c.reverse()
140 for i in range(len(sorted_c)):
141 x = sorted_c[i][0]
142 if perEvent:
143 print("%30s :%8.3F %8.3F %8.3F"%(x,counter[x][0]/N,counter[x][1]/N,counter[x][2]/N))
144 else:
145 print("%30s :%8.3F %8.3F %8.3F"%(x,counter[x][0],counter[x][1],counter[x][2]))
146

◆ errorSummary()

rootUtils.errorSummary ( )

Definition at line 88 of file rootUtils.py.

88def errorSummary():
89 l = sys.modules['__main__'].log
90 if len(l) > 0: "Summary of recorded incidents:"
91 for e in l:
92 print(e,':',l[e])

◆ findMaximumAndMinimum()

rootUtils.findMaximumAndMinimum (   histo)

Definition at line 196 of file rootUtils.py.

196def findMaximumAndMinimum(histo):
197 amin,amax = 1E30, -130
198 nmin,nmax = 0, 0
199 for n in range(1,histo.GetNbinsX()+1):
200 c = histo.GetBinContent(n)
201 if c>amax:
202 amax = c
203 nmax = n
204 if c<amin:
205 amin = c
206 nmin = n
207 return amin,amax,nmin,nmax

◆ makeIntegralDistrib()

rootUtils.makeIntegralDistrib (   h,
  key 
)

Definition at line 208 of file rootUtils.py.

208def makeIntegralDistrib(h,key):
209 name = 'I-'+key
210 h[name]=h[key].Clone(name)
211 h[name].SetTitle('Integral > '+h[key].GetTitle())
212 for n in range(1,h[key].GetNbinsX()+1):
213 if n==1: h[name].SetBinContent(1,h[key].GetSumOfWeights())
214 else: h[name].SetBinContent(n,h[name].GetBinContent(n-1)-h[key].GetBinContent(n-1))

◆ printout()

rootUtils.printout (   atc,
  name,
  Work 
)

Definition at line 93 of file rootUtils.py.

93def printout(atc,name,Work):
94 atc.Update()
95 for x in ['.gif','.eps','.jpg'] :
96 temp = name+x
97 atc.Print(temp)
98 if x!='.jpg':
99 os.system('cp '+temp+' '+Work)
100

◆ readHists()

rootUtils.readHists (   h,
  fname,
  wanted = [],
  withProj = True 
)

Definition at line 15 of file rootUtils.py.

15def readHists(h,fname,wanted=[],withProj=True):
16 if fname[0:4] == "/eos":
17 eospath = gSystem.Getenv("EOSSHIP")+fname
18 f = TFile.Open(eospath)
19 else:
20 f = TFile(fname)
21 for akey in f.GetListOfKeys():
22 name = akey.GetName()
23 try: hname = int(name)
24 except: hname = name
25 if len(wanted)>0:
26 if not hname in wanted: continue
27 obj = akey.ReadObj()
28 cln = obj.Class().GetName()
29 if not cln.find('TCanv')<0:
30 h[hname] = obj.Clone()
31 if cln.find('TH')<0 and cln.find('TP')<0 and cln.find('TE')<0: continue
32 if hname in h:
33 if hasattr(h[hname],"Add"): rc = h[hname].Add(obj)
34 if not rc: print("Error when adding histogram ",hname)
35 else:
36 h[hname] = obj.Clone()
37 if hasattr(h[hname],"GetSumw2N"):
38 if h[hname].GetSumw2N()==0 : h[hname].Sumw2()
39 h[hname].SetDirectory(gROOT)
40 if (cln == 'TH2D' or cln == 'TH2F') and withProj:
41 for p in [ '_projx','_projy']:
42 if type(hname) == type('s'): projname = hname+p
43 else: projname = str(hname)+p
44 if p.find('x')>-1: h[projname] = h[hname].ProjectionX()
45 else : h[projname] = h[hname].ProjectionY()
46 h[projname].SetName(name+p)
47 h[projname].SetDirectory(gROOT)
48 return

◆ reportError()

rootUtils.reportError (   s)

Definition at line 84 of file rootUtils.py.

84def reportError(s):
85 l = sys.modules['__main__'].log
86 if s not in l: l[s]=0
87 l[s]+=1

◆ setAttributes()

rootUtils.setAttributes (   pyl,
  leaves,
  printout = False 
)

Definition at line 101 of file rootUtils.py.

101def setAttributes(pyl,leaves,printout=False):
102 names = {}
103 if printout: print('entries',leaves.GetEntries())
104 for i in range(0,leaves.GetEntries() ) :
105 leaf = leaves.At(i)
106 name = leaf.GetName()
107 if printout: print(name)
108 names[name]=i
109 pyl.__setattr__(name,leaf)
110 return names
111# read back

◆ stripOffBranches()

rootUtils.stripOffBranches (   fout)

Definition at line 147 of file rootUtils.py.

147def stripOffBranches(fout):
148 f = TFile(fout)
149 sTree = f.cbmsim
150 nEvents = sTree.GetEntries()
151 strip = False
152 oldTargetClass = False
153 if sTree.GetBranch("SmearedHits"):
154 sTree.SetBranchStatus("SmearedHits",0)
155 strip = True
156 if sTree.GetBranch("TargetPoint"):
157 if sTree.GetLeaf("cbmroot.Target.TargetPoint.fEmTop"): oldTargetClass = True # old class
158 else:
159 for x in sTree.GetListOfLeaves():
160 if not x.GetName().find("TargetPoint")<0:
161 b = x.GetBranch().GetName()
162 sTree.SetBranchStatus(b,0)
163 strip = True
164 if not strip: return
165 sFile = fout.replace("_rec.root","_recs.root")
166 recf = TFile(sFile,"recreate")
167 if not oldTargetClass: newTree = sTree.CloneTree(-1,'fast')
168 else:
169 newTree = sTree.CloneTree(0)
170 for n in range(nEvents):
171 sTree.GetEntry(n)
172 if oldTargetClass: sTree.TargetPoint.Clear()
173 rc = newTree.Fill()
174 sTree.FitTracks.Delete() # stupid ROOT or whoever, otherwise huge memory leak, does not help
175 sTree.Clear()
176 newTree.AutoSave()
177 f.Close()
178 recf.Close()
179 # should do some sanity checks before deleting old file
180 f = TFile(sFile)
181 sTree = f.cbmsim
182 if nEvents == sTree.GetEntries(): print("looks ok, could be deleted",os.path.abspath('.'))
183 else: print("stripping failed, keep old file",os.path.abspath('.'))
184 # os.system('mv '+sFile +' '+fout)

◆ writeHists()

rootUtils.writeHists (   h,
  fname,
  plusCanvas = False 
)

Definition at line 68 of file rootUtils.py.

68def writeHists(h,fname,plusCanvas=False):
69 f = TFile(fname,'RECREATE')
70 for akey in h:
71 if not hasattr(h[akey],'Class'): continue
72 cln = h[akey].Class().GetName()
73 if not cln.find('TH')<0 or not cln.find('TP')<0 or not cln.find('TE')<0:
74 h[akey].Write()
75 if plusCanvas and not cln.find('TC')<0: h[akey].Write()
76 f.Close()