Example #1
0
    def setstyle(self):

        # Delete previous PostScript plots and set Canvas options

        gSystem.Exec('rm -f ' + 'D0.pdf')
        gSystem.Exec('rm -f ' + 'Fit.pdf')
        gStyle.SetCanvasBorderMode(0)
        gStyle.SetCanvasColor(0)
        gStyle.SetStatBorderSize(1)
        gStyle.SetStatColor(0)
Example #2
0
        Z_ExtraEl_LepBDT_MC_TTbar_hist_EB.Write()
        Z_ExtraEl_LepBDT_MC_TTbar_hist_EE.Write()

        Z_ExtraMu_LepBDT_MC_TTbar_hist.Write()
        Z_ExtraMu_LepBDT_MC_TTbar_hist_MB.Write()
        Z_ExtraMu_LepBDT_MC_TTbar_hist_ME.Write()

    LepBDT_MC_TTbar_outFile.Close()
    print "MC TTbar histo file created!"
# ********************

# ****************************************
# create output directory
# ****************************************
outputDir = "LepBDTdistrib_DATAvsMC_" + str(period) + "_" + str(treeText)
gSystem.Exec("mkdir -p " + outputDir)
print "Output directory created!"

# **************************
# read data histos from file
# **************************
histoDATA_input = TFile.Open("LepBDTdistrib_DATA_" + period + "_" + treeText +
                             ".root")
print 'Reading file', histoDATA_input.GetName(), '...'

LepBDTDATA_list = []

LepBDTDATA_list.append(histoDATA_input.Get('Z_ele_1st_LepBDT_hist'))
LepBDTDATA_list.append(histoDATA_input.Get('Z_ele_1st_LepBDT_hist_EB'))
LepBDTDATA_list.append(histoDATA_input.Get('Z_ele_1st_LepBDT_hist_EE'))
LepBDTDATA_list.append(histoDATA_input.Get('Z_mu_1st_LepBDT_hist'))
Example #3
0
    ut.set_margin_lbtr(gPad, 0.1, 0.1, 0.015, 0.15)

    #gPad.SetLogz()

    gPad.SetGrid()

    hEnThetaTag.SetMinimum(0)
    hEnThetaTag.SetMaximum(1)
    hEnThetaTag.SetContour(300)

    hEnThetaTag.Draw("colz")

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")


#acc_el_en_theta_tag

#_____________________________________________________________________________
if __name__ == "__main__":

    gROOT.SetBatch()
    gStyle.SetPadTickX(1)
    gStyle.SetFrameLineWidth(2)

    main()

    #beep when done
    gSystem.Exec("mplayer computerbeep_1.mp3 > /dev/null 2>&1")
print 'Input files with Fit values read!'
#****************************************************************************




# make output directories
outDir_fit         = "FitResults_"  + str(period) + "_" + str(treeText)
if(applyPU2017): 
    outDir_ZPlots  = "ZPlots_" + str(period) + "_" + str(treeText) + "_2017PU"
else:
    outDir_ZPlots  = "ZPlots_" + str(period) + "_" + str(treeText)
outDir_LeptonPlots = "LeptonPlots_" + str(period) + "_" + str(treeText)

gSystem.Exec("mkdir -p " + outDir_fit)
gSystem.Exec("mkdir -p " + outDir_ZPlots)
gSystem.Exec("mkdir -p " + outDir_LeptonPlots)
print "Output directories created!"

data = TFile(inputROOT)

RunNum_B  = []
LumiNum_B = []
RunNum_E  = []
LumiNum_E = []
recorded  = []

# Read the input JSON file and store it
ReadJSON(inputTXT, RunNum_B, LumiNum_B, RunNum_E, LumiNum_E, recorded)
Example #5
0
#period = "data2018"
# *****************************
if (period == "data2016"):
    lumiText = "35.92 fb^{-1}"

elif (period == "data2017"):
    lumiText = "41.53 fb^{-1}"

elif (period == "data2018"):
    lumiText = "59.74 fb^{-1}"
else:
    print("Error: wrong option!")

# create output directory
DIR = "/afs/cern.ch/work/e/elfontan/private/HZZ/RUN2_Legacy/CMSSW_10_2_15/src/ZZAnalysis/AnalysisStep/test/hzz4l_ValidationPlots/" + period + "_IDchecks_jets_issues_ZTree/"
gSystem.Exec("mkdir -p " + DIR)
print "Output directory created: " + str(DIR)

# Open file and get histos
filename = "f_" + period
filename = TFile.Open(DIR + "leadingJet_etaDistributions_" + period +
                      "_ZTree.root")
h_noID_data = filename.Get("leadingJet_Eta_noID_data")
h_jetID_data = filename.Get("leadingJet_Eta_jetID_data")
h_PUID_data = filename.Get("leadingJet_Eta_PUID_data")
h_noID_MC = filename.Get("leadingJet_Eta_noID_MC")
h_jetID_MC = filename.Get("leadingJet_Eta_jetID_MC")
h_PUID_MC = filename.Get("leadingJet_Eta_PUID_MC")

