CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
findQualityFiles.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 ######################################################
4 ### See documentation at
5 ### https://twiki.cern.ch/twiki/bin/view/CMS/FindQualityFilesPy
6 ### also run it with -h option
7 ######################################################
8 
9 import os,sys, DLFCN
10 import optparse
11 
12 # for RunInfo API
13 from pluginCondDBPyInterface import *
14 from CondCore.Utilities import iovInspector as inspect
16 
17 ######################################################
18 # functions definitions
19 
20 
21 #########################
22 # get good B field runs from RunInfo DB
23 def getGoodBRuns(options):
24 
25  runs_b_on = []
26 
27  sys.setdlopenflags(DLFCN.RTLD_GLOBAL+DLFCN.RTLD_LAZY)
28 
29  a = FWIncantation()
30  #os.putenv("CORAL_AUTH_PATH","/afs/cern.ch/cms/DB/conddb")
31  rdbms = RDBMS("/afs/cern.ch/cms/DB/conddb")
32 
33  db = rdbms.getDB(options.dbName)
34  tags = db.allTags()
35 
36  if options.printTags:
37  print "\nOverview of all tags in "+options.dbName+" :\n"
38  print tags
39  print "\n"
40  sys.exit()
41 
42  # for inspecting last run after run has started
43  #tag = 'runinfo_31X_hlt'
44  tag = options.dbTag
45 
46  # for inspecting last run after run has stopped
47  #tag = 'runinfo_test'
48 
49  try :
50  #log = db.lastLogEntry(tag)
51 
52  #for printing all log info present into log db
53  #print log.getState()
54 
55  iov = inspect.Iov(db,tag)
56  #print "########overview of tag "+tag+"########"
57  #print iov.list()
58 
59  if v>1 :
60  print "######## summries ########"
61  for x in iov.summaries():
62  print x[0], x[1], x[2] ,x[3]
63 
64  what={}
65 
66  if v>1 :
67  print "###(start_current,stop_current,avg_current,max_current,min_current,run_interval_micros) vs runnumber###"
68  print iov.trend(what)
69 
70  if v>0:
71  print "######## trends ########"
72  for x in iov.trendinrange(what,options.startRun-1,options.endRun+1):
73  if v>0 or x[0]==67647L or x[0]==66893L or x[0]==67264L:
74  print x[0],x[1] ,x[2], x[2][4], x[2][3]
75  #print x[0],x[1] ,x[2], x[2][4], timeStamptoUTC(x[2][6]), timeStamptoUTC(x[2][7])
76  if x[2][4] >= minI and x[2][3] <= maxI:
77  runs_b_on.append(int(x[0]))
78 
79  except Exception, er :
80  print er
81 
82  print "### runs with good B field ###"
83  print runs_b_on
84 
85  return runs_b_on
86 
87 
88 #########################
89 # obtaining list of good quality runs
90 
91 def getGoodQRuns(options):
92 
93  runs_good_dq = []
94 
95  dbs_quiery = "find run where dataset="+options.dqDataset+" and dq="+options.dqCriteria
96 
97  os.system('python $DBSCMD_HOME/dbsCommandLine.py -c search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_full_of_pink_bunnies')
98 
99  #print 'python $DBSCMD_HOME/dbsCommandLine.py -c search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_full_of_pink_bunnies'
100 
101  ff = open('/tmp/runs_full_of_pink_bunnies', "r")
102  line = ff.readline()
103  while line and line!='':
104  runs_good_dq.append(int(line))
105  line = ff.readline()
106  ff.close()
107 
108  os.system('rm /tmp/runs_full_of_pink_bunnies')
109 
110  print "### runs with good quality ###"
111  print runs_good_dq
112 
113  return runs_good_dq
114 
115 
116 
117 ######################################################
118 # To parse commandline args
119 
120 usage='%prog [options]\n\n'+\
121  'Creates a Python configuration file with filenames for runs in specified run range, with certain min B field and data quality requirements.'
122 
123 parser=optparse.OptionParser(usage)
124 
125 parser.add_option("-d", "--alcaDataset",
126  help="[REQUIRED] Name of the input AlCa dataset to get filenames from.",
127  type="string",
128  #default="/Cosmics/Commissioning08-2213_Tosca090322_2pi_scaled_ReReco_FromTrackerPointing-v1/RAW-RECO",
129  #default="/Cosmics/Commissioning08_CRAFT_ALL_V11_StreamALCARECOMuAlGlobalCosmics_227_Tosca090216_ReReco_FromTrackerPointing_v5/ALCARECO",
130  default='',
131  dest="alcaDataset")
132 
133 parser.add_option("-m", "--isMC",
134  help="Whether sample is MC (true) or real data (false).",
135  type="string",
136  default="false",
137  dest="isMC")
138 
139 parser.add_option("-s", "--startRun",
140  help="First run number in range.",
141  type="int",
142  default=0L,
143  dest="startRun")
144 
145 parser.add_option("-e", "--endRun",
146  help="Last run number in range.",
147  type="int",
148  default=999999999L,
149  dest="endRun")
150 
151 parser.add_option("-b", "--minB",
152  help="Lower limit on minimal B field for a run.",
153  type="float",
154  #default=3.77,
155  default=0.,
156  dest="minB")
157 
158 parser.add_option("--maxB",
159  help="Upper limit on B field for a run.",
160  type="float",
161  default=999.,
162  dest="maxB")
163 
164 parser.add_option("-t", "--dbTag",
165  help="Runinfo DB tag to use.",
166  type="string",
167  default="runinfo_31X_hlt",
168  dest="dbTag")
169 
170 parser.add_option("--printTags",
171  help="If present, the only thing script will do is printing list of tags in the DB",
172  action="store_true",
173  default=False,
174  dest="printTags")
175 
176 parser.add_option("--dbName",
177  help="RunInfo DB name to use. The default one is "+\
178  "'oracle://cms_orcoff_prod/CMS_COND_31X_RUN_INFO'",
179  type="string",
180  default="oracle://cms_orcoff_prod/CMS_COND_31X_RUN_INFO",
181  dest="dbName")
182 
183 parser.add_option("--dqDataset",
184  help="Dataset name to query for good data quality runs. "+\
185  "If this option is not used, dqDataset=alcaDataset is automatically set. "+\
186  "If alcaDataset does not have DQ information use /Cosmics/Commissioning08-v1/RAW for CRAFT08 "+\
187  "and use /Cosmics/CRAFT09-v1/RAW for CRAFT08",
188  type="string",
189  #default="/Cosmics/Commissioning08-v1/RAW",
190  #default="/Cosmics/CRAFT09-v1/RAW",
191  default="",
192  dest="dqDataset")
193 
194 parser.add_option("-c", "--dqCriteria",
195  help="Set of DQ criteria to use with -dq flag of dbs.\n"+\
196  "An example of a really strict condition:\n"
197  "'DT_Shift_Offline=GOOD&CSC_Shift_Offline=GOOD&SiStrip_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD'",
198  type="string",
199  #default="DT_Shift_Offline=GOOD&SiStrip_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD",
200  #default="DT_Shift_Offline=GOOD&Pixel_Shift_Offline=GOOD",
201  #default="DT_Shift_Offline=GOOD",
202  default="",
203  dest="dqCriteria")
204 
205 parser.add_option("-o", "--outputFile",
206  help="Name for output file (please include the .py suffix)",
207  type="string",
208  default="filelist.py",
209  dest="outputFile")
210 
211 parser.add_option("-v", "--verbose",
212  help="Degree of debug info verbosity",
213  type="int",
214  default=0,
215  dest="verbose")
216 
217 options,args=parser.parse_args()
218 
219 #if '' in (options.infilename,
220 # options.outfilename,
221 # options.outputCommands):
222 # raise ('Incomplete list of arguments!')
223 
224 
225 if options.alcaDataset=='' and not options.printTags:
226  print "--alcaDataset /your/dataset/name is required!"
227  sys.exit()
228 
229 if options.dqDataset=='':
230  options.dqDataset = options.alcaDataset
231 
232 if not (options.isMC=='true' or options.isMC=='false'):
233  print "--isMC option can have only 'true' or 'false' arguments"
234  sys.exit()
235 
236 v = options.verbose
237 
238 minI = options.minB*18160/3.8
239 maxI = options.maxB*18160/3.8
240 
241 
242 copyargs = sys.argv[:]
243 for i in range(len(copyargs)):
244  if copyargs[i] == "":
245  copyargs[i] = "\"\""
246 
247 infotofile = ["### %s\n" % " ".join(copyargs)]
248 
249 allOptions = '### ' + copyargs[0] + ' --alcaDataset ' + options.alcaDataset + ' --isMC ' + options.isMC + \
250  ' --startRun ' + str(options.startRun) + ' --endRun '+ str(options.endRun) + \
251  ' --minB ' + str(options.minB) + ' --maxB ' + str(options.maxB) + \
252  ' --dbTag ' + options.dbTag + ' --dqDataset ' + options.dqDataset + ' --dqCriteria "' + options.dqCriteria + '"'\
253  ' --outputFile ' + options.outputFile
254 
255 print "### all options, including default:"
256 print allOptions
257 
258 
259 
260 ######################################################
261 # get good B field runs from RunInfo DB
262 
263 runs_b_on = []
264 
265 if options.isMC=='false':
266  runs_b_on = getGoodBRuns(options)
267 
268  infotofile.append("### runs with good B field ###\n")
269  infotofile.append("### %s\n" % str(runs_b_on))
270 
271 ######################################################
272 # Add requiremment of good quality runs
273 
274 runs_good_dq = []
275 runs_good = []
276 
277 if options.isMC=='false':
278  runs_good_dq = getGoodQRuns(options)
279 
280  infotofile.append("### runs with good quality ###\n")
281  infotofile.append("### %s\n" % str(runs_good_dq))
282 
283  # find intersection of runs_b_on and runs_good_dq
284  runs_good = [val for val in runs_b_on if val in runs_good_dq]
285 
286  print "### runs with good B field and quality ###"
287  print runs_good
288 
289  infotofile.append("### runs with good B field and quality ###\n")
290  infotofile.append("### %s\n" % str(runs_good))
291 
292 ######################################################
293 # Find files for good runs
294 
295 dbs_quiery = "find run, file.numevents, file where dataset="+options.alcaDataset+" and run>="+str(options.startRun)+" and run <="+str(options.endRun)
296 
297 os.system('python $DBSCMD_HOME/dbsCommandLine.py -c search --noheader --query="'+dbs_quiery+'" | sort > /tmp/runs_and_files_full_of_pink_bunnies')
298 
299 list_of_files = []
300 list_of_runs = []
301 total_numevents = 0
302 
303 ff = open('/tmp/runs_and_files_full_of_pink_bunnies','r')
304 for line in ff:
305  (run, numevents, fname) = line.split(' ')
306  if options.isMC=='false' and (int(run) not in runs_good):
307  continue
308  fname = fname.rstrip('\n')
309  list_of_files.append(fname)
310  list_of_runs.append(run)
311  total_numevents += int(numevents)
312 ff.close()
313 os.system('rm /tmp/runs_and_files_full_of_pink_bunnies')
314 print "### total number of events in those runs = "+str(total_numevents)
315 
316 infotofile.append("### total number of events in those runs = "+str(total_numevents))
317 
318 ######################################################
319 # Write out results
320 
321 # ff = open(options.outputFile+'.txt','w')
322 size = len(list_of_files)
323 # for i in range(0,size):
324 # ff.write(list_of_runs[i] + ", " + list_of_files[i]+"\n")
325 # ff.close()
326 
327 ff = open(options.outputFile,'w')
328 ff.write("".join(infotofile))
329 ff.write("\nfileNames = [\n")
330 comma = ","
331 for i in range(0,size):
332  if i==size-1:
333  comma=""
334  ff.write(" '"+ list_of_files[i] +"'"+comma+" # "+ list_of_runs[i] +"\n")
335 ff.write(']\n')
336 ff.close()
337 
338 
339 
340 
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
def getGoodBRuns
functions definitions
def getGoodQRuns
obtaining list of good quality runs