12 from optparse
import OptionParser
13 import sys, os, subprocess, shutil, ROOT,datetime
20 keys = myfile.GetListOfKeys()
21 if "numu_flux" in keys:
22 if myfile.Get(
"numu_flux").Integral()>0:
24 if "numu_flux_forplots" in keys:
25 if myfile.Get(
"numu_flux_forplots").Integral()>0:
32 print "merge_hitsograms.py, mergehistos(): No files to merge." 47 for i
in range(0,len(filevec)):
49 in_tfile = ROOT.TFile(filevec[i]);
56 print "First good file: "+
str(first_file_index)
58 last_time = datetime.datetime.now()
61 for key
in in_tfile.GetListOfKeys():
62 if key.ReadObj().IsA().InheritsFrom(
"TH1"):
63 output_histos.append(in_tfile.Get(key.GetName()).Clone())
65 for i
in range(first_file_index+1,len(filevec)):
67 now = datetime.datetime.now()
68 print "Merging file number",i,
"; time to merge last 50 files:",now-last_time
72 next_tfile = ROOT.TFile(filevec[i])
75 for hist
in output_histos:
76 temphist = next_tfile.Get(hist.GetName())
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)
93 print "merge_histograms.py, merge_globes(): No files to merge." 100 total_flux.append([0,0,0,0,0,0,0])
106 print "merging",len(filevec),
"globes files" 107 last_time = datetime.datetime.now()
108 for i
in range(0,len(filevec)):
110 now = datetime.datetime.now()
111 print "Merging file number",i,
"; time since last print:",now-last_time
116 next_file =
open(filevec[i])
117 lines = next_file.readlines()
122 problem_found = (len(lines)!=501)
125 if len(line.split())!=7:
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:
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])])
133 if not problem_found:
136 total_flux[j][0] = flux[j][0]
138 total_flux[j][k] += flux[j][k]
140 print "Number of good globes files found:",n_files
143 total_flux[i][j] = total_flux[i][j]/n_files
145 out_file =
open(outfile,
"w");
146 print "writign to "+outfile
149 out_file.write(
str(total_flux[i][j])+
" ")
162 scriptdir = os.path.abspath(sys.argv[0]+
"/../..")
163 print "default g4lbne directory:",scriptdir
171 usage =
"usage: %prog [options]" 172 parser = OptionParser(usage=usage)
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")
191 (options, args) = parser.parse_args()
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"));
201 physics_list = options.physics_list
202 print "Using GEANT4 physics list",physics_list
209 version=options.version
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
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/" 232 if not os.path.exists(output_flux_dir):
233 os.makedirs(output_flux_dir)
239 for file
in os.listdir(input_flux_dir+
"/histos/"):
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));
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." 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");
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)