Drawing histograms on pads in canvas - only one plot shows

I have a simple function which takes two histograms, plots them on separate pads, and then the division of the two in the third pad. For some reason only the second pad is drawing.

     void Histogram::Superposition(  std::string Slice,  TFile *File1, TFile *File2, std::string Fit,  std::map <        TString, TH1*> &themap ){

      TString myString1 = Form( "dSigmadBDT0%s_data_-10.0", Slice.c_str() );
      TString myString2 = Form("dNdqT2_0%s_Truth_-10.0_5", Slice.c_str() );

     double PhiLow = -3.142;
     double PhiHigh = 3.142;

       c->cd(1);
       ///Obtain the TH1Ds from the root file    
       TH1D *h1;
       h1 = (TH1D*)File1->Get(myString1 );
       // h1->SetDirectory(0);
       if(h1 == nullptr){std::cout << "histogram not found" <<std::endl;}
       h1->SetTitle(Form("dSigmadqT2_%s_data_-10.0", Slice.c_str() ));
       h1->SetName(Form("dSigmadqT2_%s_data_-10.0", Slice.c_str() ));
       h1->Draw();


       c->cd(2);
       TH1D *h2;
       h2 = (TH1D*)File2->Get(myString2 );
      //h2->SetDirectory(0);
      if(h2 == nullptr){std::cout << "histogram not found" <<std::endl;}
     h2->SetTitle(myString2);
     h2->SetName(myString2);
     h2->Draw();

     c->cd(3);
    TH1D *h3;
     h3 = (TH1D*)h1->Clone();
    h3->SetTitle("Ratio");
    h3->SetName("Ratio");
    h3->Divide(h1,h2,1.,1.);
    h3->Draw();

        c->Draw();

  }

Please read tips for efficient and successful posting and posting code

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


Most probably, your “h1” doesn’t exist. Try with
if (h1) h1->Draw(); else std::cout << "no h1" << std::endl;

Hi,

They do exist. For the purposes of keeping the code short here, I removed all the error checks.

So, maybe the problem originates in the source code which you removed.

Have edited the code to include the error checks which i removed.

What do you get from:
h1->ls(); h1->Print("base");
and:
h2->ls(); h2->Print("base");

I do not see the definition of the canvas c

@couet It was ok in the original post (see the version “1” of the post) so there was just a small slip in the editing.

BTW, According to Extracting an object from a TFile, in regular code you should use TFile::GetObject(), so something like:

   TH1D *h1 = nullptr;
   file1->GetObject(myString1, h1);

Is better (safer) than h1 = (TH1D*)File1->Get(myString1);

@bellenot You do NOT need = nullptr" in any case.

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