Beispiel #1
0
// input: - Input file (result from TMVA)
//        - use of TMVA plotting TStyle
void mvas( TString fin = "TMVA.root", HistType htype = MVAType, Bool_t useTMVAStyle = kTRUE )
{
   // set style and remove existing canvas'
   TMVAGlob::Initialize( useTMVAStyle );

   // switches
   const Bool_t Save_Images     = kTRUE;

   // checks if file with name "fin" is already open, and if not opens one
   TFile* file = TMVAGlob::OpenFile( fin );  

   // define Canvas layout here!
   Int_t xPad = 1; // no of plots in x
   Int_t yPad = 1; // no of plots in y
   Int_t noPad = xPad * yPad ; 
   const Int_t width = 600;   // size of canvas

   // this defines how many canvases we need
   TCanvas *c = 0;

   // counter variables
   Int_t countCanvas = 0;

   // search for the right histograms in full list of keys
   TIter next(file->GetListOfKeys());
   TKey *key(0);   
   while ((key = (TKey*)next())) {

      if (!TString(key->GetName()).BeginsWith("Method_")) continue;
      if( ! gROOT->GetClass(key->GetClassName())->InheritsFrom("TDirectory") ) continue;

      TString methodName;
      TMVAGlob::GetMethodName(methodName,key);

      TDirectory* mDir = (TDirectory*)key->ReadObj();

      TIter keyIt(mDir->GetListOfKeys());
      TKey *titkey;
      while ((titkey = (TKey*)keyIt())) {
         if (!gROOT->GetClass(titkey->GetClassName())->InheritsFrom("TDirectory")) continue;

         TDirectory *titDir = (TDirectory *)titkey->ReadObj();
         TString methodTitle;
         TMVAGlob::GetMethodTitle(methodTitle,titDir);

         cout << "--- Found directory for method: " << methodName << "::" << methodTitle << flush;
         TString hname = "MVA_" + methodTitle;
         if      (htype == ProbaType  ) hname += "_Proba";
         else if (htype == RarityType ) hname += "_Rarity";
         TH1* sig = dynamic_cast<TH1*>(titDir->Get( hname + "_S" ));
         TH1* bgd = dynamic_cast<TH1*>(titDir->Get( hname + "_B" ));

         if (sig==0 || bgd==0) {
            if     (htype == MVAType)     
               cout << "mva distribution not available (this is normal for Cut classifier)" << endl;
            else if(htype == ProbaType)   
               cout << "probability distribution not available (this is normal for Cut classifier)" << endl;
            else if(htype == RarityType)  
               cout << "rarity distribution not available (this is normal for Cut classifier)" << endl;
            else if(htype == CompareType) 
               cout << "overtraining check not available (this is normal for Cut classifier)" << endl;
            else cout << endl;
         } 
         else {
            cout << endl;
            // chop off useless stuff
            sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) );
            if      (htype == ProbaType) 
               sig->SetTitle( Form("TMVA probability for classifier: %s", methodTitle.Data()) );
            else if (htype == RarityType) 
               sig->SetTitle( Form("TMVA Rarity for classifier: %s", methodTitle.Data()) );
            else if (htype == CompareType) 
               sig->SetTitle( Form("TMVA overtraining check for classifier: %s", methodTitle.Data()) );
         
            // create new canvas
            TString ctitle = ((htype == MVAType) ? 
                              Form("TMVA response %s",methodTitle.Data()) : 
                              (htype == ProbaType) ? 
                              Form("TMVA probability %s",methodTitle.Data()) :
                              (htype == CompareType) ? 
                              Form("TMVA comparison %s",methodTitle.Data()) :
                              Form("TMVA Rarity %s",methodTitle.Data()));
         
            TString cname = ((htype == MVAType) ? 
                             Form("output_%s",methodTitle.Data()) : 
                             (htype == ProbaType) ? 
                             Form("probability_%s",methodTitle.Data()) :
                             (htype == CompareType) ? 
                             Form("comparison_%s",methodTitle.Data()) :
                             Form("rarity_%s",methodTitle.Data()));

            c = new TCanvas( Form("canvas%d", countCanvas+1), ctitle, 
                             countCanvas*50+200, countCanvas*20, width, (Int_t)width*0.78 ); 
    
            // set the histogram style
            TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
   
            // normalise both signal and background
            TMVAGlob::NormalizeHists( sig, bgd );
   
            // frame limits (choose judicuous x range)
            Float_t nrms = 4;
            cout << "--- Mean and RMS (S): " << sig->GetMean() << ", " << sig->GetRMS() << endl;
            cout << "--- Mean and RMS (B): " << bgd->GetMean() << ", " << bgd->GetRMS() << endl;
            Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(), 
                                                  bgd->GetMean() - nrms*bgd->GetRMS() ),
                                       sig->GetXaxis()->GetXmin() );
            Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(), 
                                                  bgd->GetMean() + nrms*bgd->GetRMS() ),
                                       sig->GetXaxis()->GetXmax() );
            Float_t ymin = 0;
            Float_t maxMult = (htype == CompareType) ? 1.3 : 1.2;
            Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult;
   
            // build a frame
            Int_t nb = 500;
            TString hFrameName(TString("frame") + methodTitle);
            TObject *o = gROOT->FindObject(hFrameName);
            if(o) delete o;
            TH2F* frame = new TH2F( hFrameName, sig->GetTitle(), 
                                    nb, xmin, xmax, nb, ymin, ymax );
            frame->GetXaxis()->SetTitle( methodTitle + ((htype == MVAType || htype == CompareType) ? " response" : "") );
            if      (htype == ProbaType  ) frame->GetXaxis()->SetTitle( "Signal probability" );
            else if (htype == RarityType ) frame->GetXaxis()->SetTitle( "Signal rarity" );
            frame->GetYaxis()->SetTitle("Normalized");
            TMVAGlob::SetFrameStyle( frame );
   
            // eventually: draw the frame
            frame->Draw();  
    
            c->GetPad(0)->SetLeftMargin( 0.105 );
            frame->GetYaxis()->SetTitleOffset( 1.2 );

            // Draw legend               
            TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12, 
                                          c->GetLeftMargin() + (htype == CompareType ? 0.40 : 0.3), 1 - c->GetTopMargin() );
            legend->SetFillStyle( 1 );
            legend->AddEntry(sig,TString("Signal")     + ((htype == CompareType) ? " (test sample)" : ""), "F");
            legend->AddEntry(bgd,TString("Background") + ((htype == CompareType) ? " (test sample)" : ""), "F");
            legend->SetBorderSize(1);
            legend->SetMargin( (htype == CompareType ? 0.2 : 0.3) );
            legend->Draw("same");

            // overlay signal and background histograms
            sig->Draw("samehist");
            bgd->Draw("samehist");
   
            if (htype == CompareType) {
               // if overtraining check, load additional histograms
               TH1* sigOv = 0;
               TH1* bgdOv = 0;

               TString ovname = hname += "_Train";
               sigOv = dynamic_cast<TH1*>(titDir->Get( ovname + "_S" ));
               bgdOv = dynamic_cast<TH1*>(titDir->Get( ovname + "_B" ));
      
               if (sigOv == 0 || bgdOv == 0) {
                  cout << "+++ Problem in \"mvas.C\": overtraining check histograms do not exist" << endl;
               }
               else {
                  cout << "--- Found comparison histograms for overtraining check" << endl;

                  TLegend *legend2= new TLegend( 1 - c->GetRightMargin() - 0.42, 1 - c->GetTopMargin() - 0.12,
                                                 1 - c->GetRightMargin(), 1 - c->GetTopMargin() );
                  legend2->SetFillStyle( 1 );
                  legend2->SetBorderSize(1);
                  legend2->AddEntry(sigOv,"Signal (training sample)","P");
                  legend2->AddEntry(bgdOv,"Background (training sample)","P");
                  legend2->SetMargin( 0.1 );
                  legend2->Draw("same");
               }
               Int_t col = sig->GetLineColor();
               sigOv->SetMarkerColor( col );
               sigOv->SetMarkerSize( 0.7 );
               sigOv->SetMarkerStyle( 20 );
               sigOv->SetLineWidth( 1 );
               sigOv->SetLineColor( col );
               sigOv->Draw("e1same");
      
               col = bgd->GetLineColor();
               bgdOv->SetMarkerColor( col );
               bgdOv->SetMarkerSize( 0.7 );
               bgdOv->SetMarkerStyle( 20 );
               bgdOv->SetLineWidth( 1 );
               bgdOv->SetLineColor( col );
               bgdOv->Draw("e1same");

               ymax = TMath::Max( ymax, TMath::Max( sigOv->GetMaximum(), bgdOv->GetMaximum() )*maxMult );
               frame->GetYaxis()->SetLimits( 0, ymax );
      
               // for better visibility, plot thinner lines
               sig->SetLineWidth( 1 );
               bgd->SetLineWidth( 1 );

               // perform K-S test
               cout << "--- Perform Kolmogorov-Smirnov tests" << endl;
               Double_t kolS = sig->KolmogorovTest( sigOv );
               Double_t kolB = bgd->KolmogorovTest( bgdOv );
               cout << "--- Goodness of signal (background) consistency: " << kolS << " (" << kolB << ")" << endl;

               TString probatext = Form( "Kolmogorov-Smirnov test: signal (background) probability = %5.3g (%5.3g)", kolS, kolB );
               TText* tt = new TText( 0.12, 0.74, probatext );
               tt->SetNDC(); tt->SetTextSize( 0.032 ); tt->AppendPad(); 
            }

            // redraw axes
            frame->Draw("sameaxis");

            // text for overflows
            Int_t    nbin = sig->GetNbinsX();
            Double_t dxu  = sig->GetBinWidth(0);
            Double_t dxo  = sig->GetBinWidth(nbin+1);
            TString uoflow = Form( "U/O-flow (S,B): (%.1f, %.1f)%% / (%.1f, %.1f)%%", 
                                   sig->GetBinContent(0)*dxu*100, bgd->GetBinContent(0)*dxu*100,
                                   sig->GetBinContent(nbin+1)*dxo*100, bgd->GetBinContent(nbin+1)*dxo*100 );
            TText* t = new TText( 0.975, 0.115, uoflow );
            t->SetNDC();
            t->SetTextSize( 0.030 );
            t->SetTextAngle( 90 );
            t->AppendPad();    
   
            // update canvas
            c->Update();

            // save canvas to file

            TMVAGlob::plot_logo(1.058);
            if (Save_Images) {
               if      (htype == MVAType)     TMVAGlob::imgconv( c, Form("plots/mva_%s",     methodTitle.Data()) );
               else if (htype == ProbaType)   TMVAGlob::imgconv( c, Form("plots/proba_%s",   methodTitle.Data()) ); 
               else if (htype == CompareType) TMVAGlob::imgconv( c, Form("plots/overtrain_%s", methodTitle.Data()) ); 
               else                           TMVAGlob::imgconv( c, Form("plots/rarity_%s",  methodTitle.Data()) ); 
            }
            countCanvas++;
         }
      }
   }
}
Beispiel #2
0
//------------------------------------------------------------------------------
// DrawIt
//------------------------------------------------------------------------------
void DrawIt(TString filename,
	    TString hname,
	    TString cname,
	    TString title)
{
  TFile* inputfile = TFile::Open("../AuxiliaryFilesWZXS8TeV/" + filename + ".root");

  TH2F* h = (TH2F*)inputfile->Get(hname)->Clone(cname);

  h->SetDirectory(0);

  inputfile->Close();

  TString name = h->GetName();

  TCanvas* canvas = new TCanvas(name, name, 600, 600);

  if (name.Contains("PR")) canvas->SetLogx();
  if (name.Contains("SF")) canvas->SetLogx();

  canvas->SetLeftMargin (0.9 * canvas->GetLeftMargin());
  canvas->SetRightMargin(3.5 * canvas->GetRightMargin());
  canvas->SetTopMargin  (1.2 * canvas->GetTopMargin());

  TH2FAxisFonts(h, "x", "p_{T} [GeV]");
  TH2FAxisFonts(h, "y", "#eta");

  h->Draw("colz");

  h->SetTitle("");

  DrawTLatex(42, 0.940, 0.976, _bigLabelSize, 33, title);

  if (!title.Contains("trigger")) {

    Double_t hmin = h->GetMinimum();
    Double_t hmax = h->GetMaximum();

    for (Int_t i=1; i<=h->GetNbinsX(); i++) {
      for (Int_t j=1; j<=h->GetNbinsY(); j++) {

	Double_t value = h->GetBinContent(i,j);

	Double_t ypos = h->GetYaxis()->GetBinCenter(j);
	Double_t xpos = h->GetXaxis()->GetBinCenter(i);
      
	if (gPad->GetLogx()) xpos = h->GetXaxis()->GetBinCenterLog(i);

	TLatex* latex = new TLatex(xpos, ypos, Form("%.2f", value));

	latex->SetTextAlign(   22);
	latex->SetTextFont (   42);
	latex->SetTextSize (0.027);

	if (value < hmin + 0.3*(hmax - hmin)) latex->SetTextColor(kWhite);
	
	latex->Draw();
      }
    }
  }


  // Set the palette font
  //----------------------------------------------------------------------------
  canvas->Update();

  TPaletteAxis* palette = (TPaletteAxis*)h->GetListOfFunctions()->FindObject("palette");

  palette->SetLabelFont(42);


  // Save the plot
  //----------------------------------------------------------------------------
  canvas->Update();
  
  canvas->Modified();

  canvas->GetFrame()->DrawClone();

  canvas->SaveAs("pdf/scale_factors/" + name + ".pdf");
  canvas->SaveAs("png/scale_factors/" + name + ".png");
}
Beispiel #3
0
void allInOneLifetime(double lumi=4560., double maxInstLumi=5000.) {

  ExtraLimitPlots plots(lumi);
  plots.calculateCrossSections(4,6,3,39,9);
  
  // graphs - observed
  TGraph* g_obs      = plots.getObsLimit();
  TGraph* g_exp      = plots.getExpLimit();
  TGraphAsymmErrors* g_exp_1sig = plots.getExpLimit1Sig();
  TGraphAsymmErrors* g_exp_2sig = plots.getExpLimit2Sig();

  TGraph* g_obs_gluino = plots.getLimitGluino();
  double gluino2ref    = g_obs_gluino->GetY()[0] / g_obs->GetY()[0];
  TGraph* g_obs_stop   = plots.getLimitStop();
  double stop2ref      = g_obs_stop->GetY()[0] / g_obs->GetY()[0];
  TGraph* g_obs_stau   = plots.getLimitStau();
  double stau2ref      = g_obs_stau->GetY()[0] / g_obs->GetY()[0];

  cout << "scales: " << g_obs->GetY()[0] 
       << '/' <<g_obs_gluino->GetY()[0]
       << '/' <<g_obs_stop->GetY()[0]
       << '/' <<g_obs_stau->GetY()[0]
       <<endl;

  
  TCanvas *canvas = new TCanvas("allLifetime", "allLifetime", 1000, 600);
  
  canvas->SetLogx();
  canvas->SetLogy();

  canvas->SetRightMargin(0.8*canvas->GetLeftMargin());
  canvas->SetLeftMargin(1.2*canvas->GetLeftMargin());
  canvas->SetTicks (canvas->GetTickx(), 0);
  
  TH1F* h = new TH1F ("h", "", 1, 7.5e-8, 1e6);
  h->SetStats (0);
  h->SetMinimum (.0001);
  h->SetMaximum (0.99e1);
  // TH1* h = canvas->DrawFrame(7.5e-8, .001, 1e6, 1e2, "Y+");
  h->SetTitle("Beamgap Expt");
  //  h->GetXaxis()->SetTitle("#tau_{#tilde{g},#tilde{t},#tilde{#tau}} [s]");
  h->GetXaxis()->SetTitle("#tau [s]");
  h->GetYaxis()->SetTitle("#sigma #times BF #times #varepsilon_{stopping} #times #varepsilon_{reco}   [pb]  ");
  h->Draw ("Y+");

  ExtraAxis aGluino = anotherScale (h, gluino2ref, kRed+2, "#sigma(pp #rightarrow #tilde{g}#tilde{g}) #times BF(#tilde{g} #rightarrow g#tilde{#chi}^{0})   [pb]  ", 0.0);

  ExtraAxis aStop = anotherScale (h, stop2ref, kBlue+2, "#sigma(pp #rightarrow #tilde{t}#tilde{t}) #times BF(#tilde{t} #rightarrow t#tilde{#chi}^{0})   [pb]  ", 0.2);

  ExtraAxis aStau = anotherScale (h, stau2ref, kGreen+2, "#sigma(pp #rightarrow #tilde{#tau}#tilde{#tau}) #times BF(#tilde{#tau} #rightarrow #tau#tilde{#chi}^{0})   [pb]  ", 0.4);


  
  
  TPaveText* blurb = new TPaveText(0.25, 0.57, 0.50, 0.87, "NDC");

  blurb->AddText("CMS Preliminary 2015");
  // std::stringstream label;
  // label<<"#int L dt = "<<lumi<<" pb^{-1}";
  // blurb->AddText(label.str().c_str());
  // double peakInstLumi=maxInstLumi;
  // int exponent=30;
  // while (peakInstLumi>10) {
  //   peakInstLumi/=10;
  //   ++exponent;
  // }
  // std::stringstream label2;
  // label2<<"L^{max}_{inst} = "<<peakInstLumi<<" x 10^{"<<exponent<<"} cm^{-2}s^{-1}";
  // blurb->AddText(label2.str().c_str());

  //blurb->AddText("CMS 2011");
  blurb->AddText("#int L dt = 2.46 fb^{-1}");//,  #int L_{eff} dt = 935 pb^{-1}");
  //blurb->AddText("L^{max}_{inst} = 3.5 #times 10^{33} cm^{-2}s^{-1}");
  blurb->AddText("#sqrt{s} = 13 TeV");
  blurb->AddText("E_{g} > 120 GeV, E_{t} > 150 GeV");
  blurb->AddText("E_{jet} > 70 GeV");
  //blurb->AddText("m_{#tilde{g}} = 300 GeV/c^{2}");
  //blurb->AddText("m_{#tilde{#chi}^{0}} = 200 GeV/c^{2}");
  blurb->SetTextFont(42);
  blurb->SetBorderSize(0);
  blurb->SetFillColor(0);
  blurb->SetShadowColor(0);
  blurb->SetTextAlign(12);
  blurb->SetTextSize(0.033);
  blurb->Draw();
  
  
  // 2 sigma band
  if (g_exp_2sig) {
    g_exp_2sig->SetLineColor(0);
    g_exp_2sig->SetLineStyle(0);
    g_exp_2sig->SetLineWidth(0);
    g_exp_2sig->SetFillColor(kYellow);
    g_exp_2sig->SetFillStyle(1001);
    g_exp_2sig->Draw("3");
  }
  
  // 1 sigma band
  if (g_exp_1sig) {
    // g_exp_1sig->SetLineColor(8);
    g_exp_1sig->SetLineColor(0);
    g_exp_1sig->SetLineStyle(0);
    g_exp_1sig->SetLineWidth(0);
    // g_exp_1sig->SetFillColor(8);
    g_exp_1sig->SetFillColor(kGreen);
    g_exp_1sig->SetFillStyle(1001);
    // g_exp_1sig->SetFillStyle(3005);
    g_exp_1sig->Draw("3");
    // g_exp_1sig->Draw("lX");
  }
  
  
  // GLUINO LIMIT
  if (g_exp) {
    g_exp->SetLineColor(1);
    g_exp->SetLineStyle(4);
    g_exp->SetLineWidth(2);
    g_exp->Draw("l3");
  }
  
  TLine *l;
  l = new TLine(7.5e-8, 1.49/gluino2ref, 1e6, 1.49/gluino2ref); //600 GeV
  l->SetLineColor(kRed);
  l->SetLineWidth(2);
  l->Draw();
  
  TLatex *t1;
  t1 = new TLatex(0.1, 0.7/gluino2ref, "#sigma_{theory} (m_{#tilde{g}} = 800 GeV)");
  t1->SetTextColor(kRed);
  t1->SetTextFont(42);
  t1->SetTextSize(0.035);
  t1->Draw();

  // STOP LIMIT
  TLine *ltop = new TLine(7.5e-8, 0.028/stop2ref, 1e6, 0.028/stop2ref); //600 GeV
  ltop->SetLineColor(kBlue);
  ltop->SetLineWidth(2);
  ltop->Draw();
  
  TLatex *t1top;
  t1top = new TLatex(0.1, 0.015/stop2ref, "#sigma_{theory} (m_{#tilde{t}} = 800 GeV)");
  t1top->SetTextColor(kBlue);
  t1top->SetTextFont(42);
  t1top->SetTextSize(0.035);
  t1top->Draw();
  

  // observed limit
  if (g_obs) {
    g_obs->SetLineColor(1);
    g_obs->SetLineStyle(1);
    g_obs->SetLineWidth(2);
    g_obs->Draw("l");
  }
  
  

  TLegend* leg = new TLegend(0.67, 0.65, 0.82, 0.87,"95% CL Limits:","NDC");
  leg->SetTextSize(0.033);
  leg->SetBorderSize(0);
  leg->SetTextFont(42);
  leg->SetFillColor(0);
  TGraph* expectedStyle1 = new TGraph (*g_exp);
  expectedStyle1->SetFillColor (g_exp_1sig->GetFillColor());
  TGraph* expectedStyle2 = new TGraph (*g_exp);
  expectedStyle2->SetFillColor (g_exp_2sig->GetFillColor());
  cout << "colors: " << g_exp_1sig->GetFillColor() << ':' << g_exp_2sig->GetFillColor() << endl;
  leg->AddEntry(g_obs, "Observed", "l");
  leg->AddEntry(expectedStyle1, "Expected #pm1#sigma", "lf");
  leg->AddEntry(expectedStyle2, "Expected #pm2#sigma", "lf");
  //leg->AddEntry(g_obs_stop,"Obs.: Counting Exp. (#tilde{t})", "l");
  //leg->AddEntry(g_obs_nb, "Obs.: Counting Exp. (Neutral R-Baryon)", "l");
  //leg->AddEntry(g_obs_em, "Observed: Counting Exp. (EM only)", "l");
  //leg->AddEntry(g_obs_tp, "Observed: Timing Profile", "l");
  leg->Draw();

  h->Draw("sameaxis y+");
  aGluino.Draw();
  aStop.Draw();
  //aStau.Draw();

  canvas->Print("allInOneLifetime.png");
  canvas->Print("allInOneLifetime.pdf");
}
Beispiel #4
0
// input: - Input file (result from TMVA)
//        - use of TMVA plotting TStyle
void probas( TString fin = "TMVA.root", Bool_t useTMVAStyle = kTRUE )
{
    // set style and remove existing canvas'
    TMVAGlob::Initialize( useTMVAStyle );

    // switches
    const Bool_t Draw_CFANN_Logy = kFALSE;
    const Bool_t Save_Images     = kTRUE;

    // checks if file with name "fin" is already open, and if not opens one
    TFile* file = TMVAGlob::OpenFile( fin );

    // define Canvas layout here!
    Int_t xPad = 1; // no of plots in x
    Int_t yPad = 1; // no of plots in y
    Int_t noPad = xPad * yPad ;
    const Int_t width = 600;   // size of canvas

    // this defines how many canvases we need
    TCanvas *c = 0;

    // counter variables
    Int_t countCanvas = 0;

    // list of existing MVAs
    const Int_t nveto = 1;
    TString suffixSig = "_tr_S";
    TString suffixBgd = "_tr_B";

    // search for the right histograms in full list of keys
    TList methods;
    UInt_t nmethods = TMVAGlob::GetListOfMethods( methods );
    if (nmethods==0) {
        cout << "--- Probas.C: no methods found!" << endl;
        return;
    }
    TIter next(&methods);
    TKey *key, *hkey;
    char fname[200];
    TH1* sig(0);
    TH1* bgd(0);
    while ((key = (TKey*)next())) {
        TDirectory * mDir = (TDirectory*)key->ReadObj();
        TList titles;
        UInt_t ni = TMVAGlob::GetListOfTitles( mDir, titles );
        TString methodName;
        TMVAGlob::GetMethodName(methodName,key);
        if (ni==0) {
            cout << "+++ No titles found for classifier: " << methodName << endl;
            return;
        }
        TIter nextTitle(&titles);
        TKey *instkey;
        TDirectory *instDir;
        while ((instkey = (TKey *)nextTitle())) {
            instDir = (TDirectory *)instkey->ReadObj();
            TString instName = instkey->GetName();
            TList h1hists;
            UInt_t nhists = TMVAGlob::GetListOfKeys( h1hists, "TH1", instDir );
            if (nhists==0) cout << "*** No histograms found!" << endl;
            TIter nextInDir(&h1hists);
            TString methodTitle;
            TMVAGlob::GetMethodTitle(methodTitle,instDir);
            while (hkey = (TKey*)nextInDir()) {
                TH1 *th1 = (TH1*)hkey->ReadObj();
                TString hname= th1->GetName();
                if (hname.Contains( suffixSig ) && !hname.Contains( "Cut") &&
                        !hname.Contains("original") && !hname.Contains("smoothed")) {

                    // retrieve corresponding signal and background histograms
                    TString hnameS = hname;
                    TString hnameB = hname;
                    hnameB.ReplaceAll("_S","_B");

                    sig = (TH1*)instDir->Get( hnameS );
                    bgd = (TH1*)instDir->Get( hnameB );

                    if (sig == 0 || bgd == 0) {
                        cout << "*** probas.C: big troubles in probas.... histogram: " << hname << " not found" << endl;
                        return;
                    }

                    TH1* sigF(0);
                    TH1* bkgF(0);
                    for (int i=0; i<= 5; i++) {
                        TString hspline = hnameS + Form("_smoothed_hist_from_spline%i",i);
                        sigF = (TH1*)instDir->Get( hspline );

                        if (sigF) {
                            bkgF = (TH1*)instDir->Get( hspline.ReplaceAll("_tr_S","_tr_B") );
                            break;
                        }
                    }
                    if ((sigF == NULL || bkgF == NULL) &&!hname.Contains("hist") ) {
                        cout << "*** probas.C: big troubles - did not found histogram " << hspline.Data() << " "
                             << sigF << " " << bkgF << endl;
                        return;
                    }
                    else  {
                        // remove the signal suffix

                        // check that exist
                        if (NULL != sigF && NULL != bkgF && NULL!=sig && NULL!=bgd) {

                            TString hname = sig->GetName();

                            // chop off useless stuff
                            sig->SetTitle( TString("TMVA output for classifier: ") + methodTitle );

                            // create new canvas
                            cout << "--- Book canvas no: " << countCanvas << endl;
                            char cn[20];
                            sprintf( cn, "canvas%d", countCanvas+1 );
                            c = new TCanvas( cn, Form("TMVA Output Fit Variables %s",methodTitle.Data()),
                                             countCanvas*50+200, countCanvas*20, width, width*0.78 );

                            // set the histogram style
                            TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
                            TMVAGlob::SetSignalAndBackgroundStyle( sigF, bkgF );

                            // frame limits (choose judicuous x range)
                            Float_t nrms = 4;
                            Float_t xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(),
                                                                  bgd->GetMean() - nrms*bgd->GetRMS() ),
                                                       sig->GetXaxis()->GetXmin() );
                            Float_t xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(),
                                                                  bgd->GetMean() + nrms*bgd->GetRMS() ),
                                                       sig->GetXaxis()->GetXmax() );
                            Float_t ymin = 0;
                            Float_t ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*1.5;

                            if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") ymin = 0.01;

                            // build a frame
                            Int_t nb = 500;
                            TH2F* frame = new TH2F( TString("frame") + sig->GetName() + "_proba", sig->GetTitle(),
                                                    nb, xmin, xmax, nb, ymin, ymax );
                            frame->GetXaxis()->SetTitle(methodTitle);
                            frame->GetYaxis()->SetTitle("Normalized");
                            TMVAGlob::SetFrameStyle( frame );

                            // eventually: draw the frame
                            frame->Draw();

                            if (Draw_CFANN_Logy && mvaName[imva] == "CFANN") c->SetLogy();

                            // overlay signal and background histograms
                            sig->SetMarkerColor( TMVAGlob::c_SignalLine );
                            sig->SetMarkerSize( 0.7 );
                            sig->SetMarkerStyle( 20 );
                            sig->SetLineWidth(1);

                            bgd->SetMarkerColor( TMVAGlob::c_BackgroundLine );
                            bgd->SetMarkerSize( 0.7 );
                            bgd->SetMarkerStyle( 24 );
                            bgd->SetLineWidth(1);

                            sig->Draw("samee");
                            bgd->Draw("samee");

                            sigF->SetFillStyle( 0 );
                            bkgF->SetFillStyle( 0 );
                            sigF->Draw("samehist");
                            bkgF->Draw("samehist");

                            // redraw axes
                            frame->Draw("sameaxis");

                            // Draw legend
                            TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.2,
                                                          c->GetLeftMargin() + 0.4, 1 - c->GetTopMargin() );
                            legend->AddEntry(sig,"Signal data","P");
                            legend->AddEntry(sigF,"Signal PDF","L");
                            legend->AddEntry(bgd,"Background data","P");
                            legend->AddEntry(bkgF,"Background PDF","L");
                            legend->Draw("same");
                            legend->SetBorderSize(1);
                            legend->SetMargin( 0.3 );

                            // save canvas to file
                            c->Update();
                            TMVAGlob::plot_logo();
                            sprintf( fname, "plots/mva_pdf_%s_c%i", methodTitle.Data(), countCanvas+1 );
                            if (Save_Images) TMVAGlob::imgconv( c, fname );
                            countCanvas++;
                        }
                    }
                }
            }
        }
    }
}
Beispiel #5
0
TCanvas* makeNiceCanvasByPixMargins(Int_t pixelPerBinX, Int_t pixelPerBinY, Int_t nbinx, Int_t nbiny, Int_t top, Int_t bottom, Int_t left, Int_t right) {
  
  Int_t rubaX = 4; //determinato sperimentalmente                                                                          
  Int_t rubaY = 28; //determinato sperimentalmente                                                                                                        

  TString name = generateRandomName();

  Int_t plotBaseDimX = pixelPerBinX*nbinx;
  Int_t plotBaseDimY = pixelPerBinY*nbiny;
  Int_t XX = (Int_t)(plotBaseDimX+left+right);
  Int_t YY = (Int_t)(plotBaseDimY+top+bottom);
  TCanvas* can = new TCanvas(name,name,XX+rubaX,YY+rubaY);
  can->SetTopMargin((1.*top)/(1.*YY));
  can->SetBottomMargin((1.*bottom)/(1.*YY));
  can->SetRightMargin(right/(1.*XX));
  can->SetLeftMargin(left/(1.*XX));
  can->SetBorderMode(0);
  std::cout << "Nice canvas " << XX << " * " << YY << " Margin: t " << can->GetTopMargin() << " b " << can->GetBottomMargin() << " l " << can->GetLeftMargin() << " r " << can->GetRightMargin() << std::endl;

  return can;

}
Beispiel #6
0
void DrawMLPoutputMovie( TFile* file, const TString& methodType, const TString& methodTitle )
{
   gROOT->SetBatch( 1 );

   // define Canvas layout here!
   const Int_t width = 600;   // size of canvas

   // this defines how many canvases we need
   TCanvas* c = 0;

   Float_t nrms = 4;
   Float_t xmin = -1.2;
   Float_t xmax = 1.2;
   Float_t ymin = 0;
   Float_t ymax = 0;
   Float_t maxMult = 6.0;
   Int_t   countCanvas = 0;
   Bool_t  first = kTRUE;
            
   TString     dirname  = methodType + "/" + methodTitle + "/" + "EpochMonitoring";
   TDirectory *epochDir = (TDirectory*)file->Get( dirname );
   if (!epochDir) {
      cout << "Big troubles: could not find directory \"" << dirname << "\"" << endl;
      exit(1);
   }

   // now read all evolution histograms
   TIter keyItTit(epochDir->GetListOfKeys());
   TKey *titkeyTit;
   while ((titkeyTit = (TKey*)keyItTit())) {
      
      if (!gROOT->GetClass(titkeyTit->GetClassName())->InheritsFrom("TH1F")) continue;
      TString name = titkeyTit->GetName();
      
      if (!name.BeginsWith("convergencetest___")) continue;
      if (!name.Contains("_train_"))              continue; // only for training so far
      if (name.EndsWith( "_B"))                   continue;
      
      // must be signal histogram
      if (!name.EndsWith( "_S")) {
         cout << "Big troubles with histogram: " << name << " -> should end with _S" << endl;
         exit(1);
      }
      
      // create canvas
      countCanvas++;
      TString ctitle = Form("TMVA response %s",methodTitle.Data());
      c = new TCanvas( Form("canvas%d", countCanvas), ctitle, 0, 0, width, (Int_t)width*0.78 ); 
      
      TH1F* sig = (TH1F*)titkeyTit->ReadObj();
      sig->SetTitle( Form("TMVA response for classifier: %s", methodTitle.Data()) );
      
      TString dataType = (name.Contains("_train_") ? "(training sample)" : "(test sample)");
      
      // find background
      TString nbn = sig->GetName(); nbn[nbn.Length()-1] = 'B';            
      TH1F* bgd = dynamic_cast<TH1F*>(epochDir->Get( nbn ));
      if (bgd == 0) {
         cout << "Big troubles with histogram: " << bgd << " -> cannot find!" << endl;
         exit(1);
      }
      
      cout << "sig = " << sig->GetName() << endl;
      cout << "bgd = " << bgd->GetName() << endl;
      
      // set the histogram style
      TMVAGlob::SetSignalAndBackgroundStyle( sig, bgd );
      
      // normalise both signal and background
      TMVAGlob::NormalizeHists( sig, bgd );
      
      // set only first time, then same for all plots
      if (first) {
         if (xmin == 0 && xmax == 0) {
            xmin = TMath::Max( TMath::Min(sig->GetMean() - nrms*sig->GetRMS(), 
                                          bgd->GetMean() - nrms*bgd->GetRMS() ),
                               sig->GetXaxis()->GetXmin() );
            xmax = TMath::Min( TMath::Max(sig->GetMean() + nrms*sig->GetRMS(), 
                                          bgd->GetMean() + nrms*bgd->GetRMS() ),
                               sig->GetXaxis()->GetXmax() );
         }
         ymin = 0;
         ymax = TMath::Max( sig->GetMaximum(), bgd->GetMaximum() )*maxMult;
         first = kFALSE;
      }
      
      // build a frame
      Int_t nb = 100;
      TString hFrameName(TString("frame") + methodTitle);
      TObject *o = gROOT->FindObject(hFrameName);
      if(o) delete o;
      TH2F* frame = new TH2F( hFrameName, sig->GetTitle(), 
                              nb, xmin, xmax, nb, ymin, ymax );
      frame->GetXaxis()->SetTitle( methodTitle + " response" );
      frame->GetYaxis()->SetTitle("(1/N) dN^{ }/^{ }dx");
      TMVAGlob::SetFrameStyle( frame );
      
      // find epoch number (4th token)
      TObjArray* tokens = name.Tokenize("_");
      TString es = ((TObjString*)tokens->At(4))->GetString();
      if (!es.IsFloat()) {
         cout << "Big troubles in epoch parsing: \"" << es << "\" is not float" << endl;
         exit(1);
      }
      Int_t epoch = es.Atoi();
      
      // eventually: draw the frame
      frame->Draw();  
      
      c->GetPad(0)->SetLeftMargin( 0.105 );
      frame->GetYaxis()->SetTitleOffset( 1.2 );
      
      // Draw legend               
      TLegend *legend= new TLegend( c->GetLeftMargin(), 1 - c->GetTopMargin() - 0.12, 
                                    c->GetLeftMargin() + 0.5, 1 - c->GetTopMargin() );
      legend->SetFillStyle( 1 );
      legend->AddEntry(sig,TString("Signal ")     + dataType, "F");
      legend->AddEntry(bgd,TString("Background ") + dataType, "F");
      legend->SetBorderSize(1);
      legend->SetMargin( 0.15 );
      legend->Draw("same");
      
      TText* t = new TText();            
      t->SetTextSize( 0.04 );
      t->SetTextColor( 1 );
      t->SetTextAlign( 31 );
      t->DrawTextNDC( 1 - c->GetRightMargin(), 1 - c->GetTopMargin() + 0.015, Form( "Epoch: %i", epoch) );
      
      // overlay signal and background histograms
      sig->Draw("samehist");
      bgd->Draw("samehist");
      
      // save to file
      TString dirname  = "movieplots";
      TString foutname = dirname + "/" + name;
      foutname.Resize( foutname.Length()-2 );
      foutname.ReplaceAll("convergencetest___","");
      foutname += ".gif";
      
      cout << "storing file: " << foutname << endl;
      
      c->Update();
      c->Print(foutname);            
   }
}