GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Example6.cxx
Go to the documentation of this file.
1 // $Id: Example6.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 N6
15  Simultanious fit of two histogram
16 */
17 
18 #ifndef __CINT__
19 
20 #include "TH1.h"
21 #include "TFile.h"
22 #include "TApplication.h"
23 #include "TCollection.h"
24 
25 #include "TGo4Fitter.h"
26 #include "TGo4FitDataHistogram.h"
27 #include "TGo4FitModelPolynom.h"
28 #include "TGo4FitModelGauss1.h"
29 #include "TGo4FitAxisTrans.h"
30 #include "TGo4FitLinearTrans.h"
31 #include "TGo4FitterConfig.h"
32 
33 void Example6();
34 
35 int main(int argc, char **argv)
36 {
37  TApplication theApp("Application", 0, 0);
38 
39  Example6();
40 
41  theApp.Run();
42 
43  return 0;
44 }
45 
46 #endif
47 
48 // routine to read histogram from examples file
49 TH1D* GetHistogram(const char* HistogramName)
50 {
51  TFile *f1 = TFile::Open("histograms.root");
52  if (f1==0) return 0;
53  TH1D *histo = 0;
54  f1->GetObject(HistogramName, histo);
55  if (histo) histo->SetDirectory(0);
56  return histo;
57 }
58 
59 // construct transformation object, which recalculate bin numbers to new scale values
60 // here simple linear transformation is used
62 {
63  TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
64  trans->SetCoefByRange(3800,0.,3.8);
65  return trans;
66 }
67 
69 {
70 // create fitter and select function to fit
71  TGo4Fitter *fitter = new TGo4Fitter("Fitter",TGo4Fitter::ff_ML_Poisson, kFALSE);
72 
73 // create object to fit for first histogram, but specify histogram later
74  TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
75  data1->SetUseBinScale(kTRUE);
76  data1->SetRange(0,2.2,2.9);
77  fitter->AddData(data1);
78 
79 // create four components for model of the first histogram
80  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
81  fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
82  fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss1_1",2.553,0.015) );
83  fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss2_1",2.672,0.015) );
84 
85 // create object to fit for second histogram, but specify histogram later
86  TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
87  data2->SetUseBinScale(kTRUE);
88  data2->SetRange(0,2.2,2.9);
89  fitter->AddData(data2);
90 
91 // create six components for model of the first histogram
92  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
93  fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
94  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss1_2",2.553,0.015) );
95  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss2_2",2.672,0.015) );
96  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3_2",2.597,0.014) );
97  fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4_2",2.717,0.014) );
98 
99 // specify init value and dependency between parameters
100  TGo4FitterConfig* config = new TGo4FitterConfig("Config","fitter configuration");
101  config->SetParInit("Pol1_0.Ampl",40);
102  config->SetParInit("Pol1_1.Ampl",-0.01);
103  config->SetParInit("Pol2_0.Ampl",500);
104  config->SetParInit("Pol2_1.Ampl",-0.1);
105  config->SetParDepend("Gauss1_2.Pos","Gauss1_1.Pos");
106  config->SetParDepend("Gauss1_2.Width","Gauss1_1.Width");
107  config->SetParDepend("Gauss2_2.Pos","Gauss2_1.Pos");
108  config->SetParDepend("Gauss2_2.Width","Gauss2_1.Width");
109  fitter->AddAction(config);
110 
111 // add standard actions to fitter
112  fitter->AddStandardActions();
113 
114 // set usage of buffers only for data objects, not for models
115 // this highly increase speed of evaluations
116  fitter->SetMemoryUsage(1);
117 
118  return fitter;
119 }
120 
121 // store fitter with all supplied objects
122 void StoreFitter(TGo4Fitter* fitter)
123 {
124  TFile* f = TFile::Open("Example6.root","recreate");
125  if (f!=0) fitter->Write("Fitter");
126  delete f;
127 }
128 
129 // read fitter from file
131 {
132  TFile* f = TFile::Open("Example6.root");
133  if (!f) return 0;
134  TGo4Fitter* fitter = 0;
135  f->GetObject("Fitter", fitter);
136  delete f;
137  return fitter;
138 }
139 
140 void Example6()
141 {
142 // create fitter
143  TGo4Fitter* fitter = BuildFitter();
144 
145 // construct axis transformation object and set it for both data object, first will be owner
146  TGo4FitAxisTrans* trans = ConstructTrans();
147  fitter->FindData("data1")->AddAxisTrans(trans, kTRUE);
148  fitter->FindData("data2")->AddAxisTrans(trans, kFALSE);
149 
150 // assign histograms to fitter with ownership flag
151  fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
152  fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
153 
154 // do fit
155  fitter->DoActions();
156 
157 // store fitter to file and destroy it
158  StoreFitter(fitter);
159  delete fitter;
160 
161 
162 // restore fitter from file
163 // logically this is independent part and can be placed anywhere, just in another program
164  fitter = RestoreFitter();
165 
166 // show results of fitting
167  fitter->PrintPars();
168  fitter->Draw("#data1,Gauss1_1,Gauss2_1");
169  fitter->Draw("#data2,Gauss1_2,Gauss2_2,Gauss3_2,Gauss4_2");
170 
171  delete fitter;
172 }
TGo4FitSlot * SetObject(TObject *obj, Bool_t iOwned=kFALSE)
void SetMemoryUsage(Int_t iMemoryUsage)
Definition: TGo4Fitter.cxx:70
int main(int argc, char **argv)
Definition: Example6.cxx:35
virtual void Draw(Option_t *option)
Definition: TGo4Fitter.cxx:981
TH1D * GetHistogram(const char *HistogramName)
Definition: Example6.cxx:49
void AddAction(TGo4FitterAction *Action)
void SetParDepend(const char *FullName, const char *iExpression)
TGo4FitData * AddData(TGo4FitData *d)
Definition: TGo4Fitter.cxx:116
void SetCoefByRange(Int_t nbins, Double_t y1, Double_t y2)
void SetRange(Int_t naxis, Double_t min, Double_t max)
void DoActions(Bool_t AllowFitterChange=kFALSE, TObjArray *Actions=0)
void SetParInit(const char *FullName, Double_t iValue)
void Example6()
Definition: Example6.cxx:140
TGo4FitAxisTrans * ConstructTrans()
Definition: Example6.cxx:61
TGo4Fitter * BuildFitter()
Definition: Example6.cxx:68
TGo4FitModel * AddModel(TGo4FitModel *m)
Definition: TGo4Fitter.cxx:201
void SetUseBinScale(Bool_t iUseBinScale)
Definition: TGo4FitData.h:67
void PrintPars() const
void AddAxisTrans(TGo4FitAxisTrans *Trans, Bool_t TransOwned=kFALSE)
Definition: TGo4FitData.cxx:92
void StoreFitter(TGo4Fitter *fitter)
Definition: Example6.cxx:122
TGo4FitData * FindData(const char *DataName)
Definition: TGo4Fitter.cxx:111
TGo4Fitter * RestoreFitter()
Definition: Example6.cxx:130
void AddStandardActions()
Definition: TGo4Fitter.cxx:657