Skip to content
Snippets Groups Projects
AbsLh.cc 3.39 KiB
Newer Older
Bertram Kopf's avatar
Bertram Kopf committed
// AbsLh class definition file. -*- C++ -*-
// Copyright 2012 Bertram Kopf

Bertram Kopf's avatar
Bertram Kopf committed
#include <getopt.h>
#include <fstream>
#include <string>
Bertram Kopf's avatar
Bertram Kopf committed

#include "PwaUtils/AbsLh.hh"
#include "qft++/relativistic-quantum-mechanics/Utils.hh"
#include "ErrLogger/ErrLogger.hh"

Bertram Kopf's avatar
Bertram Kopf committed
#ifdef _OPENMP
#include <omp.h>
#endif

Bertram Kopf's avatar
Bertram Kopf committed
AbsLh::AbsLh(boost::shared_ptr<const EvtDataBaseList> theEvtList) :
  AbsParamHandler()
  ,_evtListPtr(theEvtList)
Bertram Kopf's avatar
Bertram Kopf committed
  ,_calcCounter(0)
Bertram Kopf's avatar
Bertram Kopf committed
{
  _evtDataVec=_evtListPtr->getDataVecs();
  _evtMCVec=_evtListPtr->getMcVecs();
}

AbsLh::AbsLh(boost::shared_ptr<AbsLh> theAbsLhPtr):
  AbsParamHandler()
  ,_evtListPtr(theAbsLhPtr->getEventList())
Bertram Kopf's avatar
Bertram Kopf committed
  ,_calcCounter(0)
Bertram Kopf's avatar
Bertram Kopf committed
{
  _evtDataVec=_evtListPtr->getDataVecs();
  _evtMCVec=_evtListPtr->getMcVecs();
}

AbsLh::~AbsLh()
{
}

double AbsLh::calcLogLh(fitParams& theParamVal){
Bertram Kopf's avatar
Bertram Kopf committed
  _calcCounter++;
  if (_cacheAmps && _calcCounter>1) checkRecalculation(theParamVal); 
Bertram Kopf's avatar
Bertram Kopf committed
  updateFitParams(theParamVal);
  
Bertram Kopf's avatar
Bertram Kopf committed
  double logLH=0.;
  double logLH_data=0.;
  double weightSum=0.;
Bertram Kopf's avatar
Bertram Kopf committed
  double LH_mc=0.;
Bertram Kopf's avatar
Bertram Kopf committed
#ifdef _OPENMP
Bertram Kopf's avatar
Bertram Kopf committed
#pragma omp parallel for reduction(+ : logLH_data, weightSum)
Bertram Kopf's avatar
Bertram Kopf committed
#endif
  for (unsigned int i=0; i<_evtDataVec.size(); ++i){
    EvtData* currentEvtData=_evtDataVec[i];
    double intensity=calcEvtIntensity(currentEvtData, theParamVal);
Bertram Kopf's avatar
Bertram Kopf committed

Bertram Kopf's avatar
Bertram Kopf committed
    logLH_data+=(currentEvtData->evtWeight)*log(intensity);
Bertram Kopf's avatar
Bertram Kopf committed
    weightSum+= currentEvtData->evtWeight;
  }
Bertram Kopf's avatar
Bertram Kopf committed

Bertram Kopf's avatar
Bertram Kopf committed
#ifdef _OPENMP
Bertram Kopf's avatar
Bertram Kopf committed
#pragma omp parallel for reduction(+ : LH_mc) 
Bertram Kopf's avatar
Bertram Kopf committed
#endif
  for (unsigned int i=0; i<_evtMCVec.size(); ++i){
    EvtData* currentEvtData=_evtMCVec[i];
    double intensity=calcEvtIntensity(currentEvtData, theParamVal);
Bertram Kopf's avatar
Bertram Kopf committed
    LH_mc+=intensity;
Bertram Kopf's avatar
Bertram Kopf committed
  }


  double logLH_mc_Norm=0.;  
  if (LH_mc>0.) logLH_mc_Norm=log(LH_mc/_evtMCVec.size());
  logLH=0.5*weightSum *(LH_mc/_evtMCVec.size()-1.)*(LH_mc/_evtMCVec.size()-1.)
    -logLH_data
    +weightSum*logLH_mc_Norm;
Bertram Kopf's avatar
Bertram Kopf committed
  
  Info << "current LH = " << std::setprecision(10) << logLH << endmsg;
Bertram Kopf's avatar
Bertram Kopf committed
  return logLH;
  
Bertram Kopf's avatar
Bertram Kopf committed
}

void AbsLh::setHyps( const std::map<const std::string, bool>& theMap, bool& theHyp, std::string& theKey){

  std::map<const std::string, bool>::const_iterator iter= theMap.find(theKey);
  
  if (iter !=theMap.end()){
    theHyp= iter->second;
    DebugMsg<< "hypothesis " << iter->first << "\t" << theHyp <<endmsg;
    _hypMap[iter->first]= iter->second;
  }
  else{
    Alert << theKey << " does not exist!!!" <<endmsg;
    exit(0);
  }
}
Bertram Kopf's avatar
Bertram Kopf committed

void AbsLh::getDefaultParams(fitParams& fitVal, fitParams& fitErr){ 
Bertram Kopf's avatar
Bertram Kopf committed

  std::vector< boost::shared_ptr<AbsXdecAmp> >::iterator itDecs;
  for(itDecs=_decAmps.begin(); itDecs!=_decAmps.end(); ++itDecs){
    (*itDecs)->getDefaultParams(fitVal, fitErr);
  }
void AbsLh::cacheAmplitudes(){
  _cacheAmps=true;
  std::vector< boost::shared_ptr<AbsXdecAmp> >::iterator it;
  for (it=_decAmps.begin(); it!=_decAmps.end(); ++it){
    (*it)->cacheAmplitudes();
void AbsLh::updateFitParams(fitParams& theParamVal){
Bertram Kopf's avatar
Bertram Kopf committed
#ifdef _OPENMP
#pragma omp parallel for  
#endif
  for(unsigned int i=0; i<_decAmps.size(); ++i){
    _decAmps[i]->updateFitParams(theParamVal);
bool AbsLh::checkRecalculation(fitParams& theParamVal){
  bool result=true;
Bertram Kopf's avatar
Bertram Kopf committed
#ifdef _OPENMP
#pragma omp parallel for  
#endif
  for(unsigned int i=0; i<_decAmps.size(); ++i){
    bool currentResult=_decAmps[i]->checkRecalculation(theParamVal);
#ifdef _OPENMP
#pragma omp critical (globalRecalcCheck)
    {
#endif 
    if(!currentResult) result=false;
#ifdef _OPENMP
    }
#endif 
Bertram Kopf's avatar
Bertram Kopf committed
  }
  return result;
Bertram Kopf's avatar
Bertram Kopf committed
}