BeamWidthStudy.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import ROOT
4 import argparse
5 from datetime import datetime
6 
7 parser = argparse.ArgumentParser(description='Extract the subrun-averaged beam width for a list of Ana files')
8 parser.add_argument('filelist', type=str, help='Path to file with a list of paths to Ana files')
9 parser.add_argument('outfile', type=str, help='Path to output csv file')
10 parser.add_argument('outroot', type=str, help='Path to output root file')
11 args = parser.parse_args()
12 hXwidth = ROOT.TH1F("xwidth","xwidth",150,0.5,2.0)
13 hYwidth = ROOT.TH1F("ywidth","ywidth",150,0.5,2.0)
14 
15 # .............................................................................
16 def GetDataFromInputFile(filepath):
17  file = ROOT.TFile(filepath, 'READ')
18 
19  file.cd("nearlineana")
20  # Get subrun duration and date from Header
21  header = file.FindObjectAny('Header')
22  header.LoadTree(0)
23  header.GetEntry(0)
24  year = header.StartYear
25  month = header.StartMonth
26  day = header.StartDay
27  dayEnd = header.EndDay
28  Hour = header.StartHour
29  HourEnd = header.EndHour
30  run = header.Run
31  subrun = header.Subrun
32 
33  # NOTE: This may look confusing, but the Hour variable is a float, so we
34  # need to extract the minute and second components by taking the difference
35  # between the variable and its integer form.
36  Min = (Hour-int(Hour)) * 60.0
37  Sec = (Min-int(Min)) * 60.0
38 
39  # This handles the situation where a subrun spans two dates
40  if (dayEnd == day):
41  SRduration = 3600.0 * (HourEnd - Hour)
42  else:
43  SRduration = 3600.0 * (HourEnd + 24.0 - Hour)
44 
45  # If the subrun duration is zero, bail on this
46  if (SRduration == 0):
47  return None
48 
49  # Read the histogram and find the mean (code copied from how AnaPlotMaker does this)
50  hXwidthTemp = file.FindObjectAny('fXWidth')
51  hXwidth.Add(hXwidthTemp,1.0)
52  Xwidth = hXwidthTemp.GetMean(1)
53  hYwidthTemp = file.FindObjectAny('fYWidth')
54  hYwidth.Add(hYwidthTemp,1.0)
55  Ywidth = hYwidthTemp.GetMean(1)
56 
57  unixtime = datetime(year, month, day, int(Hour), int(Min), int(Sec)).strftime("%s")
58 
59  file.Close()
60 
61  return (run, subrun, unixtime, Xwidth, Ywidth)
62 
63 # .............................................................................
64 def GetInputFilePaths(filepath):
65  file_paths = []
66 
67  file = open(filepath, 'r')
68  for line in file:
69  file_paths.append(line.rstrip('\n'))
70 
71  return file_paths
72 
73 
74 # .............................................................................
75 def main():
76  FILE_LIST = args.filelist
77  OUT_FILE_PATH = args.outfile
78  OUT_ROOT = args.outroot
79  # Extract a list of input file paths
80  file_paths = GetInputFilePaths(FILE_LIST)
81 
82  # Open the out file and write a header
83  out_file = open(OUT_FILE_PATH, "w")
84  out_file.write("run, subrun, unixtime, xwidth, ywidth\n")
85 
86  # Loop over input file paths, process the files, and write results to out file
87  file_idx = 0
88  for file in file_paths:
89  file_idx += 1
90  print('Processing file {}/{}: {}'.format(file_idx, len(file_paths), file))
91  data = GetDataFromInputFile(file)
92  if (data is not None):
93  out_file.write('{}, {}, {}, {}, {}\n'.format(data[0], data[1], data[2], data[3], data[4]))
94 
95  out_file.close()
96  outHistFile = ROOT.TFile.Open(OUT_ROOT,"RECREATE")
97  outHistFile.cd()
98  hXwidth.Write()
99  hYwidth.Write()
100  outHistFile.Close()
101 
102  return
103 
104 
105 # .............................................................................
106 if __name__ == '__main__':
107  main()
bool print
def GetInputFilePaths(filepath)
def GetDataFromInputFile(filepath)
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")