Beispiel #1
0
def ReweightFile(filepath,LumiData,rwOptions):


    paraFile=rwOptions.get('parametersFile','para_config.txt')
    entriesFromFile=rwOptions.get('entriesFromFile',True)
    entriesFromHistogram=rwOptions.get('entriesFromHistogram',False)
    if  entriesFromHistogram:
        entriesFromFile=False
        
    
    #raw_input('going to make the call with '+str(filepath))
    FileName,InDir,Sample,SubSample,Estimation,Tail,AbsPath = BreakDownInputPath(filepath)

    #=====the output should be the same as
    #=====the filename + '_RW2X', where X is the lumi
    OutFileName=filepath
    print ""
    print "---------------------------------------------"
    print "INSIDE ReweightFile"
    print '   filepath= ',str(filepath)
    print '   filename= ',OutFileName
    #
    #
    OutFileName=OutFileName.replace('.root','_RWTo'+str(int(LumiData))+'.root')
    #OutFileName=AbsPath.replace(FileName,OutFileName)
    #
    #
    print ''
    print "   Going to reweight the file",filepath, "to the lumi",LumiData
    print ""
    print "          the outFileName will be ",OutFileName
    
    #INPUT FILE#
    infile=TFile(filepath,"READ")
    #except:
    #print "Unexpected error:", sys.exc_info()[0]
    #print 'infile is ',str(infile)
    #return
    infile.cd()

    if not infile.IsOpen():
        print "       the file could not be opened. Bye"
        return
    #============
    #
    #
    #
    #
    outfile=TFile(OutFileName,"RECREATE")
    outfile.cd()
    #
    #
    #
    #=======PARAMETERS FOR THE REWEIGHTING
    parameters_dict={}
    config=open(paraFile,'r')
    for line in config:
        if  line.find('#') == -1:        
            thisline=line.split()
            #
            if len(thisline)==0:
                continue
            try:
                parameters_dict[thisline[0]]=float(thisline[2])
            except IndexError:
                print "this is not going to work"
                print "thisline is", thisline
                print 'and the index is ',thisline[0]
                raise
        else:
            pass
    #===========
    filtereff_key='FE_'+Sample+'_'+SubSample
    try:
        FE=parameters_dict[filtereff_key]
    except KeyError:
        print filtereff_key, "does not exist in the dictionary"
        
        return
    #
    xsec_key='xs_'+Sample+'_'+SubSample
    try:
        XS=parameters_dict[xsec_key]
    except KeyError:
        print xsec_key, "does not exist in the dictionary"
        return
    #

    if entriesFromFile:
        tnoe_key='TNoE_'+Sample+'_'+SubSample
        try:
            TNOE=parameters_dict[tnoe_key]
        except KeyError:
            print tnoe_key, "does not exist in the dictionary"
            return

    elif entriesFromHistogram:
        entriesHistoPath=rwOptions['entriesHistoPath']
        entriesHisto=infile.Get(entriesHistoPath)
        if str(entriesHisto).find('nil') != -1:
            print 'the entries histo ',entriesHisto.GetName(),' was not found in ',infile.GetPath()
        #
        if entriesHisto.GetNBinsX() > 1:
            print 'the histo has more than one bin!! ',entriesHisto.GetNbinsX()
        TNOE=entriesHisto.GetBinContent(1)


    #COMPUTE THE WEIGHTS
    #if LumiData==-1:
        #LumiData=5097.
    print "----Reweighting information----"
    print '        XS =',XS
    print '        FE =',FE
    print '        TNOE =',TNOE
    Weight=float(LumiData)*XS*FE/TNOE;
    print "        LumiData =",LumiData
    print "        cross section is",XS
    print "        FE is ", FE
    print "        number of entries ",TNOE
    print "        the weight is ",Weight
    #raw_input("ready to continue?")

    #
    #
    #
    #REWEIGHT ALL THE HISTOS IN THE FILE:
    LoopAndScale(infile,Weight)

    #outfile.Write()
    #
    #
    #
    #
    #MAKE THE ROOT FILE REMEMBER
    outfile.cd()
    histoname='RWto'+str(int(LumiData))
    isrw=infile.Get(histoname)
    #
    #
    if str(isrw).find('nil') != -1:
        #create it
        newh=TH1D(histoname,"",1,0.5,1.5)
        newh.Fill(1.0)
        newh.Write()
        #
    datehist=outfile.Get("date_of_reweighting_to_"+str(int(LumiData)))
    date=commands.getoutput('date +%s')    
    if str(datehist).find('nil') != -1:

        dateh=TH1D("date_of_reweighting_to_"+str(int(LumiData)),date,1,0.5,1.5)
        dateh.Fill(1.0)
        dateh.Write()
    else:
        datehist.SetTitle(date)
        datehist.Write("",TObject.kOverwrite)
    #

    outfile.Write()
    print "EXITING ReweightFile. Bye"
    print "----------------------------------"
    print ""
    return OutFileName
Beispiel #2
0
                    lumi_sf = 2260. * cross_sections[subsample] / input_nevents
                    print "For subsample {}, lumi sf = {} * {} / {} = {}".format(
                        subsample, 2260., cross_sections[subsample],
                        input_nevents, lumi_sf)
                else:
                    lumi_sf = 1.

                # First subsample: create the output sample file, and make a list of histograms to include
                if i == 0:
                    hist_names = []
                    for key in subsample_file.GetListOfKeys():
                        if "TH" in key.GetClassName():
                            hist_names.append(key.ReadObj().GetName())

                for hist_name in hist_names:
                    subsample_hist = subsample_file.Get(hist_name)
                    if not subsample_hist:
                        print "ERROR : Couldn't find histogram {} in file {}".format(
                            hist_name, subsample_file.GetPath())
                    subsample_hist.Scale(lumi_sf)
                    if i == 0:
                        hists[hist_name] = subsample_hist.Clone()
                        hists[hist_name].SetDirectory(0)
                    else:
                        hists[hist_name].Add(subsample_hist)
            # End loop over subsamples

            sample_file.cd()
            for hist_name, hist in hists.iteritems():
                hist.Write()