modify_metadata_with_upmu_sample_type.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import samweb_client as swc
4 import datetime
5 import ephem
6 import sys
7 
8 def getSampleType(t_start):
9  if t_start ==0:
10  return "NULL"
11  deg = ephem.degrees
12  sun = ephem.Sun()
13  nova = ephem.Observer()
14  nova.lat = '48:22.715780'
15  nova.lon = '92:49.876930'
16  nova.date = datetime.datetime.fromtimestamp(
17  int(t_start)).strftime('%Y/%m/%d %H:%M')
18  sun.compute(nova)
19  angle = sun.alt
20  if angle >deg('10'):
21  stype = "Day"
22  elif angle <=deg('10') and angle >deg('-10'):
23  stype="Twilight"
24  elif angle <=deg('-10') and angle > deg('-20'):
25  stype="SignalExtended"
26  else:
27  stype="SignalPrimary"
28  #print "{}\t{}\t{}".format(nova.date, angle, stype)
29  return stype
30 
31 def getSubRunType(sam, filename):
32  metadata = sam.getMetadata(filename)
33  key_start = "Analysis.UpMuSampleType"
34  key_end = "Analysis.UpMuSampleTypeEnd"
35  if key_start not in metadata or key_end not in metadata:
36  isCurrent = False
37  else:
38  isCurrent = True
39  # Get subrun start/end time, fix them if it's 0.
40  subrun_start = metadata["Online.SubRunStartTime"]
41  subrun_end = metadata["Online.SubRunEndTime"]
42  if subrun_start == 0:
43  if metadata["Online.Subrun"]==0:
44  subrun_start = metadata["Online.RunStartTime"]
45  if subrun_end == 0:
46  try:
47  fsplit = filename.split("_")
48  adjsub="%s_%s_s%d_%s" %(fsplit[0], fsplit[1],
49  int(md["Online.Subrun"])+1 ,fsplit[3])
50  md2=sam.getMetadata(adjsub)
51  subrun_end=md2["Online.SubRunStartTime"]
52  except:
53  print "Error: Retrieving adjacent subrun for run"\
54  " {}, using 3 min duration".format(f)
55  subrun_end = subrun_start+180
56  # Get subrun type (start_type and end_type) based on subrun start/end time.
57  start_type = getSampleType(subrun_start)
58  end_type = getSampleType(subrun_end)
59  # Set the flag if modifying metadata is necessary.
60  if isCurrent and (start_type != metadata[key_start]
61  or end_type != metadata[key_end]):
62  isCurrent = False
63  return (start_type, end_type, isCurrent)
64 
65 
66 if __name__ == '__main__':
67  import argparse
68  parser = argparse.ArgumentParser(
69  prog='modify_metadata_with_upmu_sample_type.py',
70  description="Add UpMu sample type to metadata.",
71  epilog="Questions and Comments to dingpf@fnal.gov")
72  parser.add_argument('-w', '--wet-run',action="store_true",
73  help="Run the script and modify metadata;")
74  parser.add_argument('-s', '--stream_type',default='DDSun',
75  choices = ['DDMoon', 'DDSun', 'DDContained', 'DDupmu','t02'],
76  help="Data stream type;")
77  parser.add_argument('-t','--data_tier', default='artdaq',
78  choices = ['artdaq','pidpart'],
79  help="Data tier type;")
80  parser.add_argument('-rb', '--run-begin',default=21000, type=int,
81  help="Run range lower limit;")
82  parser.add_argument('-re', '--run-end',default=21350, type=int,
83  help="Run range upper limit.")
84  args = parser.parse_args()
85 
86  print 80*"/"
87  rep = ''
88  if not args.wet_run:
89  print "Running without '-w' option."
90  print "File metadata will NOT BE MODIFIED."
91  print "Use option -w to modify file metadata."
92  rep = raw_input( 'Do you want to proceed? y/[n] ' )
93  if 1 < len(rep):
94  rep = rep[0]
95  if rep not in [ 'y', 'Y']:
96  sys.exit(1)
97 
98  if args.wet_run:
99  print "Running with '-w' option."
100  print "File metadata may be MODIFIED."
101  print "Remove option -w to only show the changes to file metadata."
102  rep = raw_input( 'Do you want to proceed? y/[n] ' )
103  if 1 < len(rep):
104  rep = rep[0]
105  if rep not in [ 'y', 'Y']:
106  sys.exit(1)
107  print 80*"*"
108 
109  sam = swc.SAMWebClient("nova")
110 
111  dim_run_range='(run_number >= {} and run_number <={})'.format(
112  args.run_begin, args.run_end)
113 
114 
115  dim_file_name='(file_name like "%{}%")'.format(args.stream_type)
116  #cp
117  dim_data_tier='(data_tier {})'.format(args.data_tier)
118  theDIM = dim_run_range + ' and ' + dim_file_name + ' and ' + dim_data_tier
119 
120  flist=sam.listFiles(theDIM)
121 
122  nfile = len(flist)
123  print "%d files matching dimension." % (len(flist))
124 
125  i = 0
126  for f in flist:
127  #print f
128  print "-"*80
129  print "{}/{} Checking file: {}".format(i+1, nfile, f)
130  start_type, end_type, isCurrent = getSubRunType(sam, f)
131  mfmetadata = {"Analysis.UpMuSampleType": start_type,
132  "Analysis.UpMuSampleTypeEnd": end_type}
133  if not isCurrent:
134  if args.wet_run:
135  sam.modifyFileMetadata(f,mfmetadata)
136  print "MODIFY: %s -- %s" %(f, mfmetadata)
137  else:
138  print "TO-BE-MODIFY: %s -- %s" %(f, mfmetadata)
139  else:
140  print "UP-TO-DATE: %s -- %s" %(f, mfmetadata)
141  i += 1
142  print " -- Done"
143  print 80*"*"
static constexpr Double_t deg
Definition: Munits.h:165
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14