ATLAS Offline Software
jetMakeRefSamples.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4 
5 from __future__ import print_function
6 
7 
18 
19 import os, os.path
20 import sys
21 import shlex, subprocess
22 import argparse
23 
24 parser = argparse.ArgumentParser(description='''
25 Creates reference ESD/AOD by running a series of Reco_tf.py from a given input
26 file (typically RDO or RAW).
27 Performs typically 2 jobs :
28  RDO/RAW --> ESD
29  ESD --> AOD
30 Each step produces a file which is expected to be similar to the reference
31 files in /afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/.
32 
33  Ex:
34  %s MC # -> create reference files from /afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/MC/RDO
35  %s MC MyDir # -> same but creates files under MyDir/
36  %s ESD.root MyDir # -> create an AOD reference file from ESD.root under MyDir/
37 '''%((os.path.basename(sys.argv[0]),)*3),
38  formatter_class=argparse.RawDescriptionHelpFormatter,
39  )
40 
41 parser.add_argument('sampleType', type=str, help='full path to a file. Or "mc" or "data" : will start with a reference RDO or RAW file from the JetEtmiss afs space' )
42 parser.add_argument('outputDir', type=str, nargs='?', default='jetRefFiles/')
43 
44 parser.add_argument('-n', '--noRun', action='store_true', help='Do not run the Reco_tf commands, just print them.')
45 parser.add_argument( '--maxEvents', type=int, default=50, help="Propaged to the maxEvents of the Reco_tf commands")
46 parser.add_argument( '--oneStep', action='store_true' , help="stop after 1st step processed")
47 parser.add_argument( '--overWrite', action='store_true' , help="force to overWrite the output dir")
48 
49 
50 arg=parser.parse_args()
51 
52 
53 #print (arg.sampleType , arg.outputDir, arg.noRun)
54 
55 maxEvents=arg.maxEvents
56 skipEvents=0
57 logFile="output.log"
58 
59 #Geometry/conditions tags:
60 # Matched what was done for the official production of the sample at the time of this writing
61 # geometryVersion=ATLAS-GEO-20-00-01
62 # conditionsTagMC=OFLCOND-MC12-SDR-06
63 # conditionsTagData=COMCOND-BLKPA-RUN1-01
64 # http://www-f9.ijs.si/atlpy/atlprod/prodrequest/?q=mc12_8TeV.117050.PowhegPythia_P2011C_ttbar
65 # http://panda.cern.ch/server/pandamon/query/?reqid=1162571&mode=showtask0
66 # https://twiki.cern.ch/twiki/bin/viewauth/AtlasComputing/CoolProdTags
67 specifyCondAndGeo= False # true to use the tags below, false to autoconfigure everything
68 geometryVersion="ATLAS-GEO-20-00-01"
69 conditionsTagMC="OFLCOND-MC12-SDR-06"
70 conditionsTagData="COMCOND-BLKPA-RUN1-01"
71 IsCOMMON=True
72 
73 
74 # Default input files
75 #defaultMC="/afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/MC/HITS/mc12_8TeV.CurrentRef.HITS.pool.root"
76 #defaultMC="/afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/MC/HITS/mc12_8TeV.117050.PowhegPythia_P2011C_ttbar.simul.HITS.e1728_s1581.039772.pool.root.1"
77 defaultMC="/afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/MC/RDO/mc12_8TeV.CurrentRef.RDO.pool.root"
78 defaultData="/afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/DATA/RAW/data12_8TeV.CurrentRef.RAW"
79 #defaultData="/afs/cern.ch/atlas/groups/JetEtmiss/ReferenceFiles/RTT/DATA/RAW/data12_8TeV.00209109.physics_JetTauEtmiss.merge.RAW._lb0186._SFO-1._0001.1"
80 
81 
82 def makeDirAndCd(dir):
83  if not os.path.exists(dir):
84  os.mkdir(dir)
85  elif not arg.overWrite:
86  print ()
87  print ("ERROR !!!")
88  print (" Directory ",dir,'already exists. Not overwriting it (use --overWrite option if needed)')
89  sys.exit(1)
90 
91  os.chdir(dir)
92 
93 
94 def runStep(inputFile):
95  if not os.path.exists(inputFile) and not arg.noRun:
96  print ('ERROR input file ', inputFile , ' missing')
97  sys.exit(2)
98 
99  preExec = ''
100  transform="Reco_tf.py"
101 
102  inputFileBase = os.path.basename(inputFile)
103 
104  if 'HITS' in inputFileBase:
105  inputType = 'HITS'
106  outputType = "RDO"
107  #transform="Digi_trf.py"
108  preExec=""
109 
110  elif "RDO" in inputFileBase:
111  inputType="RDO"
112  inputDataType=inputType
113  outputType="ESD"
114  preExec='''flags.Reco.EnableTrigger=False; flags.Reco.EnableBTagging=False'''
115 
116  elif "RAW" in inputFileBase:
117  inputType="RAW"
118  inputDataType="BS" #bytestream
119  outputType="ESD"
120  preExec='''flags.Reco.EnableTrigger=False'''
121 
122  elif 'ESD' in inputFileBase:
123  inputType="ESD"
124  inputDataType=inputType
125  outputType="AOD"
126  preExec='''flags.Reco.EnableTrigger=False'''
127 
128  else:
129  print ("ERROR RunStep: Input file does not appear to be a supported type (RAW, HITS, RDO, ESD)")
130  print (' -> got', inputFileBase)
131  sys.exit(3)
132 
133  print ("Starting ",inputType, ' to ', outputType)
134 
135 
136  runDir = arg.outputDir + '/' + inputType+'to'+outputType + '/'
137  if not os.path.exists(runDir):
138  os.mkdir(runDir)
139  outputFile = runDir + inputFileBase.replace(inputType, outputType)
140  if arg.overWrite and os.path.exists(outputFile):
141  os.remove(outputFile)
142 
143  outputLog = runDir+'log'
144 
145  if specifyCondAndGeo:
146  # from older sh script
147  fullComand="${transform} preExec=${preExec} input${inputDataType}File=${inputDir}/${inputFile} output${outputType}File=${outputFile} maxEvents=${maxEvents} skipEvents=${skipEvents} geometryVersion=$geometryVersion conditionsTag=$conditionsTag autoConfiguration=everything"
148  else:
149  comandArgs = [ '--preExec='+preExec,
150  '--input%sFile=%s'%(inputDataType, inputFile),
151  '--output%sFile=%s'%(outputType, outputFile),
152  '--maxEvents='+str(maxEvents),
153  '--skipEvents='+str(skipEvents),
154  '--autoConfiguration=everything',
155  #'--postExec=saxasxa'
156  ]
157 
158  print ('Running : ')
159  print (transform, ' '.join(comandArgs))
160  print()
161 
162  if arg.noRun :
163  res = 0
164  else:
165  logfile = open(outputLog, 'w')
166  res = subprocess.call( [transform] + comandArgs, stdout=logfile, stderr=logfile)
167  logfile.close()
168 
169  if res != 0:
170  print ('ERROR RunStep: Transform appears to have failed - exiting. Check ', outputLog)
171  sys.exit(4)
172 
173 
174 
175  print ('Done %s to %s step'%(inputType, outputType))
176  return outputFile
177 
178 
179 def runChain(inputFile):
180  print ('runChain ', inputFile)
181  while any( [t in os.path.basename(inputFile) for t in ['HITS','RDO','RAW','ESD'] ] ):
182  print ('runStep at ',os.path.basename(inputFile))
183  inputFile = runStep(inputFile)
184  if arg.oneStep:
185  return
186 
187 
188 if arg.sampleType.lower() == 'mc':
189  inputFile=defaultMC
190 elif arg.sampleType.lower() == 'data':
191  inputFile=defaultData
192 else:
193  # assume sampleType is a path
194  inputFile = os.path.abspath(arg.sampleType)
195 
196 if not os.path.exists(inputFile):
197  print ('ERROR Input path does not exist ', inputFile)
198  sys.exit(5)
199 
200 
201 arg.outputDir = os.path.abspath(arg.outputDir)
202 
203 makeDirAndCd( arg.outputDir )
204 
205 runChain(inputFile)
206 
207 sys.exit(0)
jetMakeRefSamples.runStep
def runStep(inputFile)
Definition: jetMakeRefSamples.py:94
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
Trk::open
@ open
Definition: BinningType.h:40
jetMakeRefSamples.makeDirAndCd
def makeDirAndCd(dir)
Definition: jetMakeRefSamples.py:82
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
jetMakeRefSamples.runChain
def runChain(inputFile)
Definition: jetMakeRefSamples.py:179