cafExposure.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 import argparse
3 import os
4 from collections import OrderedDict
5 
6 parser = argparse.ArgumentParser(description='Count the events/POT/exposure in the files specified by a set of constraints')
7 parser.add_argument('--dir', '-d', help='Directory of files', type=str)
8 parser.add_argument('--match', '-m', help='Match pattern for files', type=str, action='append')
9 parser.add_argument('--printName', '-p', help='Prints filename of counted files', action='store_true')
10 parser.add_argument('files', nargs = '*',
11  help = 'List of files to operate on. Or use --dir and --match.')
12 args = parser.parse_args()
13 
14 # ROOT hijacks the help text, so delay importing it until argparse has done its
15 # thing.
16 import ROOT
17 ROOT.gErrorIgnoreLevel=100000
18 
19 if args.files and args.dir:
20  print 'Error: --dir is not compatible with explicitly listing files. Use one or the other'
21  exit(1)
22 
23 if not args.files and not args.dir:
24  print 'Must specify either --dir or a list of files. See --help text'
25  exit(1)
26 
27 if not args.match:
28  args.match = []
29 
30 fileList = []
31 
32 if args.files:
33  fileList = args.files
34 
35 if args.dir:
36  fileList = [os.path.join(args.dir, file) for file in os.listdir(args.dir)]
37 
38 
39 if not fileList:
40  raise Exception("No file list found. Need to pass in a directory with --dir")
41 
42 hists = OrderedDict()
43 hists["TotalPOT"] = ROOT.TH1F("TotalPOT", ";Arbitrary;Total POT", 1, 0,1)
44 hists["TotalEvents"] = ROOT.TH1F("TotalEvents", ";Arbitrary; Total Events", 1,0,1)
45 hists["TotalLivetime"] = ROOT.TH1F("TotalLivetime", ";Arbitrary; Total Livetime", 1,0,1)
46 
47 nFiles = 0
48 for fileName in fileList:
49  if not ".caf." in os.path.basename(fileName):
50  continue
51 
52  match = True
53  for string in args.match:
54  if not string in os.path.basename(fileName):
55  match = False
56  break
57  if not match:
58  continue
59  file = ROOT.TFile.Open(fileName, "READ")
60  if args.printName:
61  print os.path.basename(fileName)
62  if file and not (file.IsZombie() or file.TestBit(ROOT.TFile.kRecovered)):
63  nFiles += 1
64  for histKey in hists.keys():
65  hists[histKey].Add(file.Get(histKey))
66 
67 print 'Number of files :', nFiles
68 for histKey in hists.keys():
69  print histKey, ":", hists[histKey].Integral()
70 
71 
72 
73 
74 
75 
76 
77 
78 
double Integral(const Spectrum &s, const double pot, int cvnregion)
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
exit(0)