StatWeight.cpp

Go to the documentation of this file.
00001 #include "cafe/StatWeight.hpp"
00002 #include <iostream> 
00003 #include <math.h> 
00004 
00005 using namespace std ;
00006 
00007 namespace cafe {
00008   
00009   StatSelection::StatSelection(const std::string& name, unsigned long events, bool weight) : 
00010     _name(name), _events(events)
00011   {
00012     if (events==0) 
00013       _updated = false ;
00014     else 
00015       _updated = true ;
00016     _weight = weight ;
00017   }
00018 
00019   double StatSelection::err(const StatSelection& selection) const {
00020     if (selection.nevents() < _events ) {
00021       cout << "cafe::StatSelection: error in the events sample order! "
00022            << "Number of events in the preciding selection " << selection.nevents() 
00023            << " less than in the following one " << _events << "!"  
00024            << endl ;
00025       return 0 ;
00026     }   
00027     double eff = ((double)_events) / selection.nevents() ;    
00028     return sqrt(eff*(1-eff)/selection.nevents()) ;
00029   }
00030 
00031   unsigned long StatSelection::addEvent() {    
00032     if (!_updated) { 
00033       _events++; 
00034       _updated = true ;
00035     } 
00036     return _events;
00037   }
00038 
00039 
00040   StatWeight::StatWeight(const std::string& name, bool event_selection) : StatSelection(name, 0, !event_selection), 
00041                                                     _weight_average(1.0),_weight(1.0),
00042                                                     _rms2(0), _weight_old(0), _rms2_old(0),
00043                                                     _weight_average_pos(-1.0),_weight_pos(1.0),
00044                                                     _rms2_pos(0), _weight_old_pos(0), _rms2_old_pos(0),
00045                                                     _weight_average_neg(-1.0),_weight_neg(1.0),
00046                                                     _rms2_neg(0), _weight_old_neg(0), _rms2_old_neg(0)
00047   { }
00048 
00049   void StatWeight::Clear() {
00050     StatSelection::Clear() ;
00051     _weight = 1.0 ;
00052     _weight_old = 0 ;
00053     _weight_pos = 1.0 ;
00054     _weight_old_pos = 0 ;
00055     _weight_neg = 1.0 ;
00056     _weight_old_neg = 0 ;
00057   }
00058 
00059   double StatWeight::applyWeight(double weight, double weight_pos,  double weight_neg ) {
00060 
00061     // negative weights are allowed. weight_pos and weight_neg (sytematics) 
00062     // are still can used only as positive weights
00063 
00064     if (isUpdated() && !isWeight() ) return _weight ;
00065 
00066     if (!isUpdated())  {
00067       _weight_old = _weight_average ;
00068       _rms2_old = _rms2 ;
00069       _weight_old_pos = _weight_average_pos ;
00070       _rms2_old_pos = _rms2_pos ;
00071       _weight_old_neg = _weight_average_neg ;
00072       _rms2_old_neg = _rms2_neg ;
00073     }
00074 
00075     addEvent() ;
00076     _weight *= weight ;
00077     
00078     if (weight_pos >= 0) 
00079       _weight_pos *= weight_pos ;
00080     if (weight_neg >= 0) 
00081       _weight_neg *= weight_neg ;
00082 
00083     if (nevents() == 1) {
00084       _weight_average = _weight ;
00085       if (weight_pos >= 0) 
00086         _weight_average_pos = _weight_pos ;
00087       if (weight_neg >= 0) 
00088         _weight_average_neg = _weight_neg ;
00089       _rms2 = 0 ;
00090       _rms2_pos = 0 ;
00091       _rms2_neg = 0 ;
00092     } else {
00093       _rms2 = ((double) nevents()-2)/(nevents()-1)*_rms2_old + pow(_weight-_weight_old,2)/nevents() ;
00094       if (weight_pos >= 0) 
00095         _rms2_pos = ((double) nevents()-2)/(nevents()-1)*_rms2_old_pos + 
00096           pow(_weight_pos-_weight_old_pos,2)/nevents() ;
00097       if (weight_neg >= 0) 
00098         _rms2_neg = ((double) nevents()-2)/(nevents()-1)*_rms2_old_neg + 
00099           pow(_weight_neg-_weight_old_neg,2)/nevents() ;
00100 
00101       _weight_average = _weight_old*(((double)nevents()-1)/nevents()) + _weight/nevents() ;
00102       if (weight_pos >= 0) 
00103         _weight_average_pos = _weight_old_pos*(((double)nevents()-1)/nevents()) + _weight_pos/nevents() ;
00104       if (weight_neg >= 0) 
00105         _weight_average_neg = _weight_old_neg*(((double)nevents()-1)/nevents()) + _weight_neg/nevents() ;
00106     }
00107 
00108     return _weight ;
00109   }
00110 
00111   double StatWeight::err() const {
00112     if (nevents() <= 1 || _rms2 <= 0) return 0.0 ;
00113     return sqrt(_rms2/nevents()) ;
00114   }
00115 
00116   double StatWeight::err_pos() const {
00117     if (nevents() <= 1 || _rms2_pos <= 0) return 0.0 ;
00118     return sqrt(_rms2_pos/nevents()) ;
00119   }
00120 
00121   double StatWeight::err_neg() const {
00122     if (nevents() <= 1 || _rms2_neg <= 0) return 0.0 ;
00123     return sqrt(_rms2_neg/nevents()) ;
00124   }
00125 
00126 
00127   double StatWeight::rms() const {
00128     return _rms2 >0 ? sqrt(_rms2) : 0.0 ;
00129   }
00130 
00131   double StatWeight::rms_pos() const {
00132     return _rms2_pos >0 ? sqrt(_rms2_pos) : 0.0 ;
00133   }
00134 
00135   double StatWeight::rms_neg() const {
00136     return _rms2_neg >0 ? sqrt(_rms2_neg) : 0.0 ;
00137   }
00138 
00139 }
00140 
00141 ClassImp(cafe::StatSelection);
00142 ClassImp(cafe::StatWeight);

Generated on Thu Apr 3 04:14:23 2008 for CAF by doxygen 1.3.4