ATLAS Offline Software
Loading...
Searching...
No Matches
LArG4ShowerLibFunctions.FCALDistEtaShowerLib Class Reference
Collaboration diagram for LArG4ShowerLibFunctions.FCALDistEtaShowerLib:

Public Member Functions

 __init__ (self)
 scaleEnergy (self, scalefactor)
 truncate (self, truncate, nShowersMin=0)
 moveDist (self, oldDist, newDist)
 moveEta (self, oldEta, newEta)
 removeDist (self, dist)
 removeEta (self, eta)
 fromLibs (self, libs)
 readFromFile (self, filename)
 writeToFile (self, filename)
 printInfo (self)
 drawHits (self)

Public Attributes

dict library = {}
str detector = ""
str particle = ""
str release = ""
str geometry = ""
str geant = ""
str phys = ""
str comment = ""
float xrod_cent = 0.0
float yrod_cent = 0.0
float step = 0.0

Private Attributes

 __class__

Detailed Description

Definition at line 894 of file LArG4ShowerLibFunctions.py.

Constructor & Destructor Documentation

◆ __init__()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.__init__ ( self)

Definition at line 896 of file LArG4ShowerLibFunctions.py.

896 def __init__(self) :
897 self.library = {} # key (float) - eta, value (dict), key - dist, value - (list) list of StoredEnergyShower objs
898 self.detector= ""
899 self.particle= ""
900 self.release= ""
901 self.geometry= ""
902 self.geant= ""
903 self.phys= ""
904 self.comment= ""
905 self.xrod_cent = 0.0
906 self.yrod_cent = 0.0
907 self.step = 0.0

Member Function Documentation

◆ drawHits()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.drawHits ( self)

Definition at line 1319 of file LArG4ShowerLibFunctions.py.

1319 def drawHits(self):
1320 from ROOT import TH3F
1321 from math import sqrt,copysign,log10
1322 hits = TH3F("HITS","Hits Distrib",50,1,1000,101,-300,300,100,0,500)
1323 containmentZ = TH3F("CONTZ","ContZ Distrib",50,1,1000,101,-300,300,100,0,500)
1324 containmentR = TH3F("CONTR","ContR Distrib",50,1,1000,101,-300,300,100,0,500)
1325
1326 etas = sorted(self.library.keys())
1327 for eta in etas :
1328 dists = sorted(self.library[eta].keys())
1329 for distlow,disthigh in zip(dists,(dists[1:] + [4.5])) : #looping over eta bins
1330 for storedShower in self.library[eta][distlow] :
1331 containmentR.Fill(log10(storedShower.egen)*333,storedShower.rsize,storedShower.zsize,10)
1332 containmentR.Fill(log10(storedShower.egen)*333,-storedShower.rsize,storedShower.zsize,10)
1333 containmentZ.Fill(log10(storedShower.egen)*333,0,storedShower.zsize,10)
1334 for hit in storedShower.shower :
1335 hits.Fill(log10(storedShower.egen)*333,copysign(sqrt(hit.x*hit.x + hit.y*hit.y),hit.x),hit.z)
1336
1337 return hits,containmentZ,containmentR

◆ fromLibs()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.fromLibs ( self,
libs )

Definition at line 961 of file LArG4ShowerLibFunctions.py.

961 def fromLibs(self,libs) :
962 for lib in libs :
963 if not isinstance(lib,self.__class__):
964 print ("ERROR: Different types of libs")
965 return False
966 self.detector = libs[0].detector
967 self.particle = libs[0].particle
968 self.release = libs[0].release
969 self.geometry = libs[0].geometry
970 self.geant = libs[0].geant
971 self.phys = libs[0].phys
972 self.comment = libs[0].comment
973 self.xrod_cent = libs[0].xrod_cent
974 self.yrod_cent = libs[0].yrod_cent
975 self.step = libs[0].step
976 etas = set(libs[0].library.keys())
977 for lib in libs :
978 if ( self.detector != lib.detector or
979 self.particle != lib.particle or
980 self.release != lib.release or
981 self.geometry != lib.geometry or
982 self.geant != lib.geant or
983 self.phys != lib.phys or
984 self.xrod_cent != lib.xrod_cent or
985 self.yrod_cent != lib.yrod_cent or
986 self.step != lib.step or
987 etas != set(lib.library.keys()) ) :
988 print ("ERROR: DIFFERENT LIBS!!!")
989 return False
990 for eta in libs[0].library.keys() :
991 if (set(libs[0].library[eta].keys()) != set(lib.library[eta].keys())) :
992 print ("ERROR: DIFFERENT LIBS!!!")
993 return False
994 for lib in libs :
995 for k,v in lib.library.items():
996 for ki,vi in v.items():
997 self.library.setdefault(k,dict()).setdefault(ki,set()).update(vi)
998 for k,v in self.library.items():
999 for ki,vi in v.items():
1000 self.library[k][ki] = list(vi)
1001 return True
STL class.

