Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TAGH_timewalk/scripts/offsets/fits.C
Go to the documentation of this file.
1 using namespace RooFit;
2 TH1* GetHistogram(TFile *f, TString hname, TString htype, int counter) {
3  stringstream ss; ss << counter;
4  TH1 *h;
5  if (htype == "TDCADC") {
6  TH2I *h2 = (TH2I*)f->Get("TAGH_timewalk/Offsets/"+hname);
7  h = h2->ProjectionY(TString(h2->GetName())+"_"+TString(ss.str()),counter,counter);
8  TString title = "TAGH counter "+TString(ss.str());
9  h->SetTitle(title);
10  h->GetXaxis()->SetTitle(h2->GetYaxis()->GetTitle());
11  }
12  if (htype == "TAGH") {
13  TH2I *h2 = (TH2I*)f->Get("TAGH_timewalk/Timewalk/TAGHRF_tdcTimeDiffVsPulseHeight_"+TString(ss.str()));
14  const int N = 40;
15  h = h2->ProjectionY(TString(h2->GetName()),1,N+1);
16  h->SetTitle("TAGH counter "+TString(ss.str()));
17  h->GetXaxis()->SetTitle("time(TDC) - time(RF) [ns]");
18  }
19  return h;
20 }
21 double GetMode(TH1 *h) {
22  if (h->GetEntries() == 0.0) return 999.0;
23  int max_bin = h->GetMaximumBin();
24  double max = h->GetBinContent(max_bin);
25  if (max < 7.0) return 999.0;
26  return h->GetBinCenter(max_bin);
27 }
28 void WriteFitResults(ofstream &fout, TH1 *h, TString htype, int counter) {
29  TString sep = ",";
30  double max = h->GetBinContent(h->GetMaximumBin());
31  double mode = GetMode(h);
32  if (h->GetEntries() < 100.0 || mode == 999.0) {
33  fout << counter << sep << max << sep << 0.0 << sep << 0.0 << sep << 0.0 << endl;
34  return;
35  }
36  double xlow = h->GetBinCenter(h->FindFirstBinAbove(0.5*max)) - 1.0;
37  double xhigh = h->GetBinCenter(h->FindLastBinAbove(0.5*max)) + 1.0;
38  RooRealVar x("TDC time difference","TDC time difference [ns]",xlow,xhigh);
39  RooDataHist data("data","data",RooArgList(x),h);
40  // model: gaussian
41  RooRealVar mean("mean","mean",mode,xlow,xhigh);
42  RooRealVar sigma("sigma","sigma",0.2,0.01,0.6);//0.01,0.5
43  RooGaussian gauss("gauss","gauss",x,mean,sigma);
44  gauss.fitTo(data,PrintLevel(-1));
45  // Make plot
46  TCanvas *canvas = new TCanvas("c","c",800,500);
47  RooPlot *plot = x.frame();
48  plot->SetTitle(h->GetTitle());
49  plot->SetXTitle(h->GetXaxis()->GetTitle());
50  plot->SetYTitle("TAGH hits");
51  plot->SetTitleSize(0.0474,"XY");
52  data.plotOn(plot);
53  gauss.plotOn(plot,LineColor(kRed));
54  plot->Draw();
55  gauss.paramOn(plot,Layout(0.58,0.9,0.9),Format("NEU",AutoPrecision(1)));
56  plot->Draw();
57  stringstream ss; ss << counter;
58  system(TString("mkdir -p fits_"+htype).Data());
59  canvas->Print("fits_"+htype+"/counter_"+TString(ss.str())+".gif");
60  delete canvas; delete plot;
61  fout << counter << sep << max << sep << mean.getVal() << sep << mean.getError() << sep << sigma.getVal() << endl;
62 }
63 void WriteFitResults2(ofstream &fout, TH1 *h, TString htype, int counter) {
64  TString sep = ",";
65  double max = h->GetBinContent(h->GetMaximumBin());
66  double mode = GetMode(h);
67  if (h->GetEntries() < 100.0 || mode == 999.0) {
68  if (counter > 0) fout << counter << sep << max << sep << 0.0 << sep << 0.0 << sep << 0.0 << endl;
69  return;
70  }
71  double xlow = mode - 1.0;
72  double xhigh = mode + 1.0;
73  RooRealVar x("TDC time difference","TDC time difference [ns]",xlow,xhigh);
74  RooDataHist data("data","data",RooArgList(x),h);
75  // model: add a narrow and wide gaussian with same mean
76  RooRealVar mean("mean","mean",mode,xlow,xhigh);
77  RooRealVar sigma1("sigma1","sigma1",0.2,0.01,0.4);//0.01,0.6
78  RooGaussian gauss1("gauss1","gauss1",x,mean,sigma1);
79  RooRealVar sigma2("sigma2","sigma2",0.7,0.3,3.0);//0.3,2.5
80  RooGaussian gauss2("gauss2","gauss2",x,mean,sigma2);
81  // f1: fraction of entries in first gaussian
82  RooRealVar f1("f1","f1",0.75,0.01,1.);
83  RooAddPdf doubleGauss("doubleGauss","doubleGauss",RooArgList(gauss1,gauss2),RooArgList(f1));
84  doubleGauss.fitTo(data,PrintLevel(-1));
85  // Make plot
86  TCanvas *canvas = new TCanvas("c","c",800,500);
87  RooPlot *plot = x.frame();
88  plot->SetTitle(h->GetTitle());
89  plot->SetXTitle(h->GetXaxis()->GetTitle());
90  plot->SetYTitle("TAGH hits");
91  plot->SetTitleSize(0.0474,"XY");
92  data.plotOn(plot);
93  doubleGauss.plotOn(plot,LineColor(kRed));
94  plot->Draw();
95  doubleGauss.plotOn(plot,Components("gauss1"),LineColor(kBlue),LineStyle(kDashed));
96  doubleGauss.plotOn(plot,Components("gauss2"),LineColor(kGreen),LineStyle(kDashed));
97  doubleGauss.paramOn(plot,Layout(0.15,0.45,0.9),Format("NEU",AutoPrecision(1)));
98  plot->Draw();
99  stringstream ss; ss << counter;
100  system(TString("mkdir -p fits_"+htype).Data());
101  canvas->Print("fits_"+htype+"/counter_"+TString(ss.str())+".gif");
102  delete canvas; delete plot;
103  if (counter > 0) fout << counter << sep << max << sep << mean.getVal() << sep << mean.getError() << sep << sigma1.getVal() << endl;
104 }
105 int fits(TString rootFile, bool doAllFits) {
106  TFile *f = new TFile(rootFile,"read");
107  TString hnames[] = {"TAGH_tdcadcTimeDiffVsSlotID","TAGHRF_tdcTimeDiffVsSlotID"};
108  TString htypes[] = {"TDCADC","TAGH"};
109  int N_htypes = 1;
110  if (doAllFits) N_htypes = 2;
111  for (int htype = 0; htype < N_htypes; htype++) {
112  system("mkdir -p fits-csv");
113  ofstream fout; fout.open("fits-csv/results_"+htypes[htype]+".txt");
114  const int N = 274;
115  for (int i = 1; i <= N; i++) { // counters
116  TH1 *h = GetHistogram(f,hnames[htype],htypes[htype],i);
117  if (htype == 0) WriteFitResults(fout,h,htypes[htype],i);
118  else WriteFitResults2(fout,h,htypes[htype],i);
119  }
120  fout.close();
121  }
122  return 0;
123 }
int fits(TString rootFile, bool doAllFits)
Double_t x[NCHANNELS]
Definition: st_tw_resols.C:39
void WriteFitResults2(ofstream &fout, TH1 *h, TString htype, int counter)
TF1 * f
Definition: FitGains.C:21
double counter
Definition: FitGains.C:151
TFile f1(fnam)
Double_t sigma[NCHANNELS]
Definition: st_tw_resols.C:37
double GetMode(TH1 *h)
TH1 * GetHistogram(TFile *f, TString hname, TString htype, int counter)
void WriteFitResults(ofstream &fout, TH1 *h, TString htype, int counter)