Functions | Variables
merge_histograms Namespace Reference

Functions

def is_good_histo_file (myfile)
 
def merge_histos (filevec, outfile)
 
def merge_globes (filevec, outfile)
 

Variables

 scriptdir = os.path.abspath(sys.argv[0]+"/../..")
 Determine default g4lbne directory (the directory this script is in) More...
 
string usage = "usage: %prog [options]"
 Setup parser that reads in options. More...
 
 parser = OptionParser(usage=usage)
 
 dest
 
 help
 
 default
 
 options
 
 args
 
 g4lbne_dir
 
 input_dir
 
 output_dir
 
 physics_list = options.physics_list
 
 version = options.version
 Determine G4LBNE Version. More...
 
string file_prefix = "histos_g4lbne_"
 Print options. More...
 
string input_flux_dir = options.input_dir+"/"
 
string output_flux_dir = options.output_dir+"/"
 
list plot_files = []
 
list fastmc_files = []
 
list globes_files = []
 

Function Documentation

def merge_histograms.is_good_histo_file (   myfile)

Definition at line 15 of file merge_histograms.py.

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 
def is_good_histo_file(myfile)
def merge_histograms.merge_globes (   filevec,
  outfile 
)

Definition at line 90 of file merge_histograms.py.

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 
int open(const char *, int)
Opens a file descriptor.
def merge_globes(filevec, outfile)
static QCString str
def merge_histograms.merge_histos (   filevec,
  outfile 
)

Definition at line 29 of file merge_histograms.py.

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 
def is_good_histo_file(myfile)
def merge_histos(filevec, outfile)
static QCString str

Variable Documentation

merge_histograms.args

Definition at line 191 of file merge_histograms.py.

merge_histograms.default

Definition at line 175 of file merge_histograms.py.

merge_histograms.dest

Definition at line 174 of file merge_histograms.py.

list merge_histograms.fastmc_files = []

Definition at line 236 of file merge_histograms.py.

string merge_histograms.file_prefix = "histos_g4lbne_"

Print options.

Find files

Definition at line 228 of file merge_histograms.py.

merge_histograms.g4lbne_dir

Definition at line 195 of file merge_histograms.py.

list merge_histograms.globes_files = []

Definition at line 237 of file merge_histograms.py.

merge_histograms.help

Definition at line 175 of file merge_histograms.py.

merge_histograms.input_dir

Definition at line 197 of file merge_histograms.py.

string merge_histograms.input_flux_dir = options.input_dir+"/"

Definition at line 229 of file merge_histograms.py.

merge_histograms.options

Definition at line 191 of file merge_histograms.py.

merge_histograms.output_dir

Definition at line 199 of file merge_histograms.py.

string merge_histograms.output_flux_dir = options.output_dir+"/"

Definition at line 230 of file merge_histograms.py.

merge_histograms.parser = OptionParser(usage=usage)

Definition at line 172 of file merge_histograms.py.

merge_histograms.physics_list = options.physics_list

Definition at line 201 of file merge_histograms.py.

list merge_histograms.plot_files = []

Definition at line 235 of file merge_histograms.py.

merge_histograms.scriptdir = os.path.abspath(sys.argv[0]+"/../..")

Determine default g4lbne directory (the directory this script is in)

Definition at line 162 of file merge_histograms.py.

string merge_histograms.usage = "usage: %prog [options]"

Setup parser that reads in options.

Definition at line 171 of file merge_histograms.py.

merge_histograms.version = options.version

Determine G4LBNE Version.

Definition at line 209 of file merge_histograms.py.