Choose a region to project in a 2D histogram

I have written the following code, where it creates 2d histograms containing banana plots to identify different particles. The 2D histograms have the following format

In my code you can see that I define a TCutG and later I use ProjectionX() to take the histogram of each banana plot. The thing is that I have to do this for many 2d histograms. Is there a way to automate this procedure and project each group?

My 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();

TCutG *cutg = new TCutG(“cutg”,6);
cutg->SetVarX("");
cutg->SetVarY("");
cutg->SetTitle(“Graph”);
cutg->SetFillColor(1);
cutg->SetPoint(0,390,1100);
cutg->SetPoint(1,4030,750);
cutg->SetPoint(2,4040,30);
cutg->SetPoint(3,1650,160);
cutg->SetPoint(4,370,450);
cutg->SetPoint(5,390,1100);
cutg->Draw(“l”);

TCanvas *c3 = new TCanvas(“c3”, “c3”,225,219,700,530);
dee3->ProjectionX(" “,1,4096,”[cutg]")->Draw();
TString histfilename = “”;
//SingleExportAscii(histo_e1,histfilename);
SingleExportAscii(dee3->ProjectionX(" “,1,4096,”[cutg]"),histfilename);
//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]

Thank you very much in advance!

Hi,

It is up to you to automate this procedure and make an algorithm that defines your TCutG region looking at the histogram. You would need probably something like a cluster finding algorithm. We do not have such algorithm currently in ROOT, we have something similar in TMVA, but I am not sure it can be used in your case

Best Regards

Lorenzo