SFrame 3.6
|
00001 # $Id: SFrameHelpers.py 207 2010-09-06 11:35:00Z krasznaa $ 00002 ########################################################################### 00003 # @Project: SFrame - ROOT-based analysis framework for ATLAS # 00004 # # 00005 # @author Stefan Ask <Stefan.Ask@cern.ch> - Manchester # 00006 # @author David Berge <David.Berge@cern.ch> - CERN # 00007 # @author Johannes Haller <Johannes.Haller@cern.ch> - Hamburg # 00008 # @author A. Krasznahorkay <Attila.Krasznahorkay@cern.ch> - CERN/Debrecen # 00009 # # 00010 ########################################################################### 00011 00012 ## @package SFrameHelpers 00013 # @short Collection of SFrame related python functions 00014 # 00015 # This package is a collection of python functions useful for SFrame. 00016 # They can either be used from an interactive python session by 00017 # executing 00018 # 00019 # <code> 00020 # >>> import SFrameHelpers 00021 # </code> 00022 # 00023 # or using the script(s) shipped with SFrame. 00024 00025 # Import base module(s): 00026 import os.path 00027 import time 00028 00029 # Import PyROOT: 00030 import ROOT 00031 00032 ## 00033 # @short Function creating <In ... /> configuration nodes 00034 # 00035 # The function checks the specified input files and writes the XML nodes 00036 # with their information to the specified output file. Their luminosity 00037 # is calculated using the specified cross section. 00038 # 00039 # @param crossSection Cross section of the Monte Carlo 00040 # @param files List of input files 00041 # @param output Name of the output file 00042 # @param tree Name of the main TTree in the files 00043 # @param prefix Prefix to be put before the file paths. E.g. root://mymachine/ 00044 def CreateInput( crossSection, files, output, tree, prefix, real_filenames ): 00045 00046 # Turn off ROOT error messages: 00047 oldErrorIgnoreLevel = ROOT.gErrorIgnoreLevel 00048 ROOT.gErrorIgnoreLevel = ROOT.kSysError 00049 00050 # Open the output file: 00051 outfile = open( output, "w" ) 00052 00053 # Print some header in the output text file: 00054 outfile.write( "<!-- File generated by SFrameHelpers.CreateInput(...) on %s -->\n" % \ 00055 time.asctime( time.localtime( time.time() ) ) ) 00056 outfile.write( "<!-- The supplied x-section was: %f -->\n\n" % crossSection ) 00057 00058 # Some summary values: 00059 totEvents = 0 00060 totLuminosity = 0.0 00061 00062 # Loop over all the files: 00063 for file in files: 00064 00065 # Print some status messages: 00066 print "Processing file: %s" % os.path.basename( file ) 00067 00068 # Open the AANT file: 00069 tfile = ROOT.TFile.Open( file ) 00070 if not tfile.IsOpen(): 00071 print "*ERROR* File \"" + file + "\" does not exist *ERROR*" 00072 return 255 00073 00074 # Access a tree in the ntuple: 00075 collTree = tfile.Get( tree ) 00076 if( str( collTree ) == 'None' ): 00077 print "*ERROR* " + tree + " not found in file: \"" + file + "\" *ERROR*" 00078 continue 00079 00080 # Read the number of events in the file: 00081 events = collTree.GetEntries() 00082 luminosity = float( events ) / crossSection 00083 00084 # Increment the summary variables: 00085 totEvents = totEvents + events 00086 totLuminosity = totLuminosity + luminosity 00087 00088 # Compose the XML node. Make sure that the file name has an absolute path 00089 # (no symbolic link, or relative path) and that the luminosity is printed with 00090 # a meaningful precision. 00091 if real_filenames: 00092 outfile.write( "<In FileName=\"" + prefix + file + \ 00093 ( "\" Lumi=\"%.3g" % luminosity ) + "\" />\n" ) 00094 else: 00095 outfile.write( "<In FileName=\"" + prefix + \ 00096 os.path.abspath( os.path.realpath( file ) ) + \ 00097 ( "\" Lumi=\"%.3g" % luminosity ) + "\" />\n" ) 00098 00099 # Close the opened input file: 00100 tfile.Close() 00101 00102 # Save some summary information: 00103 outfile.write( "\n<!-- Total number of events processed: %s -->\n" % totEvents ) 00104 outfile.write( "<!-- Representing a total luminosity : %.3g -->" % totLuminosity ) 00105 00106 # Close the output file: 00107 outfile.close() 00108 00109 # Print some summary information: 00110 print "\nTotal number of events processed: %s" % totEvents 00111 print "Representing a total luminosity : %.3g\n" % totLuminosity 00112 00113 # Turn back ROOT error messages: 00114 ROOT.gErrorIgnoreLevel = oldErrorIgnoreLevel 00115 00116 return 0 00117 00118 ## 00119 # @short Function creating <In ... /> configuration nodes 00120 # 00121 # The function checks the specified input files and writes the XML nodes 00122 # with their information to the specified output file. It assumes that the 00123 # input files are data files, so it just puts a dummy "1.0" as the 00124 # luminosity for them. (The luminosities are disregarded in the event 00125 # weight calculation when the InputData type is set to "data".) 00126 # 00127 # @param files List of input files 00128 # @param output Name of the output file 00129 # @param tree Name of the main TTree in the files 00130 # @param prefix Prefix to be put before the file paths. E.g. root://mymachine/ 00131 def CreateDataInput( files, output, tree, prefix, real_filenames ): 00132 00133 # Turn off ROOT error messages: 00134 oldErrorIgnoreLevel = ROOT.gErrorIgnoreLevel 00135 ROOT.gErrorIgnoreLevel = ROOT.kSysError 00136 00137 # Open the output file: 00138 outfile = open( output, "w" ) 00139 00140 # Print some header in the output text file: 00141 outfile.write( "<!-- File generated by SFrameHelpers.CreateDataInput(...) on %s -->\n\n" % \ 00142 time.asctime( time.localtime( time.time() ) ) ) 00143 00144 # Some summary values: 00145 totEvents = 0 00146 00147 # Loop over all the files: 00148 for file in files: 00149 00150 # Print some status messages: 00151 print "Processing file: %s" % os.path.basename( file ) 00152 00153 # Open the AANT file: 00154 tfile = ROOT.TFile.Open( file ) 00155 if ( not tfile ) or ( not tfile.IsOpen() ): 00156 print "*ERROR* File \"" + file + "\" does not exist *ERROR*" 00157 return 255 00158 00159 # Access a tree in the ntuple: 00160 collTree = tfile.Get( tree ) 00161 if( str( collTree ) == 'None' ): 00162 print "*ERROR* " + tree + " not found in file: \"" + file + "\" *ERROR*" 00163 continue 00164 00165 # Read the number of events in the file: 00166 events = collTree.GetEntries() 00167 00168 # Increment the summary variables: 00169 totEvents = totEvents + events 00170 00171 # Compose the XML node. Make sure that the file name has an absolute path 00172 # (no symbolic link, or relative path) and that the luminosity is printed with 00173 # a meaningful precision. 00174 if real_filenames: 00175 outfile.write( "<In FileName=\"" + prefix + file + \ 00176 "\" Lumi=\"1.0\" />\n" ) 00177 else: 00178 outfile.write( "<In FileName=\"" + prefix + \ 00179 os.path.abspath( os.path.realpath( file ) ) + \ 00180 "\" Lumi=\"1.0\" />\n" ) 00181 00182 # Close the opened input file: 00183 tfile.Close() 00184 00185 # Save some summary information: 00186 outfile.write( "\n<!-- Total number of events processed: %s -->\n" % totEvents ) 00187 00188 # Close the output file: 00189 outfile.close() 00190 00191 # Print some summary information: 00192 print "\nTotal number of events processed: %s" % totEvents 00193 00194 # Turn back ROOT error messages: 00195 ROOT.gErrorIgnoreLevel = oldErrorIgnoreLevel 00196 00197 return 0