Hello Rooters!!!
I am having two spectra in two different ascii files(each containing one). All I want to do is plot those on the same “plot”. I thought of two ways. Firstly convert ascii files in root files, each containg one histo and then open them as object so as to plot them together. For this I used
[list=]Create the first root file[/list]
[code]#include
#include
#include
include “TROOT.h”
include “TGraphErrors.h”
include “TStyle.h”
include “TMultiGraph.h”
include “TF1.h”
include “TLegend.h”
include “TPaveStats.h”
include “TArrow.h”
include “TLatex.h”
include “TPaveText.h”
include “TText.h”
include “TPavesText.h”
#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”
void basic1() {
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic1.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%s100kOhm.mca",dir.Data()));//Reads 100kOhm.mca ascii file
Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“100kOhm.root”,“RECREATE”);
TH1F *h2 = new TH1F(“h2”,“100kOhm”, 1024, 1, 1024);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);
while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h2->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);
in.close();
f->Write();
gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);
TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(kYellow);
c->SetFrameFillColor(10);
h2->Draw();
h2->SetFillColor(kRed);
c->SetLogy(1);
c->SaveAs(“100kOhm.pdf”);
}
[/code]
[ul]Create the second root file[/ul]
[code]#include
#include
#include
include “TROOT.h”
include “TGraphErrors.h”
include “TStyle.h”
include “TMultiGraph.h”
include “TF1.h”
include “TLegend.h”
include “TPaveStats.h”
include “TArrow.h”
include “TLatex.h”
include “TPaveText.h”
include “TText.h”
include “TPavesText.h”
#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”
void basic2() {
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“basic2.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%s100Ohm.mca",dir.Data()));//Reads temp.mca ascii file
Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“100Ohm.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“100Ohm”, 1024, 1, 1024);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);
while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h1->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);
in.close();
f->Write();
gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);
TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(kYellow);
c->SetFrameFillColor(10);
h1->Draw();
h1->SetFillColor(kRed);
c->SetLogy(1);
c->SaveAs(“100Ohm.pdf”);
}
[/code]
[ul]Get those histos and plot them together[/ul]
[code]#include
#include
#include
include “TROOT.h”
include “TGraphErrors.h”
include “TStyle.h”
include “TMultiGraph.h”
include “TF1.h”
include “TLegend.h”
include “TPaveStats.h”
include “TArrow.h”
include “TLatex.h”
include “TPaveText.h”
include “TText.h”
include “TPavesText.h”
#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”
void noise(){
//Open root files and get desired trees
TFile *f2 = TFile::Open("100kOhm.root");
//f2->cd();
TH1 *Myh2;
f2->GetObject("h2", Myh2);
if (!Myh2) { std::cout << "Warning: 100kOhm Histo NOT found!" << std::endl; return; }
TFile *f1 = TFile::Open("100Ohm.root");
//f1->cd();
TH1 *Myh1;
f1->GetObject("h1", Myh1);
if (!Myh1) { std::cout << "Warning: 100Ohm Histo NOT found!" << std::endl; return; }
gROOT->SetStyle("Plain");//make the plot aesthetically better
gStyle->SetOptStat(NULL);//hide OptStat box-the one with number of entries
TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(kYellow);
c->SetFrameFillColor(10);
gStyle->SetHistFillColor(2);
Myh1->UseCurrentStyle();
Myh2->Draw(“Myh2”);
Myh1->Draw(“Myh1”,"",“same”);
Myh2->SetFillColor(kRed);
Myh2->SetFillStyle(3444);
Myh1->SetTitle("cluster charge");
Myh2->SetTitle("Point 2");
Myh1->SetAxisRange(0,4000,"X");
Myh1->SetAxisRange(0,1500,"Y");
Myh1->GetXaxis()->SetTitle("[ADC value]");
TLegend *legend1 = new TLegend(0.55,0.7,0.9,0.9);
legend1->AddEntry(Myh1,“Bottom Left Corner”,“f1”);
legend1->AddEntry(Myh2,“Top Left Corner”,“f2”);
legend1->Draw();
c->Modified();
c->SetLogy(1);
c->Update();
c->Print("noise.pdf");
}
[/code]
If I run the “converters” seperately both histos appear well.
When I run the “same plot” only the first histo in order, will appear. At that point I get an error on the command
An other thought I made was to do the previous proccess in the same macro. So I tried
[code]#include
#include
#include
include “TROOT.h”
include “TGraphErrors.h”
include “TStyle.h”
include “TMultiGraph.h”
include “TF1.h”
include “TLegend.h”
include “TPaveStats.h”
include “TArrow.h”
include “TLatex.h”
include “TPaveText.h”
include “TText.h”
include “TPavesText.h”
#include “TString.h”
#include “TSystem.h”
#include “TInterpreter.h”
#include “TFile.h”
#include “TH1.h”
#include “TNtuple.h”
#include “TCanvas.h”
void double_basic() {
// Read data from an ascii file and create a root file with an histogram and an ntuple.
// read file $ROOTSYS/tutorials/tree/basic.dat
// this file has 3 columns of float data
TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
dir.ReplaceAll(“double_basic.C”,"");
dir.ReplaceAll("/./","/");
ifstream in;
in.open(Form("%s100Ohm.mca",dir.Data()));//Reads temp.mca ascii file
Float_t x,y;
Int_t nlines = 0;
TFile *f = new TFile(“100Ohm.root”,“RECREATE”);
TH1F *h1 = new TH1F(“h1”,“100Ohm”, 1024, 1, 1024);
TNtuple *ntuple = new TNtuple(“ntuple”,“data from ascii file”,“x:y”);
while (1) {
in >> x >> y;
if (!in.good()) break;
if (nlines < 5) printf(“x=%8f, y=%8f/n”,x);
h1->Fill(x,y);
ntuple->Fill(x,y);
nlines++;
}
printf(" found %d points\n",nlines);
in.close();
f->Write();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Float_t x1,y1;
Int_t nlines1 = 0;
TFile *f1 = new TFile(“100kOhm.root”,“RECREATE”);
TH1F *h2 = new TH1F(“h2”,“100kOhm”, 1024, 1, 1024);
TNtuple *ntuple1 = new TNtuple(“ntuple”,“data from ascii file”,“x1:y1”);
while (1) {
in >> x1 >> y1;
if (!in.good()) break;
if (nlines1 < 5) printf(“x1=%8f, y1=%8f/n”,x1);
h2->Fill(x1,y1);
ntuple1->Fill(x1,y1);
nlines++;
}
printf(" found %d points\n",nlines1);
in.close();
f1->Write();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
gROOT->SetStyle(“Plain”);
gStyle->SetOptStat(1111);
gStyle->SetOptFit(1111);
TCanvas *c = new TCanvas(“c”, “c”);
c->SetFillColor(kYellow);
c->SetFrameFillColor(10);
gStyle->SetHistFillColor(2);
h1->UseCurrentStyle();
h1->Draw();
h2->Draw(“h2”,"",“same”);
h2->SetFillColor(kRed);
h2->SetFillStyle(3444);
h1->SetFillColor(kBlue);
h1->SetTitle(“cluster charge”);
h2->SetTitle(“Point 2”);
h1->SetAxisRange(0,4000,“X”);
h1->SetAxisRange(0,1500,“Y”);
h1->GetXaxis()->SetTitle("[ADC value]");
//h1->SetAxisRange(0,250,“X”);
//h2->SetFillStyle(3444);
//h1->Fit(“gaus”,“W”,NULL,350,800);
//h1->GetFunction(“gaus”)->SetLineColor(kRed);
//TPaveStats st = ((TPaveStats)(h1->GetListOfFunctions()->FindObject(“stats”)));
TLegend *legend1 = new TLegend(0.55,0.7,0.9,0.9);
legend1->AddEntry(h1,“Bottom Left Corner”,“f1”);
legend1->AddEntry(h2,“Top Left Corner”,“f2”);
legend1->Draw();
c->Modified();
c->SetLogy(1);
c->Update();
c->Print("double_basic.pdf");
}
[/code]
which gives me exactly the same output and the same error. How can this been done?
edit: Something I forgot to mention is the following: If I run tha “converter macros” both histos appear. As soon as I run the “plot same” macro only the first plot appears. If at that point I open a TBrowser, if I open the root file which contains the first histo and try to plot it, it works. At the moment I open the second root file, the histo which is contained there is empty!