So i am a total newb trying to over lay 2 histogram on top of one another, and below is my code. It may look a little clumsy:
#include "Riostream.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TH1.h"
#include "TGaxis.h"
void onedhisto() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// this file has 1 column of float data
TCanvas *c1 = new TCanvas("c1","MPV distribution",600,400);
// first histogram
ifstream in;
in.open("onedhisto.txt");
Float_t x;
Int_t nlines = 0;
TFile *f = new TFile("onedhist.root","RECREATE");
TH1F *h1 = new TH1F("h1","Multiplied Charges Distribution",93,20,50);
while (1) {
in >> x;
if (!in.good()) break;
if (nlines < 42) cout<<x<<endl;
h1->Fill(x);
nlines++;
}
printf(" found %d points\n",nlines);
h1->Draw();
TH1F *h2 = h1->Rebin(3,"Rebinned");
h2->SetLineColor(kRed);
h2->Draw(hist);
// c1->update();
in.close();
//second histogram
ifstream in2;
in2.open("onedhistoafter.txt");
Float_t x2;
Int_t nlines2 = 0;
// TFile *f = new TFile("onedhist.root","RECREATE");
TH1F *h3 = new TH1F("h3","Multiplied Charges Distribution",93,20,50);
while (1) {
in >> x2;
if (!in2.good()) break;
if (nlines2 < 47) cout<<x2<<endl;
h3->Fill(x2);
nlines2++;
}
c1->cd();
printf(" found %d points\n",nlines2);
h3->Draw();
TH1F *h4 = h3->Rebin(3,"Rebinned");
h4->SetLineColor(kBlue);
h4->Draw();
// c1->update();
in2.close();
}
it prints out the x2 values so i know that the date is read. But after i run it, i only see the first histogram in blue…what did i do wrong?
(the first one should be red and the 2nd one should be blue…)
Any help is appreciated.
Edited after correcting some error in code.