CMS 3D CMS Logo

electronStore.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #========================================================================
4 #
5 # This script is used to copy a root file of histograms,
6 # together with its log files, in a destination directory.
7 # Log files will be automatically compressed.
8 #
9 # Command-line options :
10 #
11 # -f : force the copy, even if the destination file already exists.
12 # -r <name> : name of this set of histograms.
13 # -m <message> : specific comment about this set of histograms.
14 # -a <analyzers> : slash separated list of analyzers.
15 # -c <config> : slash separated list of cmsRun configurations.
16 #
17 # Command-line arguments :
18 #
19 # $1 : path of the ROOT file containing the histograms.
20 # $... : path of log files.
21 # $n : destination directory.
22 #
23 #=========================================================================
24 
25 
26 from __future__ import print_function
27 import os, sys, datetime, shutil, optparse
28 
29 
30 #============================================
31 # display a command and eventually executes
32 #============================================
33 
34 def mysystem(command,apply=1):
35  print(command)
36  if apply==1: return os.system(command)
37  elif apply==0: return 0
38  else:
39  print('[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply)
40  exit(1)
41 
42 
43 #============================================
44 # force immediate flushing of stdout
45 #============================================
46 
48  def __init__(self,f):
49  self.f = f
50  def write(self,x):
51  self.f.write(x)
52  self.f.flush()
53 
54 sys.stdout = flushfile(sys.stdout)
55 
56 
57 #===================================================================
58 # when called as an independant executable
59 #===================================================================
60 
61 if __name__ == "__main__":
62 
63 
64  #============================================
65  # command-line arguments
66  #============================================
67 
68  parser = optparse.OptionParser()
69  parser.add_option("-f", "--force", dest="force", action="store_true", default=False,
70  help="force the copy, even if the destination file already exists.")
71  parser.add_option("-r", "--release", dest="release", action="store", default="current",
72  help="release name of this set of histograms.")
73  parser.add_option("-m", "--message", dest="message", action="store", default="",
74  help="specific comment about this set of histograms")
75  parser.add_option("-a", "--analyzers", dest="analyzers", action="store", default="",
76  help="slash separated list of analyzers")
77  parser.add_option("-c", "--configs", dest="configs", action="store", default="",
78  help="slash separated list of cmsRun configurations")
79  (options, args) = parser.parse_args()
80 
81  if len(args)<2:
82  print("[electronStore.py] I NEED AT LEAST TWO ARGUMENTS.")
83  exit(2)
84  store_file = args.pop(0)
85  store_dir = args.pop()
86  if len(args)>0: store_logs = ' '.join(args)
87  else: store_logs = ''
88 
89  analyzers = options.analyzers.split('/') ;
90  configs = options.configs.split('/') ;
91 
92 
93  #============================================
94  # prepare output directory
95  #============================================
96 
97  if os.path.exists(store_dir)==False:
98  os.makedirs(store_dir)
99 
100 
101  #============================================
102  # check data files
103  #============================================
104 
105  if os.path.isfile(store_file)==True :
106  print("STORE_FILE =",store_file)
107  else :
108  print("[electronStore.py] FILE DOES NOT EXIST :",store_file)
109  exit(3)
110 
111  if ( store_logs != '' ) :
112  print("STORE_LOGS =",store_logs)
113 
114 
115  #============================================
116  # check if already done
117  #============================================
118 
119  output_file = store_dir+'/'+store_file
120  if ( options.force==False and os.path.isfile(output_file)==True ) :
121  print("[electronStore.py] ERROR: "+store_file+" ALREADY STORED IN "+store_dir+" !")
122  exit(4)
123 
124 
125  #============================================
126  # copy
127  #============================================
128 
129  files = [ store_file ]
130  for analyzer in analyzers:
131  files.append('../plugins/'+analyzer+'.h')
132  files.append('../plugins/'+analyzer+'.cc')
133  for config in configs:
134  files.append(config+'.py')
135 
136  mysystem('cp '+' '.join(files)+' '+store_dir)
137 
138  if ( store_logs != '' ) :
139  mysystem('cp '+store_logs+' '+store_dir)
140  mysystem('cd '+store_dir+' && gzip -f *.olog')
141 
142 
143  #============================================
144  # comment
145  #============================================
146 
147  store_url = store_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)
148 
149  links = []
150  for analyzer in analyzers:
151  links.append('<a href="'+store_url+'/'+analyzer+'.h">'+analyzer+'.h</a>')
152  links.append('<a href="'+store_url+'/'+analyzer+'.cc">'+analyzer+'.cc</a>')
153  for config in configs:
154  links.append('<a href="'+store_url+'/'+config+'.py">'+config+'.py</a>')
155 
156  comment_file = open(store_dir+'/'+store_file+'.comment','w')
157  print('The <a href="'+store_url+'/'+store_file+'">'+options.release+' histograms</a>', end=' ', file=comment_file)
158  if (options.message!=''):
159  print(' ('+options.message+')', end=' ', file=comment_file)
160  print(' have been prepared with those analyzers and configurations: '+', '.join(links)+'.', end=' ', file=comment_file)
161  print(file=comment_file)
162  comment_file.close()
163 
164 
165  #============================================
166  # fin
167  #============================================
168 
169  exit(0)
170 
171 
S & print(S &os, JobReport::InputFile const &f)
Definition: JobReport.cc:65
def mysystem(command, apply=1)
static std::string join(char **cmd)
Definition: RemoteFile.cc:18