c_data = ROOT.TCanvas()
c_data.cd()
Example #6
0
def main():

    try:
        # retrive command line options
        shortopts  = "w:m:i:j:f:g:t:o:a:vgh?"
        longopts   = ["weight_fold=", "methods=", "inputfilesig=", "inputfilebkg=", "friendinputfilesig=", "friendinputfilebkg=", "inputtrees=", "outputfile=", "verbose", "gui", "help", "usage"]
        opts, args = getopt.getopt( sys.argv[1:], shortopts, longopts )

    except getopt.GetoptError:
        # print help information and exit:
        print "ERROR: unknown options in argument %s" % sys.argv[1:]
        usage()
        sys.exit(1)

    infnameSig     = DEFAULT_INFNAMESIG
    infnameBkg     = DEFAULT_INFNAMEBKG
    friendfnameSig = DEFAULT_FRIENDNAMESIG
    friendfnameBkg = DEFAULT_FRIENDNAMEBKG
    treeNameSig    = DEFAULT_TREESIG
    treeNameBkg    = DEFAULT_TREEBKG
    outfname       = DEFAULT_OUTFNAME
    methods        = DEFAULT_METHODS
    weight_fold    = "weights"
    verbose        = False
    gui            = False
    addedcuts      = ""
    for o, a in opts:
        if o in ("-?", "-h", "--help", "--usage"):
            usage()
            sys.exit(0)
        elif o in ("-w", "--weight_fold"):
            weight_fold = a
        elif o in ("-m", "--methods"):
            methods = a
        elif o in ("-i", "--inputfilesig"):
            infnameSig = a
        elif o in ("-j", "--inputfilebkg"):
            infnameBkg = a
        elif o in ("-f", "--friendinputfilesig"):
            friendfnameSig = a
        elif o in ("-g", "--friendinputfilebkg"):
            friendfnameBkg = a
        elif o in ("-o", "--outputfile"):
            outfname = a
        elif o in ("-a", "--addedcuts"):
            addedcuts = a
        elif o in ("-t", "--inputtrees"):
            a.strip()
            trees = a.rsplit( ' ' )
            trees.sort()
            trees.reverse()
            if len(trees)-trees.count('') != 2:
                print "ERROR: need to give two trees (each one for signal and background)"
                print trees
                sys.exit(1)
            treeNameSig = trees[0]
            treeNameBkg = trees[1]
        elif o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-g", "--gui"):
            gui = True

    # Print methods
    mlist = methods.replace(' ',',').split(',')
    print "=== TMVAClassification: use method(s)..."
    for m in mlist:
        if m.strip() != '':
            print "=== - <%s>" % m.strip()

    # Print the file
    print "Using file " + infnameSig + " for signal..."
    print "Using file " + infnameBkg + " for background..."

    # Import ROOT classes
    from ROOT import gSystem, gROOT, gApplication, TFile, TTree, TCut
    
    # check ROOT version, give alarm if 5.18
    print "ROOT version is " + str(gROOT.GetVersionCode())
    if gROOT.GetVersionCode() >= 332288 and gROOT.GetVersionCode() < 332544:
        print "*** You are running ROOT version 5.18, which has problems in PyROOT such that TMVA"
        print "*** does not run properly (function calls with enums in the argument are ignored)."
        print "*** Solution: either use CINT or a C++ compiled version (see TMVA/macros or TMVA/examples),"
        print "*** or use another ROOT version (e.g., ROOT 5.19)."
        sys.exit(1)
    
    # Logon not automatically loaded through PyROOT (logon loads TMVA library) load also GUI
    gROOT.SetMacroPath( "./" )
## SO I TAKE DEFAULT FORM ROOT#    gROOT.Macro       ( "./TMVAlogon.C" )    
#!    gROOT.LoadMacro   ( "./TMVAGui.C" )
 
   
    # Import TMVA classes from ROOT
    from ROOT import TMVA

    # Output file
    outputFile = TFile( outfname, 'RECREATE' )
    
    # Create instance of TMVA factory (see TMVA/macros/TMVAClassification.C for more factory options)
    # All TMVA output can be suppressed by removing the "!" (not) in 
    # front of the "Silent" argument in the option string
    factory = TMVA.Factory( "TMVAClassification", outputFile, 
                            "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" )

    # Set verbosity
    factory.SetVerbose( verbose )
    
    # If you wish to modify default settings 
    # (please check "src/Config.h" to see all available global options)
    #    gConfig().GetVariablePlotting()).fTimesRMS = 8.0
    (TMVA.gConfig().GetIONames()).fWeightFileDir = weight_fold;

    # Define the input variables that shall be used for the classifier training
    # note that you may also use variable expressions, such as: "3*var1/var2*abs(var3)"
    # [all types of expressions that can also be parsed by TTree::Draw( "expression" )]
    factory.AddVariable( "dR_l1l2", "dR_l1l2", "", 'F' )
    factory.AddVariable( "dR_b1b2", "dR_b1b2", "", 'F' )
    factory.AddVariable( "dR_bl", "dR_bl", "", 'F' )
    factory.AddVariable( "dR_l1l2b1b2", "dR_l1l2b1b2", "", 'F' )
    factory.AddVariable( "MINdR_bl", "MINdR_bl", "", 'F' )
    factory.AddVariable( "dphi_l1l2b1b2", "dphi_l1l2b1b2", "", 'F' )
    factory.AddVariable( "mass_l1l2", "mass_l1l2", "", 'F' )
    factory.AddVariable( "mass_b1b2", "mass_b1b2", "", 'F' )
    factory.AddVariable( "mass_trans", "mass_trans", "", 'F' )
    factory.AddVariable( "MT2", "MT2", "", 'F' )
    factory.AddVariable( "pt_b1b2", "pt_b1b2", "", 'F' )
    #factory.AddVariable( "MMC_h2mass_MaxBin", "MMC_h2mass_MaxBin", "", 'F' )
    #factory.AddVariable( "MMC_h2mass_RMS", "MMC_h2mass_RMS", "", 'F' )
    #factory.AddVariable( "MMC_h2mass_prob", "MMC_h2mass_prob", "", 'F' )

    # You can add so-called "Spectator variables", which are not used in the MVA training, 
    # but will appear in the final "TestTree" produced by TMVA. This TestTree will contain the 
    # input variables, the response values of all trained MVAs, and the spectator variables
    # factory.AddSpectator( "spec1:=var1*2",  "Spectator 1", "units", 'F' )
    # factory.AddSpectator( "spec2:=var1*3",  "Spectator 2", "units", 'F' )

    # Read input data
    if gSystem.AccessPathName( infnameSig ) != 0 or gSystem.AccessPathName( infnameBkg ): gSystem.Exec( "wget http://root.cern.ch/files/" + infname )
        
    inputSig = TFile.Open( infnameSig )
    inputBkg = TFile.Open( infnameBkg )

    # Get the signal and background trees for training
    signal      = inputSig.Get( treeNameSig )
    background  = inputBkg.Get( treeNameBkg )

    ##signal.AddFriend( "eleIDdir/isoT1 = eleIDdir/T1", friendfnameSig )
    ##background.AddFriend( "eleIDdir/isoT1 = eleIDdir/T1", friendfnameBkg )

    # Global event weights (see below for setting event-wise weights)
    signalWeight     = 1.
    backgroundWeight = 1.

