Draw error band on already existed sub-canvas

Hi everyone,
I have a canvas saved as ‘abc.root’ that contains two sub-canvas. Currently, I want to add a band to the right side of the canvas, which is schematically represented as a yellow band over the entire x-axis range with a fixed value +/- error that will be manually entered. Please let me know if you have any recommendations. Thanks.

It very much depends how are stored your data, but lets assume they are stored in two vectors. Then you can create a TGraph and draw it with option “F” (for filled).

TGraphErrors* lnGraphErrors = new TGraphErrors(Xaxis.size(), &Xaxis[0], nullptr, nullptr, nullptr);

for (size_t i = 0; i < Xaxis.size(); ++i) {
    double RatioIn = Yaxis1[i];
    double RatioOut = Yaxis2[i];

    if (RatioIn != 0) {
        double lnRatio = std::log(RatioOut / RatioIn);
        double S = coeff * lnRatio;
        double SERR =  ((Yerror1[i] / RatioIn) * (Yerror1[i] / RatioIn) + (Yerror2[i] / RatioOut) * (Yerror2[i] / RatioOut)));
        lnGraphErrors->SetPoint(i, Xaxis[i], S);
        lnGraphErrors->SetPointError(i, 0.0, SERR); // x error is set to 0

gPad->SetGrid(1, 1);

This is the relevant section on how canvas->cd(2) was generated initially, and then the canavs was saved as a root file (let’s say abc.root). Now I want to load the abc.root and access canvas->cd(2) from the root file to draw an error band of a fixed number +/- error (say, 850 +/- 4) on top of that, as shown above. I hope this makes the question clear. thanks again.

You need something like that:

   auto f = new TFile("abc.root");
   TCanvas *c = (TCanvas *)f->Get("canvas");
   ... build the TGraph g to draw the band

Thanks, @couet . I was not doing c->Draw() before setting it to c->cd(2), which is probably why it was not working. Also, the way I defined the TGraphErrors *g,



Thank you once again for your time.

Yes is you have a TGraphErrors option 3 is the one to use.

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