Draw a graphical cut and a 2D histogram on the same canvas

I am trying to draw a graphical cut and a 2D histogram on the same canvas. A sample code is the following

TCutG *cut120 = new TCutG("cut120",6); cut120->SetVarX(""); cut120->SetVarY(""); cut120->SetTitle("Graph120"); cut120->SetFillColor(1); cut120->SetPoint(0,310,1800); cut120->SetPoint(1,3880,840); cut120->SetPoint(2,3920,330); cut120->SetPoint(3,2050,600); cut120->SetPoint(4,320,1330); cut120->SetPoint(5,310,1800); cut120->Draw("l"); TCanvas *c1 = new TCanvas("c1", "c1",225,219,700,530); TH1D *proj_e1 = dee1->ProjectionX("proj_e1",1,4096,"[cut120]"); proj_e1->Draw(); TString histfilename120 = " ";//TString::Format("%s_120",file.Data()); SingleExportAscii(proj_e1,histfilename120); TCanvas *c11 = new TCanvas("c11", "c11",225,219,700,530); dee1->Draw("[cut120]"); dee1->Draw("COLZ");

This code works as is. However if I try to add more histograms with one cut each, I only get to draw the histograms.

I’ve also tried to put the Draw() command for the graphical cut next to the Draw() command for the histogram the following but it doesn’t seem to work either

[code]TCanvas *c1 = new TCanvas(“c1”, “c1”,225,219,700,530);
TCutG *cut120 = new TCutG(“cut120”,6);
cut120->SetVarX("");
cut120->SetVarY("");
cut120->SetTitle(“Graph120”);
cut120->SetFillColor(1);
cut120->SetPoint(0,310,1800);
cut120->SetPoint(1,3880,840);
cut120->SetPoint(2,3920,330);
cut120->SetPoint(3,2050,600);
cut120->SetPoint(4,320,1330);
cut120->SetPoint(5,310,1800);

TH1D *proj_e1 = dee1->ProjectionX(“proj_e1”,1,4096,"[cut120]");
proj_e1->Draw();
TString histfilename120 = " “;//TString::Format(”%s_120",file.Data());
SingleExportAscii(proj_e1,histfilename120);
TCanvas *c11 = new TCanvas(“c11”, “c11”,225,219,700,530);
//dee1->Draw("[cut120]");
cut120->Draw(“l”);
dee1->Draw(“COLZ”);
c11->Modify();
c11->Update();[/code]

Any idea how to draw the histogram as well as the graphical cut on the same canvas?

My full code is

