GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
Example7.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 /* Go4Fit Example N7
15  Simultaneous fit of two histogram
16 */
17 
18 #ifndef __CINT__
19 
20 #include "TH1.h"
21 #include "TFile.h"
22 #include "TCollection.h"
23 #include "TApplication.h"
24 
25 #include "TGo4FitMinuit.h"
26 #include "TGo4Fitter.h"
27 #include "TGo4FitDataHistogram.h"
28 #include "TGo4FitModelPolynom.h"
29 #include "TGo4FitModelGauss1.h"
30 #include "TGo4FitAxisTrans.h"
31 #include "TGo4FitLinearTrans.h"
32 #include "TGo4FitterOutput.h"
33 
34 void Example7();
35 
36 int main(int argc, char **argv)
37 {
38  TApplication theApp("Application", nullptr, nullptr);
39 
40  Example7();
41 
42  theApp.Run();
43 
44  return 0;
45 }
46 
47 #endif
48 
49 // routine to read histogram from examples file
50 TH1D* GetHistogram(const char *HistogramName)
51 {
52  TFile *f = TFile::Open("histograms.root");
53  if (!f) return nullptr;
54  TH1D *histo = nullptr;
55  f->GetObject(HistogramName, histo);
56  if (histo) histo->SetDirectory(nullptr);
57  delete f;
58  return histo;
59 }
60 
61 // construct transformation object, which recalculate bin numbers to new scale values
62 // here simple linear transformation is used
64 {
65  TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
66  trans->SetCoefByRange(3800,0.,3.8);
67  return trans;
68 }
69 
71 {
72 // create fitter and select function to fit
73  TGo4Fitter *fitter = new TGo4Fitter("Fitter", TGo4Fitter::ff_ML_Poisson, kFALSE);
74 
75 // create object to fit for first histogram, but specify histogram later
76  TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1", nullptr);
77  data1->SetUseBinScale(kTRUE);
78  data1->SetRange(0,2.2,2.9);
79  data1->SetNumberOfTransSlots(1);
80  fitter->AddData(data1);
81 
82 // create object to fit for second histogram, but specify histogram later
83  TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2", nullptr);
84  data2->SetUseBinScale(kTRUE);
85  data2->SetRange(0,2.2,2.9);
86  data2->SetNumberOfTransSlots(1);
87  fitter->AddData(data2);
88 
89  fitter->ConnectSlots("data1.Trans0","data2.Trans0");
90 
91  TGo4FitModel *gauss1 = new TGo4FitModelGauss1("Gauss1",2.553,0.015);
92  gauss1->AssignToData("data1"); gauss1->AssignToData("data2",1.2);
93 
94  TGo4FitModel *gauss2 = new TGo4FitModelGauss1("Gauss2",2.672,0.015);
95  gauss2->AssignToData("data1"); gauss2->AssignToData("data2",1.2);
96 
97  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
98  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
99  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
100  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
101 
102  fitter->AddModel(gauss1);
103  fitter->AddModel(gauss2);
104  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3",2.597,0.014) );
105  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4",2.717,0.014) );
106 
107 // add ampl estimation action
108  fitter->AddAmplEstimation(1);
109 
110 // create minimizer and set it to fitter
111  TGo4FitMinuit* fMinuit = new TGo4FitMinuit("Minuit");
112  fMinuit->AddCommand("MIGRAD 500 1");
113  fMinuit->AddCommand("MINOS 20");
114  fitter->AddAction(fMinuit);
115 
116 // set usage of buffers only for data objects, not for models
117 // this highly increase speed of evaluations
118  fitter->SetMemoryUsage(1);
119 
120  return fitter;
121 }
122 
123 // store fitter with all supplied objects
124 void StoreFitter(TGo4Fitter *fitter)
125 {
126  TFile *f = TFile::Open("Example7.root","recreate");
127  if (f) fitter->Write("Fitter");
128  delete f;
129 }
130 
131 // read fitter from file
133 {
134  TFile *f = TFile::Open("Example7.root");
135  if (!f) return nullptr;
136  TGo4Fitter *fitter = nullptr;
137  f->GetObject("Fitter", fitter);
138  delete f;
139  return fitter;
140 }
141 
142 void Example7()
143 {
144 // create fitter
145  TGo4Fitter *fitter = BuildFitter();
146 
147 // store empty fitter to file and restore again
148  StoreFitter(fitter);
149  delete fitter;
150 
151  fitter = RestoreFitter();
152 
153 // construct axis transformation object and set it for both data object
154 // slot of both data object was connected
155  fitter->SetObject(ConstructTrans(), kTRUE);
156 
157 // assign histograms to fitter with ownership flag
158  fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
159  fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
160 
161 // do fit
162  fitter->DoActions();
163 
164 // store fitter to file and destroy it
165  StoreFitter(fitter);
166 
167  delete fitter;
168 
169 
170 // restore fitter from file
171 // logically this is independent part and can be placed anywhere, just in another program
172  fitter = RestoreFitter();
173 
174 // show results of fitting
175  fitter->Print("Pars");
176  fitter->Draw("#data1,Gauss1,Gauss2");
177  fitter->Draw("#data2,Gauss1,Gauss2,Gauss3,Gauss4");
178 
179  delete fitter;
180 }
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
Definition: TGo4Fitter.cxx:70
void AddAmplEstimation(Int_t NumIters=1)
Definition: TGo4Fitter.cxx:726
Bool_t SetNumberOfTransSlots(Int_t nslots)
Definition: TGo4FitData.cxx:53
void AssignToData(const char *DataName, Double_t RatioValue=1., Bool_t FixRatio=kFALSE)
int main(int argc, char **argv)
Definition: Example7.cxx:36
void AddCommand(const char *iCommand)
void AddAction(TGo4FitterAction *Action)
TH1D * GetHistogram(const char *HistogramName)
Definition: Example7.cxx:50
TGo4FitData * AddData(TGo4FitData *d)
Definition: TGo4Fitter.cxx:119
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
Bool_t ConnectSlots(TGo4FitSlot *slot1, TGo4FitSlot *slot2)
void SetRange(Int_t naxis, Double_t min, Double_t max)
void Example7()
Definition: Example7.cxx:142
TGo4Fitter * BuildFitter()
Definition: Example7.cxx:70
TGo4FitAxisTrans * ConstructTrans()
Definition: Example7.cxx:63
TGo4FitModel * AddModel(TGo4FitModel *m)
Definition: TGo4Fitter.cxx:210
TGo4Fitter * RestoreFitter()
Definition: Example7.cxx:132
void SetUseBinScale(Bool_t iUseBinScale)
Definition: TGo4FitData.h:73
void StoreFitter(TGo4Fitter *fitter)
Definition: Example7.cxx:124
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=nullptr)
void Draw(Option_t *option) override
void Print(Option_t *option="") const override
Definition: TGo4Fitter.cxx:779