Create a chart consists of three files .spe format

Hi guys , I’m drawing up a laboratory essay about the energy loss of a source consists of Np , Am and Cm . What I want to do is take 3 different graphs created from 3 files .spe format and merge the three graphs in a single graph. I wrote this code , but it does not work . The code is the union of three separate codes . Individually, each code works and generates the graph from me I want . Kindly help me know ?

void perditaenergia() {


TCanvas *c = new TCanvas("c","Grafico Unificato");

const char *nomeFile="calibrazione_15V.Spe";
fstream file(nomeFile,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h1=new TH1I("h1","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h1->SetBinContent(n+1,counts);
  n++;
  }
h1->SetFillColor(28);
c->draw("h1");

const char *nomeFile2="mylar_2_mium.Spe");
fstream file(nomeFile2,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h2=new TH1I("h2","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h2->SetBinContent(n+1,counts);
  n++;
  }
h2->SetFillColor(28);
c->draw("h2");


const char *nomeFile3="mylar_10_mium.Spe");
fstream file(nomeFile3,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h3=new TH1I("h3","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h3->SetBinContent(n+1,counts);
  n++;
  }
h3->SetFillColor(28);
c->draw("h3");



}

Hi,

you can draw multiple graphs on the same canvas with the “Same” option, e.g.

[...]
h1->Draw();
h2->Draw("Same");
h3->Draw("Same");
[...]

Danilo

as output I get a completely blank canvas …


void perditaenergia() {


TCanvas *c = new TCanvas("c","Grafico Unificato");

const char *nomeFile="calibrazione_15V.Spe";
fstream file(nomeFile,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h1=new TH1I("h1","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h1->SetBinContent(n+1,counts);
  n++;
  }
h1->SetFillColor(28);
h1->draw();

const char *nomeFile2="mylar_2_mium.Spe");
fstream file(nomeFile2,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h2=new TH1I("h2","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h2->SetBinContent(n+1,counts);
  n++;
  }
h2->SetFillColor(28);
h2->draw("same");


const char *nomeFile3="mylar_10_mium.Spe");
fstream file(nomeFile3,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h3=new TH1I("h3","Spettro Mixed",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h3->SetBinContent(n+1,counts);
  n++;
  }
h3->SetFillColor(28);
h3->draw("same");



}


Hi,

I am surprised you did not get any error since the method “TH1F::draw” does not exist: the “d” should be capital “D”.

I don’t understand…

I am just pointing out that in your code you are invoking the “draw” method of TH1F instances while it should be “Draw”, with an uppercase “D”.
so

TH1F myhisto("test","test",64,-4,4);
myhisto.Draw(); // correct
myhisto.draw(); // not correct, it does not compile as the method does not exist

I think this has to be changed before anything.

Well , well, I corrected d with D , I always receive as output a blank canvas and appears in the terminal :

Warning in <TROOT::Append>: Replacing existing TH1: h1 (Potential memory leak).
Error: String literal syntax error perditaenergia.C:19:

 *** Break *** segmentation violation
 Generating stack trace...
 0xb71f55c1 in <unknown> from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb7702c14 in <unknown function>
 0xb6cdc8b0 in fsetpos + 0x30 from /lib/i386-linux-gnu/libc.so.6
 0xb6723a87 in G__interpret_func + 0x2de7 from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb681e202 in G__getfunction + 0x1d82 from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb682f07f in G__getitem + 0x7ff from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb6835219 in G__getexpr + 0x5699 from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb683ef29 in G__calc_internal + 0x3c9 from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb66f2ba5 in G__process_cmd + 0x4225 from /usr/lib/i386-linux-gnu/libCint.so.5.34
 0xb718e2eb in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0x47b from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb718cebf in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0x7f from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb715d1b5 in TApplication::ExecuteFile(char const*, int*, bool) + 0x955 from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb715d5d2 in TApplication::ProcessFile(char const*, int*, bool) + 0x22 from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb715e463 in TApplication::ProcessLine(char const*, bool, int*) + 0x9e3 from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb6fe1325 in TRint::HandleTermInput() + 0x445 from /usr/lib/i386-linux-gnu/libRint.so.5.34
 0xb6fe097e in TTermInputHandler::Notify() + 0x1e from /usr/lib/i386-linux-gnu/libRint.so.5.34
 0xb6fe2ce0 in TTermInputHandler::ReadNotify() + 0x10 from /usr/lib/i386-linux-gnu/libRint.so.5.34
 0xb71cc775 in TUnixSystem::CheckDescriptors() + 0x1b5 from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb71cd52c in TUnixSystem::DispatchOneEvent(bool) + 0xec from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb7133aa8 in TSystem::InnerLoop() + 0x58 from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb7131d3d in TSystem::Run() + 0x8d from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb715b42e in TApplication::Run(bool) + 0x2e from /usr/lib/i386-linux-gnu/libCore.so.5.34
 0xb6fe2659 in TRint::Run(bool) + 0x649 from /usr/lib/i386-linux-gnu/libRint.so.5.34
 0x08048cd9 in main + 0x59 from /usr/bin/root.exe
 0xb6c96637 in __libc_start_main + 0xf7 from /lib/i386-linux-gnu/libc.so.6
 0x08048d2e in <unknown> from /usr/bin/root.exe
Root > Function perditaenergia() busy flag cleared
*** Interpreter error recovered ***

Sorry this is an issue which is not related to ROOT.
This is how one draws three histograms on the same canvas:

TH1F h1("h1","Histo 1",64,-4,4);
h1.FillRandom("gaus");
h1.SetLineColor(kRed);
TH1F h2("h2","Histo 2",64,-4,4);
h2.FillRandom("gaus");
h2.SetLineColor(kGreen);
TH1F h3("h3","Histo 3",64,-4,4);
h3.FillRandom("gaus");
h3.SetLineColor(kBlue);
TCanvas c;
h1.Draw();
h2.Draw("same");
h3.Draw("same");

I corrected the code , now work!!!


void perditaenergia() {


TCanvas *c = new TCanvas("c","Grafico Unificato");

const char *nomeFile=("calibrazione_15V.Spe");
fstream file(nomeFile,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h1=new TH1I("h1","Grafico Unificato",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h1->SetBinContent(n+1,counts);
  n++;
  }
h1->SetFillColor(10);
h1->Draw();

const char *nomeFile2=("mylar_2_mium.Spe");
fstream file(nomeFile2,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h2=new TH1I("h2","Grafico Unificato",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h2->SetBinContent(n+1,counts);
  n++;
  }
h2->SetFillColor(28);
h2->Draw("same");


const char *nomeFile3=("mylar_10_mium.Spe");
fstream file(nomeFile3,ios::in);
string dummy;
for(Int_t i=0;i<12;i++)getline(file,dummy);
TH1I *h3=new TH1I("h3","Grafico Unificato",2048,0,2048);
Int_t n=0;
Int_t counts;
while((file>>counts)&& n<2048){
h3->SetBinContent(n+1,counts);
  n++;
  }
h3->SetFillColor(38);
h3->Draw("same");


}


Thank you so much!!! :smiley: