Two files and one Legend

Hello,
I have two *.c macro file. I draw the first of them, and then draw second with command “Draw(“same”);”.
I would like to create one Legend. But when I put legend commands into second file, I get an error message about some command belong to first file.
So, where and how can I add the legend command?
Best wishes
serkan

We need a small running example showing your problem to help you further.

#include <fstream>
#include <vector>
// #include <iostream>

#include "TH1.h"

void gband1()
{

  vector<Float_t> dat;
  ifstream in;
  in.open("spec30_CAP_60g_nf15m6gb10015010.ASC");
  
  //read file
  while( in.good() )
    {
      Double_t num;
      in >> num;
      // std::cout << "in.fail():" << in.fail() << "; num=" << num << std::endl;
      if( in.fail() )
	break;
      dat.push_back(num);
    }

  // fill histo
  Int_t numbins = dat.size();
  TH1F* h = new TH1F("h1",";E_{#gamma}[keV];Counts", 4000, 0, 2000 );
  
  for ( Int_t i=0; i != numbins; ++i )
    {
      h->SetBinContent( i+1, dat.at(i) );
      h->SetEntries( h->GetEntries()+i );
    }

  //draw histo
  h->Draw();
  TLegend *legend = new TLegend(0.22,0.27,0.52,0.37);
   legend->SetTextFont(72);
   legend->SetTextSize(0.04);
   legend->AddEntry(h," +6 neutrons (E_{n}=1-5 MeV)","lp");
   legend->SetBorderSize(0); 
   legend->Draw();
}

AND the Other is;

#include <fstream>
#include <vector>
// #include <iostream>

#include "TH1.h"

void gband2()
{

  vector<Float_t> dat;
  ifstream in;
  in.open("spec30_CAP_100b_gb10015010.ASC");
  
  //read file
  while( in.good() )
    {
      Double_t num;
      in >> num;
      // std::cout << "in.fail():" << in.fail() << "; num=" << num << std::endl;
      if( in.fail() )
	break;
      dat.push_back(num);
    }

  // fill histo
  Int_t numbins = dat.size();
  TH1F* ha = new TH1F("h1",";E_{#gamma}[keV];Counts", 4000, 0, 2000 );
  
  for ( Int_t i=0; i != numbins; ++i )
    {
      ha->SetBinContent( i+1, dat.at(i) );
      ha->SetEntries( ha->GetEntries()+i );
    }

  //draw histo
  ha->Draw("same");
   TLegend *legend = new TLegend(0.22,0.26,0.52,0.26);
   legend->SetTextFont(72);
   legend->SetTextSize(0.04);
 
   legend->AddEntry(ha," no neutrons","lp");
   legend->Draw("same");
}

Thank you

I cannot run your example because the data files are missing. So I have reduced your macros to something I can execute:

void a()
{
   hpx->Draw();
   TLegend *legend = new TLegend(0.22,0.27,0.52,0.37);
   legend->AddEntry(hpx," +6 neutrons (E_{n}=1-5 MeV)","lp");
   legend->SetBorderSize(0);
   legend->Draw();
} 
void b()
{
   hpx->DrawClone("C same");
   TLegend *legend = new TLegend(0.22,0.26,0.52,0.26);
   legend->AddEntry(hpx," no neutrons","lp");
   legend->SetTextFont(72);
    legend->SetTextSize(0.04);
        
   legend->Draw(" ");
} 

Then in ROOT i do:

.x a.C
.x b.C

and I get the two legends without error.