Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:34

0001 #!/bin/sh
0002 # The next line restarts using tclsh \
0003 exec tclsh "$0" ${1+"$@"}
0004 
0005 #
0006 # This script ports the "NPStat" package to CMSSW,
0007 # in its part that is used to generate interpolation
0008 # tables for jet corrections
0009 #
0010 set inputdir "/afs/cern.ch/user/i/igv/local/src/npstat-3.0.0"
0011 set packagedir "/afs/cern.ch/user/i/igv/CMSSW_7_1_X_2014-03-22-1400/src/JetMETCorrections/InterpolationTables"
0012 
0013 # Create the map for changing include statements
0014 set includemap [list "\#include \"geners/" "\#include \"Alignment/Geners/interface/"]
0015 lappend includemap "\#include \"npstat/stat/" "\#include \"JetMETCorrections/InterpolationTables/interface/"
0016 lappend includemap "\#include \"npstat/nm/" "\#include \"JetMETCorrections/InterpolationTables/interface/"
0017 
0018 # Take care of exceptions so that they comply with CMSSW guidelines
0019 lappend includemap std::out_of_range npstat::NpstatOutOfRange
0020 lappend includemap std::invalid_argument npstat::NpstatInvalidArgument
0021 lappend includemap std::runtime_error npstat::NpstatRuntimeError
0022 lappend includemap std::domain_error npstat::NpstatDomainError
0023 lappend includemap "\#include <stdexcept>" "\#include \"JetMETCorrections/InterpolationTables/interface/NpstatException.h\""
0024 
0025 proc file_contents {filename} {
0026     set chan [open $filename "r"]
0027     set contents [read $chan [file size $filename]]
0028     close $chan
0029     set contents
0030 }
0031 
0032 proc filemap {infile outfile map} {
0033     set in_data [file_contents $infile]
0034     set chan [open $outfile "w"]
0035     puts -nonewline $chan [string map $map $in_data]
0036     close $chan
0037 }
0038 
0039 proc find_npstat_file {header {addh 1}} {
0040     global inputdir
0041     if {$addh} {
0042         set hh "${header}h"
0043     } else {
0044         set hh $header
0045     }
0046     set dirlist [list [file join $inputdir npstat nm] \
0047                       [file join $inputdir npstat stat]]
0048     foreach dir $dirlist {
0049         set htry [file join $dir $hh]
0050         if {[file readable $htry]} {
0051             return $htry
0052         }
0053     }
0054     error "Failed to find npstat file $hh"
0055 }
0056 
0057 # Procedures for reinserting .icc files
0058 proc is_icc_line {line} {
0059     set trline [string trim $line]
0060     if {[string first "\#include" $trline] != 0} {
0061         return 0
0062     }
0063     if {[string compare [string range $trline end-4 end] ".icc\""]} {
0064         return 0
0065     }
0066     return 1
0067 }
0068 
0069 proc icc_contents {icc_line icc_dir} {
0070     set trline [string trim $icc_line]
0071     set fname [string trim [lindex [split $trline /] end] "\""]
0072     file_contents [file join $icc_dir $fname]
0073 }
0074 
0075 proc insert_icc {infile outfile icc_dir} {
0076     set output [list]
0077     foreach line [split [file_contents $infile] "\n"] {
0078         if {[is_icc_line $line]} {
0079             lappend output [icc_contents $line $icc_dir]
0080         } else {
0081             lappend output $line
0082         }
0083     }
0084     set chan [open $outfile "w"]
0085     puts $chan [join $output "\n"]
0086     close $chan
0087 }
0088 
0089 # Remove pieces between "#ifdef SWIG" and corresponding #endif
0090 proc remove_swig {infile outfile} {
0091     set in_swig 0
0092     set output [list]
0093     set ifdef_count 0
0094     foreach line [split [file_contents $infile] "\n"] {
0095         set tline [string trim $line]
0096         if {!$in_swig} {
0097             if {[string equal $tline "#ifdef SWIG"]} {
0098                 set in_swig 1
0099             }
0100         }
0101         if {$in_swig} {
0102             if {[string equal -length 6 $tline "#endif"]} {
0103                 incr ifdef_count -1
0104                 if {$ifdef_count == 0} {
0105                     set in_swig 0
0106                 }
0107             }
0108             if {[string equal -length 6 $tline "#ifdef"]} {
0109                 incr ifdef_count
0110             }
0111             if {[string equal -length 7 $tline "#ifndef"]} {
0112                 incr ifdef_count
0113             }
0114         } else {
0115             lappend output $line
0116         }
0117     }
0118     set chan [open $outfile "w"]
0119     puts -nonewline $chan [join $output "\n"]
0120     close $chan
0121 }
0122 
0123 # Redo the #include statements so that they use .h instead of .hh
0124 proc fix_naming_convention {infile outfile} {
0125     set output [list]
0126     set target "\#include \"JetMETCorrections/InterpolationTables/interface/"
0127     set tlen [string length $target]
0128     foreach line [split [file_contents $infile] "\n"] {
0129         set trline [string trim $line]
0130         if {[string equal -length 8 $trline "// \\file"]} {
0131             set line [string range $trline 0 end-1]
0132         } elseif {[string equal -length $tlen $trline $target]} {
0133             set tail [string range $trline end-3 end-1]
0134             if {[string equal $tail ".hh"]} {
0135                 set line [string range $trline 0 end-2]
0136                 append line "\""
0137             }
0138         }
0139         lappend output $line
0140     }
0141     set chan [open $outfile "w"]
0142     puts -nonewline $chan [join $output "\n"]
0143     close $chan
0144 }
0145 
0146 # Other useful procedures
0147 proc replace_lines {infile outfile from to replacement} {
0148     set output [list]
0149     set linenum 1
0150     foreach line [split [file_contents $infile] "\n"] {
0151         set skip 0
0152         set replace 0
0153         if {$linenum >= $from && $linenum <= $to} {
0154             set skip 1
0155         }
0156         if {$skip} {
0157             if {$linenum == $to} {
0158                 set replace 1
0159             }
0160         }
0161         if {$replace} {
0162             lappend output $replacement
0163         }
0164         if {!$skip} {
0165             lappend output $line
0166         }
0167         incr linenum
0168     }
0169     set chan [open $outfile "w"]
0170     puts $chan [join $output "\n"]
0171     close $chan
0172 }
0173 
0174 proc tempfile {dir} {
0175     set chars "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
0176     set nrand_chars 10
0177     set maxtries 10
0178     set access [list RDWR CREAT EXCL TRUNC]
0179     set permission 0600
0180     set channel ""
0181     set checked_dir_writable 0
0182     set mypid [pid]
0183     for {set i 0} {$i < $maxtries} {incr i} {
0184         set newname ""
0185         for {set j 0} {$j < $nrand_chars} {incr j} {
0186             append newname [string index $chars \
0187                     [expr ([clock clicks] ^ $mypid) % 62]]
0188         }
0189         set newname [file join $dir $newname]
0190         if {[file exists $newname]} {
0191             after 1
0192         } else {
0193             if {[catch {open $newname $access $permission} channel]} {
0194                 if {!$checked_dir_writable} {
0195                     set dirname [file dirname $newname]
0196                     if {![file writable $dirname]} {
0197                         error "Directory $dirname is not writable"
0198                     }
0199                     set checked_dir_writable 1
0200                 }
0201             } else {
0202                 # Success
0203                 close $channel
0204                 return $newname
0205             }
0206         }
0207     }
0208     if {[string compare $channel ""]} {
0209         error "Failed to open a temporary file: $channel"
0210     } else {
0211         error "Failed to find an unused temporary file name"
0212     }
0213 }
0214 
0215 # Headers we need to include
0216 set header_list [list \
0217     AbsArrayProjector.h \
0218     absDifference.h \
0219     AbsMultivariateFunctor.h \
0220     AbsVisitor.h \
0221     allocators.h \
0222     ArrayND.h \
0223     ArrayNDScanner.h \
0224     ArrayRange.h \
0225     ArrayShape.h \
0226     BoxND.h \
0227     BoxNDScanner.h \
0228     CircularMapper1d.h \
0229     closeWithinTolerance.h \
0230     ComplexComparesAbs.h \
0231     ComplexComparesFalse.h \
0232     convertAxis.h \
0233     CoordinateSelector.h \
0234     DualAxis.h \
0235     DualHistoAxis.h \
0236     EquidistantSequence.h \
0237     GridAxis.h \
0238     HistoAxis.h \
0239     HistoNDFunctorInstances.h \
0240     HistoND.h \
0241     interpolate.h \
0242     interpolateHistoND.h \
0243     InterpolationFunctorInstances.h \
0244     Interval.h \
0245     isMonotonous.h \
0246     LinearMapper1d.h \
0247     LinInterpolatedTableND.h \
0248     MultivariateFunctorScanner.h \
0249     NUHistoAxis.h \
0250     PreciseType.h \
0251     ProperDblFromCmpl.h \
0252     rescanArray.h \
0253     SimpleFunctors.h \
0254     StorableHistoNDFunctor.h \
0255     StorableInterpolationFunctor.h \
0256     StorableMultivariateFunctor.h \
0257     StorableMultivariateFunctorReader.h \
0258     UniformAxis.h]
0259 
0260 set cc_list [list \
0261     ArrayNDScanner.cc \
0262     ArrayRange.cc \
0263     ArrayShape.cc \
0264     convertAxis.cc \
0265     DualAxis.cc \
0266     DualHistoAxis.cc \
0267     EquidistantSequence.cc \
0268     GridAxis.cc \
0269     HistoAxis.cc \
0270     NUHistoAxis.cc \
0271     StorableMultivariateFunctor.cc \
0272     StorableMultivariateFunctorReader.cc \
0273     UniformAxis.cc]
0274 
0275 # Now, do the replacements
0276 foreach header $header_list {
0277     set from_header [find_npstat_file $header]
0278     set to_header [file join "$packagedir/interface" $header]
0279     set tempfile [tempfile /tmp]
0280     insert_icc $from_header $tempfile [file dirname $from_header]
0281     remove_swig $tempfile $tempfile
0282     filemap $tempfile $to_header $includemap
0283     fix_naming_convention $to_header $to_header
0284     file delete $tempfile
0285 }
0286 
0287 foreach cc $cc_list {
0288     set fromfile [find_npstat_file $cc 0]
0289     set outfile [file join "$packagedir/src" $cc]
0290     filemap $fromfile $outfile $includemap
0291     fix_naming_convention $outfile $outfile
0292 }