Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:09:23

0001 #!/usr/bin/env python3
0002 
0003 #========================================================================
0004 #
0005 # This script is used to copy a root file of histograms,
0006 # together with its log files, in a destination directory.
0007 # Log files will be automatically compressed.
0008 #
0009 # Command-line options :
0010 #
0011 #   -f : force the copy, even if the destination file already exists.
0012 #   -r <name> : name of this set of histograms.
0013 #   -m <message> : specific comment about this set of histograms.
0014 #   -a <analyzers> : slash separated list of analyzers.
0015 #   -c <config> : slash separated list of cmsRun configurations.
0016 # 
0017 # Command-line arguments :
0018 #
0019 #   $1 : path of the ROOT file containing the histograms.
0020 #   $... : path of log files.
0021 #   $n : destination directory.
0022 #
0023 #=========================================================================
0024 
0025 
0026 from __future__ import print_function
0027 import os, sys, datetime, shutil, optparse
0028 
0029 
0030 #============================================
0031 # display a command and eventually executes
0032 #============================================
0033 
0034 def mysystem(command,apply=1):
0035   print(command)
0036   if apply==1: return os.system(command)
0037   elif apply==0:  return 0
0038   else:
0039     print('[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply)
0040     exit(1)
0041   
0042 
0043 #============================================
0044 # force immediate flushing of stdout
0045 #============================================
0046 
0047 class flushfile(object):
0048   def __init__(self,f):
0049     self.f = f
0050   def write(self,x):
0051     self.f.write(x)
0052     self.f.flush()
0053 
0054 sys.stdout = flushfile(sys.stdout)
0055 
0056 
0057 #===================================================================
0058 # when called as an independant executable
0059 #===================================================================
0060 
0061 if __name__ == "__main__":
0062 
0063 
0064   #============================================
0065   # command-line arguments
0066   #============================================
0067     
0068   parser = optparse.OptionParser()
0069   parser.add_option("-f", "--force", dest="force", action="store_true", default=False,
0070     help="force the copy, even if the destination file already exists.")  
0071   parser.add_option("-r", "--release", dest="release", action="store", default="current",
0072     help="release name of this set of histograms.")  
0073   parser.add_option("-m", "--message", dest="message", action="store", default="",
0074     help="specific comment about this set of histograms")  
0075   parser.add_option("-a", "--analyzers", dest="analyzers", action="store", default="",
0076     help="slash separated list of analyzers")  
0077   parser.add_option("-c", "--configs", dest="configs", action="store", default="",
0078     help="slash separated list of cmsRun configurations")  
0079   (options, args) = parser.parse_args()
0080   
0081   if len(args)<2:
0082     print("[electronStore.py] I NEED AT LEAST TWO ARGUMENTS.")
0083     exit(2)
0084   store_file = args.pop(0)
0085   store_dir = args.pop()
0086   if len(args)>0: store_logs = ' '.join(args)
0087   else: store_logs = ''
0088   
0089   analyzers = options.analyzers.split('/') ;
0090   configs = options.configs.split('/') ;    
0091     
0092       
0093   #============================================
0094   # prepare output directory
0095   #============================================
0096 
0097   if os.path.exists(store_dir)==False:
0098     os.makedirs(store_dir)
0099 
0100 
0101   #============================================
0102   # check data files
0103   #============================================
0104 
0105   if os.path.isfile(store_file)==True :
0106     print("STORE_FILE =",store_file)
0107   else :
0108     print("[electronStore.py] FILE DOES NOT EXIST :",store_file)
0109     exit(3)
0110     
0111   if ( store_logs != '' ) :
0112     print("STORE_LOGS =",store_logs)
0113 
0114 
0115   #============================================
0116   # check if already done
0117   #============================================
0118   
0119   output_file = store_dir+'/'+store_file
0120   if ( options.force==False and os.path.isfile(output_file)==True ) :
0121     print("[electronStore.py] ERROR: "+store_file+" ALREADY STORED IN "+store_dir+" !")
0122     exit(4)
0123 
0124 
0125   #============================================
0126   # copy
0127   #============================================
0128 
0129   files = [ store_file ]
0130   for analyzer in analyzers:
0131     files.append('../plugins/'+analyzer+'.h')
0132     files.append('../plugins/'+analyzer+'.cc')
0133   for config in configs:
0134     files.append(config+'.py')
0135 
0136   mysystem('cp '+' '.join(files)+' '+store_dir)
0137   
0138   if ( store_logs != '' ) :
0139     mysystem('cp '+store_logs+' '+store_dir)
0140     mysystem('cd '+store_dir+' && gzip -f *.olog')
0141 
0142 
0143   #============================================
0144   # comment
0145   #============================================
0146 
0147   store_url = store_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)
0148 
0149   links = []
0150   for analyzer in analyzers:
0151     links.append('<a href="'+store_url+'/'+analyzer+'.h">'+analyzer+'.h</a>')
0152     links.append('<a href="'+store_url+'/'+analyzer+'.cc">'+analyzer+'.cc</a>')
0153   for config in configs:
0154     links.append('<a href="'+store_url+'/'+config+'.py">'+config+'.py</a>')
0155 
0156   comment_file = open(store_dir+'/'+store_file+'.comment','w')
0157   print('The <a href="'+store_url+'/'+store_file+'">'+options.release+' histograms</a>', end=' ', file=comment_file)
0158   if (options.message!=''):
0159     print(' ('+options.message+')', end=' ', file=comment_file)
0160   print(' have been prepared with those analyzers and configurations: '+', '.join(links)+'.', end=' ', file=comment_file)
0161   print(file=comment_file)
0162   comment_file.close()
0163   
0164 
0165   #============================================
0166   # fin
0167   #============================================
0168   
0169   exit(0)
0170   
0171