[code]#include "Riostream.h"
void ntuple(char * file_c) {

//*****************************************************
//* First executes the script “evnt2dat”.
//* The script accepts as an input a .evnt file
//* and creates a .dat file.
//* Then root creates the ntuple, histogram from each detector
//* and the DE-E scatter plot
//*
//* Execute it using
//* root -l ‘ntuple.C(“filename”)’
//* Note that the extension .evnt MUST NOT be used
//* and evnt2dat script must be on the same directory as
//* this macro.
//*****************************************************

TString file(file_c);
gSystem->Exec(TString::Format("./evnt2dat %s",file.Data()));//Executes the script evnt2dat
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“ntuple.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(TString::Format("%s%s_Processed.dat",dir.Data(),file.Data()));

gROOT->SetStyle(“Plain”);

Float_t de1,e1,de2,e2,de3,e3;
Int_t events = 0;
Int_t channels=4096;//Change it according to the acquisition channels
TFile *f = new TFile(TString::Format("%s.root",file.Data()),“RECREATE”);
//Create the histograms
TH1F *histo_de1 = new TH1F(“histo_de1”,"#Delta E_{1}",channels,1,channels);
TH1F *histo_e1 = new TH1F(“histo_e1”,“E_{1}”,channels,1,channels);
TH1F *histo_de2 = new TH1F(“histo_de2”,"#Delta E_{2}",channels,1,channels);
TH1F *histo_e2 = new TH1F(“histo_e2”,“E_{2}”,channels,1,channels);
TH1F *histo_de3 = new TH1F(“histo_de3”,"#Delta E_{3}",channels,1,channels);
TH1F *histo_e3 = new TH1F(“histo_e3”,“E_{3}”,channels,1,channels);
TH2F *dee1 = new TH2F(“dee1”, “#Delta E_{1} vs E_{1}”,channels,1,channels,channels,1,channels);
TH2F *dee2 = new TH2F(“dee2”, “#Delta E_{2} vs E_{2}”,channels,1,channels,channels,1,channels);
TH2F *dee3 = new TH2F(“dee3”, “#Delta E_{3} vs E_{3}”,channels,1,channels,channels,1,channels);

//Create the ntuple
TNtuple *ntuple = new TNtuple(“ntuple”,“DE/E analysis”,“de1:e1:de2:e2:de3:e3”);

//Fill the histos and the ntuple
while (1) {
in >> de1 >> e1 >> de2 >> e2 >> de3 >> e3;
if (!in.good()) break;
histo_de1->Fill(de1);
histo_e1->Fill(e1);
histo_de2->Fill(de2);
histo_e2->Fill(e2);
histo_de3->Fill(de3);
histo_e3->Fill(e3);
dee1->Fill(e1, de1);
dee2->Fill(e2, de2);
dee3->Fill(e3, de3);
ntuple->Fill(de1,e1,de2,e2,de3,e3);
events++;
}
printf(“Found %d events\n”,events);
//in.close();
//f->Write();
//TCanvas c1 = new TCanvas(“c1”, “c1”,225,219,700,530);
//c1->SetFillColor(5);
//c1->SetFrameFillColor(10);
//histo_de1->Rebin(4);
/**histo_de1->SetBins( histo_de1->GetNbinsX(), 0, 1024);
/
//--------------------------->histo_de1->Draw();
/**histo_de1->Rebin(4);*/
//c1->Mofified();
//c1->Update();

//TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
//c2->SetFillColor(5);
//c2->SetFrameFillColor(10);
//dee3->Draw(“COLZ”);
//c2->Modified();
//c2->Update();

TCanvas *c1 = new TCanvas(“c1”, “c1”,225,219,700,530);
TCutG *cut120 = new TCutG(“cut120”,6);
cut120->SetVarX("");
cut120->SetVarY("");
cut120->SetTitle(“Graph120”);
cut120->SetFillColor(1);
cut120->SetPoint(0,310,1800);
cut120->SetPoint(1,3880,840);
cut120->SetPoint(2,3920,330);
cut120->SetPoint(3,2050,600);
cut120->SetPoint(4,320,1330);
cut120->SetPoint(5,310,1800);

TH1D *proj_e1 = dee1->ProjectionX(“proj_e1”,1,4096,"[cut120]");
proj_e1->Draw();
TString histfilename120 = " “;//TString::Format(”%s_120",file.Data());
SingleExportAscii(proj_e1,histfilename120);
TCanvas *c11 = new TCanvas(“c11”, “c11”,225,219,700,530);
//dee1->Draw("[cut120]");
cut120->Draw(“l”);
dee1->Draw(“COLZ”);
c11->Modify();
c11->Update();

TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
TCutG *cut170 = new TCutG(“cut170”,6);
cut170->SetVarX("");
cut170->SetVarY("");
cut170->SetTitle(“Graph170”);
cut170->SetFillColor(1);
cut170->SetPoint(0,170,2230);
cut170->SetPoint(1,3940,830);
cut170->SetPoint(2,3830,150);
cut170->SetPoint(3,1400,400);
cut170->SetPoint(4,160,840);
cut170->SetPoint(5,170,2230);

TH1D *proj_e2 = dee2->ProjectionX(“proj_e2”,1,4096,"[cut170]");
proj_e2->Draw();
TString histfilename170 = " “;//TString::Format(”%s_170",file.Data());
SingleExportAscii(proj_e2,histfilename170);
TCanvas *c22 = new TCanvas(“c22”, “c22”,225,219,700,530);
//dee2->Draw("[cut170]");
cut170->Draw(“l”);
dee2->Draw(“COLZ”);
c22->Modify();
c22->Update();

TCanvas *c3 = new TCanvas(“c3”, “c3”,225,219,700,530);
TCutG *cut150 = new TCutG(“cut150”,6);
cut150->SetVarX("");
cut150->SetVarY("");
cut150->SetTitle(“Graph150”);
cut150->SetFillColor(1);
cut150->SetPoint(0,390,1100);
cut150->SetPoint(1,4030,750);
cut150->SetPoint(2,4040,30);
cut150->SetPoint(3,1650,160);
cut150->SetPoint(4,370,450);
cut150->SetPoint(5,390,1100);
TH1D *proj_e3 = dee3->ProjectionX(“proj_e3”,1,4096,"[cut150]");
proj_e3->Draw();
TString histfilename150 = " “;//TString::Format(”%s_150",file.Data());
SingleExportAscii(proj_e3,histfilename150);
TCanvas *c33 = new TCanvas(“c33”, “c33”,225,219,700,530);
//dee3->Draw("[cut150]");
cut150->Draw(“l”);
dee3->Draw(“COLZ”);
c33->Modify();
c33->Update();
in.close();
f->Write();

//myhist.Print(“all”); > myhist.dat
//c3->SaveAs(TString::Format("%s.pdf",file.Data()));
//TCanvas *c4 = new TCanvas(“c4”, “c4”,225,219,700,530);
//dee3->Draw("[cut3]");
//dee3->Draw(“COLZ”);

//TCanvas *c2 = new TCanvas(“c2”, “c2”,225,219,700,530);
//dee1->Draw(“COLZ”);

}

