Example #1
0
void FindSections(TH1D* h_rate, vector<double> &sectionBegin, vector<double> &sectionEnd, const double& begin, const double& end, const double offset, const double skip)
{
  if(begin < h_rate->GetBinLowEdge(1) || end > h_rate->GetBinLowEdge(h_rate->GetNbinsX()+1))
    {
      cerr << "Borders outside of histogram " << begin << " " <<  h_rate->GetBinLowEdge(1) << " " << end << " " << h_rate->GetBinLowEdge(h_rate->GetNbinsX()+1) << endl;
      exit(-1);
    }
  
  //Get Median
  int n = h_rate->GetXaxis()->GetNbins(); 
  //std::vector<double>  x(n);
  //h_rate->GetXaxis()->GetCenter(&x[0]);
  double * y = h_rate->GetArray();
  // exclude underflow/overflows from bin content array y
  double median = TMath::Median(n, &y[0]);

  //Define cut values (this is a bit arbitrary)
  const double cut_up = median * 1.2;
  const double cut_down = median * 0.5;
  cout << "Using cut values = " << cut_down << " < " << median << " < " << cut_up << endl;

  //Search in histogram
  const double step = h_rate->GetBinWidth(1) / 100.;
  bool in_section = false;

  for(double current = begin; current < end; current += step)
    {
      const double value = h_rate->Interpolate(current);

      //Start a section
      if (!in_section && (cut_down <= value && value <= cut_up))
        {
          sectionBegin.push_back(current+offset);
          in_section = true;
        }

      //End a section
      if (in_section && (value < cut_down || cut_up < value))
        {
          in_section = false;
          sectionEnd.push_back(current-offset);
        }
    }

  if(in_section)
    sectionEnd.push_back(end);
    
  if(sectionBegin.size() != sectionEnd.size())
    {
      cerr << "sections size error" << endl;
      exit(-1);
    }

  ostringstream name; name << "rate_" << begin << "_" << end;
  TH1D* draw = (TH1D*)(h_rate->Clone(name.str().c_str()));
  draw->GetXaxis()->SetRangeUser(begin,end);
  draw->Draw("HIST L");
  for (int i=0; i < int(sectionBegin.size()); ++i)
    {
      TBox* boxskip = new TBox(sectionBegin[i],cut_down/2.,sectionBegin[i]+skip,cut_down);
      boxskip->SetFillColor(kRed);
      boxskip->SetFillStyle(3001);
      boxskip->DrawClone();
//       TBox* box = new TBox(sectionBegin[i]+skip,cut_down/2.,sectionEnd[i],cut_down);
//       box->SetFillColor(kGreen-2);
//       box->SetFillStyle(3001);
//       box->DrawClone();
      TPaveText* txt = new TPaveText(sectionBegin[i]+skip,cut_down/2.,sectionEnd[i],cut_down,"b t l");
      ostringstream text; text << i;
      txt->AddText(text.str().c_str());
      txt->SetFillStyle(3001);
      txt->SetFillColor(kGreen-2);
      txt->Draw("SAME");
    }
  return;
}