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

/Go4Example2Step/TXXXCalibPar.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 #include "TXXXCalibPar.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TMath.h"
00021 #include "TH1.h"
00022 #include "TGraph.h"
00023 
00024 #include "Go4Analysis/TGo4Analysis.h"
00025 
00026 //***********************************************************
00027 TXXXCalibPar::TXXXCalibPar()
00028 : TGo4Parameter("XXXParameter"),
00029 fbRecalibrate(kFALSE),fbReadDatabase(kFALSE),
00030 fxLinesFinder(0),fxCalibrator(0),
00031 fxCalibCurve(0),fxCalibSpectrum(0)
00032 {
00033   fxDatabase="calilines.txt";
00034   Text_t buf[__TEXTMAX__];
00035   for(Int_t ord=0;ord<__POLORDER__;++ord)
00036      {
00037       fdA[ord]=0;
00038      }
00039   for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00040   {
00041      fiLinesChannel[ix]=0;
00042      ffLinesEnergy[ix]=0;
00043      snprintf(buf,__TEXTMAX__,"Defaultline-%d",ix);
00044      fxLinesNames[ix]=buf;
00045   }
00046 
00047 
00048 }
00049 //***********************************************************
00050 TXXXCalibPar::TXXXCalibPar(Text_t * name,
00051                            TH1* spectrum,
00052                            TGraph* curve)
00053 : TGo4Parameter(name),
00054 fbRecalibrate(kFALSE),fbReadDatabase(kFALSE),
00055 fxLinesFinder(0),fxCalibrator(0),
00056 fxCalibCurve(curve),fxCalibSpectrum(spectrum)
00057 {
00058 // Set up fitters:
00059 fxLinesFinder=new TGo4Fitter("Linefinder", TGo4Fitter::ff_least_squares, kTRUE);
00060 fxCalibrator=new TGo4Fitter("Calibrator", TGo4Fitter::ff_least_squares, kTRUE);
00061 if(fxCalibSpectrum)
00062    {
00063       fxLinesFinder->AddH1(__DATANAME__, fxCalibSpectrum, kFALSE);
00064       fxSpectrumName=fxCalibSpectrum->GetName();
00065    }
00066 else
00067    {
00068       fxSpectrumName="Please specify calibration spectrum";
00069    }
00070 if(fxCalibCurve)
00071    {
00072       fxCalibrator->AddGraph(__GRAPHNAME__, fxCalibCurve, kFALSE);
00073       fxGraphName=fxCalibCurve->GetName();
00074    }
00075 else
00076    {
00077       fxSpectrumName="Please specify fit graph name";
00078    }
00079 fxCalibrator->AddPolynomX(__GRAPHNAME__,"A",__POLORDER__-1);
00080 // note that __POLORDER__ is number of polynom parameters here
00081 // i.e. true order of polynom +1
00082 Text_t modname[__TEXTMAX__];
00083 for(Int_t i=0; i<__POLORDER__;++i)
00084         {
00085            fdA[i]=1/(i+1);
00086            snprintf(modname,__TEXTMAX__,"A_%d",i);
00087            TGo4FitModel* mod=fxCalibrator->FindModel(modname);
00088            if(mod)
00089               {
00090                  // for the beginning, disable models beyond order 1:
00091                  if(i>1) mod->ClearAssignmentTo(__GRAPHNAME__);
00092               }
00093            else
00094             cout <<"could not find model "<<modname << endl;
00095         }
00096 
00097 for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00098 {
00099   fiLinesChannel[ix]=0;
00100   ffLinesEnergy[ix]=0;
00101 }
00102   fxDatabase="calilines.txt";
00103   ReadDatabase();
00104 }
00105 //***********************************************************
00106 TXXXCalibPar::~TXXXCalibPar()
00107 {
00108 delete fxLinesFinder;
00109 delete fxCalibrator;
00110 
00111 }
00112 //***********************************************************
00113 
00114 //-----------------------------------------------------------
00115 Int_t TXXXCalibPar::PrintParameter(Text_t * n, Int_t){
00116 
00117 
00118   return 0;
00119 }
00120 //-----------------------------------------------------------
00121 Bool_t TXXXCalibPar::UpdateFrom(TGo4Parameter *pp){
00123 
00124 
00125   if(pp->InheritsFrom("TXXXCalibPar"))
00126   {
00127     TXXXCalibPar * from;
00128     from = (TXXXCalibPar *) pp;
00129     for(Int_t ord=0;ord<__POLORDER__;++ord)
00130      {
00131       fdA[ord]=from->fdA[ord];
00132      }
00133    fbRecalibrate=from->fbRecalibrate;
00134    fbReadDatabase=from->fbReadDatabase;
00135    if(fxLinesFinder) delete fxLinesFinder;
00136    fxLinesFinder=from->fxLinesFinder;
00137    from->fxLinesFinder=0; // adopt lines finder
00138    if(fxCalibrator) delete fxCalibrator;
00139    fxCalibrator=from->fxCalibrator;
00140    from->fxCalibrator=0; // adopt calibration fitter
00141 
00142    // note: graph with calibration curve is not copied!
00143 
00144 
00145     for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00146     {
00147        fiLinesChannel[ix]=from->fiLinesChannel[ix];
00148        ffLinesEnergy[ix]=from->ffLinesEnergy[ix];
00149        fxLinesNames[ix]=from->fxLinesNames[ix];
00150        //cout <<"updated line:"<<fxLinesNames[ix].Data() << endl;
00151     }
00152    cout <<"Updated Parameter:" << endl;
00153    //PrintParameter(0,0);
00154    // get references to graph and histogram from analysis:
00155    // note that updatefrom is only used on analysis side here!
00156    fxCalibCurve=dynamic_cast<TGraph*>(TGo4Analysis::Instance()->GetObject(fxGraphName.Data())) ;
00157 
00158    if(fxCalibCurve==0)
00159       {
00160          cout <<"Graph "<<fxGraphName.Data() << " not existing in analysis"<< endl;
00161      }
00162    else
00163       {
00164           cout <<"Updated graph pointer ref to "<<fxCalibCurve << endl;
00165       }
00166 
00167 
00168 
00169    // now reread database if desired:
00170    if(fbReadDatabase)
00171       {
00172           cout <<"Reread database" << endl;
00173           ReadDatabase();
00174       }
00175 
00176    if(fbRecalibrate)
00177    {
00178       cout <<"Recalibrating..." << endl;
00179       // first we get the channels from the linesfinder fitter:
00180 
00181         for(Int_t i=0; i<__LINESNUMBER__;++i)
00182         {
00183            const Text_t* linename=fxLinesNames[i];
00184            TGo4FitModel* mod=fxLinesFinder->FindModel(linename);
00185            if(mod)
00186               {
00187                  // check here if component is active or not
00188                  if(mod->IsAssignTo(__DATANAME__))
00189                     fiLinesChannel[i]=(Int_t) mod->GetParValue("Pos");
00190                  else
00191                     fiLinesChannel[i]=0; // mark not active lines
00192               }
00193            else
00194               {
00195                   //cout <<"could not find model "<<linename << endl;
00196               }
00197         }
00198 
00199 
00200       // setup calibration graph with the new channel coords:
00201       if(fxCalibCurve)
00202          {
00203             fxCalibCurve->Set(0);
00204             Int_t point=0;
00205             for(Int_t ix=0;ix<__LINESNUMBER__;++ix)
00206                {
00207                if(fiLinesChannel[ix]!=0)
00208                   {
00209                      fxCalibCurve->SetPoint(point,
00210                                     fiLinesChannel[ix],
00211                                     ffLinesEnergy[ix]);
00212                      // we only fit active lines
00213                      ++point;
00214                   }
00215               } // for
00216         // now perform fit of calibration graph:
00217         fxCalibrator->SetObject(__GRAPHNAME__, fxCalibCurve, kFALSE);
00218         fxCalibrator->DoActions();
00219         fxCalibrator->PrintLines();
00220         // finally, copy results of calibration to the parameter fields:
00221         Text_t modname[__TEXTMAX__];
00222         for(Int_t i=0; i<__POLORDER__;++i)
00223         {
00224            snprintf(modname,__TEXTMAX__,"A_%d",i);
00225            TGo4FitModel* mod=fxCalibrator->FindModel(modname);
00226            if(mod)
00227               {
00228                  // check here if component is active or not
00229                  if(mod->IsAssignTo(__GRAPHNAME__))
00230                     fdA[i]=mod->GetParValue("Ampl");
00231                  else
00232                     fdA[i]=0;
00233               }
00234            else
00235             ;//cout <<"could not find model "<<modname << endl;
00236 
00237         }
00238 
00239          }
00240       else
00241          {
00242           TGo4Log::Error("Calibration parameter %s has no TGraph!",
00243              GetName());
00244          }
00245 
00246    }
00247 
00248   }
00249 else
00250      {
00251      cout << "Wrong parameter object: " << pp->ClassName() << endl;
00252 
00253      }
00254 return kTRUE;
00255 }
00256 
00257 void TXXXCalibPar::ReadDatabase()
00258 {
00259 // read energies from file:
00260 Text_t nextline[__TEXTMAX__];
00261 Text_t buf[__TEXTMAX__];
00262 std::ifstream database(fxDatabase.Data());
00263 if(database==0)
00264    {
00265       TGo4Log::Error("Open error of calibration energy file %s",
00266          fxDatabase.Data());
00267    }
00268 else
00269    {
00270       Int_t ix=0;
00271       while(1){
00272           do{
00273               database.getline(nextline,__TEXTMAX__,'\n' ); // read whole line
00274               if(database.eof() || !database.good())
00275                 {
00276                     break;
00277                 }
00278               //cout <<"read line:"<<nextline << endl;
00279          }while(strstr(nextline,"#") || strstr(nextline,"!") ); // skip any comments
00280       if(database.eof() || !database.good()) break;
00281       sscanf(nextline,"%s %f %d",buf,
00282              &ffLinesEnergy[ix],
00283              &fiLinesChannel[ix]);
00284       fxLinesNames[ix]=buf;
00285 //      cout <<"\tname:"<<fxLinesNames[ix].Data() << endl;
00286 //      cout <<"\te:"<<ffLinesEnergy[ix] << endl;
00287 //      cout <<"\tch:"<<fiLinesChannel[ix] << endl;
00288 
00289       fxLinesFinder->AddGauss1(__DATANAME__,
00290                                  fxLinesNames[ix].Data(),
00291                                  fiLinesChannel[ix],
00292                                  TMath::Sqrt((Long_t) fiLinesChannel[ix]));
00293       ix++;
00294       } // while(1)
00295       //cout <<"scanned lines:"<<ix << endl;
00296 
00297    } // if (database==0)
00298 
00299 
00300 }
00301 
00302 
00303 
00304 Double_t TXXXCalibPar::Energy(Int_t channel)
00305 {
00306   Double_t result=0;
00307   for(Int_t ord=0;ord<__POLORDER__; ++ord)
00308     {
00309          result+=fdA[ord]*TMath::Power(channel,ord);
00310     }
00311   return result;
00312 }
00313 
00314 
00315 
00316 ClassImp(TXXXCalibPar)
00317 
00318 
00319 
00320 
00321 //----------------------------END OF GO4 SOURCE FILE ---------------------

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