#include #include #include #include #include #include #include #include "TMath.h" #include "TChain.h" #include "TFile.h" #include "TTree.h" #include "TLatex.h" #include "TString.h" #include "TObjString.h" #include #include #include #include #include "TSystem.h" #include "TROOT.h" #include "TMVA/Factory.h" #include "TMVA/DataLoader.h" #include "TMVA/Reader.h" #include "TMVA/Tools.h" #include "TMVA/MethodCuts.h" #include "TMVA/TMVAGui.h" using namespace std; using namespace TMVA; /*--------------------------------------------------------------------------* * Function to Get TTrees from File * *--------------------------------------------------------------------------*/ TTree* getTree(TString filename, TString TupleName) { // Get the TTree objects from files TFile* inputfile = new TFile(filename); TTree* Tree = (TTree*)inputfile->Get(TupleName); return Tree; delete inputfile; } /*--------------------------------------------------------------------------* * Significance Equation * *--------------------------------------------------------------------------*/ double ZA_fcn(double s, double b) { double Z = sqrt( 2. * ( (s+b)*TMath::Log(1 + (s/b)) - s ) ); if ( Z > 100. ) { Z = 0.; } return Z; } /*--------------------------------------------------------------------------* * Significance Function for Minuit Analysis * *--------------------------------------------------------------------------*/ // fcn passes back f = chi, the function to be minimized. void fcn(int& nvars, double* deriv, double& f, double par[], int flag) { TString sigFilename = "hhttbb.root"; TString bckFilename[] = {"ttbar_new.root", "Ztautau.root", "ZtautauB.root", "ZtautauC.root", "ZtautauL.root"}; TString TupleName = "CxAODTuple_Nominal"; const int nBackgrounds = 5; // Get the TTree objects from files TFile* siginputfile = new TFile(sigFilename); TTree* sigTree = (TTree*)siginputfile->Get(TupleName); // Get the TTree objects from files TFile* bckinputfile[nBackgrounds]; TTree* bckTree[nBackgrounds]; for ( int i=0 ; iGet(TupleName); } TString varsName[] = {"mBB", "mMMC","mHH", "drBB", "drLepTau", "mtLepMet"}; double sf[] = {1e-3, 1., 1e-3, 1., 1., 1e-3}; const int nSig = sigTree->GetEntries(); int nBck[nBackgrounds]; for ( int i=0 ; iGetEntries(); } // set address of variable to cut over TString SVN = "SelVars_Nominal_"; float var[nvars], weight; sigTree->SetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); for ( int i=0 ; iSetBranchAddress(SVN + varsName[i], &var[i]); } for ( int i=0 ; iSetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); for ( int j=0 ; jSetBranchAddress(SVN + varsName[j], &var[j]); } } double cutVals[nvars][2]; for ( int i=0 ; iGetEvent(i); bool pass = true; while (pass == true) { for ( int k=0 ; k cutVals[k][1]/sf[k] ) { pass = false;} } if ( pass == false) { break; } sigCount += weight; pass = false; break; } sigTotalWeight += weight; } // get the background events for each cut double bckCount[nBackgrounds]; for( int i=0 ; iGetEvent(j); bool pass = true; for ( int l=0 ; l cutVals[l][1]/sf[l] ) { pass = false; } } if ( pass == true ) { bckCount[i] += weight; } bckTotalWeight[i] += weight; } } // find the significance value double s = sigCount/sigTotalWeight; double b = 0.; for ( int j=0 ; jmncomd(Form("scan %i 1000 %lg %lg", varsIndex+1, xLimits[0], xLimits[1]), icondn); TCanvas *cScan = new TCanvas(); TGraph *gScan = (TGraph*)gMinuit->GetPlot(); gScan->SetTitle("Scan " + varsLabel + "; " + varsLabel + "; Significance, Z"); //gScan->GetYaxis()->SetRangeUser(30, 40); gScan->Draw("al"); // Print the scan as a png TImage *img; img = TImage::Create(); img->FromPad(cScan); if ( varsIndexWriteImage(Form("./Cuts/Multiple Variable Cuts/%i. ", varsIndex+1) + varsName + "_Scan_t>tcut.png"); } else { img->WriteImage(Form("./Cuts/Multiple Variable Cuts/%i. ", varsIndex+1-nvars) + varsName + "_Scan_ttcut", cutVals[0][0], 0.5, 30., 100.); // mBB minuit.DefineParameter(1, varsName[1]+"_t>tcut", cutVals[1][0], 0.5, 70., 110.); // mMMC minuit.DefineParameter(2, varsName[2]+"_t>tcut", cutVals[2][0], 0.5, 250., 700.); // mHH minuit.DefineParameter(3, varsName[3]+"_t>tcut", cutVals[3][0], 0.001, 0.4, 2.); // drBB minuit.DefineParameter(4, varsName[4]+"_t>tcut", cutVals[4][0], 0.001, 0.4, 2.); minuit.DefineParameter(5, varsName[5]+"_t>tcut", cutVals[5][0], 0.5, 0., 200.); minuit.DefineParameter(6, varsName[0]+"_tmncomd(Form("FIX %i", i+1), icondn);} // set i!= the variable you } // Do the minimization! gMinuit->mncomd("SIMplex 1000 0.1", icondn); gMinuit->mncomd("MIGrad 20000 0.1", icondn); /* FMIN: the best function value found so far FEDM: the estimated vertical distance remaining to minimum ERRDEF: the value of UP defining parameter uncertainties NPARI: the number of currently variable parameters PARX: the highest (external) parameter number defined by user ISTAT: a status integer indicating how good is the covariance matrix: 0= not calculated at all 1= approximation only, not accurate 2= full matrix, but forced positive-definite 3= full accurate covariance matrix */ double fmin, fedm, errdef; int npari, nparx, istat; minuit.mnstat(fmin, fedm, errdef, npari, nparx, istat); cout << "fmin = " << fmin << " fedm = " << fedm << " errdef = " << errdef << endl; cout << "npari = " << npari << " nparx = " << nparx << " istat = " << istat << endl; double outpar[nvars][2], err[nvars][2]; for (int i = 0; iGetParameter(i+1, outpar[i][0], err[i][0]); gMinuit->GetParameter(i+nvars+1, outpar[i][1], err[i][1]); } cout << "minimisation of variable " << varsName[cutVarIndex] << " complete" << endl << endl; double minResults[25]; //4*nvars +1 minResults[0] = fmin; for (int i = 0; iGetEntries(); int nBck[nBackgrounds]; for ( int i=0 ; iGetEntries(); } // set address of variable to cut over TString SVN = "SelVars_Nominal_"; float var[nvars], weight; sigTree->SetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); for ( int i=0 ; iSetBranchAddress(SVN + varsName[i], &var[i]); } for ( int i=0 ; iSetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); for ( int j=0 ; jSetBranchAddress(SVN + varsName[j], &var[j]); } } // cut on 1000 signal values const int nSteps = 1000; double xMin = sigTree->GetMinimum(SVN + varsName[varIndex]); double range = (Xlimits[1]/sf[varIndex]) - xMin; double cut[nSteps]; double Z[2][nSteps], max[2][2]; // first bracket: 0 = t>t_cut, 1 = tGetEvent(i); for ( int j=0 ; j cutVals[k][1]/sf[k] ) { pass = false;} } } if ( pass == false) { break; } if ( var[varIndex] > cut[j] && var[varIndex] < cutVals[varIndex][1]/sf[varIndex]) { sigCount[0][j] += weight; } if ( var[varIndex] < cut[j] && var[varIndex] > cutVals[varIndex][0]/sf[varIndex]) { sigCount[1][j] += weight; } pass = false; } } sigTotalWeight += weight; } // get the background events for each cut double bckCount[2][nSteps][nBackgrounds]; // first bracket: 0 = t>t_cut, 1 = tGetEvent(j); for ( int k=0 ; k cutVals[l][1]/sf[l] ) { pass = false; } } } if ( pass == false) { break; } if ( (var[varIndex] > cut[k]) && (var[varIndex] < cutVals[varIndex][1]/sf[varIndex]) ) { bckCount[0][k][i] += weight; } if ( (var[varIndex] < cut[k]) && (var[varIndex] > cutVals[varIndex][0]/sf[varIndex]) ) { bckCount[1][k][i] += weight; } pass = false; } } bckTotalWeight[i] += weight; } } // CHECK: output count /*for ( int i=0 ; iSetTitle( varsLabel + "; (" + varsLabel + ")_{cut} " + units + "; Z value"); gCut[0] = new TGraph(nSteps, cut, Z[0]); gCut[1] = new TGraph(nSteps, cut, Z[1]); for ( int i=0 ; i<2 ; i++){ gCut[i]->SetLineColor(colours[i]); gCut[i]->SetMarkerColor(colours[i]); gCut[i]->SetMarkerStyle(kDot); mg->Add(gCut[i], "p"); } mg->Draw("a"); mg->GetXaxis()->SetRangeUser(Xlimits[0], Xlimits[1]); // making the legend TLegend *leg = new TLegend(0.6, 0.725, 0.875, 0.875); leg->SetBorderSize(1); leg->SetFillStyle(0); leg->SetTextSize(0.03); leg->AddEntry(gCut[0], varsLabel + " > (" + varsLabel + ")_{cut} ", "l"); leg->AddEntry(gCut[1], varsLabel + " < (" + varsLabel + ")_{cut} ", "l"); leg->Draw(); c->Update(); // Print the Signal vs Background Cuts as a png TImage *img; img = TImage::Create(); img->FromPad(c); img->WriteImage(Form("./Cuts/Multiple Variable Cuts/%i. ", varIndex+1 ) + varsName[varIndex] + "_ManualScan_Weights.png"); cout << "Manual scan of " + varsName[varIndex] + " has been saved" << endl; double results[4] = { max[0][0], max[0][1], max[1][0], max[1][1] }; cout << results[0] << ", " << results[1] << ", " << results[2] << ", " << results[3] << endl << endl; return *results; delete img; delete gCut[0]; delete gCut[1]; delete leg; delete mg; delete c; } /*--------------------------------------------------------------------------* * Single Cut Analysis * *--------------------------------------------------------------------------*/ void SingleCut(TTree* sigTree, TTree** bckTree, const int nBackgrounds, int varIndex, TString varsName, TString varsLabel, TString units, double sf, double *Xlimits) { const int nSig = sigTree->GetEntries(); int nBck[nBackgrounds]; for ( int i=0 ; iGetEntries(); } // set address of variable to cut over TString SVN = "SelVars_Nominal_"; float var, weight; sigTree->SetBranchAddress(SVN + varsName, &var); sigTree->SetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); for ( int i=0 ; iSetBranchAddress(SVN + varsName, &var); bckTree[i]->SetBranchAddress("EventWeight_Nominal_TotalWeight", &weight); } // cut on 1000 signal values const int nSteps = 1000; double xMin = sigTree->GetMinimum(SVN + varsName); double range = (Xlimits[1]/sf) - xMin; double cut[nSteps]; double Z[2][nSteps], max[2][2]; // first bracket: 0 = t>t_cut, 1 = tGetEvent(i); for ( int j=0 ; j cut[j] ) { sigCount[j] += weight; } } sigTotalWeight += weight; } // get the background events for each cut double bckCount[2][nSteps][nBackgrounds]; // first bracket: 0 = t>t_cut, 1 = tGetEvent(j); for ( int k=0 ; k cut[k] ) { bckCount[0][k][i] += weight;}; if ( var < cut[k] ) { bckCount[1][k][i] += weight;}; } bckTotalWeight[i] += weight; } } // CHECK: output count /*for ( int i=0 ; iSetTitle( varsLabel + "; (" + varsLabel + ")_{cut} " + units + "; Z value"); gCut[0] = new TGraph(nSteps, cut, Z[0]); gCut[1] = new TGraph(nSteps, cut, Z[1]); for ( int i=0 ; i<2 ; i++){ gCut[i]->SetLineColor(colours[i]); gCut[i]->SetMarkerColor(colours[i]); gCut[i]->SetMarkerStyle(kDot); mg->Add(gCut[i], "p"); } mg->Draw("a"); mg->GetXaxis()->SetRangeUser(Xlimits[0], Xlimits[1]); // making the legend TLegend *leg = new TLegend(0.6, 0.725, 0.875, 0.875); leg->SetBorderSize(1); leg->SetFillStyle(0); leg->SetTextSize(0.03); leg->AddEntry(gCut[0], varsLabel + " > (" + varsLabel + ")_{cut} ", "l"); leg->AddEntry(gCut[1], varsLabel + " < (" + varsLabel + ")_{cut} ", "l"); leg->Draw(); c->Update(); // Print the Signal vs Background Cuts as a png TImage *img; img = TImage::Create(); img->FromPad(c); img->WriteImage(Form("./Cuts/Multiple Variable Cuts/%i. Cuts_", varIndex+1 ) + varsName + "_Weights.png"); cout << "Graph " + varsName + " has been saved" << endl; double results[4] = { max[0][0], max[0][1], max[1][0], max[1][1] }; cout << results[0] << ", " << results[1] << ", " << results[2] << ", " << results[3] << endl << endl; delete img; delete gCut[0]; delete gCut[1]; delete leg; delete mg; delete c; } void SLT_NR_MVC() { /*--------------------------------------------------------------------------* * Set up basic data for all variables * *--------------------------------------------------------------------------*/ TString sigFilename = "hhttbb.root"; TString bckFilename[] = {"ttbar_new.root", "Ztautau.root", "ZtautauB.root", "ZtautauC.root", "ZtautauL.root"}; TString NTupleName = "CxAODTuple_Nominal"; const int nBackgrounds = 5; TTree* sigTree = getTree(sigFilename, NTupleName); TTree *bckTree[nBackgrounds]; for ( int i=0 ; i