Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4FitExample/Example6.cxx

Go to the documentation of this file.
00001 //---------------------------------------------------------------
00002 //        Go4 Release Package v2.10-5 (build 21005) 
00003 //                      03-Nov-2005
00004 //---------------------------------------------------------------
00005 //       The GSI Online Offline Object Oriented (Go4) Project
00006 //       Experiment Data Processing at DVEE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 /* Go4Fit Example N6
00017    Simultanious fit of two histogram
00018 */
00019 
00020 #ifndef __CINT__
00021 
00022 #include "TH1.h"
00023 #include "TFile.h"
00024 #include "TApplication.h"
00025 
00026 #include "../Go4Fit/TGo4Fitter.h"
00027 #include "../Go4Fit/TGo4FitDataHistogram.h"
00028 #include "../Go4Fit/TGo4FitModelPolynom.h"
00029 #include "../Go4Fit/TGo4FitModelGauss1.h"
00030 #include "../Go4Fit/TGo4FitAxisTrans.h"
00031 #include "../Go4Fit/TGo4FitLinearTrans.h"
00032 #include "TCollection.h"
00033 
00034 void Example6();
00035 
00036 int main(int argc, char **argv)
00037 {
00038    TApplication theApp("Application", 0, 0);
00039 
00040    Example6();
00041 
00042    theApp.Run();
00043 
00044    return 0;
00045 }
00046 
00047 #endif
00048 
00049 // routine to read histogram from examples file
00050 TH1D* GetHistogram(const char* HistogramName) {
00051    TFile f1("histograms.root");
00052    TH1D* histo = (TH1D*) f1.Get(HistogramName);
00053    histo->SetDirectory(0);
00054    return histo;
00055 }
00056 
00057 // constrcut transformation object, which recalculate bin numbers to new scale values
00058 // here simple linear transformation  is used
00059 TGo4FitAxisTrans* ConstructTrans() {
00060    TGo4FitLinearTrans* trans = new TGo4FitLinearTrans("trans","linear axis transformation");
00061    trans->SetCoefByRange(3800,0.,3.8);
00062    return trans;
00063 }
00064 
00065 TGo4Fitter* BuildFitter() {
00066 // create fitter and select function to fit
00067    TGo4Fitter *fitter = new TGo4Fitter("Fitter",TGo4Fitter::ff_ML_Poisson, kFALSE);
00068 
00069 // create object to fit for first histogram, but specify histogram later
00070    TGo4FitDataHistogram *data1 = new TGo4FitDataHistogram("data1",0);
00071    data1->SetUseBinScale(kTRUE);
00072    data1->SetRange(0,2.2,2.9);
00073    fitter->AddData(data1);
00074 
00075 // create four components for model of the first histogram
00076    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_0",0.) );
00077    fitter->AddModel( "data1", new TGo4FitModelPolynom("Pol1_1",1.) );
00078    fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss1_1",2.553,0.015) );
00079    fitter->AddModel( "data1", new TGo4FitModelGauss1("Gauss2_1",2.672,0.015) );
00080 
00081 // create object to fit for second histogram, but specify histogram later
00082    TGo4FitDataHistogram *data2 = new TGo4FitDataHistogram("data2",0);
00083    data2->SetUseBinScale(kTRUE);
00084    data2->SetRange(0,2.2,2.9);
00085    fitter->AddData(data2);
00086 
00087 // create six components for model of the first histogram
00088    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_0",0.) );
00089    fitter->AddModel( "data2", new TGo4FitModelPolynom("Pol2_1",1.) );
00090    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss1_2",2.553,0.015) );
00091    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss2_2",2.672,0.015) );
00092    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss3_2",2.597,0.014) );
00093    fitter->AddModel( "data2", new TGo4FitModelGauss1("Gauss4_2",2.717,0.014) );
00094 
00095 // specify init value and dependency between parameters
00096    TGo4FitterConfig* config = new TGo4FitterConfig("Config","fitter configuration");
00097    config->SetParInit("Pol1_0.Ampl",40);
00098    config->SetParInit("Pol1_1.Ampl",-0.01);
00099    config->SetParInit("Pol2_0.Ampl",500);
00100    config->SetParInit("Pol2_1.Ampl",-0.1);
00101    config->SetParDepend("Gauss1_2.Pos","Gauss1_1.Pos");
00102    config->SetParDepend("Gauss1_2.Width","Gauss1_1.Width");
00103    config->SetParDepend("Gauss2_2.Pos","Gauss2_1.Pos");
00104    config->SetParDepend("Gauss2_2.Width","Gauss2_1.Width");
00105    fitter->AddAction(config);
00106 
00107 // add standard actions to fitter
00108    fitter->AddStandardActions();
00109 
00110 // set usage of buffers only for data objects, not for models
00111 // this highly increase speed of evaluations
00112    fitter->SetMemoryUsage(1);
00113 
00114    return fitter;
00115 }
00116 
00117 // store fitter with all supplied objects
00118 void StoreFitter(TGo4Fitter* fitter) {
00119     TFile *f = new TFile("Example6.root","recreate");
00120     fitter->Write("Fitter");
00121     delete f;
00122 }
00123 
00124 // read fitter from file
00125 TGo4Fitter* RestoreFitter() {
00126     TFile *f = new TFile("Example6.root");
00127     TGo4Fitter* fitter = (TGo4Fitter*) f->Get("Fitter");
00128     delete f;
00129     return fitter;
00130 }
00131 
00132 void Example6() {
00133 // create fitter
00134    TGo4Fitter* fitter = BuildFitter();
00135 
00136 // construct axis transformation object and set it for both data object, first will be owner
00137    TGo4FitAxisTrans* trans = ConstructTrans();
00138    fitter->FindData("data1")->AddAxisTrans(trans, kTRUE);
00139    fitter->FindData("data2")->AddAxisTrans(trans, kFALSE);
00140 
00141 // assign histograms to fitter with ownership flag
00142    fitter->SetObject("data1", GetHistogram("hDeg120_P_c"), kTRUE);
00143    fitter->SetObject("data2", GetHistogram("hDeg120_CND"), kTRUE);
00144 
00145 // do fit
00146    fitter->DoActions();
00147 
00148 // store fitter to file and destroy it
00149    StoreFitter(fitter);
00150    delete fitter;
00151 
00152 
00153 // restore fitter from file
00154 // logically this is independent part and can be placed anywhere, just in another program
00155    fitter = RestoreFitter();
00156 
00157 // show results of fitting
00158    fitter->PrintPars();
00159    fitter->Draw("#data1,Gauss1_1,Gauss2_1");
00160    fitter->Draw("#data2,Gauss1_2,Gauss2_2,Gauss3_2,Gauss4_2");
00161 
00162    delete fitter;
00163 }
00164 
00165 
00166 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Tue Nov 8 10:55:57 2005 for Go4-v2.10-5 by doxygen1.2.15