Multiple histograms on one canvas

Hi all,

I’m trying to plot multiple histograms on one canvas and plot a separate histogram on a second canvas and am finding it hard to find anything on where to start. If anyone has any ideas they would be hugely appreciated.

Here is my code:

void test7(UInt_t NrOfEvents = 1000000) {
if (!gROOT->GetClass(“TGenPhaseSpace”)) gSystem->Load(“libPhysics”);

TLorentzVector target(0.0, 0.0, 0.0, 1.879);
TLorentzVector beam(0.0, 0.0, 2.2, 2.2);
TLorentzVector W = beam + target;
// cout << “W=”<< W << endl;

//(Momentum, Energy units are Gev/C, GeV)
Double_t masses[4] = {0.938, 0.493, 0.493, 0.938} ;

   int c=0;

TRandom3 p(-1);
for (UInt_t iEvent = 0; iEvent < NrOfEvents; ++iEvent) {
Double_t num = p.Uniform(0,360);
if (num > 0.05){
c=c+1;
}
}
cout << “c=”<< c << endl;

double E2 = 4.079;
double E2 = 16.638241;
double C = 299792458;
double C2 = 89875518000000000;

//int detectn = 10000; //detect n will eventually be an output based on detector efficiency and angle p
TGenPhaseSpace event;
event.SetDecay(W, 4, masses);

TH2F *h2 = new TH2F(“h2”,“h2”, 1000,0,10, 1000,0,10);
TH1F *h1 = new TH1F(“h1”,“h1”, 1000,0,10);
TH1F *h3 = new TH1F(“h3”,“h3”, 1000,0,10);
TH1F *h4 = new TH1F(“h4”,“h4”, 1000,0,10);
TH1F *h5 = new TH1F(“h5”,“h5”, 1000,0,10);
TH1F *h6 = new TH1F(“h6”,“h6”,1000,0,10);

for (Int_t n=0; n<c; n++) { //instead of being 100000 events this will be 100000 runs where each event is ‘tested’ to see if it is detected
Double_t weight = event.Generate();

  TLorentzVector *pNeutron = event.GetDecay(0);
  TLorentzVector *pKp    = event.GetDecay(1);
  TLorentzVector *pKm    = event.GetDecay(2);
  TLorentzVector *pProton = event.GetDecay(3);

  TLorentzVector pNKp = *pNeutron + *pKp;
  TLorentzVector pKmP = *pKm + *pProton;
  TLorentzVector pKms = *pKm;
  TLorentzVector pNs = *pNeutron;
  TLorentzVector pPs = *pProton;
  TLorentzVector pKps = *pKp;
  TLorentzVector mKms = ( E2- *pKm * C2 )/ (C2 * C2) ;  //use E^2 eqn here to find the mass
//next I'll be using th evectors to find the relevant angles 

  h2->Fill(pNKp.M2() ,pKmP.M2() ,weight); //histogram of momentum for the two pairs of particles
  h1->Fill(pKms.M2()); //1D histogram of individual particle momentum
  h3->Fill(pNs.M2());
  h4->Fill(pPs.M2());
  h5->Fill(pKps.M2());
  h6->Fill(pKps.M2());

}
h2->Draw();
h1->Draw();
h3->Draw();
h4->Draw();
h5->Draw();
h6->Draw();

}

Any help on where to start would be really appreciated!

Thanks in advance!

[code]
TCanvas *c1 = new TCanvas(“c1”,“c1”);
TCanvas *c2 = new TCanvas(“c2”,“c2”);
TH1D *h1 = new TH1D(“h1”,“h1”,500,-5,5);
h1->FillRandom(“gaus”);

TH1D *h2 = new TH1D(“h2”,“h2”,500,-5,5);
h2->FillRandom(“gaus”);
c1->cd();
h1->Draw();
c2->cd();
h2->Draw();[/code]

For drawing multiple histograms in one pad, see the THStack class.

You can also see the “same” option of the Draw method of TH1 classes

[quote=“pamputt”][code]
TCanvas *c1 = new TCanvas(“c1”,“c1”);
TCanvas *c2 = new TCanvas(“c2”,“c2”);
TH1D *h1 = new TH1D(“h1”,“h1”,500,-5,5);
h1->FillRandom(“gaus”);

TH1D *h2 = new TH1D(“h2”,“h2”,500,-5,5);
h2->FillRandom(“gaus”);
c1->cd();
h1->Draw();
c2->cd();
h2->Draw();[/code][/quote]

Thanks! I can get two canvasses now, but I still can’t seem to get multiple histograms to load on the same canvas :confused:


TCanvas *c1 = new TCanvas("c1","c1");

TH1D *h1 = new TH1D("h1","h1",500,-5,5);
h1->FillRandom("gaus");

TH1D *h2 = new TH1D("h2","h2",500,-5,5);
h2->FillRandom("gaus");

h1->Draw();
h2->Draw("SAME");