82 void add(RooRealMPFE& mpfe) ;
83 void remove(RooRealMPFE& mpfe) ;
88RooMPSentinel& RooMPSentinel::instance() {
89 static RooMPSentinel inst;
94using std::string, std::ostringstream, std::list;
103RooRealMPFE::RooRealMPFE(
const char *
name,
const char *title,
RooAbsReal& arg,
bool calcInline) :
106 _arg(
"arg",
"arg",this,arg),
107 _vars(
"vars",
"vars",this),
108 _calcInProgress(false),
109 _verboseClient(false),
110 _verboseServer(false),
111 _inlineMode(calcInline),
112 _remoteEvalErrorLoggingState(
RooAbsReal::PrintErrors),
114 _updateMaster(nullptr),
115 _retrieveDispatched(false), _evalCarry(0.)
121 RooMPSentinel::instance().add(*
this) ;
131RooRealMPFE::RooRealMPFE(
const RooRealMPFE& other,
const char*
name) :
134 _arg(
"arg",this,other._arg),
135 _vars(
"vars",this,other._vars),
136 _calcInProgress(false),
137 _verboseClient(other._verboseClient),
138 _verboseServer(other._verboseServer),
139 _inlineMode(other._inlineMode),
140 _forceCalc(other._forceCalc),
141 _remoteEvalErrorLoggingState(other._remoteEvalErrorLoggingState),
143 _updateMaster(nullptr),
144 _retrieveDispatched(false), _evalCarry(other._evalCarry)
147 RooMPSentinel::instance().add(*
this) ;
155RooRealMPFE::~RooRealMPFE()
157 if (_state==Client) standby();
158 RooMPSentinel::instance().remove(*
this);
166void RooRealMPFE::initVars()
170 _saveVars.removeAll() ;
173 auto vars = _arg->getParameters(
RooArgSet());
179 _saveVars.addClone(varList) ;
180 _valueChanged.resize(_vars.size()) ;
181 _constChanged.resize(_vars.size()) ;
187double RooRealMPFE::getCarry()
const
190 RooAbsTestStatistic* tmp =
dynamic_cast<RooAbsTestStatistic*
>(_arg.absArg());
191 if (tmp)
return tmp->getCarry();
202void RooRealMPFE::initialize()
213 clearEvalErrorLog() ;
215 _pipe =
new BidirMMapPipe();
217 if (_pipe->isChild()) {
225 GetName() <<
") server process terminating" << std::endl ;
227 delete _arg.absArg();
232 if (_verboseClient) {
233 ccoutD(
Minimization) <<
"RooRealMPFE::initialize(" << GetName() <<
") successfully forked server process "
234 << _pipe->pidOtherEnd() << std::endl;
237 _calcInProgress = false ;
248void RooRealMPFE::serverLoop()
259 clearEvalErrorLog() ;
261 while(*_pipe && !_pipe->eof()) {
263 if (Terminate == msg) {
264 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
265 <<
") IPC fromClient> Terminate" << std::endl;
267 *_pipe << msg << BidirMMapPipe::flush;
274 *_pipe >> idx >> value >> isConst;
275 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
276 <<
") IPC fromClient> SendReal [" << idx <<
"]=" << value << std::endl ;
287 *_pipe >> idx >> index;
288 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
289 <<
") IPC fromClient> SendCat [" << idx <<
"]=" << index << std::endl ;
290 (
static_cast<RooCategory*
>(_vars.at(idx)))->setIndex(index) ;
295 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
296 <<
") IPC fromClient> Calculate" << std::endl ;
300 case CalculateNoOffset:
301 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
302 <<
") IPC fromClient> Calculate" << std::endl ;
311 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
312 <<
") IPC fromClient> Retrieve" << std::endl ;
314 numErrors = numEvalErrors();
315 *_pipe << msg << _value << getCarry() << numErrors;
317 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
318 <<
") IPC toClient> ReturnValue " << _value <<
" NumError " << numErrors << std::endl ;
322 std::string objidstr;
326 oss2 <<
"PID" <<
gSystem->GetPid() <<
"/";
327 printStream(oss2,
kName|kClassName|kArgs,kInline);
328 objidstr = oss2.str();
330 std::map<const RooAbsArg*,std::pair<string,list<EvalError> > >::const_iterator iter = evalErrorIter();
332 for (
int i = 0; i < numEvalErrorItems(); ++i) {
333 list<EvalError>::const_iterator iter2 = iter->second.second.begin();
334 for (; iter->second.second.end() != iter2; ++iter2) {
336 *_pipe << ptr << iter2->_msg << iter2->_srvval << objidstr;
337 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
338 <<
") IPC toClient> sending error log Arg " << iter->first <<
" Msg " << iter2->_msg << std::endl ;
347 *_pipe << BidirMMapPipe::flush;
355 *_pipe >> code >> doTrack;
356 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
357 <<
") IPC fromClient> ConstOpt " << code <<
" doTrack = " << (doTrack?
"T":
"F") << std::endl ;
366 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
367 <<
") IPC fromClient> Verbose " << (flag?1:0) << std::endl ;
368 _verboseServer = flag ;
377 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
378 <<
") IPC fromClient> ApplyNLLW2 " << (flag?1:0) << std::endl ;
389 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
390 <<
") IPC fromClient> EnableOffset " << (flag?1:0) << std::endl ;
393 ((
RooAbsReal&)_arg.arg()).enableOffsetting(flag) ;
403 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
404 <<
") IPC fromClient> LogEvalError flag = " << flag2 << std::endl ;
410 if (_verboseServer) std::cout <<
"RooRealMPFE::serverLoop(" << GetName()
411 <<
") IPC fromClient> Unknown message (code = " << msg <<
")" << std::endl ;
427void RooRealMPFE::calculate()
const
431 if (_state==Initialize) {
433 const_cast<RooRealMPFE*
>(
this)->
initialize() ;
437 if (_state==Inline) {
445 if (_state==Client) {
452 for (std::size_t j=0 ; j<_vars.size() ; j++) {
454 saveVar = _saveVars.at(j);
459 if (!_updateMaster) {
462 _valueChanged[i] = valChanged ;
463 _constChanged[i] = constChanged ;
465 valChanged = _updateMaster->_valueChanged[i] ;
466 constChanged = _updateMaster->_constChanged[i] ;
469 if ( valChanged || constChanged || _forceCalc) {
471 if (_verboseClient) std::cout <<
"RooRealMPFE::calculate(" << GetName()
472 <<
") variable " << _vars.at(i)->GetName() <<
" changed" << std::endl ;
481 double val = (
static_cast<RooAbsReal*
>(var))->getVal() ;
483 *_pipe << msg << i << val << isC;
485 if (_verboseServer) std::cout <<
"RooRealMPFE::calculate(" << GetName()
486 <<
") IPC toServer> SendReal [" << i <<
"]=" << val << (isC?
" (Constant)":
"") << std::endl ;
490 *_pipe << msg << i << idx;
491 if (_verboseServer) std::cout <<
"RooRealMPFE::calculate(" << GetName()
492 <<
") IPC toServer> SendCat [" << i <<
"]=" << idx << std::endl ;
498 int msg = hideOffset() ? Calculate : CalculateNoOffset;
500 if (_verboseServer) std::cout <<
"RooRealMPFE::calculate(" << GetName()
501 <<
") IPC toServer> Calculate " << std::endl ;
505 _calcInProgress = true ;
509 *_pipe << msg << BidirMMapPipe::flush;
510 if (_verboseServer) std::cout <<
"RooRealMPFE::evaluate(" << GetName()
511 <<
") IPC toServer> Retrieve " << std::endl ;
512 _retrieveDispatched = true ;
514 }
else if (_state!=Inline) {
515 std::cout <<
"RooRealMPFE::calculate(" << GetName()
516 <<
") ERROR not in Client or Inline mode" << std::endl ;
532double RooRealMPFE::getValV(
const RooArgSet* )
const
535 if (isValueDirty()) {
540 }
else if (_calcInProgress) {
560double RooRealMPFE::evaluate()
const
563 double return_value = 0;
564 if (_state==Inline) {
565 return_value = _arg ;
566 }
else if (_state==Client) {
568 bool needflush =
false;
574 if (evalErrorLoggingMode() != _remoteEvalErrorLoggingState) {
577 *_pipe << msg << flag;
579 _remoteEvalErrorLoggingState = evalErrorLoggingMode() ;
582 if (!_retrieveDispatched) {
586 if (_verboseServer) std::cout <<
"RooRealMPFE::evaluate(" << GetName()
587 <<
") IPC toServer> Retrieve " << std::endl ;
589 if (needflush) *_pipe << BidirMMapPipe::flush;
590 _retrieveDispatched = false ;
595 *_pipe >> msg >> value >> _evalCarry >> numError;
597 if (msg!=ReturnValue) {
598 std::cout <<
"RooRealMPFE::evaluate(" << GetName()
599 <<
") ERROR: unexpected message from server process: " << msg << std::endl ;
602 if (_verboseServer) std::cout <<
"RooRealMPFE::evaluate(" << GetName()
603 <<
") IPC fromServer> ReturnValue " << value << std::endl ;
605 if (_verboseServer) std::cout <<
"RooRealMPFE::evaluate(" << GetName()
606 <<
") IPC fromServer> NumErrors " << numError << std::endl ;
609 char *msgbuf1 =
nullptr;
610 char *msgbuf2 =
nullptr;
611 char *msgbuf3 =
nullptr;
616 *_pipe >> msgbuf1 >> msgbuf2 >> msgbuf3;
617 if (_verboseServer) std::cout <<
"RooRealMPFE::evaluate(" << GetName()
618 <<
") IPC fromServer> retrieving error log Arg " << ptr <<
" Msg " << msgbuf1 << std::endl ;
620 logEvalError(
reinterpret_cast<RooAbsReal*
>(ptr),msgbuf3,msgbuf1,msgbuf2) ;
628 _calcInProgress = false ;
629 return_value = value ;
643void RooRealMPFE::standby()
646 if (_state==Client) {
649 if (_verboseServer) std::cout <<
"RooRealMPFE::standby(" << GetName()
650 <<
") IPC toServer> Terminate " << std::endl;
652 *_pipe << msg << BidirMMapPipe::flush;
656 if (Terminate != msg || 0 != _pipe->close()) {
657 std::cerr <<
"In " << __func__ <<
"(" << __FILE__
", " << __LINE__ <<
658 "): Server shutdown failed." << std::endl;
661 if (_verboseServer) {
662 std::cerr <<
"In " << __func__ <<
"(" << __FILE__
", " <<
663 __LINE__ <<
"): Pipe has already shut down, not sending "
664 "Terminate to server." << std::endl;
683void RooRealMPFE::constOptimizeTestStatistic(ConstOpCode opcode,
bool doAlsoTracking)
686 if (_state==Client) {
690 *_pipe << msg << op << doAlsoTracking;
691 if (_verboseServer) std::cout <<
"RooRealMPFE::constOptimize(" << GetName()
692 <<
") IPC toServer> ConstOpt " << opcode << std::endl ;
698 if (_state==Inline) {
699 ((
RooAbsReal&)_arg.arg()).constOptimizeTestStatistic(opcode,doAlsoTracking) ;
709void RooRealMPFE::setVerbose(
bool clientFlag,
bool serverFlag)
712 if (_state==Client) {
714 *_pipe << msg << serverFlag;
715 if (_verboseServer) std::cout <<
"RooRealMPFE::setVerbose(" << GetName()
716 <<
") IPC toServer> Verbose " << (serverFlag?1:0) << std::endl ;
719 _verboseClient = clientFlag ; _verboseServer = serverFlag ;
727void RooRealMPFE::applyNLLWeightSquared(
bool flag)
730 if (_state==Client) {
731 int msg = ApplyNLLW2 ;
732 *_pipe << msg << flag;
733 if (_verboseServer) std::cout <<
"RooRealMPFE::applyNLLWeightSquared(" << GetName()
734 <<
") IPC toServer> ApplyNLLW2 " << (flag?1:0) << std::endl ;
743void RooRealMPFE::doApplyNLLW2(
bool flag)
745 RooNLLVar*
nll =
dynamic_cast<RooNLLVar*
>(_arg.absArg()) ;
747 nll->applyWeightSquared(flag) ;
756void RooRealMPFE::enableOffsetting(
bool flag)
759 if (_state==Client) {
760 int msg = EnableOffset ;
761 *_pipe << msg << flag;
762 if (_verboseServer) std::cout <<
"RooRealMPFE::enableOffsetting(" << GetName()
763 <<
") IPC toServer> EnableOffset " << (flag?1:0) << std::endl ;
766 ((
RooAbsReal&)_arg.arg()).enableOffsetting(flag) ;
775RooMPSentinel::~RooMPSentinel()
787void RooMPSentinel::add(RooRealMPFE& mpfe)
789 _mpfeSet.add(mpfe,
true) ;
797void RooMPSentinel::remove(RooRealMPFE& mpfe)
799 _mpfeSet.remove(mpfe,
true) ;
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
static Roo_reg_AGKInteg1D instance
double evaluate() const override
int Int_t
Signed integer 4 bytes (int).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Common abstract base class for objects that represent a value and a "shape" in RooFit.
virtual void copyCache(const RooAbsArg *source, bool valueOnly=false, bool setValDirty=true)=0
bool isConstant() const
Check if the "Constant" attribute is set.
virtual bool isIdentical(const RooAbsArg &other, bool assumeSameType=false) const =0
A space to attach TBranches.
void setConstant(bool value=true)
Abstract base class for objects that represent a real value and implements functionality common to al...
static void setHideOffset(bool flag)
static void setEvalErrorLoggingMode(ErrorLoggingMode m)
Set evaluation error logging mode.
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Object to represent discrete states.
Variable that can be changed from the outside.
void setVal(double value) override
Set value of variable to 'value'.
static void callgrind_zero()
Utility function to trigger zeroing of callgrind counters.
RooCmdArg Verbose(bool flag=true)
double nll(double pdf, double weight, int binnedL, int doBinOffset)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
void Initialize(Bool_t useTMVAStyle=kTRUE)