◆ moveDist()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.moveDist ( self,
oldDist,
newDist )

Definition at line 937 of file LArG4ShowerLibFunctions.py.

937 def moveDist(self,oldDist,newDist) :
938 rez = False
939 for eta,etabin in self.library.items():
940 if (oldDist in etabin.keys()) :
941 etabin[newDist] = etabin.pop(oldDist)
942 rez=True
943 return rez

◆ moveEta()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.moveEta ( self,
oldEta,
newEta )

Definition at line 944 of file LArG4ShowerLibFunctions.py.

944 def moveEta(self,oldEta,newEta) :
945 if not (oldEta in self.library.keys()) :
946 return False
947 self.library[newEta] = self.library.pop(oldEta)
948 return True

◆ printInfo()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.printInfo ( self)

Definition at line 1249 of file LArG4ShowerLibFunctions.py.

1249 def printInfo(self) :
1250 print ("VERSION: FCALDistEtaEnergyLib","PARTICLE:",self.particle,"DETECTOR:",self.detector)
1251 print (self.release, self.geometry, self.geant, self.phys)
1252 print ("xrodcent:",self.xrod_cent,"yrodcent:",self.yrod_cent,"step:",self.step)
1253 print (self.comment)
1254 ebins = [1,2,3,4,5,10,20,50,100,200,500,1000]
1255 etas = sorted(self.library.keys())
1256 print ("Number of etabins:",str(len(etas)))
1257 fstot = 0
1258 for etabin in self.library.values():
1259 for distbin in etabin.values():
1260 fstot +=len(distbin)
1261 print ("Number of showers:",str(fstot))
1262 print ("-"*(13+len(ebins)*8)) #horizontal line
1263 infostr = "|dists|ebins|"
1264 for ebin in ebins : #header for energy bins
1265 infostr += ("<%d" %ebin).rjust(7) #str(ebin).rjust(7)
1266 infostr += "|"
1267 print (infostr)
1268 print ("-"*(13+len(ebins)*8)) #horizontal line
1269 for eta in etas :
1270 dists = sorted(self.library[eta].keys())
1271 for distlow,disthigh in zip(dists,(dists[1:] + [4.5])) : #looping over eta bins
1272 prevebin = 0
1273 erec = {}
1274 egen = {}
1275 hits = {}
1276 count = {}
1277 for ebin in ebins : # for all energy bins
1278 count[ebin] = 0
1279 erec[ebin] = 0.
1280 egen[ebin] = 0.
1281 hits[ebin] = 0.
1282 for shower in self.library[eta][distlow] :
1283 if (shower.egen <= ebin) and (shower.egen > prevebin) :
1284 count[ebin] += 1
1285 egenshow = shower.egen
1286 erecshow = 0
1287 for hit in shower.shower :
1288 erecshow += hit.e
1289 erec[ebin] += erecshow
1290 egen[ebin] += egenshow
1291 hits[ebin] += len(shower.shower)
1292 if (count[ebin] > 0) :
1293 hits[ebin] /= count[ebin]
1294 prevebin = ebin
1295 infostr = "|#" # |
1296 infostr+= str(eta).rjust(10) # | eta header
1297 infostr+= "|" # |\
1298 infostr2 = "|Hits"
1299 infostr2+= str(round(distlow,5)).rjust(7) # | eta header
1300 infostr2+= "|" # |
1301 infostr3 = "|E/E"
1302 infostr3+= str(round(disthigh,3)).rjust(8) # | eta header
1303 infostr3+= "|" # |
1304 for ebin in ebins :
1305 infostr+= str(count[ebin]).rjust(7) #print the number of showers
1306 if (egen[ebin] > 0) :
1307 infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
1308 infostr3+= ("%.5f" %(erec[ebin]/egen[ebin])).rjust(7)
1309 else :
1310 infostr2+= ("%.2f" %(hits[ebin])).rjust(7)
1311 infostr3+= "0.0".rjust(7) #else print "xxx"
1312 infostr+="|"
1313 infostr2+="|"
1314 infostr3+="|"
1315 print (infostr)
1316 print (infostr2)
1317 print (infostr3)
1318 print ("-"*(12+len(ebins)*8)) #horizontal line

◆ readFromFile()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.readFromFile ( self,
filename )

Definition at line 1002 of file LArG4ShowerLibFunctions.py.

