2 from __future__
import print_function
10 from collections
import defaultdict
13 ROOTPREFIX =
"root://cms-xrd-global.cern.ch/" 16 parser = argparse.ArgumentParser(description=
"Collect MEs for given lumisections from DQMIO data and upload to a DQMGUI. " +
17 "The from-to lumi range will be shown in an artificial run number of form 1xxxxyyyy, while the run number goes into the lumi number field.")
19 parser.add_argument(
'dataset', help=
'dataset name, like "/StreamHIExpress/HIRun2018A-Express-v1/DQMIO"')
20 parser.add_argument(
'-r',
'--run', help=
'Run number of run to process', default=
None, type=int)
21 parser.add_argument(
'-l',
'--lumis', help=
'JSON file with runs/lumisecitons to process (golden JSON format)', default=
None)
22 parser.add_argument(
'-u',
'--upload', help=
'Upload files to this GUI, instead of just creating them. Delete files after upload.', default=
None)
23 parser.add_argument(
'-j',
'--njobs', help=
'Number of threads to read files', type=int, default=1)
24 parser.add_argument(
'-m',
'--me', help=
'Glob pattern of MEs to load.', default=[], action=
'append')
25 parser.add_argument(
'--limit', help=
'Only load up to LIMIT files', type=int, default=-1)
26 parser.add_argument(
'--perlumionly', help=
'Only save MEs that cover exactly one lumisection, and use simplified "run" numbers (10xxxx)', action=
'store_true')
27 args = parser.parse_args()
41 interesting_mes = args.me
42 if not interesting_mes:
43 print(
"No --me patterns given. This is fine, but output *will* be empty.")
45 if args.upload
and "https:" in args.upload:
46 print(
"Refuing to upload to production servers, only http upload to local servers allowed.")
49 uploadurl = args.upload
52 if not dataset.endswith(
"DQMIO"):
53 raise Exception(
"This tool probably cannot read the dataset you specified. The name should end with DQMIO.")
54 dasquery = [
"dasgoclient",
"-query=file dataset=%s" % dataset]
55 print(
"Querying das ... %s" % dasquery)
56 files = subprocess.check_output(dasquery)
57 files = files.splitlines()
58 print(
"Got %d files." % len(files))
62 if args.limit > 0: files = files[:args.limit]
65 with open(args.lumis)
as f:
67 lumiranges = {
int(run): lumis
for run, lumis
in j.iteritems()}
71 lumiranges = {args.run : []}
79 return "1%05d" % (lumi)
83 return "1%04d%04d" % (lumi, endlumi)
102 for pattern
in interesting_mes:
103 if fnmatch.fnmatch(mename, pattern):
107 if not lumiranges:
return True 108 if run
not in lumiranges:
return False 109 lumis = lumiranges[run]
110 if not lumis:
return True 111 for start, end
in lumis:
112 if lumi >= start
and lumi <= end:
117 dir = parent_dir.Get(name)
119 dir = parent_dir.mkdir(name)
124 for directory
in path[:-1]:
130 f = ROOT.TFile.Open(ROOTPREFIX + fname)
131 idxtree = getattr(f,
"Indices")
141 for i
in range(idxtree.GetEntries()):
143 run, lumi, metype = idxtree.Run, idxtree.Lumi, idxtree.Type
147 if not treenames[metype]
in interesting_types:
153 endlumi =
max(knownlumis)
154 lumi =
min(knownlumis)
160 if perlumionly
and lumi != endlumi:
168 filename =
"DQM_V0001_R%s__perlumiharvested__perlumi%d_%s_v1__DQMIO.root" % (
fake_run(lumi, endlumi), run, treenames[metype])
169 prefix = [
"DQMData",
"Run %s" %
fake_run(lumi, endlumi)]
175 firstidx, lastidx = idxtree.FirstIndex, idxtree.LastIndex
176 metree = getattr(f, treenames[metype])
179 metree.SetBranchStatus(
"*",0)
180 metree.SetBranchStatus(
"FullName",1)
182 for x
in range(firstidx, lastidx+1):
184 mename =
str(metree.FullName)
186 metree.GetEntry(x, 1)
191 result_file = ROOT.TFile(filename,
'recreate')
192 path = mename.split(
"/")
193 filepath = prefix + [path[0],
"Run summary"] + path[1:]
194 subsystems.add(path[0])
203 for subsys
in subsystems:
205 gotodir(result_file, prefix + [subsys,
"Run summary",
"EventInfo",
"blub"])
206 s = ROOT.TObjString(
"<iRun>i=%s</iRun>" %
fake_run(lumi, endlumi))
208 s = ROOT.TObjString(
"<iLumiSection>i=%s</iLumiSection>" % run)
212 files.append(filename)
217 uploadcommand = [
"visDQMUpload.py", uploadurl, filename]
218 print(
"Uploading ... %s" % uploadcommand)
219 subprocess.check_call(uploadcommand)
221 pool = multiprocessing.Pool(processes=args.njobs)
223 for outfiles
in pool.imap_unordered(harvestfile, files):
230 print(
"Processed %d files of %d, got %d out files...\r" % (ctr, len(files), len(outfiles)), end=
'')
def check_interesting(mename)
def create_dir(parent_dir, name)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
def fake_run(lumi, endlumi)
def rangecheck(run, lumi)