Set your own value in hist box array

Dear experts,

I have a canvas in which I have several histogram, and I set a stat box array for each hist. Now I want to add a line in each stat box. Following: root.cern.ch/doc/master/statsEditing_8C.html
I wrote a piece of code in [1], and I posted the 177 line codes with the root file to be read. Without the section with trouble lines [149-157] the code run.
in TPaveStats ps[npaveStat] = (TPaveStats)canvas[i]->GetPrimitive(“stats”); as I have a stat box array, I do not see how to “->GetPrimitive” for each box stat array. Do you know how I can achieve this?

Regards

[1]

void test(){

vector<TH1D*> vh;
int nhist{27};
TPaveStats *paveStat[nhist];
int npaveStat{0};
gStyle->SetOptStat(“rm”);
TCanvas *canvas[ndir];


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

  canvas[i]->cd(1); 
      vh.back()->Draw("hist&sames"); legend[i]->Draw("sames"); gPad->Modified(); gPad->Update();

  paveStat[npaveStat]=(TPaveStats*)vh.back()->FindObject("stats");
  paveStat[npaveStat]->SetLineColor(vh.back()->GetLineColor());
  paveStat[npaveStat]->SetLineStyle(vh.back()->GetLineStyle());
  paveStat[npaveStat]->SetLineWidth(vh.back()->GetLineWidth());
  paveStat[npaveStat]->SetTextSize(0.025);
  paveStat[npaveStat]->SetY1NDC(0.89); 

  if(npaveStat!=0 && npaveStat!=9 && npaveStat!=18 ){ 
    double delta{paveStat[npaveStat-1]->GetY2NDC()-paveStat[npaveStat-1]->GetY1NDC()};
    paveStat[npaveStat]->SetY2NDC(paveStat[npaveStat-1]->GetY1NDC());
    paveStat[npaveStat]->SetY1NDC(paveStat[npaveStat-1]->GetY1NDC()-delta);
  }


      // trouble!!
  TPaveStats *ps[npaveStat] = (TPaveStats*)canvas[i]->GetPrimitive("stats"); //??
  ps[npaveStat]->SetName("mystats");
  // Add a new line in the stat box.
  TList *list[npaveStat]  = ps[npaveStat]->GetListOfLines();
  TLatex *myt[npaveStat]  = new TLatex(0,0,"Test = 10"); // "=" is a control character
  myt[npaveStat]  ->SetTextFont(42);
  myt[npaveStat]  ->SetTextSize(0.04);
  myt[npaveStat]  ->SetTextColor(kRed);
  list[npaveStat]   ->Add(myt);

      npaveStat++;

} // for

}
hist_SUSYGluGluToHToTauTauM300_all.root (593 KB)
plot_kOptimization.cc (7.28 KB)

Why do you try to create a new “ps” array if you already have “paveStat” which keeps all pointers?

Dear pepe,

thank you for your answer. I modify the section as fellow:

  list[npaveStat] = paveStat[npaveStat]->GetListOfLines();
  myt[npaveStat] = new TLatex(0,0,"Test = 10"); // "=" is a control character
  myt[npaveStat] ->SetTextFont(42);
  myt[npaveStat] ->SetTextSize(0.04);
  myt[npaveStat] ->SetTextColor(kRed);
  list[npaveStat]->Add(myt);

then I have the error message [1], do you know what is wrong?

Regards

[1]
#######################
/home/calpas/svfitMEM/CMSSW_8_0_0/src/TauAnalysis/SVfitPerformanceStudies/test/plot/plot_kOptimization.cc:157:21: error: no matching member function for
call to 'Add’
list[npaveStat]->Add(myt);
~~~~~~~~~~~~~~~^
/build/cmsbuild/auto-builds/CMSSW_8_0_0-slc6_amd64_gcc493/build/CMSSW_8_0_0-build/BUILD/slc6_amd64_gcc493/lcg/root/6.06.00-ikhhed4/build/include/TList.h:81:22: note:
candidate function not viable: no known conversion from ‘TLatex *[nhist]’ to ‘TObject *’ for 1st argument
virtual void Add(TObject *obj) { AddLast(obj); }
^
/build/cmsbuild/auto-builds/CMSSW_8_0_0-slc6_amd64_gcc493/build/CMSSW_8_0_0-build/BUILD/slc6_amd64_gcc493/lcg/root/6.06.00-ikhhed4/build/include/TList.h:82:22: note:
candidate function not viable: requires 2 arguments, but 1 was provided
virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
#######################

First, move the line “npaveStat++;” to the place AFTER you “Add” the new line (i.e. to right before “} else {”).
Then, I guess it should be:
list[npaveStat]->Add(myt[npaveStat]);

I did [1], the code run but it did not add the new line.
If I add vh.back()->SetStats(0); I do not have any stat box. Do you know what is missing?

Regards

[1]
list[npaveStat] = paveStat[npaveStat]->GetListOfLines();
myt[npaveStat] = new TLatex(0,0,“Test = 10”); // “=” is a control character
myt[npaveStat] ->SetTextFont(42);
myt[npaveStat] ->SetTextSize(0.04);
myt[npaveStat] ->SetTextColor(kRed);
list[npaveStat]->Add(myt[npaveStat]);
npaveStat++;
//vh.back()->SetStats(0);
canvas[i]->Modified();

Sorry, can you be more explicit, is
gPad->Modified();
gPad->Update();
missing? because adding both lines does not change anything.

Regards

Dear experts,

does anyone know what is wrong in my code?

Regards

func.C is missing

Processing plot_kOptimization.cc...
In file included from input_line_10:1:
/Users/couet/Downloads/plot_kOptimization.cc:1:10: fatal error: 'func.C' file
      not found
#include "func.C"
         ^

Dear Couet,

sorry you can find func.C in cc.

Regards
func.C (873 Bytes)

Try the attached “plot_kOptimization.cc” file (for some unexplainable reason, one needs to “paveStat[npaveStat]->Draw();” manually -> maybe Olivier can comment).

BTW. Rename your “list” variable into e.g. “llist” (ROOT 6 may confuse your “list” variable with the “std::list” container class).
plot_kOptimization.cc (7.58 KB)

I do not see any obvious reason. emu->ls() looks correct with this Draw().

Sorry, does it means that I just can’t do it?

Yes use the macro Pepe sent you … it works fine.

Well, the question is why the “mystat” is not drawn / disappears when “vh.back()->SetStats(0);” is in effect and there is no additional “Draw” (I never needed it up to now).

Hi Pepe and Couet,

Pepe, I just saw your code. It worked. Thank you very much for you help!

Regards

Dear experts,

I modified the code a little bit by adding a variable to be print and I do not understand why I have a line bellow Mean in the stat boxes, I never asked for that. Do you know what happened? I posted my new code.

Regards
plot_kOptimization.cc (8.17 KB)

Either: string meanOverRms{"mean/rms: = " + to_string(rounded(vh.back()->GetMean()/vh.back()->GetRMS()))}; or: string meanOverRms{"mean/rms: " + to_string(rounded(vh.back()->GetMean()/vh.back()->GetRMS())) + " = "}; or: string meanOverRms{" = mean/rms: " + to_string(rounded(vh.back()->GetMean()/vh.back()->GetRMS()))};
I have no idea why the “=” character is “mandatory” -> maybe Olivier can comment.

Dear Pepe,

thank you, the “=” solved my remaining issues…

Regards

Dear Pepe,

from what I can see, the “=” is there to display the txt. Depending where you put it, the text are set to the right, center…

Regards