1002 def readFromFile(self,filename) :
1003 log = logging.getLogger("FCALDistEtaShowerLib::readFromFile()")
1004 from ROOT import TFile
1005 #from sets import Set
1006 tfile = TFile(filename)
1007 try:
1008 ver = int(tfile.Get("version").GetVal())
1009 except Exception:
1010 print ("Not an FCALDistEtaEnergyLib: Broken file")
1011 tfile.Close()
1012 return False
1013
1014 if (ver != 5) : #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
1015 print ("Not an FCALDistEtaEnergyLib")
1016 tfile.Close()
1017 return False
1018 meta = tfile.Get("meta")
1019 libr = tfile.Get("library")
1020
1021 for event in meta :
1022 self.detector=str(event.detector)
1023 self.particle=str(event.particle)
1024 self.release=str(event.release)
1025 self.geometry=str(event.geometry)
1026 self.geant=str(event.geantVersion)
1027 self.phys=str(event.physicsList)
1028 self.comment=str(event.comment)
1029
1030 state = -1
1031 lastShower = False
1032 lastEta = False
1033
1034 log.debug("dector: %s", str(event.detector))
1035 log.debug("particle: %s", str(event.particle))
1036 log.debug("release: %s", str(event.release))
1037 log.debug("geometry: %s", str(event.geometry))
1038 log.debug("geant ver: %s", str(event.geantVersion))
1039 log.debug("physList: %s", str(event.physicsList))
1040 log.debug("comment: %s", str(event.comment))
1041
1042 for event in libr : #this is quite unclear, but easy to implement, we change the "state" depending on what we are reading
1043 log.debug("-------")
1044 log.debug("x=%f, y=%f, z=%f, e=%f",event.x,event.y,event.z,event.e)
1045 log.debug("beginnnig ev loop. lastShower: %s",str(lastShower))
1046 log.debug("beginnnig ev loop. state: %s",str(state))
1047
1048 if (state == -1) : #library header (calculator parameters)
1049 log.debug("in state=-1")
1050
1051 self.xrod_cent = event.x
1052 self.yrod_cent = event.y
1053 self.step = event.z
1054 state = 0
1055 elif (state == 0) : #eta bin header
1056 log.debug("in state=0")
1057 log.debug("x=distsInCurEta, y=curEta")
1058
1059 distsInCurEta = event.x
1060 curEta = round(event.y,4)
1061 self.library[curEta] = {}
1062 if (distsInCurEta > 0) :
1063 state = 1 #go to dist header
1064 elif (state == 1) :
1065 log.debug("in state=1")
1066 log.debug("x=showersInCurDist, y=curDist")
1067
1068 showersInCurDist = event.x
1069 curDist = round(event.y,4)
1070 self.library[curEta][curDist] = []
1071 distsInCurEta -= 1
1072 if (distsInCurEta == 0) :
1073 lastEta = True
1074 if (showersInCurDist > 0) :
1075 state = 2 #go to shower header
1076 else : #empty dist bin
1077 if (lastEta) : #special case of last eta bin being the empty one
1078 lastEta = False
1079 state = 0
1080 elif (state == 2) :
1081 # writing shower info
1082 log.debug("in state=2")
1083 log.debug("x=hitsInCurShower, y=curShower.rSize, z=curShower.zSize, e=curShower.genEnergy")
1084
1085 hitsInCurShower = event.x
1086 rSize = event.y
1087 zSize = event.z
1088 genEnergy = event.e
1089 showersInCurDist -= 1
1090 if (showersInCurDist == 0) : #last shower
1091 lastShower = True
1092 curShower = StoredEnergyShower()
1093 curShower.egen = genEnergy
1094 curShower.rsize = rSize
1095 curShower.zsize = zSize
1096 #curShower["hits"] = []
1097 if (hitsInCurShower > 0) :
1098 state = 3 #go to hits
1099 else : #empty shower
1100 log.debug("Appending shower to lib pos %s %s",curEta,curDist)
1101
1102 self.library[curEta][curDist].append(curShower)
1103 if (lastShower) : #special case of last shower in bin being the empty one
1104 lastShower = False
1105 if (lastEta) : #double special case: last shower in last eta bin is empty
1106 lastEta = False
1107 state = 0 #next eta bin
1108 else :
1109 state = 1 #next dist bin
1110 elif (state == 3) :
1111
1112 log.debug("in state=3")
1113 log.debug("x=hit.x, y=hit.y, z=hit.z, e=hit.e")
1114
1115 hit = FourVector()
1116 hit.e = event.e
1117 hit.x = event.x
1118 hit.y = event.y
1119 hit.z = event.z
1120 hit.time = event.time
1121 curShower.shower.append(hit)
1122 hitsInCurShower -= 1
1123 if (hitsInCurShower == 0) : #last hit
1124 log.debug("Appending shower+hit to lib pos %s %s",curEta,curDist)
1125
1126 self.library[curEta][curDist].append(curShower)
1127 if (lastShower) : # end of dist bin
1128 lastShower = False
1129 if (lastEta) : #end of eta bin as well
1130 lastEta = False
1131 state = 0 #next eta bin
1132 else :
1133 state = 1 #next dist bin
1134 else : #not yet
1135 state = 2
1136
1137 log.debug("ending ev loop. lastShower: %s", lastShower)
1138 log.debug("ending ev loop. state %s", state)
1139 if log.root.level == logging.DEBUG:
1140 input("Continue? Press Enter.")
1141
1142 tfile.Close()
1143 if (state != 0) : #the last entry should be the last hit of the last shower in the last bin. if not - file is corrupted
1144 print ("FILE CORRUPTED!!")
1145 return False
1146 return True

