RooRealConstant.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooRealConstant.cxx 28259 2009-04-16 16:21:16Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // BEGIN_HTML
00020 // RooRealConstant provides static functions to create and keep track
00021 // of RooRealVar constants. Instead of creating such constants by
00022 // hand (e.g. RooRealVar one("one","one",1)), simply use
00023 // <pre>
00024 //  RooRealConstant::value(1.0)
00025 // </pre>
00026 // whenever a reference to RooRealVar with constant value 1.0 is needed.
00027 // RooRealConstant keeps an internal database of previously created
00028 // RooRealVar objects and will recycle them as appropriate.
00029 // END_HTML
00030 //
00031 
00032 #include "RooFit.h"
00033 
00034 #include <math.h>
00035 #include <sstream>
00036 #include "RooRealConstant.h"
00037 #include "RooRealConstant.h"
00038 #include "RooConstVar.h"
00039 #include "RooArgList.h"
00040 #include "RooSentinel.h"
00041 
00042 ClassImp(RooRealConstant)
00043 ;
00044 
00045 
00046 RooArgList* RooRealConstant::_constDB = 0;
00047 TIterator* RooRealConstant::_constDBIter = 0;
00048 
00049 
00050 
00051 //_____________________________________________________________________________
00052 void RooRealConstant::cleanup() 
00053 {
00054   // Cleanup function register with RooSentinel for cleanup in atexit()
00055   if (_constDB) {
00056     delete _constDB ;
00057     delete _constDBIter ;
00058     _constDB = 0 ;
00059   }
00060 }
00061 
00062 
00063 
00064 //_____________________________________________________________________________
00065 RooConstVar& RooRealConstant::value(Double_t value) 
00066 {
00067   // Return a constant value object with given value.
00068   // Return previously created object if avaliable,
00069   // otherwise create a new one on the fly.
00070   
00071   // Lookup existing constant
00072   init() ;
00073   RooConstVar* var ;
00074   while((var=(RooConstVar*)_constDBIter->Next())) {
00075     if (var->getVal()==value) return *var ;
00076   }
00077 
00078   // Create new constant
00079   std::ostringstream s ;
00080   s << value ;  
00081 
00082   var = new RooConstVar(s.str().c_str(),s.str().c_str(),value) ;
00083   var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
00084   _constDB->addOwned(*var) ;
00085 
00086   return *var ;
00087 }
00088 
00089 
00090 
00091 //_____________________________________________________________________________
00092 void RooRealConstant::init() 
00093 {
00094   // One-time initialization of constants database
00095 
00096   if (!_constDB) {
00097     _constDB = new RooArgList("RooRealVar Constants Database") ;
00098     _constDBIter = _constDB->createIterator() ;
00099     RooSentinel::activate() ;
00100   } else {
00101     _constDBIter->Reset() ;
00102   }
00103 }

Generated on Tue Jul 5 15:07:09 2011 for ROOT_528-00b_version by  doxygen 1.5.1