GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Example7.cxx
Go to the documentation of this file.
1 // $Id: Example7.cxx 3058 2021-03-12 11:01:32Z linev $
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", 0, 0);
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 *f1 = TFile::Open("histograms.root");
53  if (f1==0) return 0;
54  TH1D *histo = 0;
55  f1->GetObject(HistogramName, histo);
56  if (histo) histo->SetDirectory(0);
57  return histo;
58 }
59 
60 // construct transformation object, which recalculate bin numbers to new scale values
61 // here simple linear transformation is used
63 {
64  TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
65  trans->SetCoefByRange(3800,0.,3.8);
66  return trans;
67 }
68 
70 {
71 // create fitter and select function to fit
72  TGo4Fitter *fitter = new TGo4Fitter("Fitter", TGo4Fitter::ff_ML_Poisson, kFALSE);
73 
74 // create object to fit for first histogram, but specify histogram later
75  TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
76  data1->SetUseBinScale(kTRUE);
77  data1->SetRange(0,2.2,2.9);
78  data1->SetNumberOfTransSlots(1);
79  fitter->AddData(data1);
80 
81 // create object to fit for second histogram, but specify histogram later
82  TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
83  data2->SetUseBinScale(kTRUE);
84  data2->SetRange(0,2.2,2.9);
85  data2->SetNumberOfTransSlots(1);
86  fitter->AddData(data2);
87 
88  fitter->ConnectSlots("data1.Trans0","data2.Trans0");
89 
90  TGo4FitModel* gauss1 = new TGo4FitModelGauss1("Gauss1",2.553,0.015);
91  gauss1->AssignToData("data1"); gauss1->AssignToData("data2",1.2);
92 
93  TGo4FitModel* gauss2 = new TGo4FitModelGauss1("Gauss2",2.672,0.015);
94  gauss2->AssignToData("data1"); gauss2->AssignToData("data2",1.2);
95 
96  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
97  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
98  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
99  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
100 
101  fitter->AddModel(gauss1);
102  fitter->AddModel(gauss2);
103  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3",2.597,0.014) );
104  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4",2.717,0.014) );
105 
106 // add ampl estimation action
107  fitter->AddAmplEstimation(1);
108 
109 // create minimizer and set it to fitter
110  TGo4FitMinuit* fMinuit = new TGo4FitMinuit("Minuit");
111  fMinuit->AddCommand("MIGRAD 500 1");
112  fMinuit->AddCommand("MINOS 20");
113  fitter->AddAction(fMinuit);
114 
115 // set usage of buffers only for data objects, not for models
116 // this highly increase speed of evaluations
117  fitter->SetMemoryUsage(1);
118 
119  return fitter;
120 }
121 
122 // store fitter with all supplied objects
123 void StoreFitter(TGo4Fitter* fitter)
124 {
125  TFile* f = TFile::Open("Example7.root","recreate");
126  if (f!=0) fitter->Write("Fitter");
127  delete f;
128 }
129 
130 // read fitter from file
132 {
133  TFile* f = TFile::Open("Example7.root");
134  if (!f) return 0;
135  TGo4Fitter* fitter = 0;
136  f->GetObject("Fitter", fitter);
137  delete f;
138  return fitter;
139 }
140 
141 void Example7()
142 {
143 // create fitter
144  TGo4Fitter* fitter = BuildFitter();
145 
146 // store empty fitter to file and restore again
147  StoreFitter(fitter);
148  delete fitter;
149 
150  fitter = RestoreFitter();
151 
152 // construct axis transformation object and set it for both data object
153 // slot of both data object was connected
154  fitter->SetObject(ConstructTrans(), kTRUE);
155 
156 // assign histograms to fitter with ownership flag
157  fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
158  fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
159 
160 // do fit
161  fitter->DoActions();
162 
163 // store fitter to file and destroy it
164  StoreFitter(fitter);
165 
166  delete fitter;
167 
168 
169 // restore fitter from file
170 // logically this is independent part and can be placed anywhere, just in another program
171  fitter = RestoreFitter();
172 
173 // show results of fitting
174  fitter->Print("Pars");
175  fitter->Draw("#data1,Gauss1,Gauss2");
176  fitter->Draw("#data2,Gauss1,Gauss2,Gauss3,Gauss4");
177 
178  delete fitter;
179 }
virtual void Print(Option_t *option) const
Definition: TGo4Fitter.cxx:705
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
Definition: TGo4Fitter.cxx:70
virtual void Draw(Option_t *option)
Definition: TGo4Fitter.cxx:981
void AddAmplEstimation(Int_t NumIters=1)
Definition: TGo4Fitter.cxx:652
Bool_t SetNumberOfTransSlots(Int_t nslots)
Definition: TGo4FitData.cxx:56
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:116
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 DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=0)
void Example7()
Definition: Example7.cxx:141
TGo4Fitter * BuildFitter()
Definition: Example7.cxx:69
TGo4FitAxisTrans * ConstructTrans()
Definition: Example7.cxx:62
TGo4FitModel * AddModel(TGo4FitModel *m)
Definition: TGo4Fitter.cxx:201
TGo4Fitter * RestoreFitter()
Definition: Example7.cxx:131
void SetUseBinScale(Bool_t iUseBinScale)
Definition: TGo4FitData.h:67
void StoreFitter(TGo4Fitter *fitter)
Definition: Example7.cxx:123