NNoiseSliceHitPerSRStudy.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 Number of Noise Slice Hits 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 args = parser.parse_args()
11 
12 # .............................................................................
13 def GetDataFromInputFile(filepath):
14  file = ROOT.TFile(filepath, 'READ')
15 
16  file.cd("nearlineana")
17  # Get subrun duration and date from Header
18  header = file.FindObjectAny('Header')
19  header.LoadTree(0)
20  header.GetEntry(0)
21  year = header.StartYear
22  month = header.StartMonth
23  day = header.StartDay
24  dayEnd = header.EndDay
25  Hour = header.StartHour
26  HourEnd = header.EndHour
27  run = header.Run
28  subrun = header.Subrun
29 
30  # NOTE: This may look confusing, but the Hour variable is a float, so we
31  # need to extract the minute and second components by taking the difference
32  # between the variable and its integer form.
33  Min = (Hour-int(Hour)) * 60.0
34  Sec = (Min-int(Min)) * 60.0
35 
36  # This handles the situation where a subrun spans two dates
37  if (dayEnd == day):
38  SRduration = 3600.0 * (HourEnd - Hour)
39  else:
40  SRduration = 3600.0 * (HourEnd + 24.0 - Hour)
41 
42  # If the subrun duration is zero, bail on this
43  if (SRduration == 0):
44  return None
45 
46  # Read the histogram and find the mean (code copied from how AnaPlotMaker does this)
47  hNNoiseSliceHit = file.FindObjectAny('fNumHitsNoise')
48  nNoiseSliceHits = hNNoiseSliceHit.GetMean(1)
49 
50  unixtime = datetime(year, month, day, int(Hour), int(Min), int(Sec)).strftime("%s")
51 
52  file.Close()
53 
54  return (run, subrun, unixtime, nNoiseSliceHits)
55 
56 # .............................................................................
57 def GetInputFilePaths(filepath):
58  file_paths = []
59 
60  file = open(filepath, 'r')
61  for line in file:
62  file_paths.append(line.rstrip('\n'))
63 
64  return file_paths
65 
66 
67 # .............................................................................
68 def main():
69  FILE_LIST = args.filelist
70  OUT_FILE_PATH = args.outfile
71 
72  # Extract a list of input file paths
73  file_paths = GetInputFilePaths(FILE_LIST)
74 
75  # Open the out file and write a header
76  out_file = open(OUT_FILE_PATH, "w")
77  out_file.write("run, subrun, unixtime, rate\n")
78 
79  # Loop over input file paths, process the files, and write results to out file
80  file_idx = 0
81  for file in file_paths:
82  file_idx += 1
83  print('Processing file {}/{}: {}'.format(file_idx, len(file_paths), file))
84  data = GetDataFromInputFile(file)
85  if (data is not None):
86  out_file.write('{}, {}, {}, {}\n'.format(data[0], data[1], data[2], data[3]))
87 
88  out_file.close()
89 
90  return
91 
92 
93 # .............................................................................
94 if __name__ == '__main__':
95  main()
bool print
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
procfile open("FD_BRL_v0.txt")