Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gaussian_fits.C
Go to the documentation of this file.
1 using namespace RooFit;
2 TH1* GetHistogram(TFile *f, int counter, int ph_bin) {
3  stringstream ss_c; ss_c << counter;
4  TH2I *h2 = (TH2I*)f->Get("TAGH_timewalk/Timewalk/TAGHRF_tdcTimeDiffVsPulseHeight_"+TString(ss_c.str()));
5  stringstream ss_ph; ss_ph << ph_bin;
6  TH1 *h;
7  if (counter == 0 && ph_bin == 0) {
8  const int N = 40;
9  h = h2->ProjectionY(TString(h2->GetName())+"_"+TString(ss_ph.str()),1,N+1);
10  h->SetTitle("TAGH TDC timing - relative to RF, all counters");
11  }
12  else {
13  h = h2->ProjectionY(TString(h2->GetName())+"_"+TString(ss_ph.str()),ph_bin,ph_bin);
14  h->SetTitle("TAGH counter "+TString(ss_c.str())+", Pulse-height bin "+TString(ss_ph.str()));
15  }
16  h->GetXaxis()->SetTitle("time(TDC) - time(RF) [ns]");
17  return h;
18 }
19 double GetMode(TH1 *h) {
20  if (h->GetEntries() == 0.0) return 999.0;
21  int max_bin = h->GetMaximumBin();
22  double max = h->GetBinContent(max_bin);
23  if (max < 7.0) return 999.0;
24  return h->GetBinCenter(max_bin);
25 }
26 void WriteGaussianFitResults(ofstream &fout, TH1 *h, int counter, int ph_bin) {
27  TString sep = ",";
28  double mode = GetMode(h);
29  if (h->GetEntries() < 100.0 || mode == 999.0) {
30  if (counter > 0) fout << counter << sep << ph_bin << sep << h->GetEntries() << sep << 0.0 << sep << 0.0 << sep << 0.0 << endl;
31  return;
32  }
33  double xlow = mode - 1.0;
34  double xhigh = mode + 1.0;
35  RooRealVar x("TDC time difference","TDC time difference [ns]",xlow,xhigh);
36  RooDataHist data("data","data",RooArgList(x),h);
37  // model: add a narrow and wide gaussian with same mean
38  RooRealVar mean("mean","mean",mode,xlow,xhigh);
39  RooRealVar sigma1("sigma1","sigma1",0.2,0.01,0.4);//0.01,0.6
40  RooGaussian gauss1("gauss1","gauss1",x,mean,sigma1);
41  RooRealVar sigma2("sigma2","sigma2",0.7,0.3,3.0);//0.3,2.5
42  RooGaussian gauss2("gauss2","gauss2",x,mean,sigma2);
43  // f1: fraction of entries in first gaussian
44  RooRealVar f1("f1","f1",0.75,0.01,1.);
45  RooAddPdf doubleGauss("doubleGauss","doubleGauss",RooArgList(gauss1,gauss2),RooArgList(f1));
46  doubleGauss.fitTo(data,PrintLevel(-1));
47  // Make plot
48  TCanvas *canvas = new TCanvas("c","c",800,500);
49  canvas->SetBatch(kTRUE);
50  RooPlot *plot = x.frame();
51  plot->SetTitle(h->GetTitle());
52  plot->SetXTitle(h->GetXaxis()->GetTitle());
53  plot->SetYTitle("TAGH hits");
54  plot->SetTitleOffset(1.1,"Y");
55  data.plotOn(plot);
56  doubleGauss.plotOn(plot,LineColor(kRed));
57  plot->Draw();
58  doubleGauss.plotOn(plot,Components("gauss1"),LineColor(kBlue),LineStyle(kDashed));
59  doubleGauss.plotOn(plot,Components("gauss2"),LineColor(kGreen),LineStyle(kDashed));
60  if (counter == 0)
61  doubleGauss.paramOn(plot,Layout(0.56,0.9,0.9),Format("NEU",AutoPrecision(1)));
62  else
63  doubleGauss.paramOn(plot,Layout(0.15,0.45,0.9),Format("NEU",AutoPrecision(1)));
64  plot->Draw();
65  if (counter == 0) {
66  canvas->Print("overall_gaussian_fit.gif");
67  }
68  else {
69  stringstream ss; ss << counter;
70  system(TString("mkdir -p fits_gaussian/counter_"+ss.str()).Data());
71  canvas->Print("fits_gaussian/counter_"+TString(ss.str())+"/"+TString(h->GetName())+".gif");
72  }
73  delete canvas; delete plot;
74  if (counter > 0) fout << counter << sep << ph_bin << sep << h->GetEntries() << sep << mean.getVal() << sep << mean.getError() << sep << sigma1.getVal() << endl;
75 }
76 int gaussian_fits(TString rootFile, bool doAllFits) {
77  TFile *f = new TFile(rootFile,"read");
78  if (doAllFits) {
79  system("mkdir -p gaussian-fits-csv");
80  for (int i = 1; i <= 274; i++) { // counters
81  stringstream ss; ss << i;
82  ofstream fout; fout.open("gaussian-fits-csv/counter_"+TString(ss.str())+".txt");
83  for (int j = 1; j <= 41; j++) { // pulse-height bins
84  TH1 *h = GetHistogram(f,i,j);
85  WriteGaussianFitResults(fout,h,i,j);
86  }
87  fout.close();
88  }
89  }
90  // Do overall fit
91  ofstream fout; TH1 *h_overall = GetHistogram(f,0,0); WriteGaussianFitResults(fout,h_overall,0,0);
92  return 0;
93 }
void WriteGaussianFitResults(ofstream &fout, TH1 *h, int counter, int ph_bin)
Definition: gaussian_fits.C:26
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
TF1 * f
Definition: FitGains.C:21
int gaussian_fits(TString rootFile, bool doAllFits)
Definition: gaussian_fits.C:76
double counter
Definition: FitGains.C:151
TFile f1(fnam)
double GetMode(TH1 *h)
TH1 * GetHistogram(TFile *f, TString hname, TString htype, int counter)