◆ removeDist()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.removeDist ( self,
dist )

Definition at line 949 of file LArG4ShowerLibFunctions.py.

949 def removeDist(self,dist) :
950 rez = False
951 for eta,etabin in self.library.items():
952 if (dist in etabin.keys()) :
953 self.library.pop(dist)
954 rez=True
955 return rez

◆ removeEta()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.removeEta ( self,
eta )

Definition at line 956 of file LArG4ShowerLibFunctions.py.

956 def removeEta(self,eta) :
957 if not (eta in self.library.keys()) :
958 return False
959 self.library.pop(eta)
960 return True

◆ scaleEnergy()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.scaleEnergy ( self,
scalefactor )

Definition at line 908 of file LArG4ShowerLibFunctions.py.

908 def scaleEnergy(self,scalefactor) :
909 for etabin in self.library.values():
910 for distbin in etabin.values():
911 for storedShower in distbin :
912 for hit in storedShower.shower :
913 hit.e *= scalefactor
914 self.comment += " SCALED: "+str(scalefactor)

◆ truncate()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.truncate ( self,
truncate,
nShowersMin = 0 )

Definition at line 915 of file LArG4ShowerLibFunctions.py.

915 def truncate(self,truncate,nShowersMin=0) :
916 log = logging.getLogger("FCALDistEtaShowerLib::truncate()")
917 showers = []
918 for eta,etabin in self.library.items():
919 for dist,distbin in etabin.items():
920 log.info("Number of showers in %s %s is %d",str(eta),str(dist),len(distbin))
921 for storedShower in distbin :
922 showers += [(eta, dist, storedShower)]
923 log.info("total number of showers: %d", len(showers))
924 if nShowersMin:
925 log.info("will not remove from eta-dist bins with less then %d showers", nShowersMin)
926 if len(showers) <= truncate :
927 log.warning("Size of the library is already less: %d < %d",truncate,len(showers))
928 return
929 from random import randint
930 while (len(showers) > truncate) :
931 rand = randint(0,len(showers)-1)
932 if len(self.library[showers[rand][0]][showers[rand][1]]) < nShowersMin:
933 continue
934 self.library[showers[rand][0]][showers[rand][1]].remove(showers[rand][2])
935 del showers[rand]
936 return

◆ writeToFile()

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.writeToFile ( self,
filename )

Definition at line 1147 of file LArG4ShowerLibFunctions.py.

