merge_histograms.py
Go to the documentation of this file.
1 ##################################################
2 ##
3 ## merge_histograms.py
4 ##
5 ## Laura Fields
6 ## Jul 2014
7 ##
8 ## Merges many flux histogram files into one
9 ##
10 ###################################################
11 
12 from optparse import OptionParser
13 import sys, os, subprocess, shutil, ROOT,datetime
14 
15 def is_good_histo_file(myfile):
16  # Check that file is not a zombie
17  if myfile.IsZombie():
18  return False
19  # Check that numu flux is not empty
20  keys = myfile.GetListOfKeys()
21  if "numu_flux" in keys:
22  if myfile.Get("numu_flux").Integral()>0:
23  return True
24  if "numu_flux_forplots" in keys:
25  if myfile.Get("numu_flux_forplots").Integral()>0:
26  return True
27  return False
28 
29 def merge_histos(filevec,outfile):
30 
31  if len(filevec)==0:
32  print "merge_hitsograms.py, mergehistos(): No files to merge."
33  return;
34 
35  # Temporary staging area
36  #temppath = "/dune/data2/users/"+os.getenv("USER")+"/scratch/"
37  #if not os.path.exists(temppath):
38  # os.makedirs(temppath)
39 
40  #for i in range (0,len(filevec)):
41  # tempfile = temppath + "/" +os.path.basename(filevec[i])
42  # shutil.copyfile(filevec[i],tempfile)
43 
44  # find first good file
45  first_file_index = -1
46 
47  for i in range(0,len(filevec)):
48  #tempfile = temppath + "/" +os.path.basename(filevec[i])
49  in_tfile = ROOT.TFile(filevec[i]);
50  if is_good_histo_file(in_tfile):
51  first_file_index = i
52  break
53 
54  n_files = 1
55 
56  print "First good file: "+str(first_file_index)
57 
58  last_time = datetime.datetime.now()
59  # Look through first file to find list of histograms that we should merge
60  output_histos = []
61  for key in in_tfile.GetListOfKeys():
62  if key.ReadObj().IsA().InheritsFrom("TH1"):
63  output_histos.append(in_tfile.Get(key.GetName()).Clone())
64 
65  for i in range(first_file_index+1,len(filevec)):
66  if i%50 == 0:
67  now = datetime.datetime.now()
68  print "Merging file number",i,"; time to merge last 50 files:",now-last_time
69  last_time = now
70 
71  #tempfile = temppath + "/" +os.path.basename(filevec[i])
72  next_tfile = ROOT.TFile(filevec[i])
73  if is_good_histo_file(next_tfile):
74  n_files = n_files + 1
75  for hist in output_histos:
76  temphist = next_tfile.Get(hist.GetName())
77  hist.Add(temphist)
78  next_tfile.Close()
79  #os.remove(tempfile)
80  out_tfile = ROOT.TFile(outfile,"RECREATE");
81  print "writing to "+outfile
82  scale_factor = 1.0/n_files;
83  for hist in output_histos:
84  hist.Scale(scale_factor)
85  hist.Write()
86 
87  return
88 
89 
90 def merge_globes(filevec,outfile):
91 
92  if len(filevec)==0:
93  print "merge_histograms.py, merge_globes(): No files to merge."
94  return;
95 
96  n_files = 0
97 
98  total_flux = []
99  for i in range(501):
100  total_flux.append([0,0,0,0,0,0,0])
101 
102  #temppath = "/scratch/dune/"+os.getenv("USER")
103  #if not os.path.exists(temppath):
104  # os.makedirs(temppath)
105 
106  print "merging",len(filevec),"globes files"
107  last_time = datetime.datetime.now()
108  for i in range(0,len(filevec)):
109  if i%50==0:
110  now = datetime.datetime.now()
111  print "Merging file number",i,"; time since last print:",now-last_time
112  last_time = now
113 
114  #tempfile = temppath+"/"+os.path.basename(filevec[i])
115  #shutil.copyfile(filevec[i],tempfile)
116  next_file = open(filevec[i])
117  lines = next_file.readlines()
118  next_file.close()
119  #os.remove(tempfile)
120 
121  flux = []
122  problem_found = (len(lines)!=501)
123  for line in lines:
124 
125  if len(line.split())!=7:
126  problem_found = True
127  elif float(line.split()[0])>1 and float(line.split()[0])<5 and (float(line.split()[1])+float(line.split()[2])+float(line.split()[3])+float(line.split()[4])+float(line.split()[5])+float(line.split()[6]))==0.0:
128  # make sure fluxes aren't all zero (by looking in peak)
129  problem_found = True
130  else:
131  flux.append([float(line.split()[0]),float(line.split()[1]),float(line.split()[2]),float(line.split()[3]),float(line.split()[4]),float(line.split()[5]),float(line.split()[6])])
132 
133  if not problem_found:
134  n_files = n_files+1
135  for j in range(501):
136  total_flux[j][0] = flux[j][0]
137  for k in range(1,7):
138  total_flux[j][k] += flux[j][k]
139 
140  print "Number of good globes files found:",n_files
141  for i in range(501):
142  for j in range(1,7):
143  total_flux[i][j] = total_flux[i][j]/n_files
144 
145  out_file = open(outfile,"w");
146  print "writign to "+outfile
147  for i in range(501):
148  for j in range(7):
149  out_file.write(str(total_flux[i][j])+" ")
150  out_file.write("\n")
151 
152  return
153 
154 
155 
156 ###################################################
157 #
158 # Determine default g4lbne directory
159 # (the directory this script is in)
160 #
161 ###################################################
162 scriptdir = os.path.abspath(sys.argv[0]+"/../..")
163 print "default g4lbne directory:",scriptdir
164 
165 ###################################################
166 #
167 # Setup parser that reads in options
168 #
169 ###################################################
170 
171 usage = "usage: %prog [options]"
172 parser = OptionParser(usage=usage)
173 
174 parser.add_option("-g", "--g4lbne_dir", dest="g4lbne_dir",
175  help="g4lbne directory", default=scriptdir)
176 parser.add_option("-p", "--physics_list", dest="physics_list",
177  help="Geant4 Physics List", default="QGSP_BERT");
178 parser.add_option("-m", "--macro", dest="macro",
179  help="Template Macro", default="Nominal")
180 parser.add_option("-d", "--mode", dest="mode",
181  help="neutrino or antineutrino mode", default="neutrino")
182 parser.add_option("-n", "--n_events", dest="n_events",
183  help="Number of events per job", default=100000)
184 parser.add_option("-i", "--input_dir", dest="input_dir",
185  help="Input directory", default="/pnfs/dune/scratch/users/$USER/fluxfiles/g4lbne")
186 parser.add_option("-o", "--output_dir", dest="output_dir",
187  help="Input directory", default="/dune/data/users/$USER/fluxfiles/g4lbne")
188 parser.add_option("-l","--location",dest="location",help="detector location",default="LBNEFD")
189 parser.add_option("-v","--version",dest="version",help="g4lbne version number",default="v3r5p4")
190 
191 (options, args) = parser.parse_args()
192 
193 
194 if options.g4lbne_dir.find("$USER")>=0:
195  options.g4lbne_dir = options.g4lbne_dir.replace("$USER",os.getenv("USER"));
196 if options.input_dir.find("$USER")>=0:
197  options.input_dir = options.input_dir.replace("$USER",os.getenv("USER"));
198 if options.output_dir.find("$USER")>=0:
199  options.output_dir = options.output_dir.replace("$USER",os.getenv("USER"));
200 
201 physics_list = options.physics_list
202 print "Using GEANT4 physics list",physics_list
203 
204 ###################################################
205 #
206 # Determine G4LBNE Version
207 #
208 ###################################################
209 version=options.version
210 
211 ###################################################
212 #
213 # Print options
214 #
215 ###################################################
216 print "Using the g4lbne installed at",options.g4lbne_dir
217 print "Assuming each file corresponds to ",options.n_events,"protons on target."
218 print "Using macro "+options.macro
219 print "Using input location:",options.input_dir
220 print "Using output location:",options.output_dir
221 print "Using location:",options.location
222 
223 ###################################################
224 #
225 # Find files
226 #
227 ###################################################
228 file_prefix = "histos_g4lbne_"+version+"_"+physics_list+"_"+options.macro+"_"+options.mode;
229 input_flux_dir = options.input_dir+"/"+version+"/"+physics_list+"/"+options.macro+"/"+options.mode+"/flux/"
230 output_flux_dir = options.output_dir+"/"+version+"/"+physics_list+"/"+options.macro+"/"+options.mode+"/flux/"
231 
232 if not os.path.exists(output_flux_dir):
233  os.makedirs(output_flux_dir)
234 
235 plot_files = []
236 fastmc_files = []
237 globes_files = []
238 
239 for file in os.listdir(input_flux_dir+"/histos/"):
240 
241  if file.endswith("_"+options.location+".root"):
242  plot_files.append(os.path.join(input_flux_dir+"/histos/",file));
243  elif file.endswith("_"+options.location+"_fastmc.root"):
244  fastmc_files.append(os.path.join(input_flux_dir+"/histos/",file));
245  elif file.endswith("_"+options.location+"_globes_flux.txt"):
246  globes_files.append(os.path.join(input_flux_dir+"/histos/",file));
247  else:
248  continue
249 
250 print "Found "+str(len(plot_files))+" histograms to merge."
251 print "Found "+str(len(fastmc_files))+" fastmc files to merge."
252 print "Found "+str(len(globes_files))+" globes files to merge."
253 
254 merge_histos(plot_files,output_flux_dir+"/"+file_prefix+"_"+options.location+".root");
255 merge_histos(fastmc_files,output_flux_dir+"/"+file_prefix+"_"+options.location+"_fastmc.root");
256 merge_globes(globes_files,output_flux_dir+"/"+file_prefix+"_"+options.location+"_globes_flux.txt");
257 
258 
int open(const char *, int)
Opens a file descriptor.
def is_good_histo_file(myfile)
def merge_globes(filevec, outfile)
def merge_histos(filevec, outfile)
static QCString str