Locked History Actions

attachment:ch2ns.C of ANAROOT/Macros

Attachment 'ch2ns.C'

Download

void ch2ns(TH2* hist, int IDsta=1, TString opt=""){
  /*
    ROOT Macro for TDC calibration from 2d histogram (ID vs Traw)
    input: 2d-hist
           option="nstop" -> wait is skipped
    output: ch2ns.txt  -> calibration values
  */

  // modify this line if you need
  const double period=10;// period of TDC calibrator (ns)


  Int_t nbin=hist->GetXaxis()->GetNbins();
  Int_t num=0;
  vector<double> prm;

  hist->Draw();
  gPad->GetCanvas()->Clear();
  gPad->GetCanvas()->Divide(1,2);
  for (Int_t i=IDsta-1;i<nbin;i++){// begin of loop ------->
    num++;
    Int_t ID = i+1;
    TH1 *h=MakeBanY(hist,ID);

    gPad->GetCanvas()->cd(1);
    h->Draw();
    gPad->GetCanvas()->cd(2);
    double val=GetPrmFromHist(h,period);
    prm.push_back(val);

    // Draw
    gPad->GetCanvas()->Update();
    cout<<"ID="<<ID<<" prm="<<val<<endl;

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

  }// end of loop <--------------

  // file write
  ofstream ofs1;
  ofs1.open("ch2ns.txt");
  vector<double>::iterator it=prm.begin();
  while(it!=prm.end()){
    ofs1<<*it<<endl;
    it++;
  }
  ofs1.close();
  cout<<"parameters are writen in \"ch2ns.txt\""<<endl;

  cout<<num<<" channels are fitted, END"<<endl;
}
//___________________________________________________________________________
double GetPrmFromHist(TH1 *h, double period){
  double calval;// calibration value
  int thre = h->GetMaximum()*0.2;// threshold for peak search
  int nbin = h->GetXaxis()->GetNbins();
  Double_t xmin,xmax;
  double p[3];

  vector<double> peaks;
  vector<double> tvals;

  int OverThre=0;
  for(int ibin=1;ibin<nbin+1;ibin++){
    if( OverThre==0 && h->GetBinContent(ibin)>thre ){
      OverThre=1;
      xmin=h->GetXaxis()->GetBinCenter(ibin);
    }else if ( OverThre==1 && h->GetBinContent(ibin)<thre){
      OverThre=2;
      xmax=h->GetXaxis()->GetBinCenter(ibin);
    }
    if (OverThre==2){
      OverThre=0;
      TF1 *func = new TF1("myfunc","gaus");
      double mean = 0.5*(xmin+xmax);
      double rms = 0.5*0.3*(xmax-xmin);
      func->SetParameters( h->GetMaximum(),
                           h->GetMean(),
                           h->GetRMS()      );
      h->Fit(func,"Q","",xmin,xmax);
      func->GetParameters(p);
//      cout<<"peak value is "<<p[1]<<endl;
      peaks.push_back(p[1]);
      tvals.push_back(period*peaks.size());
    }
  }

  double* xpointer=&(peaks.at(0));
  double* ypointer=&(tvals.at(0));
  TGraph* tg=new TGraph(peaks.size(),xpointer,ypointer);
  tg->Fit("pol1","Q");
  TString title=h->GetName();
  tg->SetTitle(title);
  tg->Draw("AP*");
  tg->GetXaxis()->SetRangeUser(h->GetXaxis()->GetXmin(),
                               h->GetXaxis()->GetXmax());
  calval=tg->GetFunction("pol1")->GetParameter(1);
  return calval;
}
//___________________________________________________________________________
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.