Adding histograms

Dear Rooters,

I recently posted a question about opening a large number of root-files containing histograms and after help from Rene and Axel this now works. However I have found a new problem with my macro.

The intension of opening all these files is to add the histograms together and investigate the variance between them. I loop over the 500 files with histograms, and the fist time I take a copy of the first histogram and then I add the other 499 histgrams using Add. I had this macro under windows and it produced outputs that I believe where correct. However I have now changed to Ubuntu and no things don’t work anymore (I still have the same root version 5.18 ). The final (total) histogram looks more like one randomly selected histogram of the 500 than the total of them. When I merge the histograms using hadd it looks good so I trust my input files to be ok, but hadd doesn’t give me the variance of the histograms, so I would really like my own macro to work!

I am most grateful for any help concerning this!
Regards,
Karin

void TEST(){
  //This is a test macro
  TH1::AddDirectory(kFALSE);
  
  //Declaring some global objects
  TH1F *h_PE;
  char fluence[1000];
  
  //Loop over 500 histograms to see if the avarage is correct
  for(int iR=1000; iR<1500;iR++){
    sprintf(fluence, "/media/FREECOM\ HDD/DATA/Emax8MeV/VRT/plots/spectra/histos/histos_Spectrum_Water_xCoord_0cm_yCoord_0cm_depth_10.05cm_Elekta6MV_%u.root", iR);
    
    TFile* fluenceFile = TFile::Open(fluence,"read"); 
    cout<<"Opened file: "<<fluence<<endl;
    
    if(iR==1000){
      h_PE = new TH1F(*h_primaryElectronFluenceSpectrum);  //the histogram in the root-file is called h_primaryElectronFluenceSpectrum.
      h_PE->SetName("h_PE");
    }
    else if(iR>1000){
      h_PE->Add(h_primaryElectronFluenceSpectrum);
    }

    delete fluenceFile;

  }//end loop over files
  
}//end macro

Hi Karin,

I see one bug: it’s “\” to get a backslash, or you will get an “escaped space” in C++, think of “\n” vs. “\n”. Other than that: could you compile your code? .x TEST.C+ (if the code’s filename is TEST.C) will do it. You will have to add the proper #includes, and you will have to say

TH1* h_primaryElectronFluenceSpectrum = 0; fluenceFile->GetObject("h_primaryElectronFluenceSpectrum", h_primaryElectronFluenceSpectrum);
to extract the histogram from the file; what you use (directly using the name in the TFile) is a CINT shortcut.

Does that work better?

Cheers, Axel.

Yes, it works much better, thank you!!!
/Karin