2 from __future__
import print_function
12 ROOTPREFIX =
"root://cms-xrd-global.cern.ch/"
15 parser = argparse.ArgumentParser(description=
"Collect a MEs from DQMIO data, with maximum possible granularity")
17 parser.add_argument(
'dataset', help=
'dataset name, like "/StreamHIExpress/HIRun2018A-Express-v1/DQMIO"')
18 parser.add_argument(
'-o',
'--output', help=
'SQLite file to write', default=
'dqmio.sqlite')
19 parser.add_argument(
'-j',
'--njobs', help=
'Number of threads to read files', type=int, default=1)
20 parser.add_argument(
'-l',
'--limit', help=
'Only load up to LIMIT files', type=int, default=-1)
21 args = parser.parse_args()
34 "PixelPhase1/Phase1_MechanicalView/PXBarrel/adc_PXLayer*",
38 inf = re.compile(
"([- \[])inf([,}\]])")
39 nan = re.compile(
"([- \[])nan([,}\]])")
42 for pattern
in interesting_mes:
43 if fnmatch.fnmatch(mename,pattern):
48 if isinstance(x, ROOT.string):
53 if isinstance(x, int):
55 if isinstance(x, float):
57 if isinstance(x, int):
61 rootobj =
unicode(ROOT.TBufferJSON.ConvertToJSON(x))
63 clean = nan.sub(
'\\g<1>0\\g<2>', inf.sub(
'\\g<1>1e38\\g<2>', rootobj))
64 obj = json.loads(clean)
65 jsonobj = json.dumps(obj, allow_nan=
False)
67 except Exception
as e:
68 return json.dumps({
"root2sqlite_error": e.__repr__(),
"root2sqlite_object": x.__repr__()})
71 if not dataset.endswith(
"DQMIO"):
72 raise Exception(
"This tool probably cannot read the dataset you specified. The name should end with DQMIO.")
73 dasquery = [
"dasgoclient",
"-query=file dataset=%s" % dataset]
74 print(
"Querying das ... %s" % dasquery)
75 files = subprocess.check_output(dasquery)
76 files = files.splitlines()
77 print(
"Got %d files." % len(files))
97 CREATE TABLE IF NOT EXISTS monitorelements (
99 fromrun, fromlumi, torun, tolumi,
104 CREATE INDEX runorder ON monitorelements(fromrun, fromlumi);
107 INSERT INTO monitorelements (
109 fromrun, fromlumi, torun, tolumi,
116 SELECT fromlumi, tolumi, fromrun, name, value FROM monitorelements ORDER BY fromrun, fromlumi ASC;
119 db = sqlite3.connect(args.output)
120 db.execute(maketable)
121 db.execute(makeindex)
124 f = ROOT.TFile.Open(ROOTPREFIX + fname)
125 idxtree = getattr(f,
"Indices")
135 for i
in range(idxtree.GetEntries()):
137 run, lumi, metype = idxtree.Run, idxtree.Lumi, idxtree.Type
141 if not treenames[metype]
in interesting_types:
146 endlumi =
max(knownlumis)
147 lumi =
min(knownlumis)
152 firstidx, lastidx = idxtree.FirstIndex, idxtree.LastIndex
153 metree = getattr(f, treenames[metype])
155 metree.SetBranchStatus(
"*",0)
156 metree.SetBranchStatus(
"FullName",1)
158 for x
in range(firstidx, lastidx+1):
160 mename =
str(metree.FullName)
162 if mename.find(
"AlCaReco") != -1:
165 if mename.find(
"Isolated") != -1:
168 if mename.find(
"HLT") != -1:
171 if not ((mename.find(
"SiStrip") >= 0)
or (mename.find(
"OfflinePV") >= 0)
or (mename.find(
"PixelPhase1") >= 0)
or (mename.find(
"Tracking") >= 0 )):
175 metree.GetEntry(x, 1)
178 mes_to_store.append((
180 run, lumi, endrun, endlumi,
188 if args.limit > 0: files = files[:args.limit]
190 pool = multiprocessing.Pool(processes=args.njobs)
192 for mes_to_store
in pool.imap_unordered(harvestfile, files):
194 db.executemany(insertinto, mes_to_store);
197 print(
"Processed %d files of %d, got %d MEs...\r" % (ctr, len(files), len(mes_to_store)), end=
'')
201 // Convert the sqlite format saved above back into a TTree.
202 // Saving TTrees with objects (TH1's) seems to be close to impossible in Python,
203 // so we do the roundtrip via SQLite and JSON in a ROOT macro.
204 // This needs a ROOT with TBufferJSON::FromJSON, which the 6.12 in CMSSW for
205 // for now does not have. We can load a newer version from SFT (on lxplus6,
207 // source /cvmfs/sft.cern.ch/lcg/releases/ROOT/6.16.00-f8770/x86_64-slc6-gcc8-opt/bin/thisroot.sh
208 // root sqlite2tree.C
209 // It is rather slow, but the root file is a lot more compact.
219 auto sql = TSQLiteServer("sqlite:///dev/shm/schneiml/CMSSW_10_5_0_pre1/src/dqmio.sqlite");
220 auto query = "SELECT fromlumi, tolumi, fromrun, name, value FROM monitorelements ORDER BY fromrun, fromlumi ASC;";
221 auto res = sql.Query(query);
223 TFile outfile("/dev/shm/dqmio.root", "RECREATE");
224 auto outtree = new TTree("MEs", "MonitorElements by run and lumisection");
225 auto nameb = outtree->Branch("name", &name);
226 auto valueb = outtree->Branch("value", &value,128*1024);
227 auto runb = outtree->Branch("run", &run);
228 auto fromlumib = outtree->Branch("fromlumi",&fromlumi);
229 auto tolumib = outtree->Branch("tolumi", &tolumi);
232 while (auto row = res->Next()) {
233 fromlumi = atoi(row->GetField(0));
234 tolumi = atoi(row->GetField(1));
235 run = atoi(row->GetField(2));
236 name = new TString(row->GetField(3));
238 TBufferJSON::FromJSON(value, row->GetField(4));