/**

  • \brief Export Single Histogram into ASCII file
    /
    Bool_t SingleExportAscii(TH1
    hist, TString &filename, TString folder="", TString separator="\t")
    {
    Int_t i,j;
    Double_t xcenter, xwidth;
    Bool_t success=kFALSE;
    filename = folder + hist->GetName() + “.dat”;
    ofstream file_out(filename);
    file_out << "# Output " << hist->ClassName() << “: " << hist->GetName() << " (” << hist->GetTitle() << “)\n”;
    if (hist->GetDimension()==1)
    {
    file_out << “# BinCenter” << separator << “Content” << separator << “BinHalfWidth” << separator << “Error\n”;
    for (i=1; i<=hist->GetNbinsX(); i++)
    file_out << hist->GetBinCenter(i) << separator << hist->GetBinContent(i) << separator << hist->GetBinWidth(i)/2 << separator << hist->GetBinError(i) << endl;
    if (i>1)
    success=kTRUE;
    }
    else if (hist->GetDimension()==2)
    {
    file_out << “# xBinCenter” << separator << “yBinCenter” << separator << “Content” << separator << “xBinHalfWidth” << separator << “yBinHalfWidth” << separator << “Error” << endl;
    for (i=1; i <= hist->GetNbinsX(); i++)
    {
    xcenter = hist->GetXaxis()->GetBinCenter(i);
    xwidth = hist->GetXaxis()->GetBinWidth(i)/2;
    for (j=1; j <= hist->GetNbinsY(); j++)
    file_out << xcenter << separator << hist->GetYaxis()->GetBinCenter(j) << separator << hist->GetBinContent(i,j) << separator << xwidth << separator << hist->GetYaxis()->GetBinWidth(j)/2 << separator << hist->GetBinError(i,j) << endl;
    if (j>1)
    file_out << endl; // produce a blank line after each set of Ybins for a certain Xbin. Gnuplot likes this.
    }
    if (i>1)
    success=kTRUE;
    }
    file_out.close();
    if (success == kTRUE)
    cout << "*** TRemiHistExport: Histogram " << hist->GetName() << " written to " << filename << endl;
    return success;
    }
    [/code]

dee1->Draw(“COLZ”);
cut120->Draw(“L”);

Thank you very much! It really worked!