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++;
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]);
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"
^
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)
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).
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.
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.