TH2F deformation when calling TTree::Draw method

ROOT Version (e.g. 6.12/02):
Platform, compiler (e.g. CentOS 7.3, gcc6.2):

Hello everyone, i am currently working on a program that requires to draw bi-dimensional histograms (TH2F) from E and E+DeltaE TH1F. I have written the code below, and unfortunately im getting some issues depending on slight changes…
I am a ROOT beginner, i start 1 week ago so please forgive my ignorance.


 for (int i = 1 ; i<= n ; i++)
    {
      stringstream ss ;
      double e  = lowestE+ (i-1)*dE ;
      ss << e ;
      string num = ss.str() ;
      string opename = nameg+num+unit+extension ;
      const char* namefile = opename.c_str() ;
      TFile *File = new TFile(namefile) ;
      TTree *tree = (TTree *)File->Get(nametree) ;
      tree->Draw(bidimaxes, "", "goff") ;
      TH2F *bidim1 = tree ->GetHistogram() ;
      //tree->Draw(bidimaxes,"","") ;
      bidim1->Draw() ;
      TFile out(namefileout, "update") ;
      string namebidim1 = nameg+num+unit+bidimhisto ;
      const char* namebidim = namebidim1.c_str() ;
      bidimlist.push_back(namebidim1) ;
      bidim1->SetName(namebidim) ;
      bidim1->Write(namebidim) ;
      out.Close();
    }

the result is

the TH2F histogram drawn is … squared.
What i was expecting to have was more (i will post it in the next message because i cant actually) :
(obtained when i modified my cod this way )

 for (int i = 1 ; i<= n ; i++)
    {
      stringstream ss ;
      double e  = lowestE+ (i-1)*dE ;
      ss << e ;
      string num = ss.str() ;
      string opename = nameg+num+unit+extension ;
      const char* namefile = opename.c_str() ;
      TFile *File = new TFile(namefile) ;
      TTree *tree = (TTree *)File->Get(nametree) ;
      tree->Draw(bidimaxes, "", ""); 
      TH2F *bidim1 = tree ->GetHistogram() ;
      //tree->Draw(bidimaxes,"","") ;
      // bidim1->Draw() ;
      TFile out(namefileout, "update") ;
      string namebidim1 = nameg+num+unit+bidimhisto ;
      const char* namebidim = namebidim1.c_str() ;
      bidimlist.push_back(namebidim1) ;
      bidim1->SetName(namebidim) ;
      bidim1->Write(namebidim) ;
      out.Close();
    }

just mentioning that if i remove // before bidim1->Draw() , i obtain an empty histogram…

thanks for your attention

Search for “Retrieving the result of Draw” and “How to obtain more info from TTree::Draw” in the TTree::Draw method description.

i have tried to do as presented ;

 for (int i = 1 ; i<= n ; i++)
    {
      stringstream ss ;
      double e  = lowestE+ (i-1)*dE ;
      ss << e ;
      string num = ss.str() ;
      string opename = nameg+num+unit+extension ;
      const char* namefile = opename.c_str() ;
      string namebidim1 = nameg+num+unit+bidimhisto ;
      const char* namebidim = namebidim1.c_str() ;
      string insert = ">>" ;
      string namedraw1 = bidimaxes+insert+namebidim;
      const char* namedraw = namedraw1.c_str() ;
      TFile *File = new TFile(namefile) ;
      TTree *tree = (TTree *)File->Get(nametree) ;
      tree->Draw(bidimaxes, "","goff"); // changer paramètres
      TGraph *graph = (TGraph*)gPad->GetPrimitive("Graph");
      // TH2F *htemp = (TH2F*)gPad->GetPrimitive("htemp") ;     

      //tree->Draw(bidimaxes,"","") ;
      //  htemp->Draw() ;
      //  TFile out(namefileout, "update") ;
      
      bidimlist.push_back(namebidim1) ;
      // bidim1->SetName(namebidim) ;
      // bidim1->Write(namebidim) ;
      // out.Close();
    }

the result is a break segmentation violation issue

and in other hand i tried to redirect the flux of histogram via ;

 for (int i = 1 ; i<= n ; i++)
    {
      stringstream ss ;
      double e  = lowestE+ (i-1)*dE ;
      ss << e ;
      string num = ss.str() ;
      string opename = nameg+num+unit+extension ;
      const char* namefile = opename.c_str() ;
      string namebidim1 = nameg+num+unit+bidimhisto ;
      const char* namebidim = namebidim1.c_str() ;
      string insert = ">>" ;
      string namedraw1 = bidimaxes+insert+namebidim;
      const char* namedraw = namedraw1.c_str() ;
      TFile *File = new TFile(namefile) ;
      TTree *tree = (TTree *)File->Get(nametree) ;
      tree->Draw("bSi2:bSi2+bSi1>>try(200,0,2,200,0,2)", "","goff");
      TH2F *bidim1 = new TH2F("title", "title",200,0,2,200,0,2);
      bidim1 =  try ;
      bidim1->Draw() ;
      //  TFile out(namefileout, "update") ;
         bidimlist.push_back(namebidim1) ;
      // bidim1->SetName(namebidim) ;
      // bidim1->Write(namebidim) ;
      // out.Close();
    }

And it is working, thanks !

Try:

tree->SetEstimate(-1);
tree->Draw(bidimaxes, "", "goff"); // bidimaxes = "e1:e2" (unbinned 2D scatter plot)
TGraph *graph = new TGraph(tree->GetSelectedRows(), tree->GetV2(), tree->GetV1());
graph->Draw("AP");

May be post a small reproducer we can run ?

Why do you need a “reproducer”?
To me, it’s clear that with “goff” nothing is drawn so “gPad” is null (hence the “segmentation violation”).

just to try … but yes you are right.

My problem was coming from the binning. When using the draw method, it produces a TGraph with automatic binning, so when i ask to save it as a TH2F it was rebin and looks like above. When i finally ask the draw method to put everything directly into a TH2F with a correct binning, everything get back to normal !

When you draw a 2D distribution in a 2D histogram with “goff” and draw the histogram, afterwards, the “scatter plot” you get is produced as explained here. That’s why you see these “boxes” … That’s a bit misleading and you better use the COL of BOX option which are less confusing, or plot a real scatter plot like the 2nd you showed.

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