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.RCompressionSetting.EAlgorithm.kLZMA
110 compressionAlgo = ROOT.RCompressionSetting.EAlgorithm.kZLIB
112 compressionAlgo = ROOT.RCompressionSetting.EAlgorithm.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()
162 print(time.strftime(
"%d-%b-%Y %H:%M:%S %Z", time.localtime()),
" Initiating request to open file %s" %(fname), flush=
True)
165 inFile = ROOT.TFile.Open(ftoread)
167 inFile = ROOT.TFile.Open(fname)
170 inTree = inFile.Get(
"Events")
172 inTree = inFile.Get(
"Friends")
173 nEntries =
min(inTree.GetEntries() -
175 totEntriesRead += nEntries
180 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))
186 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))
189 for ffname
in ffnames:
190 inAddFiles.append(ROOT.TFile.Open(ffname))
191 inAddTree = inAddFiles[-1].
Get(
"Events")
192 if inAddTree
is None:
193 inAddTree = inAddFiles[-1].
Get(
"Friends")
194 inAddTrees.append(inAddTree)
195 inTree.AddFriend(inAddTree)
200 inTree.SetEntryList(elist)
210 outFileName = os.path.join(self.
outputDir, os.path.basename(
211 fname).
replace(
".root", outpostfix +
".root"))
212 outFile = ROOT.TFile.Open(
213 outFileName,
"RECREATE",
"", compressionLevel)
214 outFileNames.append(outFileName)
216 outFile.SetCompressionAlgorithm(compressionAlgo)
219 outTree = FriendOutput(inFile, inTree, outFile)
221 firstEntry = 0
if fullClone
and elist
else self.
firstEntry 222 outTree = FullOutput(
230 firstEntry=firstEntry,
231 jsonFilter=jsonFilter,
240 if not fullClone
and not (elist
and elist.GetN() == 0):
242 nEntries)
if nEntries > 0
and not elist
else None 244 self.
modules, inFile, outFile, inTree, outTree,
245 eventRange=eventRange, maxEvents=self.
maxEntries 247 print(
'Processed %d preselected entries from %s (%s entries). Finally selected %d entries' % (nall, fname, nEntries, npass))
248 elif outTree
is not None:
250 print(
'Selected %d / %d entries from %s (%.2f%%)' % (outTree.tree().GetEntries(), nall, fname, outTree.tree().GetEntries() / (0.01 * nall)
if nall
else 0))
256 print(
"Done %s" % outFileName)
270 print(
"Total time %.1f sec. to process %i events. Rate = %.1f Hz." % ((time.time() - t0), totEntriesRead, totEntriesRead / (time.time() - t0)))
273 haddnano =
"./haddnano.py" if os.path.isfile(
274 "./haddnano.py")
else "haddnano.py" 275 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)
def split(sequence, size)
static std::string join(char **cmd)
def preSkim(tree, jsonInput=None, cutstring=None, maxEntries=None, firstEntry=0)
bool decode(bool &, std::string_view)