Re: Superimpose two or more histograms from different files

From: Otto Schaile <Otto.Schaile_at_Physik.uni-muenchen.de>
Date: Tue, 25 Oct 2005 09:48:34 +0200


Hi Addy,
one way: write your histograms to a ROOT file giving them different names (avoid spaces and C++ operators in names!, see mod macros below). You can of course have any number of hists on one root file. Then do:

TCanvas *c1 = new ...
TFile *hfile = new TFile("hists.root");
TH1F *h1 = hfile->Get("elpertrack_elec"); TH1F *h2 = hfile->Get("elpertrack_pion");

h1->Draw();
h1->SetLineColor(kRed);
h2->Draw("same");
h2->SetLineColor(kBlue);
c1->Update();



ntaddy_at_jlab.org wrote:
> Dear rooters,
> I am a begginner in root and will be grateful if anyone could
> show me how to superimpose two or more histograms from
> different C++ macros, example: tb_9GeV_elec.C and tb_9GeV_pion.C all in
> the same directory. I have been able to execute these macros without any
> error, and display the histograms on a TCanvas which all came out nicely
> but now my problem is how to superimpose the two named histograms: h10 in
> tb_9GeV_elec.C and another h10 in tb_9GeV_pion.C.
> However, below is my key parts of my macro. Thank you all in advance for
> your assistance.
> I am working with root 4.02/02 on Linux machine.
> Regards,
> Addy.
>
> macro:tb_9GeV_elec.C, looks this
> #include tb_elec2.C
> #include "tb_9GeV_elec.h"
> #include <TH2.h>
> #include <TStyle.h>
> #include <TCanvas.h>
> void tb_9GeV_elec()
> {
> gStyle->SetCanvasBorderMode(0);
> gStyle->SetPadBorderMode(0);
> char name0[80],
> //Get Branches needed
> tree->SetBranchesAddress("trk_nTrtHits", trk_nTrthits")
> //Defining the Canvas
>
> TCanvas *c1 = new TCanvas("c1", "electron track selection @9GeV,
> 50,50,600,800);
> //Histograms
> //TH1F *h10 =new TH1F("HL electron per track", "nHL per electron
> //track",30,0.,30.);

TFile *hfile = new TFile("hists.root", "UPDATE"); TH1F *h10 =new TH1F("elpertrack_elec", "nHL per electron track",30,0.,30.);

> //Loop over hits
> for (int hit=0;hit<trk_nTrrtHits[track];hit++) {
> if (sADC_C2>650){
> h10->Fill(nhigh);
> }

hfile->Write();

> c1-cd();
> h10-Draw();
> h10->SetFillColor(7)
> h10->SetXaxis()->SetTitle("nHL hit per track with cut")
> c1->Update();
> }
>
> macro2 tb_9GeV_pion.C
> I used the same macro but pion beam energy.
> macro:tb_9GeV_elec.C, looks this
> #include tb_9GeV_pion.C
> #include "tb_9GeV_pion.h"
> #include <TH2.h>
> #include <TStyle.h>
> #include <TCanvas.h>
> void tb_9GeV_pion()
> {
> gStyle->SetCanvasBorderMode(0);
> gStyle->SetPadBorderMode(0);
> char name0[80],
> //Get Branches needed
> tree->SetBranchesAddress("trk_nTrtHits", trk_nTrthits")
> //Defining the Canvas
>
> TCanvas *c1 = new TCanvas("c1", "electron track selection @9GeV,
> 50,50,600,800);
> //Histograms
> TH1F *h10 =new TH1F("HL electron per track", "nHL per electron
> track",30,0.,30.);

TFile *hfile = new TFile("hists.root", "UPDATE"); TH1F *h10 =new TH1F("elpertrack_pion", "nHL per electron track",30,0.,30.);

> //Loop over hits
> for (int hit=0;hit<trk_nTrrtHits[track];hit++) {
> if (sADC_C2>650){
> h10->Fill(nhigh);
> }

hfile->Write();

> c1-cd();
> h10-Draw();
> h10->SetFillColor(7)
> h10->SetXaxis()->SetTitle("nHL hit per track with cut")
> c1->Update();
> }
>
>
>
>
>
>
>
>
>
Received on Tue Oct 25 2005 - 09:48:55 MEST

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:13 MET