Locked History Actions

attachment:getped.C of ANAROOT/Macros

Attachment 'getped.C'

Download

void getped(TH2* hist, int idsta=1, TString opt=""){
  /*
    ROOT Macro for obtaining pedestal values from 2d histogram (id vs Qraw)
    input: 2d-hist
           option="nstop" -> wait is skipped
    output: ped_peak.txt  -> peak values
            ped_sigma.txt -> Gaussian width
  */

  Int_t nbin=hist->GetXaxis()->GetNbins();
  Int_t num=0;
  vector<double> peak;
  vector<double> sigma;
  if (nbin>1000){
    cout<<"Too much channels, STOP."<<endl;
    exit(0);
  }

  for (Int_t i=idsta-1;i<nbin;i++){// begin of loop ------->
    num++;
    Int_t id = i+1;
    TH1 *h=MakeBanY(hist,id);

    Double_t p[3];
    FitHist(h,p);
    peak.push_back(p[1]);
    sigma.push_back(p[2]);

    // Draw
    h->Draw();
    gPad->Update();
    cout<<"ID="<<id<<" peak="<<p[1]<<"  sig="<<p[2]<<endl;

    if (opt!="nstop")
      if(Wait()) break;

  }// end of loop <--------------
  cout<<endl;

  // file write
  ofstream ofs1;
  ofs1.open("ped_peak.txt");
  vector<double>::iterator it=peak.begin();
  while(it!=peak.end()){
    ofs1<<*it<<endl;
    it++;
  }
  ofs1.close();
  cout<<"peak value is writtin in \"ped_peak.txt\""<<endl;

  ofstream ofs2;
  ofs2.open("ped_sigma.txt");
  vector<double>::iterator it=sigma.begin();
  while(it!=sigma.end()){
    ofs2<<*it<<endl;
    it++;
  }
  ofs2.close();
  cout<<"sigma value is writtin in \"ped_peak.txt\""<<endl;

  cout<<num<<" peaks are fitted, END"<<endl;
}
//___________________________________________________________________________
void FitHist(TH1 *h, double p[]){
  double p_new=0;
  double p_old=0;
  Double_t xmin,xmax;
  Double_t diff=100;

  TF1 *func = new TF1("myfunc","gaus");
  double mean = h->GetMean();
  double rms = h->GetRMS();
  func->SetParameters( h->GetMaximum(),
                       h->GetMean(),
                       h->GetRMS()      );
  xmin = mean-3*rms;
  xmax = mean+3*rms;
  h->Fit(func,"Q","",xmin,xmax);
  func->GetParameters(p);

  while(diff>0.01){
    xmin = p[1]-3*p[2];
    xmax = p[1]+3*p[2];
    h->Fit(func,"Q","",xmin,xmax);
    func->GetParameters(p);

    p_new=p[1];
    if (abs(p_new)<0.000001){
      diff=100;
    }else{
      diff=std::abs((p_new-p_old)/p_new);
    }
    p_old=p_new;
  }// end of while
  return;
}
//___________________________________________________________________________
TH1* MakeBanY(TH2* hist, Int_t id){
  TString str=hist->GetName();
  ostringstream oss;
  oss<<id;
  str+="_bny"+oss.str();
  return hist->ProjectionY(str.Data(),id,id);
}
//___________________________________________________________________________
bool Wait(){
  //check and quit if q is typed
  printf("[enter]:continue, [q]:quit >");
  char s[1];
  gets(s);
  if(s[0]=='q') {
    cout<<"Break fit loop"<<endl;
    return true;
  }
  return false;
}
//___________________________________________________________________________

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2012-11-02 05:21:15, 1.6 KB) [[attachment:BPCHist.C]]
  • [get | view] (2013-02-27 09:18:59, 0.8 KB) [[attachment:CoinHist.C]]
  • [get | view] (2012-11-02 05:10:49, 2.6 KB) [[attachment:FDC2Hist.C]]
  • [get | view] (2012-11-02 04:31:49, 2.1 KB) [[attachment:HODHist.C]]
  • [get | view] (2012-11-02 04:31:32, 1.6 KB) [[attachment:ICBHist.C]]
  • [get | view] (2014-12-31 11:41:13, 19.5 KB) [[attachment:OnlineMonitor.cc]]
  • [get | view] (2014-12-31 11:41:02, 2.6 KB) [[attachment:OnlineMonitor.hh]]
  • [get | view] (2012-11-02 05:25:41, 1.7 KB) [[attachment:PlaHist.C]]
  • [get | view] (2012-02-15 09:14:24, 3.2 KB) [[attachment:ch2ns.C]]
  • [get | view] (2012-02-15 10:05:13, 3.0 KB) [[attachment:getlandaupeak.C]]
  • [get | view] (2012-02-15 09:14:53, 2.7 KB) [[attachment:getped.C]]
  • [get | view] (2012-02-13 09:49:04, 2.6 KB) [[attachment:makevsta.C]]
  • [get | view] (2012-02-19 14:44:04, 0.5 KB) [[attachment:slew.C]]

You are not allowed to attach a file to this page.