#I don't think there's a general answer to this. The safest 'default'
#is to use the envent weight such that you have equal amounts of signal
#and background
#for the training, otherwise for example:  if you look for a rare
#signal and you use the weight to scale the number of events according
#to the expected ratio of signal and background
#according to the luminosity... the classifier sees hardly any signal
#events and "thinks" .. Oh I just classify everything background and do
#a good job!
#
#One can try to 'optimize' the training a bit more in  either 'high
#purity' or 'high efficiency' by choosing different weights, but as I
#said, there's no fixed rule. You'd have
#to 'try' and see if you get better restults by playing with the weights.

    # ====== register trees ====================================================
    #
    # the following method is the prefered one:
    # you can add an arbitrary number of signal or background trees
    factory.AddSignalTree    ( signal,     signalWeight     )
    factory.AddBackgroundTree( background, backgroundWeight )

    # To give different trees for training and testing, do as follows:
    #    factory.AddSignalTree( signalTrainingTree, signalTrainWeight, "Training" )
    #    factory.AddSignalTree( signalTestTree,     signalTestWeight,  "Test" )
    
    # Use the following code instead of the above two or four lines to add signal and background 
    # training and test events "by hand"
    # NOTE that in this case one should not give expressions (such as "var1+var2") in the input 
    #      variable definition, but simply compute the expression before adding the event
    #
    #    # --- begin ----------------------------------------------------------
    #    
    # ... *** please lookup code in TMVA/macros/TMVAClassification.C ***
    #    
    #    # --- end ------------------------------------------------------------
    #
    # ====== end of register trees ==============================================    
            
    # Set individual event weights (the variables must exist in the original TTree)
    #    for signal    : factory.SetSignalWeightExpression    ("weight1*weight2");
    #    for background: factory.SetBackgroundWeightExpression("weight1*weight2");

    # Apply additional cuts on the signal and background sample. 
    # example for cut: mycut = TCut( "abs(var1)<0.5 && abs(var2-0.5)<1" )
    #mycutSig = TCut( "nu1and2_diBaxis_t>-900 && met_diBaxis_t>-900&& hasb1jet && hasb2jet && hasMET && hasGenMET && hasdRljet && hastwomuons" ) 
    mycutSig = TCut( addedcuts ) 
    #mycutBkg = TCut( "event_n%2!=0 && " + addedcuts ) 
    mycutBkg = TCut( addedcuts ) 
    #mycutBkg = TCut( "nu1and2_diBaxis_t>-900 && met_diBaxis_t>-900&& hasb1jet && hasb2jet && hasMET && hasGenMET && hasdRljet && hastwomuons" ) 

    print mycutSig

    # Here, the relevant variables are copied over in new, slim trees that are
    # used for TMVA training and testing
    # "SplitMode=Random" means that the input events are randomly shuffled before
    # splitting them into training and test samples
    factory.PrepareTrainingAndTestTree( mycutSig, mycutBkg,
                                        "nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" )

    # --------------------------------------------------------------------------------------------------

    # ---- Book MVA methods
    #
    # please lookup the various method configuration options in the corresponding cxx files, eg:
    # src/MethoCuts.cxx, etc, or here: http://tmva.sourceforge.net/optionRef.html
    # it is possible to preset ranges in the option string in which the cut optimisation should be done:
    # "...:CutRangeMin[2]=-1:CutRangeMax[2]=1"...", where [2] is the third input variable

    # Cut optimisation
    if "Cuts" in mlist:
        factory.BookMethod( TMVA.Types.kCuts, "Cuts",
                            "!H:!V:FitMethod=MC:EffSel:VarProp[0]=FMax:VarProp[1]=FMin" )

    if "CutsD" in mlist:
        factory.BookMethod( TMVA.Types.kCuts, "CutsD",
                            "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=Decorrelate" )

    if "CutsPCA" in mlist:
        factory.BookMethod( TMVA.Types.kCuts, "CutsPCA",
                            "!H:!V:FitMethod=MC:EffSel:SampleSize=200000:VarProp=FSmart:VarTransform=PCA" )

    if "CutsGA" in mlist:
        factory.BookMethod( TMVA.Types.kCuts, "CutsGA",
                            "H:!V:FitMethod=GA:EffSel:Steps=30:Cycles=3:PopSize=400:SC_steps=10:SC_rate=5:SC_factor=0.95:VarProp[0]=FMin:VarProp[1]=FMax" )

    if "CutsSA" in mlist:
        factory.BookMethod( TMVA.Types.kCuts, "CutsSA",
                            "!H:!V:FitMethod=SA:EffSel:MaxCalls=150000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" )

    # Likelihood ("naive Bayes estimator")
    if "Likelihood" in mlist:
        factory.BookMethod( TMVA.Types.kLikelihood, "Likelihood",
                            "H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmoothBkg[1]=10:NSmooth=1:NAvEvtPerBin=50" )

    # Decorrelated likelihood
    if "LikelihoodD" in mlist:
        factory.BookMethod( TMVA.Types.kLikelihood, "LikelihoodD",
                            "!H:!V:TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=Decorrelate" )

    # PCA-transformed likelihood
    if "LikelihoodPCA" in mlist:
        factory.BookMethod( TMVA.Types.kLikelihood, "LikelihoodPCA",
                            "!H:!V:!TransformOutput:PDFInterpol=Spline2:NSmoothSig[0]=20:NSmoothBkg[0]=20:NSmooth=5:NAvEvtPerBin=50:VarTransform=PCA" ) 

    # Use a kernel density estimator to approximate the PDFs
    if "LikelihoodKDE" in mlist:
        factory.BookMethod( TMVA.Types.kLikelihood, "LikelihoodKDE",
                            "!H:!V:!TransformOutput:PDFInterpol=KDE:KDEtype=Gauss:KDEiter=Adaptive:KDEFineFactor=0.3:KDEborder=None:NAvEvtPerBin=50" ) 

    # Use a variable-dependent mix of splines and kernel density estimator
    if "LikelihoodMIX" in mlist:
        factory.BookMethod( TMVA.Types.kLikelihood, "LikelihoodMIX",
                            "!H:!V:!TransformOutput:PDFInterpolSig[0]=KDE:PDFInterpolBkg[0]=KDE:PDFInterpolSig[1]=KDE:PDFInterpolBkg[1]=KDE:PDFInterpolSig[2]=Spline2:PDFInterpolBkg[2]=Spline2:PDFInterpolSig[3]=Spline2:PDFInterpolBkg[3]=Spline2:KDEtype=Gauss:KDEiter=Nonadaptive:KDEborder=None:NAvEvtPerBin=50" ) 

    # Test the multi-dimensional probability density estimator
    # here are the options strings for the MinMax and RMS methods, respectively:
    #      "!H:!V:VolumeRangeMode=MinMax:DeltaFrac=0.2:KernelEstimator=Gauss:GaussSigma=0.3" );
    #      "!H:!V:VolumeRangeMode=RMS:DeltaFrac=3:KernelEstimator=Gauss:GaussSigma=0.3" );
    if "PDERS" in mlist:
        factory.BookMethod( TMVA.Types.kPDERS, "PDERS",
                            "!H:!V:NormTree=T:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600" )

    if "PDERSD" in mlist:
        factory.BookMethod( TMVA.Types.kPDERS, "PDERSD",
                            "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=Decorrelate" )

    if "PDERSPCA" in mlist:
        factory.BookMethod( TMVA.Types.kPDERS, "PDERSPCA",
                             "!H:!V:VolumeRangeMode=Adaptive:KernelEstimator=Gauss:GaussSigma=0.3:NEventsMin=400:NEventsMax=600:VarTransform=PCA" )

   # Multi-dimensional likelihood estimator using self-adapting phase-space binning
    if "PDEFoam" in mlist:
        factory.BookMethod( TMVA.Types.kPDEFoam, "PDEFoam",
                            "!H:!V:SigBgSeparate=F:TailCut=0.001:VolFrac=0.0666:nActiveCells=500:nSampl=2000:nBin=5:Nmin=100:Kernel=None:Compress=T" )

    if "PDEFoamBoost" in mlist:
        factory.BookMethod( TMVA.Types.kPDEFoam, "PDEFoamBoost",
                            "!H:!V:Boost_Num=30:Boost_Transform=linear:SigBgSeparate=F:MaxDepth=4:UseYesNoCell=T:DTLogic=MisClassificationError:FillFoamWithOrigWeights=F:TailCut=0:nActiveCells=500:nBin=20:Nmin=400:Kernel=None:Compress=T" )

    # K-Nearest Neighbour classifier (KNN)
    if "KNN" in mlist:
        factory.BookMethod( TMVA.Types.kKNN, "KNN",
                            "H:nkNN=20:ScaleFrac=0.8:SigmaFact=1.0:Kernel=Gaus:UseKernel=F:UseWeight=T:!Trim" )

    # H-Matrix (chi2-squared) method
    if "HMatrix" in mlist:
        factory.BookMethod( TMVA.Types.kHMatrix, "HMatrix", "!H:!V" )

    # Linear discriminant (same as Fisher discriminant)
    if "LD" in mlist:
        factory.BookMethod( TMVA.Types.kLD, "LD", "H:!V:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" )

    # Fisher discriminant (same as LD)
    if "Fisher" in mlist:
        factory.BookMethod( TMVA.Types.kFisher, "Fisher", "H:!V:Fisher:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" )

    # Fisher with Gauss-transformed input variables
    if "FisherG" in mlist:
        factory.BookMethod( TMVA.Types.kFisher, "FisherG", "H:!V:VarTransform=Gauss" )

    # Composite classifier: ensemble (tree) of boosted Fisher classifiers
    if "BoostedFisher" in mlist:
        factory.BookMethod( TMVA.Types.kFisher, "BoostedFisher", 
                            "H:!V:Boost_Num=20:Boost_Transform=log:Boost_Type=AdaBoost:Boost_AdaBoostBeta=0.2" )

    # Function discrimination analysis (FDA) -- test of various fitters - the recommended one is Minuit (or GA or SA)
    if "FDA_MC" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_MC",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:SampleSize=100000:Sigma=0.1" );

    if "FDA_GA" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_GA",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:PopSize=300:Cycles=3:Steps=20:Trim=True:SaveBestGen=1" );

    if "FDA_SA" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_SA",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=SA:MaxCalls=15000:KernelTemp=IncAdaptive:InitialTemp=1e+6:MinTemp=1e-6:Eps=1e-10:UseDefaultScale" );

    if "FDA_MT" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_MT",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=2:UseImprove:UseMinos:SetBatch" );

    if "FDA_GAMT" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_GAMT",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=GA:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:Cycles=1:PopSize=5:Steps=5:Trim" );

    if "FDA_MCMT" in mlist:
        factory.BookMethod( TMVA.Types.kFDA, "FDA_MCMT",
                            "H:!V:Formula=(0)+(1)*x0+(2)*x1+(3)*x2+(4)*x3:ParRanges=(-1,1)(-10,10);(-10,10);(-10,10);(-10,10):FitMethod=MC:Converger=MINUIT:ErrorLevel=1:PrintLevel=-1:FitStrategy=0:!UseImprove:!UseMinos:SetBatch:SampleSize=20" );

    # TMVA ANN: MLP (recommended ANN) -- all ANNs in TMVA are Multilayer Perceptrons
    if "MLP" in mlist:
        factory.BookMethod( TMVA.Types.kMLP, "MLP", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:!UseRegulator" )

    if "MLPBFGS" in mlist:
        factory.BookMethod( TMVA.Types.kMLP, "MLPBFGS", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:!UseRegulator" )

    if "MLPBNN" in mlist:
        factory.BookMethod( TMVA.Types.kMLP, "MLPBNN", "H:!V:NeuronType=tanh:VarTransform=N:NCycles=600:HiddenLayers=N+5:TestRate=5:TrainingMethod=BFGS:UseRegulator" ) # BFGS training with bayesian regulators

    # CF(Clermont-Ferrand)ANN
    if "CFMlpANN" in mlist:
        factory.BookMethod( TMVA.Types.kCFMlpANN, "CFMlpANN", "!H:!V:NCycles=2000:HiddenLayers=N+1,N"  ) # n_cycles:#nodes:#nodes:...  

    # Tmlp(Root)ANN
    if "TMlpANN" in mlist:
        factory.BookMethod( TMVA.Types.kTMlpANN, "TMlpANN", "!H:!V:NCycles=200:HiddenLayers=N+1,N:LearningMethod=BFGS:ValidationFraction=0.3"  ) #n_cycles:#nodes:#nodes:...

    # Support Vector Machine
    if "SVM" in mlist:
        factory.BookMethod( TMVA.Types.kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm" )

    # Boosted Decision Trees
    if "BDTG" in mlist:
        factory.BookMethod( TMVA.Types.kBDT, "BDTG",
                            "!H:!V:NTrees=1000:BoostType=Grad:Shrinkage=0.30:UseBaggedGrad:GradBaggingFraction=0.6:SeparationType=GiniIndex:nCuts=20:NNodesMax=5" )

    if "BDT" in mlist:
        factory.BookMethod( TMVA.Types.kBDT, "BDT",
                            "!H:!V:NTrees=850:nEventsMin=150:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" )

    if "BDTB" in mlist:
        factory.BookMethod( TMVA.Types.kBDT, "BDTB",
                            "!H:!V:NTrees=400:BoostType=Bagging:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning" )

    if "BDTD" in mlist:
        factory.BookMethod( TMVA.Types.kBDT, "BDTD",
                            "!H:!V:NTrees=400:nEventsMin=400:MaxDepth=3:BoostType=AdaBoost:SeparationType=GiniIndex:nCuts=20:PruneMethod=NoPruning:VarTransform=Decorrelate" )

    # RuleFit -- TMVA implementation of Friedman's method
    if "RuleFit" in mlist:
        factory.BookMethod( TMVA.Types.kRuleFit, "RuleFit",
                            "H:!V:RuleFitModule=RFTMVA:Model=ModRuleLinear:MinImp=0.001:RuleMinDist=0.001:NTrees=20:fEventsMin=0.01:fEventsMax=0.5:GDTau=-1.0:GDTauPrec=0.01:GDStep=0.01:GDNSteps=10000:GDErrScale=1.02" )

    # --------------------------------------------------------------------------------------------------
            
    # ---- Now you can tell the factory to train, test, and evaluate the MVAs. 

    # Train MVAs
    factory.TrainAllMethods()
    
    # Test MVAs
    factory.TestAllMethods()
    
    # Evaluate MVAs
    factory.EvaluateAllMethods()    
    
    # Save the output.
    outputFile.Close()
    
    print "=== wrote root file %s\n" % outfname
    print "=== TMVAClassification is done!\n"
    
    # open the GUI for the result macros    
    if( gui ):
        gROOT.ProcessLine( "TMVAGui(\"%s\")" % outfname )
        # keep the ROOT thread running
        gApplication.Run() 
Example #7
0
# period = "data2017"
period = "data2018"
# *****************************
lumiText = '16.6 fb^{-1}'
#******************************

if (ZZTree): treeText = "ZZTree"
elif (CRZLTree): treeText = "CRZLTree"
elif (ZTree): treeText = "ZTree"
else: print("Error: wrong option!")

#******************************

# create output directory
OutputPath = "SipDistrib_DATAvsMC_" + str(period) + "_" + str(treeText)
gSystem.Exec("mkdir -p " + OutputPath)
print "Output directory created!"

# *** DATA ***
#read histos from data file

histoDATA_input = TFile.Open("SipDistrib_DATA_" + str(period) + "_" +
                             str(treeText) + ".root")
print 'Reading file', histoDATA_input.GetName(), '...'

SipDATA = []

SipDATA.append(histoDATA_input.Get('SIP leading ele'))
SipDATA.append(histoDATA_input.Get('SIP leading ele in ECAL Barrel'))
SipDATA.append(histoDATA_input.Get('SIP leading ele in ECAL Endcap'))
Example #8
0
    out.Close()

#main

#_____________________________________________________________________________
if __name__ == "__main__":

    gROOT.SetBatch()
    gStyle.SetPadTickX(1)
    gStyle.SetFrameLineWidth(2)

    main()

    #beep when finished
    #gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")
    gSystem.Exec("mplayer ../input_ok_3_clean.mp3 > /dev/null 2>&1")














Example #9
0
def main():

    #testing macro for bin centers from the data

    gROOT.SetBatch()

    #range for |t|
    ptmin = 0.
    ptmax = 0.109  #   0.109  0.01 for interference range

    #default binning
    ptbin = 0.004  # 0.004  0.0005 for interference range

    #long bins at high |t|
    ptmid = 0.06  # 0.08, value > ptmax will switch it off   0.06
    ptlon = 0.01  # 0.01

    #mass interval
    mmin = 2.8
    mmax = 3.2

    #data
    basedir = "../../../star-upc-data/ana/muDst/muDst_run1/sel5"
    infile = "ana_muDst_run1_all_sel5z.root"

    #open the inputs
    inp = TFile.Open(basedir + "/" + infile)
    tree = inp.Get("jRecTree")

    #evaluate binning
    bins = ut.get_bins_vec_2pt(ptbin, ptlon, ptmin, ptmax, ptmid)

    #load the data
    strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax)

    #input pT^2
    hPt = ut.prepare_TH1D_vec("hPt", bins)
    tree.Draw("jRecPt*jRecPt >> hPt", strsel)

    #bin centers from the data
    cen_tree = get_centers_from_tree(hPt, tree, strsel)
    #print
    cen_toyMC = get_centers_from_toyMC(hPt)

    for i in xrange(len(cen_tree)):
        #print i, cen_tree[i], cen_toyMC[i], cen_tree[i] - cen_toyMC[i]
        pass

    #gr = get_data_graph(hPt, cen_tree)
    gr = get_data_graph(hPt, cen_toyMC)

    #plot the distribution
    can = ut.box_canvas()
    frame = ut.prepare_TH1D("frame", ptbin, ptmin, ptmax)
    #ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.03, 0.03)
    ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.055, 0.03)
    gPad.SetLogy()

    frame.SetMaximum(310)
    #frame.SetMinimum(1.e-6)
    frame.SetMinimum(3)
    #frame.SetMinimum(1e-5)  # 3e-5
    frame.Draw()

    #hPt.Draw("e1same")
    gr.Draw("epsame")

    ut.invert_col(gPad)
    can.SaveAs("01fig.pdf")

    #beep when finished
    gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")
Example #10
0
def main():

    gROOT.SetBatch()

    #range for |t|
    ptmin = 0.
    ptmax = 0.109  #   0.109  0.01 for interference range

    #default binning
    ptbin = 0.004  # 0.004  0.0005 for interference range

    #long bins at high |t|
    ptmid = 0.06  # 0.08, value > ptmax will switch it off   0.06
    ptlon = 0.01  # 0.01

    #short bins at low |t|
    ptlow = 0.01
    ptshort = 0.0005

    #mass interval
    mmin = 2.8
    mmax = 3.2

    #dy = 2. # rapidity interval, for integrated sigma
    dy = 1.

    ngg = 131  # number of gamma-gamma from mass fit

    lumi = 13871.907  # lumi in inv. ub

    #correction to luminosity for ana/triggered events
    ratio_ana = 3420950. / 3694000

    #scale the lumi for |z| around nominal bunch crossing
    ratio_zdc_vtx = 0.502

    Reta = 0.503  # pseudorapidity preselection
    #Reta = 1.

    trg_eff = 0.67  # bemc trigger efficiency

    ratio_tof = 1.433  # tof correction to efficiency

    bbceff = 0.97  # BBC veto inefficiency

    zdc_acc = 0.49  # ZDC acceptance to XnXn 0.7
    #zdc_acc = 1.

    br = 0.05971  # dielectrons branching ratio

    #data
    basedir = "../../../star-upc-data/ana/muDst/muDst_run1/sel5"
    infile = "ana_muDst_run1_all_sel5z.root"

    #MC
    basedir_sl = "../../../star-upc-data/ana/starsim/slight14e/sel5"
    #infile_sl = "ana_slight14e1x2_s6_sel5z.root"
    infile_sl = "ana_slight14e1x3_s6_sel5z.root"
    #
    basedir_sart = "../../../star-upc-data/ana/starsim/sartre14a/sel5"
    infile_sart = "ana_sartre14a1_sel5z_s6_v2.root"
    #
    basedir_bgen = "../../../star-upc-data/ana/starsim/bgen14a/sel5"
    infile_bgen = "ana_bgen14a1_v0_sel5z_s6.root"
    #infile_bgen = "ana_bgen14a2_sel5z_s6.root"
    #
    basedir_gg = "../../../star-upc-data/ana/starsim/slight14e/sel5"
    infile_gg = "ana_slight14e2x1_sel5_nzvtx.root"

    #model predictions
    gSlight = load_starlight(dy)
    gSartre = load_sartre()
    gFlat = loat_flat_pt2()
    gMS = load_ms()
    gCCK = load_cck()

    #open the inputs
    inp = TFile.Open(basedir + "/" + infile)
    tree = inp.Get("jRecTree")
    #
    inp_gg = TFile.Open(basedir_gg + "/" + infile_gg)
    tree_gg = inp_gg.Get("jRecTree")
    #
    inp_sl = TFile.Open(basedir_sl + "/" + infile_sl)
    tree_sl_gen = inp_sl.Get("jGenTree")
    #
    inp_sart = TFile.Open(basedir_sart + "/" + infile_sart)
    tree_sart_gen = inp_sart.Get("jGenTree")
    #
    inp_bgen = TFile.Open(basedir_bgen + "/" + infile_bgen)
    tree_bgen_gen = inp_bgen.Get("jGenTree")

    #evaluate binning
    #print "bins:", ut.get_nbins(ptbin, ptmin, ptmax)

    bins = ut.get_bins_vec_2pt(ptbin, ptlon, ptmin, ptmax, ptmid)
    #bins = ut.get_bins_vec_3pt(ptshort, ptbin, ptlon, ptmin, ptmax, ptlow, ptmid)
    #print "bins2:", bins.size()-1

    #load the data
    strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax)

    hPt = ut.prepare_TH1D_vec("hPt", bins)
    tree.Draw("jRecPt*jRecPt >> hPt", strsel)

    #distribution for bin centers
    hPtCen = hPt.Clone("hPtCen")

    #gamma-gamma component
    hPtGG = ut.prepare_TH1D_vec("hPtGG", bins)
    tree_gg.Draw("jRecPt*jRecPt >> hPtGG", strsel)

    #normalize the gamma-gamma component
    ut.norm_to_num(hPtGG, ngg, rt.kGreen)

    #incoherent functional shape
    func_incoh_pt2 = TF1("func_incoh", "[0]*exp(-[1]*x)", 0., 10.)
    func_incoh_pt2.SetParameters(873.04, 3.28)

    #fill incoherent histogram from functional shape
    hPtIncoh = ut.prepare_TH1D_vec("hPtIncoh", bins)
    ut.fill_h1_tf(hPtIncoh, func_incoh_pt2, rt.kRed)

    #print "Entries before gamma-gamma and incoherent subtraction:", hPt.GetEntries()

    #subtract gamma-gamma and incoherent components
    hPt.Sumw2()
    hPt.Add(hPtGG, -1)
    #print "Gamma-gamma entries:", hPtGG.Integral()
    #print "Entries after gamma-gamma subtraction:", hPt.Integral()
    #print "Incoherent entries:", hPtIncoh.Integral()
    hPt.Add(hPtIncoh, -1)

    #print "Entries after all subtraction:", hPt.Integral()

    #scale the luminosity
    lumi_scaled = lumi * ratio_ana * ratio_zdc_vtx
    #print "lumi_scaled:", lumi_scaled

    #denominator for deconvoluted distribution, conversion ub to mb
    den = Reta * br * zdc_acc * trg_eff * bbceff * ratio_tof * lumi_scaled * 1000. * dy

    #deconvolution
    deconv_min = bins[0]
    deconv_max = bins[bins.size() - 1]
    deconv_nbin = bins.size() - 1
    gROOT.LoadMacro("fill_response_matrix.C")

    #Starlight response
    #resp_sl = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    resp_sl = RooUnfoldResponse(hPt, hPt)
    rt.fill_response_matrix(tree_sl_gen, resp_sl)
    #
    unfold_sl = RooUnfoldBayes(resp_sl, hPt, 15)
    #unfold_sl = RooUnfoldSvd(resp_sl, hPt, 15)
    hPtSl = unfold_sl.Hreco()
    #ut.set_H1D(hPtSl)
    #apply the denominator and bin width
    ut.norm_to_den_w(hPtSl, den)

    #Sartre response
    #resp_sart = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    #resp_sart = RooUnfoldResponse(hPt, hPt)
    #rt.fill_response_matrix(tree_sart_gen, resp_sart)
    #
    #unfold_sart = RooUnfoldBayes(resp_sart, hPt, 10)
    #hPtSart = unfold_sart.Hreco()
    #ut.set_H1D(hPtSart)
    #hPtSart.SetMarkerStyle(21)

    #Flat pT^2 response
    #resp_bgen = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max)
    resp_bgen = RooUnfoldResponse(hPt, hPt)
    rt.fill_response_matrix(tree_bgen_gen, resp_bgen)
    #
    unfold_bgen = RooUnfoldBayes(resp_bgen, hPt, 14)
    hPtFlat = unfold_bgen.Hreco()
    #ut.set_H1D(hPtFlat)
    #apply the denominator and bin width
    ut.norm_to_den_w(hPtFlat, den)
    #hPtFlat.SetMarkerStyle(22)
    #hPtFlat.SetMarkerSize(1.3)

    #systematical errors
    err_zdc_acc = 0.1
    err_bemc_eff = 0.03
    #sys_err = rt.TMath.Sqrt(err_zdc_acc*err_zdc_acc + err_bemc_eff*err_bemc_eff)
    sys_err = err_zdc_acc * err_zdc_acc + err_bemc_eff * err_bemc_eff
    #print "Total sys err:", sys_err
    hSys = ut.prepare_TH1D_vec("hSys", bins)
    hSys.SetOption("E2")
    hSys.SetFillColor(rt.kOrange + 1)
    hSys.SetLineColor(rt.kOrange)
    for ibin in xrange(1, hPtFlat.GetNbinsX() + 1):
        hSys.SetBinContent(ibin, hPtFlat.GetBinContent(ibin))
        sig_sl = hPtSl.GetBinContent(ibin)
        sig_fl = hPtFlat.GetBinContent(ibin)
        err_deconv = TMath.Abs(sig_fl - sig_sl) / sig_fl
        #print "err_deconv", err_deconv
        #sys_err += err_deconv*err_deconv
        sys_err_sq = sys_err + err_deconv * err_deconv
        sys_err_bin = TMath.Sqrt(sys_err_sq)
        stat_err = hPtFlat.GetBinError(ibin) / hPtFlat.GetBinContent(ibin)
        tot_err = TMath.Sqrt(stat_err * stat_err + sys_err_sq)
        #hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*err_deconv)
        hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin) * sys_err_bin)
        #hPtFlat.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*tot_err)

    #draw the results
    gStyle.SetPadTickX(1)
    gStyle.SetFrameLineWidth(2)

    #frame for models plot only
    frame = ut.prepare_TH1D("frame", ptbin, ptmin, ptmax)

    can = ut.box_canvas()
    #ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.03, 0.03)
    ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.055, 0.01)

    ytit = "d#it{#sigma}/d#it{t}d#it{y} (mb/(GeV/c)^{2})"
    xtit = "|#kern[0.3]{#it{t}}| ((GeV/c)^{2})"

    ut.put_yx_tit(frame, ytit, xtit, 1.4, 1.2)
    frame.SetMaximum(11)
    #frame.SetMinimum(1.e-6)
    #frame.SetMinimum(2e-4)
    frame.SetMinimum(1e-5)  # 3e-5
    frame.Draw()

    #hSys.Draw("e2same")

    #bin center points from data
    #gSig = apply_centers(hPtFlat, hPtCen)
    gSig = fixed_centers(hPtFlat)
    ut.set_graph(gSig)

    #hPtSl.Draw("e1same")
    #hPtSart.Draw("e1same")
    #hPtFlat.Draw("e1same")

    #put model predictions
    #gSartre.Draw("lsame")
    #gFlat.Draw("lsame")
    gMS.Draw("lsame")
    gCCK.Draw("lsame")
    gSlight.Draw("lsame")

    gSig.Draw("P")

    frame.Draw("same")

    gPad.SetLogy()

    cleg = ut.prepare_leg(0.1, 0.96, 0.14, 0.01, 0.035)
    cleg.AddEntry(
        None,
        "Au+Au #rightarrow J/#psi + Au+Au + XnXn, #sqrt{#it{s}_{#it{NN}}} = 200 GeV",
        "")
    cleg.Draw("same")

    leg = ut.prepare_leg(0.45, 0.82, 0.18, 0.1, 0.035)
    leg.AddEntry(None, "#bf{|#kern[0.3]{#it{y}}| < 1}", "")
    hx = ut.prepare_TH1D("hx", 1, 0, 1)
    leg.AddEntry(hx, "STAR")
    hx.Draw("same")
    leg.Draw("same")

    #legend for models
    mleg = ut.prepare_leg(0.68, 0.76, 0.3, 0.16, 0.035)
    #mleg = ut.prepare_leg(0.68, 0.8, 0.3, 0.12, 0.035)
    mleg.AddEntry(gSlight, "STARLIGHT", "l")
    mleg.AddEntry(gMS, "MS", "l")
    mleg.AddEntry(gCCK, "CCK-hs", "l")
    #mleg.AddEntry(gSartre, "Sartre", "l")
    #mleg.AddEntry(gFlat, "Flat #it{p}_{T}^{2}", "l")
    mleg.Draw("same")

    #legend for deconvolution method
    dleg = ut.prepare_leg(0.3, 0.75, 0.2, 0.18, 0.035)
    #dleg = ut.prepare_leg(0.3, 0.83, 0.2, 0.1, 0.035)
    dleg.AddEntry(None, "Unfolding with:", "")
    dleg.AddEntry(hPtSl, "Starlight", "p")
    #dleg.AddEntry(hPtSart, "Sartre", "p")
    dleg.AddEntry(hPtFlat, "Flat #it{p}_{T}^{2}", "p")
    #dleg.Draw("same")

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")

    #to prevent 'pure virtual method called'
    gPad.Close()

    #save the cross section to output file
    out = TFile("sigma.root", "recreate")
    gSig.Write("sigma")
    out.Close()

    #beep when finished
    gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")
Example #11
0
    hEff.Draw("AP")
    #hEff.Draw()
    fitFunc.Draw("same")
    desc.draw()
    leg.Draw("same")

    ut.invert_col(rt.gPad)
    can.SaveAs("01fig.pdf")

    #to prevent 'pure virtual method called'
    gPad.Close()

    #remove the temporary
    outfile.Close()
    gSystem.Exec("rm -f "+outnam)

    #beep when finished
    gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")