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