Script issue / order mistake


ROOT Version: 6.16
Platform: Fedora
Compiler: Emacs


I need to create one final histogram as a sum of many histograms. Any ideas about why I have errors in this code?

void scsum()
{
 TCanvas *time = new TCanvas("c1", "overlap", 0, 0, 800, 600);
 //TH1F sum("sum","Sum",21620,0,21619);

 auto sum = new TH1F("sum","Sum",21620,0,2162);
 
   const int firstNumber = 10153;
   const int NFiles = 3;

  for (int fileNumber = firstNumber; fileNumber < firstNumber+NFiles; fileNumber++)
   {
       const char* filename = Form("hists%i_blinded.root", fileNumber);

       TFile* myFile = TFile::Open(filename);
       if (!myFile)
       {
           printf("Can not find a file named \"%s\"!\n", filename);
           continue;
       }

       const char* histoname = Form("sc%i", fileNumber);
       TH1* h1 = (TH1*)myFile->Get(histoname);
   {
   Int_t bin[21621];
         for (int bin=1; bin <= "sc%i"->GetNbinsX(); bin++)
       {sum->SetBinContent(bin, sum->GetXaxis()->GetBinContent(bin) + " sc%i" ->GetXaxis()->GetBinContent(bin));
       }        
     
         if (!h1)
       {
           printf("Can not find a histogram named \"%s\" in the file named \"%s\"!\n", histoname, filename);
           continue;
   }
     sum->SetBinContent(bin);
       	}
   }
       h1->SetDirectory(gROOT);
       h1->Draw("sum");
       myFile->Close();
}
   }

Hi,
what is the error?

One way to debug C++ macros is to add a main function int main() { scsum(); return 0; } at the end of the file, then compile with debug symbols (g++ -g -o macro macro.C) and run it through gdb (gdb ./main).

Cheers,
Enrico

So I have a directory, and it contains 3 hists files: hists10153_blinded.root … hists10155_blinded.root. I need to sum them all up and create 1 final histogram (Y-axis will grow up, but X-axis stays constant). I have the code above, but it does not work. I have many errors like “use of undeclared identifier ‘h1’” etc.

Hi,
those are compilation errors, they are not related to ROOT: your C++ contains some mistakes.

use of undeclared identifier h1, for example, means that you are using variable h1 in a scope where it’s not defined.

Cheers,
Enrico

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.