mkDefs.py
Go to the documentation of this file.
1 #!/bin/env python
2 from __future__ import print_function
3 import os
4 import sys
5 import argparse
6 import string
7 import NovaGridUtils
8 from NovaGridUtils import *
9 import samweb_client
10 
11 test=False
12 
13 daqReleases={}
14 daqReleases["fd"]="S15-03-11"
15 daqReleases["nd"]="S16-02-02"
16 
17 samweb = samweb_client.SAMWebClient(experiment="nova")
18 
19 def deleteDef(defname):
20  if test:
21  info("Would delete %s" % defname)
22  return
23 
24  info("About to delete %s, y/n?" % defname)
25  while True:
26  c=sys.stdin.read(1)
27  if c=="y":
28  break
29  elif c=="n":
30  print("Quitting w/o doing anything")
31  sys.exit(0)
32  else:
33  print("Please enter 'y' or 'n' only")
34 
35  samweb.deleteDefinition(defname)
36 
37 def createDef(defname,constraints):
38  if test:
39  print("Would create %s with contraints" % defname)
40  print(constraints)
41  if count:
42  nfiles=samweb.countFiles(constraints)
43  print("and %d files" %(nfiles))
44 
45  return
46 
47  samweb.createDefinition(defname,constraints)
48  if count:
49  nfiles=samweb.countFiles(constraints)
50  info("Made %s with %d files" %(defname,nfiles))
51 
52 prog=sys.argv[0]
53 NovaGridUtils.prog=prog
54 
55 parser=argparse.ArgumentParser(prog=prog,
56  description="Create raw and artdaq definitions")
57 
58 parser.add_argument('--det',
59  required=True,
60  help='Detector (can be neardet or fardet)',
61  type=str)
62 
63 parser.add_argument('--span',
64  required=True,
65  help='Timespan: must be a valid period (eg period1) or epoch (eg epoch3a)',
66  type=str)
67 
68 parser.add_argument('--defType',
69  required=True,
70  help='Type of definition to create (base, raw or artdaq); base implies basic definition w/o specifying a tier',
71  type=str)
72 
73 parser.add_argument('--fileType',
74  required=True,
75  help='Type of files: data or mc',
76  type=str)
77 
78 parser.add_argument('--stream',
79  help='Type of stream (numi or cosmic), required for anything other than --defType=base',
80  type=str)
81 
82 parser.add_argument('--test',
83  help='Do not delete or create datasets, but print definitions and constraints',
84  action='store_true',default=False)
85 
86 parser.add_argument('--count',
87  help='Count files in',
88  action='store_true',default=False)
89 
90 
91 parser.add_argument('--delete',
92  help='delete old definition first (if it exists)',
93  action='store_true',default=False)
94 
95 
96 args=parser.parse_args()
97 test=args.test
98 count=args.count
99 
100 span=args.span
101 
102 defType=args.defType
103 detName=""
104 
105 det=args.det
106 if det=="fd":
107  detName="fardet"
108 elif det=="nd":
109  detName="neardet"
110 else:
111  fail("Invalid Detector %s" %det)
112 
113 fileType=args.fileType
114 if fileType not in ("data","mc") :
115  fail("invalid file type %s, must be data or mc" %fileType)
116 
117 baseDef="base_%s_%s_%s" %(det, fileType, span)
118 
119 
120 if not defType in ("base","raw","artdaq"):
121  fail("Invalid definition type %s" % args.defType)
122 
123 elif fileType == "mc" and defType != "base":
124  fail("fileType mc is only consistent with defType base")
125 
126 streamConstraint=""
127 streamName=""
128 if args.defType != "base":
129  streamName=args.stream
130  try:
131  if streamName=="numi":
132  streamConstraint="online.stream 0"
133  elif streamName=="cosmic":
134  streamConstraint="online.stream 2"
135  elif streamName=="ddactivity":
136  streamConstraint="(file_name like %DDActivity1% or file_name like %ddactivity1%)"
137  else:
138  fail("Invalid stream %s" % streamName)
139  except:
140  fail("Must specify stream if not creating base definition")
141 
142 if args.defType == "raw":
143 
144  defname="prod_raw_%s_%s_%s" %(det,streamName,span)
145  if args.delete:
146  deleteDef(defname)
147 
148  constraints="data_tier raw and %s and defname: %s " %(streamConstraint,baseDef)
149  createDef(defname,constraints)
150 
151  sys.exit(0)
152 
153 elif args.defType == "artdaq":
154 
155  daqRelease=daqReleases[det]
156  defname="prod_artdaq_%s_%s_%s_%s" %(daqRelease,det,streamName,span)
157  if args.delete:
158  deleteDef(defname)
159 
160  #constraints="defname: %s and %s and nova.standard true and data_tier artdaq and daq2rawdigit.base_release %s" %(baseDef,streamConstraint,daqRelease)
161  constraints="data_tier artdaq and %s and nova.standard true and defname: %s and daq2rawdigit.base_release %s" %(streamConstraint,baseDef,daqRelease)
162 
163  createDef(defname,constraints)
164 
165  sys.exit(0)
166 
167 ##if we get here, we're doing the base definitions:
168 
169 inFileName=det + ".txt"
170 inFile=open(inFileName)
171 
172 firstRun=None
173 lastRun=None
174 
175 for line in inFile.readlines():
176  line=line[:-1]
177  fields=line.split()
178  if len(fields) != 3:
179  msg="Malformed line:\n" + line
180  fail(msg)
181 
182  if fields[0] == span:
183  firstRun=fields[1]
184  lastRun=fields[2]
185  break
186 
187 if not firstRun and not lastRun:
188  fail("Invalid time span " % span)
189 
190 try:
191  string.atoi(firstRun)
192 except:
193  fail("First run %s is not an integer" % firstRun)
194 
195 try:
196  string.atoi(lastRun)
197 except:
198  fail("Last run %s is not an integer" % lastRun)
199 
200 constraints=""
201 if fileType=="data":
202  constraints="online.detector %s and online.runnumber >= %s and online.runnumber <= %s and online.partition 1 and file_type importedDetector and event_count > 0 minus NOVA.ProductionSkip True " %(detName,firstRun,lastRun)
203 else:
204  constraints="nova.detectorid %s and simulated.firstrun >= %s and simulated.firstrun <= %s and file_type importedSimulated" %(det,firstRun,lastRun)
205 if args.delete:
206  deleteDef(baseDef)
207 
208 createDef(baseDef,constraints)
209 
210 
def deleteDef(defname)
Definition: mkDefs.py:19
def createDef(defname, constraints)
Definition: mkDefs.py:37
bool print
def fail(msg)
print a failure message, from: https://cdcvs.fnal.gov/redmine/projects/novaart/repository/entry/trunk...
Definition: common_tools.py:7
procfile open("FD_BRL_v0.txt")