Problem in 2D graph

Hi, I’m trying to get a 2D plot

This is the ROOT file si-500596.root (1.4 MB) and thi is the macro caloeff.cpp (72.7 KB)


This is the code that I used
	TH2F *henecdsubstringcutrelTHFcut = new TH2F("henecdsubstringcutrelTHFcut", "", 500, 0., 20.,500, 0.,5000.);
         TString what2 = TString::Format("Calo_EnDep[%d] : xh","subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);
         t->Draw(what2, cut && cut3sub && cut4sub && cut5sub);
         gPad->Modified();
         gPad->Update(); // make sure it's really (re)drawn
         henecdsubstringcutrelTHFcut->SetTitle(cdhenecdsubstringcutrelTHFname);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetTitle(cdeneXnameabenecvsd);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetTitle(cdeneYnameabenecvsd);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetTitleSize(c2_YTitleSize);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetTitleFont(c2_YTitleFont);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetTitleOffset(c2_YTitleOffset);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetLabelFont(c2_YLabelFont);
         henecdsubstringcutrelTHFcut->GetYaxis()->SetLabelSize(c2_YLabelSize);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetTitleSize(c2_XTitleSize);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetTitleFont(c2_XTitleFont);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetTitleOffset(c2_XTitleOffset);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetLabelFont(c2_XLabelFont);
         henecdsubstringcutrelTHFcut->GetXaxis()->SetLabelSize(c2_XLabelSize);
         henecdsubstringcutrelTHFcut->SetMarkerSize(50);
         henecdsubstringcutrelTHFcut->Draw("COLZ");
         henecdsubstringcutrelTHFcut->SetName(heneabname);
            gPad->Modified();
         gPad->Update(); // make sure it's really (re)drawn
         TH1 *hhb = henecdsubstringcutrelTHFcut->DrawCopy("colz"); // creates all axes
         gPad->Modified();
         gPad->Update(); // make sure it's really (re)drawn
         TPaletteAxis * palette2 = (TPaletteAxis *) hhb->GetListOfFunctions()->FindObject("palette");
   palette2->SetX1NDC(0.86);
   palette2->SetX2NDC(0.90);
   palette2->SetY1NDC(0.2);
   palette2->SetY2NDC(0.8);
         c97->Modified();
         c97->Update();
            TLegend* leghenecdsubstringcutrelTHFcut = new TLegend(0.65, 0.7, .75, .75);
            leghenecdsubstringcutrelTHFcut->SetHeader("Legend");
         leghenecdsubstringcutrelTHFcut->SetNColumns(1);
         leghenecdsubstringcutrelTHFcut->AddEntry(leghenecdsubstringcutrelTHFcut, "Data", "l");
         leghenecdsubstringcutrelTHFcut->Draw();
         gPad->Update();
         TPaveStats *statsghenecdsubstringcutrelTHFcut = (TPaveStats*)henecdsubstringcutrelTHFcut->GetListOfFunctions()->FindObject("stats");
         statsghenecdsubstringcutrelTHFcut->SetTextColor(kBlue);
         statsghenecdsubstringcutrelTHFcut->SetX1NDC(0.80); statsghenecdsubstringcutrelTHFcut->SetX2NDC(0.98);
         statsghenecdsubstringcutrelTHFcut->SetY1NDC(0.77); statsghenecdsubstringcutrelTHFcut->SetY2NDC(0.92);
         gPad->Update();
         c97->Print(myplothenecdsubstringcutrelTHFcut);
         delete c97;

By writing
TString what2 = TString::Format("Calo_EnDep[%d] : xh","subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);
I get this error

but I setted f=0, so why does ROOT say that the index is too high?
Anyway I tried to write

TString what2 = TString::Format("Calo_EnDep[0] : xh","subdet==36 && xh>-99>> henecdsubstringcutrelTHFcut");

In this way, I didn’t get errors, but ROOT doesn’t create the plot…


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.20.24
Platform: Windows
Compiler: Not Provided


remove the quotes around the comma:
TString what2 = TString::Format("Calo_EnDep[%d] : xh, subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);
or escape them:
TString what2 = TString::Format("Calo_EnDep[%d] : xh\",\" subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);

Also, may be first fix the warnings you get when the macro starts:

Processing caloeff.cpp...
In file included from input_line_11:1:
/Users/couet/Downloads/caloeff.cpp:323:120: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
  ...myplothenecdsubstringcutrelTHFcut = TString::Format("%ssi-%d_ene%d-vs-xhn_%d.pdf",outfolder5sub.Data(),ifile,timeenergynamesubf,f,su...
                                                                     ~~                                           ^~~~~~~~~~~~~~~~~~
                                                                     %s
/Users/couet/Downloads/caloeff.cpp:323:141: warning: data argument not used by format string [-Wformat-extra-args]
  ...= TString::Format("%ssi-%d_ene%d-vs-xhn_%d.pdf",outfolder5sub.Data(),ifile,timeenergynamesubf,f,subdet); // TO USE FOR FILES WHIT TA...
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                 ^
/Users/couet/Downloads/caloeff.cpp:1330:64: warning: format specifies type 'int' but the argument has type 'const char *' [-Wformat]
         TString what2 = TString::Format("Calo_EnDep[%d] : xh","subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);
                                                     ~~        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                     %s
/Users/couet/Downloads/caloeff.cpp:1330:116: warning: data argument not used by format string [-Wformat-extra-args]
         TString what2 = TString::Format("Calo_EnDep[%d] : xh","subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);
                                         ~~~~~~~~~~~~~~~~~~~~~                                                     ^

thank you the both @couet and @bellenot

I fixed these errors showed by couet,

but ROOT for windows didnt’ show them!

I wrote
TString what2 = TString::Format("Calo_EnDep[%d] : xh\",\" subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);

as @bellenot said, but I got this error

Your string formatting (the varexp Draw() first argument) looks weird, I don’t know why the comma is there (or did I miss something?) What are you trying to do exactly?

In the ROOT file xh[] is the x-coordinate of the strip and Calo_EnDep[] is the released energy. My supervisor asked me to plot the relased energy in three subdetectors (called 0, 1 and 2) vs the the xh for the subdetectors called 36 and 37. My supervisor’s colleague sent us an example about what I’ve to do, that is

and he wrote that he used this command line

lemma->Draw("Calo_EnDep[0] : xh","subdet==37 && xh>-99")

to get the plot!

That’s because I wrote

TString what2 = TString::Format("Calo_EnDep[%d] : xh\",\" subdet==%d && xh>%d>> henecdsubstringcutrelTHFcut",f,subdet,xhn);

in the macro.

OK, so the way you did it cannot work. for example, to reproduce exactly this example:

lemma->Draw("Calo_EnDep[0] : xh","subdet==37 && xh>-99")

you shout write:

TString varexp = TString::Format("Calo_EnDep[%d] : xh", f);
TString selection = TString::Format("subdet==%d && xh>%d", subdet, xhn);
lemma->Draw(varexp, selection);

Now, up to you to adapt to your own code…

Thank you @bellenot I wrote:

TH2F *henecdsubstringcutrelTHFcut = new TH2F("henecdsubstringcutrelTHFcut", "", 500, 0., 20.,500, 0.,5000.);

TString varexp = TString::Format("Calo_EnDep[%d] : xh >> henecdsubstringcutrelTHFcut" );

TString selection = TString::Format("subdet==%d && xh>%d", subdet, xhn);

t->Draw(varexp, selection, cut && cut3sub && cut4sub && cut5sub);

I don’t get errors, but ROOT doesn’t create the plot!
This is the last macro
caloeff.cpp (73.0 KB)
and here the data

t->Draw(varexp, selection && cut && cut3sub && cut4sub && cut5sub);

thank you @Wile_E_Coyote but writing
t->Draw(varexp, selection && cut && cut3sub && cut4sub && cut5sub);

I get this error

What is the type of the f variable? Can you print its value before using it (just to check)?

sorry @bellenot…I just noticed that I forgot the f in the line

TString varexp = TString::Format(“Calo_EnDep[%d] : xh >> henecdsubstringcutrelTHFcut”, f);

That’s because I got the error.

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