Unable to clone histogram

I can open the file and access the first histogram. The script then crashes when I try to clone it with the error message :

warning: null passed to a callee that requires a non-null argument [-Wnonnull]
          TH1D *F0 = (TH1D*)h->Clone("F0");

Here is a short snippet:

     for (int i=0; i<1; i++){

        File[i] = TFile::Open(Ratio_File_Names[i].c_str() ); 

        File[i]->Print();
    
      
	    for (Int_t r=0; r<8; r++){
	   
	     TH1D *h;
	       File[i]->GetObject(Ratio_Histogram_Names[r].c_str(), h);

	        if (h==0){

	         std::cout << Ratio_Histogram_Names[r].c_str() << std::endl;
	    }

	    // TH1D *F0 = new TH1D(*h);  //Create a clone of h for the correct binning
	     TH1D *F0 = (TH1D*)h->Clone("F0");
	     F0->Print();

      }
      }

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Try with:

if (h != 0) {
  // TH1D *F0 = new TH1D(*h); F0->SetName("F0");
  TH1D *F0 = ((TH1D*)(h->Clone("F0")));
  F0->Print();
 } else {
  std::cout << Ratio_Histogram_Names[r].c_str() << " NOT found!" << std::endl;
}

Hey,

So I implemented the change:

 for (int i=0; i<1; i++){

    File[i] = TFile::Open(Ratio_File_Names[i].c_str() ); 

     File[i]->Print();
    
      
	 for (Int_t r=0; r<8; r++){
	   
	  TH1D *h;
	  File[i]->GetObject(Ratio_Histogram_Names[r].c_str(), h);

	  if (h==0){

	    std::cout << Ratio_Histogram_Names[r].c_str() << std::endl;
	  } 
 } else {
  std::cout << Ratio_Histogram_Names[r].c_str() << "NOT found!" << std::endl;
}

And it says:

      TFile: name=Ratio_Plots.root, title=, option=READ
       R_29099_N0A2byN2A0
      R_29099_N0A2byN2A0NOT found!
      R_20099_N0A2byN2A0
      R_20099_N0A2byN2A0NOT found!
      R_28099_N0A2byN2A0
      R_28099_N0A2byN2A0NOT found!
      R_26099_N0A2byN2A0
      R_26099_N0A2byN2A0NOT found!
      R_29099_N2A4byN4A0
      R_29099_N2A4byN4A0NOT found!
      R_20099_N2A4byN4A0
      R_20099_N2A4byN4A0NOT found!
      R_28099_N2A4byN4A0
      R_28099_N2A4byN4A0NOT found!
      R_26099_N0A2byN2A0
      R_26099_N0A2byN2A0NOT found!

It’s suggesting that it can’t find the histograms in the root file which is odd. I’ve double checked the root file and I can see the histograms in there, and its located in the same directory as the script.

You edited it wrong. See my previous post again.

Also, post here the output of: File[i]->ls();

BTW. When you post “source code” or “output” here, do remember to enclose them into two lines which contain just three characters ``` (see how your posts have been edited above).

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