TGraph for each bin

#include <math.h>
#include "tdrLimitStyle.cc"

void plotlimit_hist(){

   tdrLimitStyle();

   vector<int> vcolorlimit{
      kYellow,
      kGreen,
      kBlack,
      kGreen,
      kYellow,
      kBlack,
   };

   //vector<string>vdir{"comb", "mt", "et", "em"};
   vector<string>vdir{"comb", "mt"};
   vector<string>vlabel{"comb.", "#mu#tau", "e#tau", "e#mu"};

   TCanvas *c = new TCanvas("c","c",800, 700);
   c->SetGridx();
   c->SetGridy();
   c->SetRightMargin(0.06);
   c->SetLeftMargin(0.2);

   TH1D *hlimit = new TH1D("hlimit","hlimit", 4, 0, 4);
   hlimit->GetXaxis()->SetTitle("Channel");
   hlimit->GetYaxis()->SetTitle("#sigma/#sigma_{SM}");
   hlimit->SetMinimum(0.0);
   hlimit->SetMaximum(5.0);
   hlimit->Draw();

   // hist bin label
   for (int i=0; i<hlimit->GetNbinsX(); i++) hlimit->GetXaxis()->SetBinLabel(i+1, vlabel[i].c_str());

   // hist axis
   TH1D *hlimits[4][6]; // [dir][limit]

   for(int dir=0; dir<vdir.size(); dir++) {

      vector<double>vlimit; // m2, m1, exp, p1, p2, obs

      // input file
      string input{"higgsCombine."+vdir[dir]+"_1j.Asymptotic.mH125.root"};
      TFile* ifile = new TFile(input.c_str(),"READ");
      if (!ifile->IsOpen()) { cout<<"main(): can't open "<<input<<endl; return; }
      cout<<"input file: "<<ifile->GetName()<<endl;

      // tree
      TTree *tree = (TTree*) ifile->GetObjectChecked("limit", "TTree");
      if(!tree) {cout<<"tree "<<tree->GetName()<<" does not exist!!\n"; return; }

      double limit;
      tree->SetBranchAddress("limit", &limit);

      tree->GetEntry(4);
      vlimit.push_back(limit); // p2

      tree->GetEntry(3);
      vlimit.push_back(limit); // p1

      tree->GetEntry(2);
      vlimit.push_back(limit); // exp

      tree->GetEntry(1);
      vlimit.push_back(limit); // m1

      tree->GetEntry(0);
      vlimit.push_back(limit); // m2

      for (int i=0; i<vlimit.size(); i++){
         string name{vdir[dir]+"_"+to_string(i)};
         hlimits[dir][i] = (TH1D*)hlimit->Clone(name.c_str());
         hlimits[dir][i] -> SetBinContent(dir+1, vlimit[i]);
         if (i==2) {
            TH1D *h = hlimits[dir][i];
            Int_t n = h->GetNbinsX();
            TLine l;
            l.SetLineStyle(2);
            l.SetLineWidth(2);
            Double_t x1,y1,x2,y2;
            for (int j=1; j<=n; j++) {
               y1 = h->GetBinContent(j);
               if (y1>0) {
                  y2 = y1;
                  x1 = h->GetBinLowEdge(j);
                  x2 = x1+h->GetBinWidth(j);
                  l.DrawLine(x1,y1,x2,y2);
               }
            }
         } else {
            if (i==0 || i==4) hlimits[dir][i] -> SetFillColor(kYellow);
            if (i==1 || i==3) hlimits[dir][i] -> SetFillColor(kGreen);
            if (i==3)hlimits[dir][i] -> SetFillColor(kYellow);
            if (i==4)hlimits[dir][i] -> SetFillColorAlpha(kWhite, 0.5);;
            hlimits[dir][i] -> Draw("hist same");
         }
      }
   }
   gPad->RedrawAxis();
}

Dear Couet,
thank you for the change, but could you please explain what was the problem in my code?
Because I can’t see why I had this trouble.
Regards

See how I draw the histogram with dotted line …

Dear Couet,

I saw how you did it, but I wonder why using hist->SetLineStyle(2); as people used to does not work properly?

Regards

When you draw the histogram with lines it draws the complete contour of the histogram … vertical lines, horizontal lines at 0 … etc … i.e. a bar chart …

In your case you do not want the vertical lines and you do not want the lines at 0 because they will show (as they have a bigger line width … (you asked for 2) …) So to have a clean plot, ready for publication, you need to do some more clever drawing adapted to your particular case. That’s what I proposed with the small piece code of I sent you .

That’s the power of ROOT: you have predefined plotting options which work in most cases, but you can also have very specific drawings adapted to your own problem thanks to the C++ interpreter with allows you to do all kind of graphics tricks you need.

Dear Couet,
ok, thank you for your answer.
Regards