Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#!/usr/bin/env python3

#========================================================================
#
# This script is used to copy a root file of histograms,
# together with its log files, in a destination directory.
# Log files will be automatically compressed.
#
# Command-line options :
#
#   -f : force the copy, even if the destination file already exists.
#   -r <name> : name of this set of histograms.
#   -m <message> : specific comment about this set of histograms.
#   -a <analyzers> : slash separated list of analyzers.
#   -c <config> : slash separated list of cmsRun configurations.
# 
# Command-line arguments :
#
#   $1 : path of the ROOT file containing the histograms.
#   $... : path of log files.
#   $n : destination directory.
#
#=========================================================================


import os, sys, datetime, shutil, optparse


#============================================
# display a command and eventually executes
#============================================

def mysystem(command,apply=1):
  print(command)
  if apply==1: return os.system(command)
  elif apply==0:  return 0
  else:
    print('[electronStore.py] UNSUPPORTED ARGUMENT VALUE FOR mysystem(,apply):',apply)
    exit(1)
  

#============================================
# force immediate flushing of stdout
#============================================

class flushfile(object):
  def __init__(self,f):
    self.f = f
  def write(self,x):
    self.f.write(x)
    self.f.flush()

sys.stdout = flushfile(sys.stdout)


#===================================================================
# when called as an independant executable
#===================================================================

if __name__ == "__main__":


  #============================================
  # command-line arguments
  #============================================
    
  parser = optparse.OptionParser()
  parser.add_option("-f", "--force", dest="force", action="store_true", default=False,
    help="force the copy, even if the destination file already exists.")  
  parser.add_option("-r", "--release", dest="release", action="store", default="current",
    help="release name of this set of histograms.")  
  parser.add_option("-m", "--message", dest="message", action="store", default="",
    help="specific comment about this set of histograms")  
  parser.add_option("-a", "--analyzers", dest="analyzers", action="store", default="",
    help="slash separated list of analyzers")  
  parser.add_option("-c", "--configs", dest="configs", action="store", default="",
    help="slash separated list of cmsRun configurations")  
  (options, args) = parser.parse_args()
  
  if len(args)<2:
    print("[electronStore.py] I NEED AT LEAST TWO ARGUMENTS.")
    exit(2)
  store_file = args.pop(0)
  store_dir = args.pop()
  if len(args)>0: store_logs = ' '.join(args)
  else: store_logs = ''
  
  analyzers = options.analyzers.split('/') ;
  configs = options.configs.split('/') ;    
    
      
  #============================================
  # prepare output directory
  #============================================

  if os.path.exists(store_dir)==False:
    os.makedirs(store_dir)


  #============================================
  # check data files
  #============================================

  if os.path.isfile(store_file)==True :
    print("STORE_FILE =",store_file)
  else :
    print("[electronStore.py] FILE DOES NOT EXIST :",store_file)
    exit(3)
    
  if ( store_logs != '' ) :
    print("STORE_LOGS =",store_logs)


  #============================================
  # check if already done
  #============================================
  
  output_file = store_dir+'/'+store_file
  if ( options.force==False and os.path.isfile(output_file)==True ) :
    print("[electronStore.py] ERROR: "+store_file+" ALREADY STORED IN "+store_dir+" !")
    exit(4)


  #============================================
  # copy
  #============================================

  files = [ store_file ]
  for analyzer in analyzers:
    files.append('../plugins/'+analyzer+'.h')
    files.append('../plugins/'+analyzer+'.cc')
  for config in configs:
    files.append(config+'.py')

  mysystem('cp '+' '.join(files)+' '+store_dir)
  
  if ( store_logs != '' ) :
    mysystem('cp '+store_logs+' '+store_dir)
    mysystem('cd '+store_dir+' && gzip -f *.olog')


  #============================================
  # comment
  #============================================

  store_url = store_dir.replace('/afs/cern.ch/cms/','http://cmsdoc.cern.ch/',1)

  links = []
  for analyzer in analyzers:
    links.append('<a href="'+store_url+'/'+analyzer+'.h">'+analyzer+'.h</a>')
    links.append('<a href="'+store_url+'/'+analyzer+'.cc">'+analyzer+'.cc</a>')
  for config in configs:
    links.append('<a href="'+store_url+'/'+config+'.py">'+config+'.py</a>')

  comment_file = open(store_dir+'/'+store_file+'.comment','w')
  print('The <a href="'+store_url+'/'+store_file+'">'+options.release+' histograms</a>', end=' ', file=comment_file)
  if (options.message!=''):
    print(' ('+options.message+')', end=' ', file=comment_file)
  print(' have been prepared with those analyzers and configurations: '+', '.join(links)+'.', end=' ', file=comment_file)
  print(file=comment_file)
  comment_file.close()
  

  #============================================
  # fin
  #============================================
  
  exit(0)