ATLAS Offline Software
LArG4ShowerLibProcessing.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 
4 #
5 # $Id: LArG4ShowerLibProcessing.py 711210 2015-11-27 15:56:00Z jchapman $
6 #
7 # python script for merging libs
8 #
9 
10 from __future__ import print_function
11 
12 __version__ = '$Revision: 711210 $'
13 __author__ = 'Radist Morse radist.morse@gmail.com'
14 __doc__ = 'Script for merging FS libs'
15 
16 import sys
17 import logging
18 from optparse import OptionParser
19 from LArG4GenShowerLib.LArG4ShowerLibFunctions import \
20  EtaEnergyShowerLib, TestShowerLib, FCALDistShowerLib, FCALDistEtaShowerLib
21 
22 usage = "usage: %prog [options] lib1 [lib2 ...]"
23 
24 parser = OptionParser(usage=usage, version="%prog v0.0.1 $Id: LArG4ShowerLibProcessing.py 711210 2015-11-27 15:56:00Z jchapman $")
25 
26 #parser.add_option("-o","--output", dest="outfile", help="Name of output file (default: %default)")
27 parser.add_option("-i","--info",dest="info", action="store_true", help="Print info about the lib(s)")
28 parser.add_option("-a","--add",dest="add", action="store_true", help="Add input libs together")
29 parser.add_option("-o","--output",dest="output", help="Name of output file")
30 parser.add_option("-s","--scale",dest="scale", type = float, help="Scale library by energy")
31 parser.add_option("-d","--moveDist",dest="moveD", action="append", help="move dist bin")
32 parser.add_option("-e","--moveEta",dest="moveE", action="append", help="move eta bin")
33 parser.add_option("--removeDist",dest="removeD", action="append", help="remove dist bin")
34 parser.add_option("--removeEta",dest="removeE", action="append", help="remove eta bin")
35 parser.add_option("-t","--truncate",dest="truncate", type = int, help="truncate lib")
36 parser.add_option("-p","--paint",dest="draw", action="store_true", help="paint hits distribution histogram")
37 parser.add_option('--log', dest="loglevel", help='Logging level. Use \"--log debug\" to debug the library content step-by-step' )
38 
39 parser.set_defaults(info = False, add = False, output = "", scale = 0.0, moveD = [], moveE = [], removeD = [], removeE = [], draw = False, truncate = 0, loglevel="info")
40 
41 (options, args) = parser.parse_args()
42 
43 logging.basicConfig(stream=sys.stdout,level=options.loglevel.upper() )
44 log=logging.getLogger("LArG4ShowerLibProcessing.py")
45 
46 if len(args) == 0:
47  log.error("No input specified")
48  sys.exit(1)
49 
50 libs = []
51 
52 for filename in args:
53  log.info("Reading file %s",filename)
54  tmplib = EtaEnergyShowerLib()
55  if tmplib.readFromFile(filename) :
56  libs.append(tmplib)
57  tmplib = TestShowerLib()
58  if tmplib.readFromFile(filename) :
59  libs.append(tmplib)
60  tmplib = FCALDistShowerLib()
61  if tmplib.readFromFile(filename) :
62  libs.append(tmplib)
63  tmplib = FCALDistEtaShowerLib()
64  if tmplib.readFromFile(filename) :
65  libs.append(tmplib)
66 
67 if (options.truncate > 0) :
68  for lib in libs :
69  lib.truncate(options.truncate)
70 
71 if (options.info) :
72  for arg,lib in zip(args,libs) :
73  print ("LIB",arg)
74  lib.printInfo()
75 
76 if (options.add and len(libs) > 1) :
77  print ("INFO: Adding libs")
78  outlib = libs[0].__class__()
79  if not outlib.fromLibs(libs) :
80  print ("ABORTING")
81  sys.exit(1)
82  del libs
83  libs = []
84  libs.append(outlib)
85 
86 if (options.scale > 0) :
87  print ("Scaling to",options.scale)
88  libs[0].scaleEnergy(options.scale)
89 
90 if (len(options.moveD) > 0) :
91  for move in options.moveD :
92  print ("Moving bin:",move)
93  movl = map(float,move.split(":"))
94  if not libs[0].moveDist(*movl) :
95  print ("WARNING: no",movl[0],"in lib")
96 
97 if (len(options.moveE) > 0) :
98  for move in options.moveE :
99  print ("Moving bin:",move)
100  movl = map(float,move.split(":"))
101  if not libs[0].moveEta(*movl) :
102  print ("WARNING: no",movl[0],"in lib")
103 
104 if (len(options.removeD) > 0) :
105  for move in options.removeD :
106  print ("Removing bin:",move)
107  movl = float(move)
108  if not libs[0].removeDist(movl) :
109  print ("WARNING: no",movl,"in lib")
110 
111 if (len(options.removeE) > 0) :
112  for move in options.removeE :
113  print ("Removing bin:",move)
114  movl = float(move)
115  if not libs[0].removeEta(movl) :
116  print ("WARNING: no",movl,"in lib")
117 
118 if (len(options.output) > 0) :
119  print ("INFO: Saving file",options.output)
120  libs[0].writeToFile(options.output)
121 
122 if (options.draw) :
123  print ("INFO: Drawing lib",options.output)
124  from ROOT import TCanvas
125  from ROOT import gROOT, gStyle,gPad
126  gROOT.Reset()
127  gROOT.SetStyle("Plain")
128  gStyle.SetOptStat(0)
129  gStyle.SetPalette(1)
130  gStyle.SetNumberContours(50)
131 
132  c1 = TCanvas( 'c1', "Library Viewer")
133  #gStyle.SetOptLogx(1)
134  gPad.SetLogx(1)
135 
136  hits,containmentZ,containmentR = libs[0].drawHits()
137 
138  hits.Draw()
139  containmentZ.SetMarkerColor(2)
140  containmentZ.Draw("SAME")
141  containmentR.SetMarkerColor(4)
142  containmentR.Draw("SAME")
143  c1.Update()
144  from time import sleep
145  while not not(c1) :
146  sleep(1)
systematicsTool.writeToFile
def writeToFile(histDict, fOut)
Definition: systematicsTool.py:1035
LArG4ShowerLibProcessing.float
float
Definition: LArG4ShowerLibProcessing.py:30