ATLAS Offline Software
LArG4ShowerLibFunctions.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
2 
3 from __future__ import print_function
4 
5 import logging
6 
7 __author__ = 'Radist Morse radist.morse@gmail.com'
8 
9 
10 class FourVector() :
11  def __init__(self) :
12  self.x = 0.0
13  self.y = 0.0
14  self.z = 0.0
15  self.e = 0.0
16 
18  def __init__(self):
19  self.vertex = FourVector()
21  self.rsize = 0.0
22  self.zsize = 0.0
23  self.shower = [] #hits, list of four vectors (which are actually 5-vector in this case)
24 
26  def __init__(self):
27  self.shower = [] #hits, list of four vectors (which are actually 5-vector in this case)
28  self.egen = 0.0
29  self.rsize = 0.0
30  self.zsize = 0.0
31  def __hash__(self): #needed for making a set
32  return hash(self.egen)
33  def __eq__(self,other): #same here
34  return self.egen == other.egen
35 
36 class TestShowerLib() :
37  def __init__(self) :
38  self.library = [] # list of StoredTestShower objs
39  self.detector= ""
40  self.particle= ""
41  self.release= ""
42  self.geometry= ""
43  self.geant= ""
44  self.phys= ""
45  self.comment= ""
46  def fromLibs(self,libs) :
47  for lib in libs :
48  if not isinstance(lib,self.__class__):
49  print ("ERROR: Different types of libs")
50  return False
51  self.detector = libs[0].detector
52  self.particle = libs[0].particle
53  self.release = libs[0].release
54  self.geometry = libs[0].geometry
55  self.geant = libs[0].geant
56  self.phys = libs[0].phys
57  for lib in libs :
58  if ( self.detector != lib.detector or
59  self.particle != lib.particle or
60  self.release != lib.release or
61  self.geometry != lib.geometry or
62  self.geant != lib.geant or
63  self.phys != lib.phys ) :
64  print ("ERROR: DIFFERENT LIBS!!!")
65  return False
66  from datetime import datetime
67  self.comment = "ADDED "+str(datetime.now())
68  for lib in libs :
69  self.library += lib.library
70  return True
71  def readFromFile(self,filename) :
72  from ROOT import TFile
73  tfile = TFile(filename)
74  try :
75  ver = int(tfile.Get("version").GetVal())
76  except Exception:
77  print ("Not a TestShowerLib: Broken file")
78  tfile.Close()
79  return False
80  if (ver != 10) : #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
81  print ("Not a TestShowerLib")
82  tfile.Close()
83  return False
84  meta = tfile.Get("meta")
85  libr = tfile.Get("library")
86  for event in meta :
87  self.detector=str(event.detector)
88  self.particle=str(event.particle)
89  self.release=str(event.release)
90  self.geometry=str(event.geometry)
91  self.geant=str(event.geantVersion)
92  self.phys=str(event.physicsList)
93  self.comment=str(event.comment)
94 
95  state = 0
96 
97  #state == 0 : first line of shower header
98  #state == 1 : second line of sower header
99  #state == 2 : hit
100 
101  for event in libr : #this is quite unclear, but easy to implement
102  if (state == 0) : #first line
103  curShower = StoredTestShower()
104  curShower.vertex.x = event.x
105  curShower.vertex.y = event.y
106  curShower.vertex.z = event.z
107  curShower.zsize = event.time
108  hitsInCurShower = event.e
109  state = 1
110  elif (state == 1) : #shower header
111  curShower.momentum.x = event.x
112  curShower.momentum.y = event.y
113  curShower.momentum.z = event.z
114  curShower.momentum.e = event.e
115  curShower.rsize = event.time
116  if (hitsInCurShower > 0) :
117  state = 2
118  else :
119  self.library.append(curShower)
120  state = 0
121  elif (state == 2) :
122  hit = FourVector()
123  hit.e = event.e
124  hit.x = event.x
125  hit.y = event.y
126  hit.z = event.z
127  hit.time = event.time
128  curShower.shower.append(hit)
129  hitsInCurShower -= 1
130  if (hitsInCurShower == 0) : #last hit
131  self.library.append(curShower)
132  state = 0
133  tfile.Close()
134  if (state != 0) :
135  print ("FILE CORRUPTED!!")
136  return False
137  return True
138  def writeToFile(self,filename) :
139  from ROOT import TFile,TTree,TParameter
140  from ROOT import gROOT, addressof
141  gROOT.ProcessLine(
142  "struct MyMetaStruct {\
143  Char_t detector[40];\
144  Char_t release[40];\
145  Char_t geometry[40];\
146  Char_t geant[40];\
147  Char_t phys[40];\
148  Char_t comment[400];\
149  Int_t particle;\
150  };" )
151  from ROOT import MyMetaStruct
152  gROOT.ProcessLine(
153  "struct MyStruct {\
154  Float_t x;\
155  Float_t y;\
156  Float_t z;\
157  Float_t e;\
158  Float_t time;\
159  };" )
160  from ROOT import MyStruct
161 
162  tfile = TFile(filename,"RECREATE")
163 
164  ver = TParameter(int)("version",10) #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
165  ver.Write("version")
166 
167  meta = TTree()
168  libr = TTree()
169 
170  mmstruct = MyMetaStruct()
171 
172  mmstruct.detector = "%s" % (str(self.detector))
173  mmstruct.particle = int(self.particle)
174  mmstruct.release = "%s" % (str(self.release))
175  mmstruct.geometry = "%s" % (str(self.geometry))
176  mmstruct.geant = "%s" % (str(self.geant))
177  mmstruct.phys = "%s" % (str(self.phys))
178  mmstruct.comment = "%s" % (str(self.comment))
179 
180  meta.Branch("detector",addressof(mmstruct,"detector"),"detector/C")
181  meta.Branch("particle",addressof(mmstruct,"particle"),"particle/I")
182  meta.Branch("release",addressof(mmstruct,"release"),"release/C")
183  meta.Branch("geometry",addressof(mmstruct,"geometry"),"geometry/C")
184  meta.Branch("geantVersion",addressof(mmstruct,"geant"),"geantVersion/C")
185  meta.Branch("physicsList",addressof(mmstruct,"phys"),"physicsList/C")
186  meta.Branch("comment",addressof(mmstruct,"comment"),"physicsList/C")
187 
188  meta.Fill()
189 
190  mstruct = MyStruct()
191 
192  libr.Branch("x",addressof(mstruct,"x"),"x/F")
193  libr.Branch("y",addressof(mstruct,"y"),"y/F")
194  libr.Branch("z",addressof(mstruct,"z"),"z/F")
195  libr.Branch("e",addressof(mstruct,"e"),"e/F")
196  libr.Branch("time",addressof(mstruct,"time"),"time/F")
197 
198  for storedShower in self.library :
199  mstruct.x = storedShower.vertex.x
200  mstruct.y = storedShower.vertex.y
201  mstruct.z = storedShower.vertex.z
202  mstruct.time = storedShower.zsize
203  mstruct.e = len(storedShower.shower)
204  libr.Fill()
205  mstruct.x = storedShower.momentum.x
206  mstruct.y = storedShower.momentum.y
207  mstruct.z = storedShower.momentum.z
208  mstruct.e = storedShower.momentum.e
209  mstruct.time = storedShower.rsize
210  libr.Fill()
211  for hit in storedShower.shower:
212  mstruct.e = hit.e
213  mstruct.x = hit.x
214  mstruct.y = hit.y
215  mstruct.z = hit.z
216  mstruct.time = hit.time
217  libr.Fill()
218  meta.Write("meta")
219  libr.Write("library")
220  tfile.Close()
221  def printInfo(self) :
222  pass
223  def drawHits(self):
224  from ROOT import TH3F
225  from math import sqrt,copysign,log10
226  hits = TH3F("HITS","Hits Distrib",50,1,1000,101,-300,300,100,0,500)
227  containmentZ = TH3F("CONTZ","ContZ Distrib",50,1,1000,101,-300,300,100,0,500)
228  containmentR = TH3F("CONTR","ContR Distrib",50,1,1000,101,-300,300,100,0,500)
229  for storedShower in self.library :
230  containmentR.Fill(log10(storedShower.momentum.e)*333,storedShower.rsize,storedShower.zsize/2,10)
231  containmentR.Fill(log10(storedShower.momentum.e)*333,-storedShower.rsize,storedShower.zsize/2,10)
232  containmentZ.Fill(log10(storedShower.momentum.e)*333,0,storedShower.zsize,10)
233  for hit in storedShower.shower :
234  hits.Fill(log10(storedShower.momentum.e)*333,copysign(sqrt(hit.x*hit.x + hit.y*hit.y),hit.x),hit.z)
235  return hits,containmentZ,containmentR
236 
238  def __init__(self) :
239  self.library = {} # key (float) - eta, value (list) - list of StoredEnergyShower objs
240  self.detector= ""
241  self.particle= ""
242  self.release= ""
243  self.geometry= ""
244  self.geant= ""
245  self.phys= ""
246  self.comment= ""
247  def scaleEnergy(self,scalefactor) :
248  for etabin in self.library.values():
249  for storedShower in etabin :
250  for hit in storedShower.shower :
251  hit.e *= scalefactor
252  self.comment += " SCALED: "+str(scalefactor)
253  def truncate(self,truncate) :
254  showers = []
255  for eta,etabin in self.library.items():
256  for storedShower in etabin :
257  showers += [(eta,storedShower)]
258  if len(showers) <= truncate :
259  print ("WARNING: Size of the library is already less:",truncate,"<",len(showers))
260  return
261  from random import randint
262  while (len(showers) > truncate) :
263  rand = randint(0,len(showers)-1)
264  self.library[showers[rand][0]].remove(showers[rand][1])
265  del showers[rand]
266  return
267  def fromLibs(self,libs) :
268  for lib in libs :
269  if not isinstance(lib,self.__class__):
270  print ("ERROR: Different types of libs")
271  return False
272  self.detector = libs[0].detector
273  self.particle = libs[0].particle
274  self.release = libs[0].release
275  self.geometry = libs[0].geometry
276  self.geant = libs[0].geant
277  self.phys = libs[0].phys
278  self.mineta = libs[0].mineta
279  self.maxeta = libs[0].maxeta
280  self.comment = libs[0].comment
281  etas = set(libs[0].library.keys())
282  for lib in libs :
283  if ( self.detector != lib.detector or
284  self.particle != lib.particle or
285  self.release != lib.release or
286  self.geometry != lib.geometry or
287  self.geant != lib.geant or
288  self.phys != lib.phys or
289  self.mineta != lib.mineta or
290  self.maxeta != lib.maxeta or
291  etas != set(lib.library.keys()) ) :
292  print ("ERROR: DIFFERENT LIBS!!!")
293  return False
294  for lib in libs :
295  for k,v in lib.library.items():
296  self.library.setdefault(k,set()).update(v)
297  for k,v in self.library.items():
298  self.library[k] = list(v)
299  return True
300  def moveEta(self,oldEta,newEta) :
301  if not (oldEta in self.library.keys()) :
302  return False
303  self.library[newEta] = self.library.pop(oldEta)
304  return True
305  def removeEta(self,eta) :
306  if not (eta in self.library.keys()) :
307  return False
308  self.library.pop(eta)
309  return True
310  def readFromFile(self,filename) :
311  from ROOT import TFile
312  #from sets import Set
313  tfile = TFile(filename)
314  try:
315  ver = int(tfile.Get("version").GetVal())
316  except Exception:
317  print ("Not an EtaEnergyLib: Broken file")
318  tfile.Close()
319  return False
320 
321  if (ver != 1) : #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
322  print ("Not an EtaEnergyLib")
323  tfile.Close()
324  return False
325  meta = tfile.Get("meta")
326  libr = tfile.Get("library")
327 
328  for event in meta :
329  self.detector=str(event.detector)
330  self.particle=str(event.particle)
331  self.release=str(event.release)
332  self.geometry=str(event.geometry)
333  self.geant=str(event.geantVersion)
334  self.phys=str(event.physicsList)
335  self.comment=str(event.comment)
336 
337  state = 0
338  lastShower = False
339 
340  for event in libr : #this is quite unclear, but easy to implement
341  if (state == 0) : #eta bin header
342  showersInCurEta = event.x
343  curEta = round(event.y,4)
344  self.mineta = event.z
345  self.maxeta = event.e
346  self.library[curEta] = []
347  if (showersInCurEta > 0) :
348  state = 1 #go to shower header
349  elif (state == 1) : #shower header
350  hitsInCurShower = event.x
351  rSize = event.y
352  zSize = event.z
353  genEnergy = event.e
354  showersInCurEta -= 1
355  if (showersInCurEta == 0) : #last shower
356  lastShower = True
357  curShower = StoredEnergyShower()
358  curShower.egen = genEnergy
359  curShower.rsize = rSize
360  curShower.zsize = zSize
361  #curShower["hits"] = []
362  if (hitsInCurShower > 0) :
363  state = 2 #go to hits
364  else : #empty shower
365  self.library[curEta].append(curShower)
366  if (lastShower) : #special case of last shower in bin being the empty one
367  lastShower = False
368  state = 0 #next bin
369  elif (state == 2) :
370  hit = FourVector()
371  hit.e = event.e
372  hit.x = event.x
373  hit.y = event.y
374  hit.z = event.z
375  hit.time = event.time
376  curShower.shower.append(hit)
377  hitsInCurShower -= 1
378  if (hitsInCurShower == 0) : #last hit
379  self.library[curEta].append(curShower)
380  if (lastShower) : # end of eta bin
381  lastShower = False
382  state = 0
383  else : #not yet
384  state = 1
385  tfile.Close()
386  if (state != 0) :
387  print ("FILE CORRUPTED!!")
388  return False
389  return True
390  def writeToFile(self,filename) :
391  from ROOT import TFile,TTree,TParameter
392  from ROOT import gROOT, addressof
393  gROOT.ProcessLine(
394  "struct MyMetaStruct {\
395  Char_t detector[40];\
396  Char_t release[40];\
397  Char_t geometry[40];\
398  Char_t geant[40];\
399  Char_t phys[40];\
400  Char_t comment[400];\
401  Int_t particle;\
402  };" )
403  from ROOT import MyMetaStruct
404  gROOT.ProcessLine(
405  "struct MyStruct {\
406  Float_t x;\
407  Float_t y;\
408  Float_t z;\
409  Float_t e;\
410  Float_t time;\
411  };" )
412  from ROOT import MyStruct
413 
414  tfile = TFile(filename,"RECREATE")
415 
416  ver = TParameter(int)("version",1) #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
417  ver.Write("version")
418 
419  meta = TTree()
420  libr = TTree()
421 
422  mmstruct = MyMetaStruct()
423 
424  mmstruct.detector = "%s" % (str(self.detector))
425  mmstruct.particle = int(self.particle)
426  mmstruct.release = "%s" % (str(self.release))
427  mmstruct.geometry = "%s" % (str(self.geometry))
428  mmstruct.geant = "%s" % (str(self.geant))
429  mmstruct.phys = "%s" % (str(self.phys))
430  mmstruct.comment = "%s" % (str(self.comment))
431 
432  meta.Branch("detector",addressof(mmstruct,"detector"),"detector/C")
433  meta.Branch("particle",addressof(mmstruct,"particle"),"particle/I")
434  meta.Branch("release",addressof(mmstruct,"release"),"release/C")
435  meta.Branch("geometry",addressof(mmstruct,"geometry"),"geometry/C")
436  meta.Branch("geantVersion",addressof(mmstruct,"geant"),"geantVersion/C")
437  meta.Branch("physicsList",addressof(mmstruct,"phys"),"physicsList/C")
438  meta.Branch("comment",addressof(mmstruct,"comment"),"physicsList/C")
439 
440  meta.Fill()
441 
442  mstruct = MyStruct()
443 
444  libr.Branch("x",addressof(mstruct,"x"),"x/F")
445  libr.Branch("y",addressof(mstruct,"y"),"y/F")
446  libr.Branch("z",addressof(mstruct,"z"),"z/F")
447  libr.Branch("e",addressof(mstruct,"e"),"e/F")
448  libr.Branch("time",addressof(mstruct,"time"),"time/F")
449 
450  etas = self.library.keys()
451 
452  for eta in sorted(etas) :
453  mstruct.x = len(self.library[eta])
454  mstruct.y = eta
455  mstruct.z = self.mineta
456  mstruct.e = self.maxeta
457  mstruct.time = 0
458  libr.Fill()
459  self.library[eta].sort(key=lambda x: x.egen)
460  for storedShower in self.library[eta] :
461  mstruct.x = len(storedShower.shower)
462  mstruct.y = storedShower.rsize
463  mstruct.z = storedShower.zsize
464  mstruct.e = storedShower.egen
465  mstruct.time = 0
466  libr.Fill()
467  for hit in storedShower.shower:
468  mstruct.e = hit.e
469  mstruct.x = hit.x
470  mstruct.y = hit.y
471  mstruct.z = hit.z
472  mstruct.time = hit.time
473  libr.Fill()
474  meta.Write("meta")
475  libr.Write("library")
476  tfile.Close()
477  def printInfo(self) :
478  print ("VERSION: EtaEnergyLib","PARTICLE:",self.particle,"DETECTOR:",self.detector)
479  print (self.release, self.geometry, self.geant, self.phys)
480  print (self.comment)
481  ebins = [1,2,5,10,20,50,100,200,500,1000]
482  etas = sorted(self.library.keys())
483  print ("Number of etabins:",str(len(etas)))
484  print ("MinEta:",self.mineta,"MaxEta:",self.maxeta)
485  fstot = 0
486  for eta in etas :
487  fstot +=len(self.library[eta])
488  print ("Number of showers:",str(fstot))
489  print ("-"*(12+len(ebins)*8)) #horizontal line
490  infostr = "|etas|ebins|"
491  for ebin in ebins : #header for energy bins
492  infostr += ("<%d" %ebin).rjust(7) #str(ebin).rjust(7)
493  infostr += "|"
494  print (infostr)
495  print ("-"*(12+len(ebins)*8)) #horizontal line
496  for etalow,etahigh in zip(etas,(etas[1:] + [self.maxeta])) : #looping over eta bins
497  prevebin = 0
498  erec = {}
499  egen = {}
500  hits = {}
501  count = {}
502  for ebin in ebins : # for all energy bins
503  count[ebin] = 0
504  erec[ebin] = 0.
505  egen[ebin] = 0.
506  hits[ebin] = 0.
507  for shower in self.library[etalow] :
508  if (shower.egen <= ebin) and (shower.egen > prevebin) :
509  count[ebin] += 1
510  egenshow = shower.egen
511  erecshow = 0
512  for hit in shower.shower :
513  erecshow += hit.e
514  erec[ebin] += erecshow
515  egen[ebin] += egenshow
516  hits[ebin] += len(shower.shower)
517  if (count[ebin] > 0) :
518  hits[ebin] /= count[ebin]
519  prevebin = ebin
520  infostr = "|#" # |
521  infostr+= str(round(etalow,5)).rjust(9) # | eta header
522  infostr+= "|" # |\
523  infostr2 = "|Hits"
524  infostr2+= str(round(etahigh,3)).rjust(6) # | eta header
525  infostr2+= "|" # |
526  infostr3 = "|ErecEgen"
527  infostr3+= " ".rjust(2) # | eta header
528  infostr3+= "|" # |
529  for ebin in ebins :
530  infostr+= str(count[ebin]).rjust(7) #print the number of showers
531  if (egen[ebin] > 0) :
532  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
533  infostr3+= ("%.5f" %(erec[ebin]/egen[ebin])).rjust(7)
534  else :
535  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
536  infostr3+= "0.0".rjust(7) #else print "xxx"
537  infostr+="|"
538  infostr2+="|"
539  infostr3+="|"
540  print (infostr)
541  print (infostr2)
542  print (infostr3)
543  print ("-"*(12+len(ebins)*8)) #horizontal line
544  def drawHits(self):
545  from ROOT import TH3F
546  from math import sqrt,copysign,log10
547  hits = TH3F("HITS","Hits Distrib",50,1,1000,101,-300,300,100,0,500)
548  containmentZ = TH3F("CONTZ","ContZ Distrib",50,1,1000,101,-300,300,100,0,500)
549  containmentR = TH3F("CONTR","ContR Distrib",50,1,1000,101,-300,300,100,0,500)
550  for etabin in self.library.values():
551  for storedShower in etabin :
552  containmentR.Fill(log10(storedShower.egen)*333,storedShower.rsize,storedShower.zsize,10)
553  containmentR.Fill(log10(storedShower.egen)*333,-storedShower.rsize,storedShower.zsize,10)
554  containmentZ.Fill(log10(storedShower.egen)*333,0,storedShower.zsize,10)
555  for hit in storedShower.shower :
556  hits.Fill(log10(storedShower.egen)*333,copysign(sqrt(hit.x*hit.x + hit.y*hit.y),hit.x),hit.z)
557  return hits,containmentZ,containmentR
558 
560  def __init__(self) :
561  self.library = {} # key (float) - dist, value (list) - list of StoredEnergyShower objs
562  self.detector= ""
563  self.particle= ""
564  self.release= ""
565  self.geometry= ""
566  self.geant= ""
567  self.phys= ""
568  self.comment= ""
569  self.xrod_cent = 0.0
570  self.yrod_cent = 0.0
571  self.step = 0.0
572  def scaleEnergy(self,scalefactor) :
573  for distbin in self.library.values():
574  for storedShower in distbin :
575  for hit in storedShower.shower :
576  hit.e *= scalefactor
577  self.comment += " SCALED: "+str(scalefactor)
578  def truncate(self,truncate) :
579  showers = []
580  for dist,distbin in self.library.items():
581  for storedShower in distbin :
582  showers += [(dist,storedShower)]
583  if len(showers) <= truncate :
584  print ("WARNING: Size of the library is already less:",truncate,"<",len(showers))
585  return
586  from random import randint
587  while (len(showers) > truncate) :
588  rand = randint(0,len(showers)-1)
589  self.library[showers[rand][0]].remove(showers[rand][1])
590  del showers[rand]
591  return
592  def moveDist(self,oldDist,newDist) :
593  if not (oldDist in self.library.keys()) :
594  return False
595  self.library[newDist] = self.library.pop(oldDist)
596  return True
597  def removeDist(self,dist) :
598  if not (dist in self.library.keys()) :
599  return False
600  self.library.pop(dist)
601  return True
602  def fromLibs(self,libs) :
603  for lib in libs :
604  if not isinstance(lib,self.__class__):
605  print ("ERROR: Different types of libs")
606  return False
607  self.detector = libs[0].detector
608  self.particle = libs[0].particle
609  self.release = libs[0].release
610  self.geometry = libs[0].geometry
611  self.geant = libs[0].geant
612  self.phys = libs[0].phys
613  self.comment = libs[0].comment
614  self.xrod_cent = libs[0].xrod_cent
615  self.yrod_cent = libs[0].yrod_cent
616  self.step = libs[0].step
617  dists = set(libs[0].library.keys())
618  for lib in libs :
619  if ( self.detector != lib.detector or
620  self.particle != lib.particle or
621  self.release != lib.release or
622  self.geometry != lib.geometry or
623  self.geant != lib.geant or
624  self.phys != lib.phys or
625  self.xrod_cent != lib.xrod_cent or
626  self.yrod_cent != lib.yrod_cent or
627  self.step != lib.step or
628  dists != set(lib.library.keys()) ) :
629  print ("ERROR: DIFFERENT LIBS!!!")
630  return False
631  for lib in libs :
632  for k,v in lib.library.items():
633  self.library.setdefault(k,set()).update(v)
634  for k,v in self.library.items():
635  self.library[k] = list(v)
636  return True
637  def readFromFile(self,filename) :
638  from ROOT import TFile
639  #from sets import Set
640  tfile = TFile(filename)
641  try:
642  ver = int(tfile.Get("version").GetVal())
643  except Exception:
644  print ("Not an FCALDistEnergyLib: Broken file")
645  tfile.Close()
646  return False
647  if (ver != 4) : #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
648  print ("Not an FCALDistEnergyLib")
649  tfile.Close()
650  return False
651  meta = tfile.Get("meta")
652  libr = tfile.Get("library")
653 
654  for event in meta :
655  self.detector=str(event.detector)
656  self.particle=str(event.particle)
657  self.release=str(event.release)
658  self.geometry=str(event.geometry)
659  self.geant=str(event.geantVersion)
660  self.phys=str(event.physicsList)
661  self.comment=str(event.comment)
662 
663  state = -1
664  lastShower = False
665 
666  for event in libr : #this is quite unclear, but easy to implement
667  if (state == -1) : #library header (calculator parameters)
668  self.xrod_cent = event.x
669  self.yrod_cent = event.y
670  self.step = event.z
671  state = 0
672  elif (state == 0) : #eta bin header
673  showersInCurDist = event.x
674  curDist = round(event.y,4)
675  self.library[curDist] = []
676  if (showersInCurDist > 0) :
677  state = 1 #go to shower header
678  elif (state == 1) : #shower header
679  hitsInCurShower = event.x
680  rSize = event.y
681  zSize = event.z
682  genEnergy = event.e
683  showersInCurDist -= 1
684  if (showersInCurDist == 0) : #last shower
685  lastShower = True
686  curShower = StoredEnergyShower()
687  curShower.egen = genEnergy
688  curShower.rsize = rSize
689  curShower.zsize = zSize
690  #curShower["hits"] = []
691  if (hitsInCurShower > 0) :
692  state = 2 #go to hits
693  else : #empty shower
694  self.library[curDist].append(curShower)
695  if (lastShower) : #special case of last shower in bin being the empty one
696  lastShower = False
697  state = 0 #next bin
698  elif (state == 2) :
699  hit = FourVector()
700  hit.e = event.e
701  hit.x = event.x
702  hit.y = event.y
703  hit.z = event.z
704  hit.time = event.time
705  curShower.shower.append(hit)
706  hitsInCurShower -= 1
707  if (hitsInCurShower == 0) : #last hit
708  self.library[curDist].append(curShower)
709  if (lastShower) : # end of eta bin
710  lastShower = False
711  state = 0
712  else : #not yet
713  state = 1
714  tfile.Close()
715  if (state != 0) :
716  print ("FILE CORRUPTED!!")
717  return False
718  return True
719  def writeToFile(self,filename) :
720  from ROOT import TFile,TTree,TParameter
721  from ROOT import gROOT, addressof
722  gROOT.ProcessLine(
723  "struct MyMetaStruct {\
724  Char_t detector[40];\
725  Char_t release[40];\
726  Char_t geometry[40];\
727  Char_t geant[40];\
728  Char_t phys[40];\
729  Char_t comment[400];\
730  Int_t particle;\
731  };" )
732  from ROOT import MyMetaStruct
733  gROOT.ProcessLine(
734  "struct MyStruct {\
735  Float_t x;\
736  Float_t y;\
737  Float_t z;\
738  Float_t e;\
739  Float_t time;\
740  };" )
741  from ROOT import MyStruct
742 
743  tfile = TFile(filename,"RECREATE")
744 
745  ver = TParameter(int)("version",4) #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
746  ver.Write("version")
747 
748  meta = TTree()
749  libr = TTree()
750 
751  mmstruct = MyMetaStruct()
752 
753  mmstruct.detector = "%s" % (str(self.detector))
754  mmstruct.particle = int(self.particle)
755  mmstruct.release = "%s" % (str(self.release))
756  mmstruct.geometry = "%s" % (str(self.geometry))
757  mmstruct.geant = "%s" % (str(self.geant))
758  mmstruct.phys = "%s" % (str(self.phys))
759  mmstruct.comment = "%s" % (str(self.comment))
760 
761  meta.Branch("detector",addressof(mmstruct,"detector"),"detector/C")
762  meta.Branch("particle",addressof(mmstruct,"particle"),"particle/I")
763  meta.Branch("release",addressof(mmstruct,"release"),"release/C")
764  meta.Branch("geometry",addressof(mmstruct,"geometry"),"geometry/C")
765  meta.Branch("geantVersion",addressof(mmstruct,"geant"),"geantVersion/C")
766  meta.Branch("physicsList",addressof(mmstruct,"phys"),"physicsList/C")
767  meta.Branch("comment",addressof(mmstruct,"comment"),"physicsList/C")
768 
769  meta.Fill()
770 
771  mstruct = MyStruct()
772 
773  libr.Branch("x",addressof(mstruct,"x"),"x/F")
774  libr.Branch("y",addressof(mstruct,"y"),"y/F")
775  libr.Branch("z",addressof(mstruct,"z"),"z/F")
776  libr.Branch("e",addressof(mstruct,"e"),"e/F")
777  libr.Branch("time",addressof(mstruct,"time"),"time/F")
778 
779  mstruct.x = self.xrod_cent
780  mstruct.y = self.yrod_cent
781  mstruct.z = self.step
782  mstruct.e = 0
783  mstruct.time = 0
784  libr.Fill()
785 
786  dists = sorted(self.library.keys())
787 
788  for dist in dists :
789  mstruct.x = len(self.library[dist])
790  mstruct.y = dist
791  mstruct.z = 0
792  mstruct.e = 0
793  mstruct.time = 0
794  libr.Fill()
795  self.library[dist].sort(key=lambda x: x.egen)
796  for storedShower in self.library[dist] :
797  mstruct.x = len(storedShower.shower)
798  mstruct.y = storedShower.rsize
799  mstruct.z = storedShower.zsize
800  mstruct.e = storedShower.egen
801  mstruct.time = 0
802  libr.Fill()
803  for hit in storedShower.shower:
804  mstruct.e = hit.e
805  mstruct.x = hit.x
806  mstruct.y = hit.y
807  mstruct.z = hit.z
808  mstruct.time = hit.time
809  libr.Fill()
810  meta.Write("meta")
811  libr.Write("library")
812  tfile.Close()
813  def printInfo(self) :
814  print ("VERSION: FCALDistEnergyLib","PARTICLE:",self.particle,"DETECTOR:",self.detector)
815  print (self.release, self.geometry, self.geant, self.phys)
816  print ("xrodcent:",self.xrod_cent,"yrodcent:",self.yrod_cent,"step:",self.step)
817  print (self.comment)
818  ebins = [1,2,5,10,20,50,100,200,500,1000]
819  dists = sorted(self.library.keys())
820  print ("Number of etabins:",str(len(dists)))
821  fstot = 0
822  for dist in dists :
823  fstot +=len(self.library[dist])
824  print ("Number of showers:",str(fstot))
825  print ("-"*(13+len(ebins)*8)) #horizontal line
826  infostr = "|dists|ebins|"
827  for ebin in ebins : #header for energy bins
828  infostr += ("<%d" %ebin).rjust(7) #str(ebin).rjust(7)
829  infostr += "|"
830  print (infostr)
831  print ("-"*(13+len(ebins)*8)) #horizontal line
832  for distlow,disthigh in zip(dists,(dists[1:] + [4.5])) : #looping over eta bins
833  prevebin = 0
834  erec = {}
835  egen = {}
836  hits = {}
837  count = {}
838  for ebin in ebins : # for all energy bins
839  count[ebin] = 0
840  erec[ebin] = 0.
841  egen[ebin] = 0.
842  hits[ebin] = 0.
843  for shower in self.library[distlow] :
844  if (shower.egen <= ebin) and (shower.egen > prevebin) :
845  count[ebin] += 1
846  egenshow = shower.egen
847  erecshow = 0
848  for hit in shower.shower :
849  erecshow += hit.e
850  erec[ebin] += erecshow
851  egen[ebin] += egenshow
852  hits[ebin] += len(shower.shower)
853  if (count[ebin] > 0) :
854  hits[ebin] /= count[ebin]
855  prevebin = ebin
856  infostr = "|#" # |
857  infostr+= str(round(distlow,5)).rjust(10) # | eta header
858  infostr+= "|" # |\
859  infostr2 = "|Hits"
860  infostr2+= str(round(disthigh,3)).rjust(7) # | eta header
861  infostr2+= "|" # |
862  infostr3 = "|ErecEgen"
863  infostr3+= " ".rjust(3) # | eta header
864  infostr3+= "|" # |
865  for ebin in ebins :
866  infostr+= str(count[ebin]).rjust(7) #print the number of showers
867  if (egen[ebin] > 0) :
868  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
869  infostr3+= ("%.5f" %(erec[ebin]/egen[ebin])).rjust(7)
870  else :
871  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
872  infostr3+= "0.0".rjust(7) #else print "xxx"
873  infostr+="|"
874  infostr2+="|"
875  infostr3+="|"
876  print (infostr)
877  print (infostr2)
878  print (infostr3)
879  print ("-"*(12+len(ebins)*8)) #horizontal line
880  def drawHits(self):
881  from ROOT import TH3F
882  from math import sqrt,copysign,log10
883  hits = TH3F("HITS","Hits Distrib",50,1,1000,101,-300,300,100,0,500)
884  containmentZ = TH3F("CONTZ","ContZ Distrib",50,1,1000,101,-300,300,100,0,500)
885  containmentR = TH3F("CONTR","ContR Distrib",50,1,1000,101,-300,300,100,0,500)
886  for distbin in self.library.values():
887  for storedShower in distbin :
888  containmentR.Fill(log10(storedShower.egen)*333,storedShower.rsize,storedShower.zsize,10)
889  containmentR.Fill(log10(storedShower.egen)*333,-storedShower.rsize,storedShower.zsize,10)
890  containmentZ.Fill(log10(storedShower.egen)*333,0,storedShower.zsize,10)
891  for hit in storedShower.shower :
892  hits.Fill(log10(storedShower.egen)*333,copysign(sqrt(hit.x*hit.x + hit.y*hit.y),hit.x),hit.z)
893  return hits,containmentZ,containmentR
894 
896 
897  def __init__(self) :
898  self.library = {} # key (float) - eta, value (dict), key - dist, value - (list) list of StoredEnergyShower objs
899  self.detector= ""
900  self.particle= ""
901  self.release= ""
902  self.geometry= ""
903  self.geant= ""
904  self.phys= ""
905  self.comment= ""
906  self.xrod_cent = 0.0
907  self.yrod_cent = 0.0
908  self.step = 0.0
909  def scaleEnergy(self,scalefactor) :
910  for etabin in self.library.values():
911  for distbin in etabin.values():
912  for storedShower in distbin :
913  for hit in storedShower.shower :
914  hit.e *= scalefactor
915  self.comment += " SCALED: "+str(scalefactor)
916  def truncate(self,truncate,nShowersMin=0) :
917  log = logging.getLogger("FCALDistEtaShowerLib::truncate()")
918  showers = []
919  for eta,etabin in self.library.items():
920  for dist,distbin in etabin.items():
921  log.info("Number of showers in %s %s is %d",str(eta),str(dist),len(distbin))
922  for storedShower in distbin :
923  showers += [(eta, dist, storedShower)]
924  log.info("total number of showers: %d", len(showers))
925  if nShowersMin:
926  log.info("will not remove from eta-dist bins with less then %d showers", nShowersMin)
927  if len(showers) <= truncate :
928  log.warning("Size of the library is already less: %d < %d",truncate,len(showers))
929  return
930  from random import randint
931  while (len(showers) > truncate) :
932  rand = randint(0,len(showers)-1)
933  if len(self.library[showers[rand][0]][showers[rand][1]]) < nShowersMin:
934  continue
935  self.library[showers[rand][0]][showers[rand][1]].remove(showers[rand][2])
936  del showers[rand]
937  return
938  def moveDist(self,oldDist,newDist) :
939  rez = False
940  for eta,etabin in self.library.items():
941  if (oldDist in etabin.keys()) :
942  etabin[newDist] = etabin.pop(oldDist)
943  rez=True
944  return rez
945  def moveEta(self,oldEta,newEta) :
946  if not (oldEta in self.library.keys()) :
947  return False
948  self.library[newEta] = self.library.pop(oldEta)
949  return True
950  def removeDist(self,dist) :
951  rez = False
952  for eta,etabin in self.library.items():
953  if (dist in etabin.keys()) :
954  self.library.pop(dist)
955  rez=True
956  return rez
957  def removeEta(self,eta) :
958  if not (eta in self.library.keys()) :
959  return False
960  self.library.pop(eta)
961  return True
962  def fromLibs(self,libs) :
963  for lib in libs :
964  if not isinstance(lib,self.__class__):
965  print ("ERROR: Different types of libs")
966  return False
967  self.detector = libs[0].detector
968  self.particle = libs[0].particle
969  self.release = libs[0].release
970  self.geometry = libs[0].geometry
971  self.geant = libs[0].geant
972  self.phys = libs[0].phys
973  self.comment = libs[0].comment
974  self.xrod_cent = libs[0].xrod_cent
975  self.yrod_cent = libs[0].yrod_cent
976  self.step = libs[0].step
977  etas = set(libs[0].library.keys())
978  for lib in libs :
979  if ( self.detector != lib.detector or
980  self.particle != lib.particle or
981  self.release != lib.release or
982  self.geometry != lib.geometry or
983  self.geant != lib.geant or
984  self.phys != lib.phys or
985  self.xrod_cent != lib.xrod_cent or
986  self.yrod_cent != lib.yrod_cent or
987  self.step != lib.step or
988  etas != set(lib.library.keys()) ) :
989  print ("ERROR: DIFFERENT LIBS!!!")
990  return False
991  for eta in libs[0].library.keys() :
992  if (set(libs[0].library[eta].keys()) != set(lib.library[eta].keys())) :
993  print ("ERROR: DIFFERENT LIBS!!!")
994  return False
995  for lib in libs :
996  for k,v in lib.library.items():
997  for ki,vi in v.items():
998  self.library.setdefault(k,dict()).setdefault(ki,set()).update(vi)
999  for k,v in self.library.items():
1000  for ki,vi in v.items():
1001  self.library[k][ki] = list(vi)
1002  return True
1003  def readFromFile(self,filename) :
1004  log = logging.getLogger("FCALDistEtaShowerLib::readFromFile()")
1005  from ROOT import TFile
1006  #from sets import Set
1007  tfile = TFile(filename)
1008  try:
1009  ver = int(tfile.Get("version").GetVal())
1010  except Exception:
1011  print ("Not an FCALDistEtaEnergyLib: Broken file")
1012  tfile.Close()
1013  return False
1014 
1015  if (ver != 5) : #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
1016  print ("Not an FCALDistEtaEnergyLib")
1017  tfile.Close()
1018  return False
1019  meta = tfile.Get("meta")
1020  libr = tfile.Get("library")
1021 
1022  for event in meta :
1023  self.detector=str(event.detector)
1024  self.particle=str(event.particle)
1025  self.release=str(event.release)
1026  self.geometry=str(event.geometry)
1027  self.geant=str(event.geantVersion)
1028  self.phys=str(event.physicsList)
1029  self.comment=str(event.comment)
1030 
1031  state = -1
1032  lastShower = False
1033  lastEta = False
1034 
1035  log.debug("dector: %s", str(event.detector))
1036  log.debug("particle: %s", str(event.particle))
1037  log.debug("release: %s", str(event.release))
1038  log.debug("geometry: %s", str(event.geometry))
1039  log.debug("geant ver: %s", str(event.geantVersion))
1040  log.debug("physList: %s", str(event.physicsList))
1041  log.debug("comment: %s", str(event.comment))
1042 
1043  for event in libr : #this is quite unclear, but easy to implement, we change the "state" depending on what we are reading
1044  log.debug("-------")
1045  log.debug("x=%f, y=%f, z=%f, e=%f",event.x,event.y,event.z,event.e)
1046  log.debug("beginnnig ev loop. lastShower: %s",str(lastShower))
1047  log.debug("beginnnig ev loop. state: %s",str(state))
1048 
1049  if (state == -1) : #library header (calculator parameters)
1050  log.debug("in state=-1")
1051 
1052  self.xrod_cent = event.x
1053  self.yrod_cent = event.y
1054  self.step = event.z
1055  state = 0
1056  elif (state == 0) : #eta bin header
1057  log.debug("in state=0")
1058  log.debug("x=distsInCurEta, y=curEta")
1059 
1060  distsInCurEta = event.x
1061  curEta = round(event.y,4)
1062  self.library[curEta] = {}
1063  if (distsInCurEta > 0) :
1064  state = 1 #go to dist header
1065  elif (state == 1) :
1066  log.debug("in state=1")
1067  log.debug("x=showersInCurDist, y=curDist")
1068 
1069  showersInCurDist = event.x
1070  curDist = round(event.y,4)
1071  self.library[curEta][curDist] = []
1072  distsInCurEta -= 1
1073  if (distsInCurEta == 0) :
1074  lastEta = True
1075  if (showersInCurDist > 0) :
1076  state = 2 #go to shower header
1077  else : #empty dist bin
1078  if (lastEta) : #special case of last eta bin being the empty one
1079  lastEta = False
1080  state = 0
1081  elif (state == 2) :
1082  # writing shower info
1083  log.debug("in state=2")
1084  log.debug("x=hitsInCurShower, y=curShower.rSize, z=curShower.zSize, e=curShower.genEnergy")
1085 
1086  hitsInCurShower = event.x
1087  rSize = event.y
1088  zSize = event.z
1089  genEnergy = event.e
1090  showersInCurDist -= 1
1091  if (showersInCurDist == 0) : #last shower
1092  lastShower = True
1093  curShower = StoredEnergyShower()
1094  curShower.egen = genEnergy
1095  curShower.rsize = rSize
1096  curShower.zsize = zSize
1097  #curShower["hits"] = []
1098  if (hitsInCurShower > 0) :
1099  state = 3 #go to hits
1100  else : #empty shower
1101  log.debug("Appending shower to lib pos %s %s",curEta,curDist)
1102 
1103  self.library[curEta][curDist].append(curShower)
1104  if (lastShower) : #special case of last shower in bin being the empty one
1105  lastShower = False
1106  if (lastEta) : #double special case: last shower in last eta bin is empty
1107  lastEta = False
1108  state = 0 #next eta bin
1109  else :
1110  state = 1 #next dist bin
1111  elif (state == 3) :
1112 
1113  log.debug("in state=3")
1114  log.debug("x=hit.x, y=hit.y, z=hit.z, e=hit.e")
1115 
1116  hit = FourVector()
1117  hit.e = event.e
1118  hit.x = event.x
1119  hit.y = event.y
1120  hit.z = event.z
1121  hit.time = event.time
1122  curShower.shower.append(hit)
1123  hitsInCurShower -= 1
1124  if (hitsInCurShower == 0) : #last hit
1125  log.debug("Appending shower+hit to lib pos %s %s",curEta,curDist)
1126 
1127  self.library[curEta][curDist].append(curShower)
1128  if (lastShower) : # end of dist bin
1129  lastShower = False
1130  if (lastEta) : #end of eta bin as well
1131  lastEta = False
1132  state = 0 #next eta bin
1133  else :
1134  state = 1 #next dist bin
1135  else : #not yet
1136  state = 2
1137 
1138  log.debug("ending ev loop. lastShower: %s", lastShower)
1139  log.debug("ending ev loop. state %s", state)
1140  if log.root.level == logging.DEBUG:
1141  input("Continue? Press Enter.")
1142 
1143  tfile.Close()
1144  if (state != 0) : #the last entry should be the last hit of the last shower in the last bin. if not - file is corrupted
1145  print ("FILE CORRUPTED!!")
1146  return False
1147  return True
1148  def writeToFile(self,filename) :
1149  from ROOT import TFile,TTree,TParameter
1150  from ROOT import gROOT, addressof
1151  gROOT.ProcessLine(
1152  "struct MyMetaStruct {\
1153  Char_t detector[40];\
1154  Char_t release[40];\
1155  Char_t geometry[40];\
1156  Char_t geant[40];\
1157  Char_t phys[40];\
1158  Char_t comment[400];\
1159  Int_t particle;\
1160  };" )
1161  from ROOT import MyMetaStruct
1162  gROOT.ProcessLine(
1163  "struct MyStruct {\
1164  Float_t x;\
1165  Float_t y;\
1166  Float_t z;\
1167  Float_t e;\
1168  Float_t time;\
1169  };" )
1170  from ROOT import MyStruct
1171 
1172  tfile = TFile(filename,"RECREATE")
1173 
1174  ver = TParameter(int)("version",5) #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
1175  ver.Write("version")
1176 
1177  meta = TTree()
1178  libr = TTree()
1179 
1180  mmstruct = MyMetaStruct()
1181 
1182  mmstruct.detector = "%s" % (str(self.detector))
1183  mmstruct.particle = int(self.particle)
1184  mmstruct.release = "%s" % (str(self.release))
1185  mmstruct.geometry = "%s" % (str(self.geometry))
1186  mmstruct.geant = "%s" % (str(self.geant))
1187  mmstruct.phys = "%s" % (str(self.phys))
1188  mmstruct.comment = "%s" % (str(self.comment))
1189 
1190  meta.Branch("detector",addressof(mmstruct,"detector"),"detector/C")
1191  meta.Branch("particle",addressof(mmstruct,"particle"),"particle/I")
1192  meta.Branch("release",addressof(mmstruct,"release"),"release/C")
1193  meta.Branch("geometry",addressof(mmstruct,"geometry"),"geometry/C")
1194  meta.Branch("geantVersion",addressof(mmstruct,"geant"),"geantVersion/C")
1195  meta.Branch("physicsList",addressof(mmstruct,"phys"),"physicsList/C")
1196  meta.Branch("comment",addressof(mmstruct,"comment"),"physicsList/C")
1197 
1198  meta.Fill()
1199 
1200  mstruct = MyStruct()
1201 
1202  libr.Branch("x",addressof(mstruct,"x"),"x/F")
1203  libr.Branch("y",addressof(mstruct,"y"),"y/F")
1204  libr.Branch("z",addressof(mstruct,"z"),"z/F")
1205  libr.Branch("e",addressof(mstruct,"e"),"e/F")
1206  libr.Branch("time",addressof(mstruct,"time"),"time/F")
1207 
1208  etas = sorted(self.library.keys())
1209 
1210  mstruct.x = self.xrod_cent
1211  mstruct.y = self.yrod_cent
1212  mstruct.z = self.step
1213  mstruct.e = 0
1214  mstruct.time = 0
1215  libr.Fill()
1216 
1217  for eta in etas :
1218  dists = sorted(self.library[eta].keys())
1219  mstruct.x = len(self.library[eta])
1220  mstruct.y = eta
1221  mstruct.z = 0
1222  mstruct.e = 0
1223  mstruct.time = 0
1224  libr.Fill()
1225  for dist in dists :
1226  mstruct.x = len(self.library[eta][dist])
1227  mstruct.y = dist
1228  mstruct.z = 0
1229  mstruct.e = 0
1230  mstruct.time = 0
1231  libr.Fill()
1232  self.library[eta][dist].sort(key=lambda x: x.egen)
1233  for storedShower in self.library[eta][dist] :
1234  mstruct.x = len(storedShower.shower)
1235  mstruct.y = storedShower.rsize
1236  mstruct.z = storedShower.zsize
1237  mstruct.e = storedShower.egen
1238  mstruct.time = 0
1239  libr.Fill()
1240  for hit in storedShower.shower:
1241  mstruct.e = hit.e
1242  mstruct.x = hit.x
1243  mstruct.y = hit.y
1244  mstruct.z = hit.z
1245  mstruct.time = hit.time
1246  libr.Fill()
1247  meta.Write("meta")
1248  libr.Write("library")
1249  tfile.Close()
1250  def printInfo(self) :
1251  print ("VERSION: FCALDistEtaEnergyLib","PARTICLE:",self.particle,"DETECTOR:",self.detector)
1252  print (self.release, self.geometry, self.geant, self.phys)
1253  print ("xrodcent:",self.xrod_cent,"yrodcent:",self.yrod_cent,"step:",self.step)
1254  print (self.comment)
1255  ebins = [1,2,3,4,5,10,20,50,100,200,500,1000]
1256  etas = sorted(self.library.keys())
1257  print ("Number of etabins:",str(len(etas)))
1258  fstot = 0
1259  for etabin in self.library.values():
1260  for distbin in etabin.values():
1261  fstot +=len(distbin)
1262  print ("Number of showers:",str(fstot))
1263  print ("-"*(13+len(ebins)*8)) #horizontal line
1264  infostr = "|dists|ebins|"
1265  for ebin in ebins : #header for energy bins
1266  infostr += ("<%d" %ebin).rjust(7) #str(ebin).rjust(7)
1267  infostr += "|"
1268  print (infostr)
1269  print ("-"*(13+len(ebins)*8)) #horizontal line
1270  for eta in etas :
1271  dists = sorted(self.library[eta].keys())
1272  for distlow,disthigh in zip(dists,(dists[1:] + [4.5])) : #looping over eta bins
1273  prevebin = 0
1274  erec = {}
1275  egen = {}
1276  hits = {}
1277  count = {}
1278  for ebin in ebins : # for all energy bins
1279  count[ebin] = 0
1280  erec[ebin] = 0.
1281  egen[ebin] = 0.
1282  hits[ebin] = 0.
1283  for shower in self.library[eta][distlow] :
1284  if (shower.egen <= ebin) and (shower.egen > prevebin) :
1285  count[ebin] += 1
1286  egenshow = shower.egen
1287  erecshow = 0
1288  for hit in shower.shower :
1289  erecshow += hit.e
1290  erec[ebin] += erecshow
1291  egen[ebin] += egenshow
1292  hits[ebin] += len(shower.shower)
1293  if (count[ebin] > 0) :
1294  hits[ebin] /= count[ebin]
1295  prevebin = ebin
1296  infostr = "|#" # |
1297  infostr+= str(eta).rjust(10) # | eta header
1298  infostr+= "|" # |\
1299  infostr2 = "|Hits"
1300  infostr2+= str(round(distlow,5)).rjust(7) # | eta header
1301  infostr2+= "|" # |
1302  infostr3 = "|E/E"
1303  infostr3+= str(round(disthigh,3)).rjust(8) # | eta header
1304  infostr3+= "|" # |
1305  for ebin in ebins :
1306  infostr+= str(count[ebin]).rjust(7) #print the number of showers
1307  if (egen[ebin] > 0) :
1308  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
1309  infostr3+= ("%.5f" %(erec[ebin]/egen[ebin])).rjust(7)
1310  else :
1311  infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
1312  infostr3+= "0.0".rjust(7) #else print "xxx"
1313  infostr+="|"
1314  infostr2+="|"
1315  infostr3+="|"
1316  print (infostr)
1317  print (infostr2)
1318  print (infostr3)
1319  print ("-"*(12+len(ebins)*8)) #horizontal line
1320  def drawHits(self):
1321  from ROOT import TH3F
1322  from math import sqrt,copysign,log10
1323  hits = TH3F("HITS","Hits Distrib",50,1,1000,101,-300,300,100,0,500)
1324  containmentZ = TH3F("CONTZ","ContZ Distrib",50,1,1000,101,-300,300,100,0,500)
1325  containmentR = TH3F("CONTR","ContR Distrib",50,1,1000,101,-300,300,100,0,500)
1326 
1327  etas = sorted(self.library.keys())
1328  for eta in etas :
1329  dists = sorted(self.library[eta].keys())
1330  for distlow,disthigh in zip(dists,(dists[1:] + [4.5])) : #looping over eta bins
1331  for storedShower in self.library[eta][distlow] :
1332  containmentR.Fill(log10(storedShower.egen)*333,storedShower.rsize,storedShower.zsize,10)
1333  containmentR.Fill(log10(storedShower.egen)*333,-storedShower.rsize,storedShower.zsize,10)
1334  containmentZ.Fill(log10(storedShower.egen)*333,0,storedShower.zsize,10)
1335  for hit in storedShower.shower :
1336  hits.Fill(log10(storedShower.egen)*333,copysign(sqrt(hit.x*hit.x + hit.y*hit.y),hit.x),hit.z)
1337 
1338  return hits,containmentZ,containmentR
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.scaleEnergy
def scaleEnergy(self, scalefactor)
Definition: LArG4ShowerLibFunctions.py:909
LArG4ShowerLibFunctions.EtaEnergyShowerLib.geometry
geometry
Definition: LArG4ShowerLibFunctions.py:243
LArG4ShowerLibFunctions.EtaEnergyShowerLib.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:238
LArG4ShowerLibFunctions.StoredEnergyShower.__hash__
def __hash__(self)
Definition: LArG4ShowerLibFunctions.py:31
LArG4ShowerLibFunctions.StoredTestShower.rsize
rsize
Definition: LArG4ShowerLibFunctions.py:21
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.printInfo
def printInfo(self)
Definition: LArG4ShowerLibFunctions.py:1250
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.truncate
def truncate(self, truncate, nShowersMin=0)
Definition: LArG4ShowerLibFunctions.py:916
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.phys
phys
Definition: LArG4ShowerLibFunctions.py:904
LArG4ShowerLibFunctions.FCALDistShowerLib.readFromFile
def readFromFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:637
LArG4ShowerLibFunctions.StoredEnergyShower.rsize
rsize
Definition: LArG4ShowerLibFunctions.py:29
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.drawHits
def drawHits(self)
Definition: LArG4ShowerLibFunctions.py:1320
LArG4ShowerLibFunctions.FCALDistShowerLib.printInfo
def printInfo(self)
Definition: LArG4ShowerLibFunctions.py:813
LArG4ShowerLibFunctions.TestShowerLib
Definition: LArG4ShowerLibFunctions.py:36
LArG4ShowerLibFunctions.EtaEnergyShowerLib.truncate
def truncate(self, truncate)
Definition: LArG4ShowerLibFunctions.py:253
LArG4ShowerLibFunctions.EtaEnergyShowerLib.mineta
mineta
Definition: LArG4ShowerLibFunctions.py:278
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
LArG4ShowerLibFunctions.FCALDistEtaShowerLib
Definition: LArG4ShowerLibFunctions.py:895
LArG4ShowerLibFunctions.TestShowerLib.particle
particle
Definition: LArG4ShowerLibFunctions.py:40
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.comment
comment
Definition: LArG4ShowerLibFunctions.py:905
LArG4ShowerLibFunctions.StoredTestShower.momentum
momentum
Definition: LArG4ShowerLibFunctions.py:20
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.geant
geant
Definition: LArG4ShowerLibFunctions.py:903
LArG4ShowerLibFunctions.FCALDistShowerLib.geometry
geometry
Definition: LArG4ShowerLibFunctions.py:565
LArG4ShowerLibFunctions.TestShowerLib.detector
detector
Definition: LArG4ShowerLibFunctions.py:39
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.moveDist
def moveDist(self, oldDist, newDist)
Definition: LArG4ShowerLibFunctions.py:938
LArG4ShowerLibFunctions.EtaEnergyShowerLib.phys
phys
Definition: LArG4ShowerLibFunctions.py:245
LArG4ShowerLibFunctions.TestShowerLib.fromLibs
def fromLibs(self, libs)
Definition: LArG4ShowerLibFunctions.py:46
LArG4ShowerLibFunctions.StoredTestShower.shower
shower
Definition: LArG4ShowerLibFunctions.py:23
MuonGM::round
float round(const float toRound, const unsigned int decimals)
Definition: Mdt.cxx:27
LArG4ShowerLibFunctions.FourVector.x
x
Definition: LArG4ShowerLibFunctions.py:12
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.moveEta
def moveEta(self, oldEta, newEta)
Definition: LArG4ShowerLibFunctions.py:945
LArG4ShowerLibFunctions.TestShowerLib.drawHits
def drawHits(self)
Definition: LArG4ShowerLibFunctions.py:223
LArG4ShowerLibFunctions.EtaEnergyShowerLib.fromLibs
def fromLibs(self, libs)
Definition: LArG4ShowerLibFunctions.py:267
LArG4ShowerLibFunctions.FCALDistShowerLib.particle
particle
Definition: LArG4ShowerLibFunctions.py:563
LArG4ShowerLibFunctions.FourVector.e
e
Definition: LArG4ShowerLibFunctions.py:15
LArG4ShowerLibFunctions.EtaEnergyShowerLib.geant
geant
Definition: LArG4ShowerLibFunctions.py:244
LArG4ShowerLibFunctions.EtaEnergyShowerLib.removeEta
def removeEta(self, eta)
Definition: LArG4ShowerLibFunctions.py:305
LArG4ShowerLibFunctions.FCALDistShowerLib.fromLibs
def fromLibs(self, libs)
Definition: LArG4ShowerLibFunctions.py:602
LArG4ShowerLibFunctions.StoredEnergyShower.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:26
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.writeToFile
def writeToFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:1148
LArG4ShowerLibFunctions.EtaEnergyShowerLib.detector
detector
Definition: LArG4ShowerLibFunctions.py:240
LArG4ShowerLibFunctions.FCALDistShowerLib.step
step
Definition: LArG4ShowerLibFunctions.py:571
LArG4ShowerLibFunctions.TestShowerLib.geant
geant
Definition: LArG4ShowerLibFunctions.py:43
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:805
LArG4ShowerLibFunctions.FCALDistShowerLib.release
release
Definition: LArG4ShowerLibFunctions.py:564
LArG4ShowerLibFunctions.StoredEnergyShower
Definition: LArG4ShowerLibFunctions.py:25
LArG4ShowerLibFunctions.EtaEnergyShowerLib.writeToFile
def writeToFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:390
LArG4ShowerLibFunctions.FourVector
Definition: LArG4ShowerLibFunctions.py:10
LArG4ShowerLibFunctions.FCALDistShowerLib.scaleEnergy
def scaleEnergy(self, scalefactor)
Definition: LArG4ShowerLibFunctions.py:572
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.readFromFile
def readFromFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:1003
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.step
step
Definition: LArG4ShowerLibFunctions.py:908
LArG4ShowerLibFunctions.EtaEnergyShowerLib.release
release
Definition: LArG4ShowerLibFunctions.py:242
LArG4ShowerLibFunctions.EtaEnergyShowerLib
Definition: LArG4ShowerLibFunctions.py:237
PixelModuleFeMask_create_db.remove
string remove
Definition: PixelModuleFeMask_create_db.py:83
LArG4ShowerLibFunctions.EtaEnergyShowerLib.printInfo
def printInfo(self)
Definition: LArG4ShowerLibFunctions.py:477
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.library
library
Definition: LArG4ShowerLibFunctions.py:898
LArG4ShowerLibFunctions.TestShowerLib.library
library
Definition: LArG4ShowerLibFunctions.py:38
LArG4ShowerLibFunctions.FCALDistShowerLib.detector
detector
Definition: LArG4ShowerLibFunctions.py:562
LArG4ShowerLibFunctions.StoredTestShower.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:18
LArG4ShowerLibFunctions.FCALDistShowerLib.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:560
LArG4ShowerLibFunctions.EtaEnergyShowerLib.scaleEnergy
def scaleEnergy(self, scalefactor)
Definition: LArG4ShowerLibFunctions.py:247
LArG4ShowerLibFunctions.TestShowerLib.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:37
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.removeDist
def removeDist(self, dist)
Definition: LArG4ShowerLibFunctions.py:950
LArG4ShowerLibFunctions.EtaEnergyShowerLib.drawHits
def drawHits(self)
Definition: LArG4ShowerLibFunctions.py:544
LArG4ShowerLibFunctions.EtaEnergyShowerLib.library
library
Definition: LArG4ShowerLibFunctions.py:239
LArG4ShowerLibFunctions.TestShowerLib.geometry
geometry
Definition: LArG4ShowerLibFunctions.py:42
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
LArG4ShowerLibFunctions.TestShowerLib.phys
phys
Definition: LArG4ShowerLibFunctions.py:44
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
LArG4ShowerLibFunctions.EtaEnergyShowerLib.particle
particle
Definition: LArG4ShowerLibFunctions.py:241
LArG4ShowerLibFunctions.FCALDistShowerLib.truncate
def truncate(self, truncate)
Definition: LArG4ShowerLibFunctions.py:578
LArG4ShowerLibFunctions.FCALDistShowerLib.xrod_cent
xrod_cent
Definition: LArG4ShowerLibFunctions.py:569
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
LArG4ShowerLibFunctions.StoredEnergyShower.__eq__
def __eq__(self, other)
Definition: LArG4ShowerLibFunctions.py:33
CxxUtils::set
constexpr std::enable_if_t< is_bitmask_v< E >, E & > set(E &lhs, E rhs)
Convenience function to set bits in a class enum bitmask.
Definition: bitmask.h:232
LArG4ShowerLibFunctions.FCALDistShowerLib
Definition: LArG4ShowerLibFunctions.py:559
LArG4ShowerLibFunctions.FCALDistShowerLib.comment
comment
Definition: LArG4ShowerLibFunctions.py:568
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.fromLibs
def fromLibs(self, libs)
Definition: LArG4ShowerLibFunctions.py:962
LArG4ShowerLibFunctions.FCALDistShowerLib.writeToFile
def writeToFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:719
LArG4ShowerLibFunctions.TestShowerLib.printInfo
def printInfo(self)
Definition: LArG4ShowerLibFunctions.py:221
LArG4ShowerLibFunctions.FCALDistShowerLib.library
library
Definition: LArG4ShowerLibFunctions.py:561
LArG4ShowerLibFunctions.FourVector.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:11
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.particle
particle
Definition: LArG4ShowerLibFunctions.py:900
LArG4ShowerLibFunctions.TestShowerLib.writeToFile
def writeToFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:138
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
LArG4ShowerLibFunctions.StoredTestShower
Definition: LArG4ShowerLibFunctions.py:17
LArG4ShowerLibFunctions.FourVector.y
y
Definition: LArG4ShowerLibFunctions.py:13
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.__init__
def __init__(self)
Definition: LArG4ShowerLibFunctions.py:897
LArG4ShowerLibFunctions.EtaEnergyShowerLib.readFromFile
def readFromFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:310
LArG4ShowerLibFunctions.StoredTestShower.zsize
zsize
Definition: LArG4ShowerLibFunctions.py:22
LArG4ShowerLibFunctions.FCALDistShowerLib.phys
phys
Definition: LArG4ShowerLibFunctions.py:567
LArG4ShowerLibFunctions.StoredEnergyShower.shower
shower
Definition: LArG4ShowerLibFunctions.py:27
LArG4ShowerLibFunctions.StoredEnergyShower.egen
egen
Definition: LArG4ShowerLibFunctions.py:28
LArG4ShowerLibFunctions.TestShowerLib.readFromFile
def readFromFile(self, filename)
Definition: LArG4ShowerLibFunctions.py:71
LArG4ShowerLibFunctions.TestShowerLib.comment
comment
Definition: LArG4ShowerLibFunctions.py:45
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.removeEta
def removeEta(self, eta)
Definition: LArG4ShowerLibFunctions.py:957
CaloCondBlobAlgs_fillNoiseFromASCII.hash
dictionary hash
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:109
LArG4ShowerLibFunctions.EtaEnergyShowerLib.comment
comment
Definition: LArG4ShowerLibFunctions.py:246
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.yrod_cent
yrod_cent
Definition: LArG4ShowerLibFunctions.py:907
LArG4ShowerLibFunctions.FCALDistShowerLib.yrod_cent
yrod_cent
Definition: LArG4ShowerLibFunctions.py:570
LArG4ShowerLibFunctions.EtaEnergyShowerLib.maxeta
maxeta
Definition: LArG4ShowerLibFunctions.py:279
LArG4ShowerLibFunctions.FCALDistShowerLib.moveDist
def moveDist(self, oldDist, newDist)
Definition: LArG4ShowerLibFunctions.py:592
LArG4ShowerLibFunctions.StoredTestShower.vertex
vertex
Definition: LArG4ShowerLibFunctions.py:19
str
Definition: BTagTrackIpAccessor.cxx:11
python.Bindings.keys
keys
Definition: Control/AthenaPython/python/Bindings.py:798
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.detector
detector
Definition: LArG4ShowerLibFunctions.py:899
LArG4ShowerLibFunctions.EtaEnergyShowerLib.moveEta
def moveEta(self, oldEta, newEta)
Definition: LArG4ShowerLibFunctions.py:300
LArG4ShowerLibFunctions.FCALDistShowerLib.drawHits
def drawHits(self)
Definition: LArG4ShowerLibFunctions.py:880
LArG4ShowerLibFunctions.FCALDistShowerLib.geant
geant
Definition: LArG4ShowerLibFunctions.py:566
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.geometry
geometry
Definition: LArG4ShowerLibFunctions.py:902
LArG4ShowerLibFunctions.FCALDistShowerLib.removeDist
def removeDist(self, dist)
Definition: LArG4ShowerLibFunctions.py:597
LArG4ShowerLibFunctions.TestShowerLib.release
release
Definition: LArG4ShowerLibFunctions.py:41
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.release
release
Definition: LArG4ShowerLibFunctions.py:901
WriteBchToCool.update
update
Definition: WriteBchToCool.py:67
LArG4ShowerLibFunctions.StoredEnergyShower.zsize
zsize
Definition: LArG4ShowerLibFunctions.py:30
LArG4ShowerLibFunctions.FCALDistEtaShowerLib.xrod_cent
xrod_cent
Definition: LArG4ShowerLibFunctions.py:906
LArG4ShowerLibFunctions.FourVector.z
z
Definition: LArG4ShowerLibFunctions.py:14