1147 def writeToFile(self,filename) :
1148 from ROOT import TFile,TTree,TParameter
1149 from ROOT import gROOT, addressof
1150 gROOT.ProcessLine(
1151 "struct MyMetaStruct {\
1152 Char_t detector[40];\
1153 Char_t release[40];\
1154 Char_t geometry[40];\
1155 Char_t geant[40];\
1156 Char_t phys[40];\
1157 Char_t comment[400];\
1158 Int_t particle;\
1159 };" )
1160 from ROOT import MyMetaStruct
1161 gROOT.ProcessLine(
1162 "struct MyStruct {\
1163 Float_t x;\
1164 Float_t y;\
1165 Float_t z;\
1166 Float_t e;\
1167 Float_t time;\
1168 };" )
1169 from ROOT import MyStruct
1170
1171 tfile = TFile(filename,"RECREATE")
1172
1173 ver = TParameter(int)("version",5) #<<<<<<<<<<<<<<<<<<<<<<-------------- lib ver
1174 ver.Write("version")
1175
1176 meta = TTree()
1177 libr = TTree()
1178
1179 mmstruct = MyMetaStruct()
1180
1181 mmstruct.detector = "%s" % (str(self.detector))
1182 mmstruct.particle = int(self.particle)
1183 mmstruct.release = "%s" % (str(self.release))
1184 mmstruct.geometry = "%s" % (str(self.geometry))
1185 mmstruct.geant = "%s" % (str(self.geant))
1186 mmstruct.phys = "%s" % (str(self.phys))
1187 mmstruct.comment = "%s" % (str(self.comment))
1188
1189 meta.Branch("detector",addressof(mmstruct,"detector"),"detector/C")
1190 meta.Branch("particle",addressof(mmstruct,"particle"),"particle/I")
1191 meta.Branch("release",addressof(mmstruct,"release"),"release/C")
1192 meta.Branch("geometry",addressof(mmstruct,"geometry"),"geometry/C")
1193 meta.Branch("geantVersion",addressof(mmstruct,"geant"),"geantVersion/C")
1194 meta.Branch("physicsList",addressof(mmstruct,"phys"),"physicsList/C")
1195 meta.Branch("comment",addressof(mmstruct,"comment"),"physicsList/C")
1196
1197 meta.Fill()
1198
1199 mstruct = MyStruct()
1200
1201 libr.Branch("x",addressof(mstruct,"x"),"x/F")
1202 libr.Branch("y",addressof(mstruct,"y"),"y/F")
1203 libr.Branch("z",addressof(mstruct,"z"),"z/F")
1204 libr.Branch("e",addressof(mstruct,"e"),"e/F")
1205 libr.Branch("time",addressof(mstruct,"time"),"time/F")
1206
1207 etas = sorted(self.library.keys())
1208
1209 mstruct.x = self.xrod_cent
1210 mstruct.y = self.yrod_cent
1211 mstruct.z = self.step
1212 mstruct.e = 0
1213 mstruct.time = 0
1214 libr.Fill()
1215
1216 for eta in etas :
1217 dists = sorted(self.library[eta].keys())
1218 mstruct.x = len(self.library[eta])
1219 mstruct.y = eta
1220 mstruct.z = 0
1221 mstruct.e = 0
1222 mstruct.time = 0
1223 libr.Fill()
1224 for dist in dists :
1225 mstruct.x = len(self.library[eta][dist])
1226 mstruct.y = dist
1227 mstruct.z = 0
1228 mstruct.e = 0
1229 mstruct.time = 0
1230 libr.Fill()
1231 self.library[eta][dist].sort(key=lambda x: x.egen)
1232 for storedShower in self.library[eta][dist] :
1233 mstruct.x = len(storedShower.shower)
1234 mstruct.y = storedShower.rsize
1235 mstruct.z = storedShower.zsize
1236 mstruct.e = storedShower.egen
1237 mstruct.time = 0
1238 libr.Fill()
1239 for hit in storedShower.shower:
1240 mstruct.e = hit.e
1241 mstruct.x = hit.x
1242 mstruct.y = hit.y
1243 mstruct.z = hit.z
1244 mstruct.time = hit.time
1245 libr.Fill()
1246 meta.Write("meta")
1247 libr.Write("library")
1248 tfile.Close()
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.

Member Data Documentation

◆ __class__

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.__class__
private

Definition at line 963 of file LArG4ShowerLibFunctions.py.

◆ comment

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.comment = ""

Definition at line 904 of file LArG4ShowerLibFunctions.py.

◆ detector

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.detector = ""

Definition at line 898 of file LArG4ShowerLibFunctions.py.

◆ geant

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.geant = ""

Definition at line 902 of file LArG4ShowerLibFunctions.py.

◆ geometry

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.geometry = ""

Definition at line 901 of file LArG4ShowerLibFunctions.py.

◆ library

dict LArG4ShowerLibFunctions.FCALDistEtaShowerLib.library = {}

Definition at line 897 of file LArG4ShowerLibFunctions.py.

◆ particle

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.particle = ""

Definition at line 899 of file LArG4ShowerLibFunctions.py.

◆ phys

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.phys = ""

Definition at line 903 of file LArG4ShowerLibFunctions.py.

◆ release

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.release = ""

Definition at line 900 of file LArG4ShowerLibFunctions.py.

◆ step

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.step = 0.0

Definition at line 907 of file LArG4ShowerLibFunctions.py.

◆ xrod_cent

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.xrod_cent = 0.0

Definition at line 905 of file LArG4ShowerLibFunctions.py.

◆ yrod_cent

LArG4ShowerLibFunctions.FCALDistEtaShowerLib.yrod_cent = 0.0

Definition at line 906 of file LArG4ShowerLibFunctions.py.


The documentation for this class was generated from the following file: