Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:11

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