2 from PhysicsTools.NanoAODTools.postprocessing.framework.jobreport
import JobReport
3 from PhysicsTools.NanoAODTools.postprocessing.framework.preskimming
import preSkim
4 from PhysicsTools.NanoAODTools.postprocessing.framework.output
import FriendOutput, FullOutput
6 from PhysicsTools.NanoAODTools.postprocessing.framework.datamodel
import InputTree
7 from PhysicsTools.NanoAODTools.postprocessing.framework.branchselection
import BranchSelection
13 ROOT.PyConfig.IgnoreCommandLineOptions =
True 18 self, outputDir, inputFiles, cut=None, branchsel=None, modules=[],
19 compression="LZMA:9
", friend=False, postfix=None, jsonInput=None, 20 noOut=False, justcount=
False, provenance=
False, haddFileName=
None,
21 fwkJobReport=
False, histFileName=
None, histDirName=
None,
22 outputbranchsel=
None, maxEntries=
None, firstEntry=0, prefetch=
False,
36 self.
jobReport = JobReport()
if fwkJobReport
else None 41 print(
"Because you requested a FJR we assume you want the final " \
42 "hadd. No name specified for the output file, will use tree.root")
45 if outputbranchsel
is not None:
47 elif outputbranchsel
is None and branchsel
is not None:
56 self.
maxEntries = maxEntries
if maxEntries
else 9223372036854775807
63 tmpdir = os.environ[
'TMPDIR']
if 'TMPDIR' in os.environ
else "/tmp" 64 if not fname.startswith(
"root://"):
66 rndchars =
"".
join([hex(i)[2:]
for i
in bytearray(os.urandom(8))]) \
68 % (os.getuid(), hashlib.sha1(fname.encode(
'utf-8')).hexdigest())
69 localfile =
"%s/%s-%s.root" \
70 % (tmpdir, os.path.basename(fname).
replace(
".root",
""), rndchars)
73 print(
"Filename %s is already available in local path %s " \
75 return localfile,
False 78 print(
"Filename %s is remote, will do a copy to local path %s"\
81 subprocess.check_output([
"xrdcp",
"-f",
"-N", fname, localfile])
83 print(
"Time used for transferring the file locally: %.2f s"\
84 % (time.time() - start))
88 print(
"Error: could not save file locally, will run from remote")
89 if os.path.exists(localfile):
91 print(
"Deleting partially transferred file %s" % localfile)
100 "_Friend" if self.
friend else "_Skim")
104 ROOT.gInterpreter.ProcessLine(
"#include <Compression.h>")
106 compressionLevel =
int(level)
108 compressionAlgo = ROOT.ROOT.kLZMA
110 compressionAlgo = ROOT.ROOT.kZLIB
112 compressionAlgo = ROOT.ROOT.kLZ4
114 raise RuntimeError(
"Unsupported compression %s" % algo)
127 "Running with --noout and no modules does nothing!")
132 "Must specify both histogram file and histogram directory!")
139 if hasattr(m,
'writeHistFile')
and m.writeHistFile:
145 fullClone = (len(self.
modules) == 0)
152 fnames = fname.split(
',')
153 fname, ffnames = fnames[0], fnames[1:]
155 fname = fname.strip()
158 if fname.startswith(
'/store/') :
159 fname = subprocess.check_output([
'edmFileUtil',
'-d',
'-f '+fname]).
decode(
"utf-8").
strip()
164 inFile = ROOT.TFile.Open(ftoread)
166 inFile = ROOT.TFile.Open(fname)
169 inTree = inFile.Get(
"Events")
171 inTree = inFile.Get(
"Friends")
172 nEntries =
min(inTree.GetEntries() -
174 totEntriesRead += nEntries
179 print(
'Would select %d / %d entries from %s (%.2f%%)' % (elist.GetN()
if elist
else nEntries, nEntries, fname, (elist.GetN()
if elist
else nEntries) / (0.01 * nEntries)
if nEntries
else 0))
185 print(
'Pre-select %d entries out of %s (%.2f%%)' % (elist.GetN()
if elist
else nEntries, nEntries, (elist.GetN()
if elist
else nEntries) / (0.01 * nEntries)
if nEntries
else 0))
188 for ffname
in ffnames:
189 inAddFiles.append(ROOT.TFile.Open(ffname))
190 inAddTree = inAddFiles[-1].
Get(
"Events")
191 if inAddTree
is None:
192 inAddTree = inAddFiles[-1].
Get(
"Friends")
193 inAddTrees.append(inAddTree)
194 inTree.AddFriend(inAddTree)
199 inTree.SetEntryList(elist)
209 outFileName = os.path.join(self.
outputDir, os.path.basename(
210 fname).
replace(
".root", outpostfix +
".root"))
211 outFile = ROOT.TFile.Open(
212 outFileName,
"RECREATE",
"", compressionLevel)
213 outFileNames.append(outFileName)
215 outFile.SetCompressionAlgorithm(compressionAlgo)
218 outTree = FriendOutput(inFile, inTree, outFile)
220 firstEntry = 0
if fullClone
and elist
else self.
firstEntry 221 outTree = FullOutput(
229 firstEntry=firstEntry,
230 jsonFilter=jsonFilter,
239 if not fullClone
and not (elist
and elist.GetN() == 0):
241 nEntries)
if nEntries > 0
and not elist
else None 243 self.
modules, inFile, outFile, inTree, outTree,
244 eventRange=eventRange, maxEvents=self.
maxEntries 246 print(
'Processed %d preselected entries from %s (%s entries). Finally selected %d entries' % (nall, fname, nEntries, npass))
247 elif outTree
is not None:
249 print(
'Selected %d / %d entries from %s (%.2f%%)' % (outTree.tree().GetEntries(), nall, fname, outTree.tree().GetEntries() / (0.01 * nall)
if nall
else 0))
255 print(
"Done %s" % outFileName)
265 print(
"Total time %.1f sec. to process %i events. Rate = %.1f Hz." % ((time.time() - t0), totEntriesRead, totEntriesRead / (time.time() - t0)))
268 haddnano =
"./haddnano.py" if os.path.isfile(
269 "./haddnano.py")
else "haddnano.py" 270 os.system(
"%s %s %s" %
def prefetchFile(self, fname, verbose=True)
def replace(string, replacements)
def __init__(self, outputDir, inputFiles, cut=None, branchsel=None, modules=[], compression="LZMA:9", friend=False, postfix=None, jsonInput=None, noOut=False, justcount=False, provenance=False, haddFileName=None, fwkJobReport=False, histFileName=None, histDirName=None, outputbranchsel=None, maxEntries=None, firstEntry=0, prefetch=False, longTermCache=False)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
bool decode(bool &, std::string const &)
def split(sequence, size)
static std::string join(char **cmd)
def preSkim(tree, jsonInput=None, cutstring=None, maxEntries=None, firstEntry=0)