68 RooMPSentinel RooRealMPFE::_sentinel ;
71 using namespace RooFit;
73 ClassImp(RooRealMPFE);
82 RooRealMPFE::RooRealMPFE(
const char *name,
const char *title, RooAbsReal& arg, Bool_t calcInline) :
83 RooAbsReal(name,title),
85 _arg(
"arg",
"arg",this,arg),
86 _vars(
"vars",
"vars",this),
87 _calcInProgress(kFALSE),
88 _verboseClient(kFALSE),
89 _verboseServer(kFALSE),
90 _inlineMode(calcInline),
91 _remoteEvalErrorLoggingState(RooAbsReal::PrintErrors),
94 _retrieveDispatched(kFALSE), _evalCarry(0.)
100 _sentinel.add(*
this) ;
110 RooRealMPFE::RooRealMPFE(
const RooRealMPFE& other,
const char* name) :
111 RooAbsReal(other, name),
113 _arg(
"arg",this,other._arg),
114 _vars(
"vars",this,other._vars),
115 _calcInProgress(kFALSE),
116 _verboseClient(other._verboseClient),
117 _verboseServer(other._verboseServer),
118 _inlineMode(other._inlineMode),
119 _forceCalc(other._forceCalc),
120 _remoteEvalErrorLoggingState(other._remoteEvalErrorLoggingState),
123 _retrieveDispatched(kFALSE), _evalCarry(other._evalCarry)
126 _sentinel.add(*
this) ;
134 RooRealMPFE::~RooRealMPFE()
136 if (_state==Client) standby();
137 _sentinel.remove(*
this);
145 void RooRealMPFE::initVars()
149 _saveVars.removeAll() ;
152 RooArgSet* vars = _arg.arg().getParameters(RooArgSet()) ;
154 RooArgList varList(*vars) ;
158 _saveVars.addClone(varList) ;
159 _valueChanged.resize(_vars.getSize()) ;
160 _constChanged.resize(_vars.getSize()) ;
169 Double_t RooRealMPFE::getCarry()
const
172 RooAbsTestStatistic* tmp =
dynamic_cast<RooAbsTestStatistic*
>(_arg.absArg());
173 if (tmp)
return tmp->getCarry();
184 void RooRealMPFE::initialize()
195 clearEvalErrorLog() ;
197 _pipe =
new BidirMMapPipe();
199 if (_pipe->isChild()) {
201 RooTrace::callgrind_zero() ;
206 if (_verboseServer) ccoutD(Minimization) <<
"RooRealMPFE::initialize(" <<
207 GetName() <<
") server process terminating" << endl ;
209 delete _arg.absArg();
214 if (_verboseClient) ccoutD(Minimization) <<
"RooRealMPFE::initialize(" <<
215 GetName() <<
") successfully forked server process " <<
216 _pipe->pidOtherEnd() << endl ;
218 _calcInProgress = kFALSE ;
229 void RooRealMPFE::serverLoop()
234 Int_t idx, index, numErrors ;
238 clearEvalErrorLog() ;
240 while(*_pipe && !_pipe->eof()) {
242 if (Terminate == msg) {
243 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
244 <<
") IPC fromClient> Terminate" << endl;
246 *_pipe << msg << BidirMMapPipe::flush;
253 *_pipe >> idx >> value >> isConst;
254 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
255 <<
") IPC fromClient> SendReal [" << idx <<
"]=" << value << endl ;
256 RooRealVar* rvar = (RooRealVar*)_vars.at(idx) ;
257 rvar->setVal(value) ;
258 if (rvar->isConstant() != isConst) {
259 rvar->setConstant(isConst) ;
266 *_pipe >> idx >> index;
267 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
268 <<
") IPC fromClient> SendCat [" << idx <<
"]=" << index << endl ;
269 ((RooCategory*)_vars.at(idx))->setIndex(index) ;
274 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
275 <<
") IPC fromClient> Calculate" << endl ;
279 case CalculateNoOffset:
280 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
281 <<
") IPC fromClient> Calculate" << endl ;
283 RooAbsReal::setHideOffset(kFALSE) ;
285 RooAbsReal::setHideOffset(kTRUE) ;
290 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
291 <<
") IPC fromClient> Retrieve" << endl ;
293 numErrors = numEvalErrors();
294 *_pipe << msg << _value << getCarry() << numErrors;
296 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
297 <<
") IPC toClient> ReturnValue " << _value <<
" NumError " << numErrors << endl ;
301 std::string objidstr;
305 oss2 <<
"PID" << gSystem->GetPid() <<
"/";
306 printStream(oss2,kName|kClassName|kArgs,kInline);
307 objidstr = oss2.str();
309 std::map<const RooAbsArg*,pair<string,list<EvalError> > >::const_iterator iter = evalErrorIter();
310 const RooAbsArg* ptr = 0;
311 for (
int i = 0; i < numEvalErrorItems(); ++i) {
312 list<EvalError>::const_iterator iter2 = iter->second.second.begin();
313 for (; iter->second.second.end() != iter2; ++iter2) {
315 *_pipe << ptr << iter2->_msg << iter2->_srvval << objidstr;
316 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
317 <<
") IPC toClient> sending error log Arg " << iter->first <<
" Msg " << iter2->_msg << endl ;
326 *_pipe << BidirMMapPipe::flush;
334 *_pipe >> code >> doTrack;
335 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
336 <<
") IPC fromClient> ConstOpt " << code <<
" doTrack = " << (doTrack?
"T":
"F") << endl ;
337 ((RooAbsReal&)_arg.arg()).constOptimizeTestStatistic(static_cast<RooAbsArg::ConstOpCode>(code),doTrack) ;
345 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
346 <<
") IPC fromClient> Verbose " << (flag?1:0) << endl ;
347 _verboseServer = flag ;
356 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
357 <<
") IPC fromClient> ApplyNLLW2 " << (flag?1:0) << endl ;
368 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
369 <<
") IPC fromClient> EnableOffset " << (flag?1:0) << endl ;
372 ((RooAbsReal&)_arg.arg()).enableOffsetting(flag) ;
380 RooAbsReal::ErrorLoggingMode flag2 =
static_cast<RooAbsReal::ErrorLoggingMode
>(iflag2);
381 RooAbsReal::setEvalErrorLoggingMode(flag2) ;
382 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
383 <<
") IPC fromClient> LogEvalError flag = " << flag2 << endl ;
389 if (_verboseServer) cout <<
"RooRealMPFE::serverLoop(" << GetName()
390 <<
") IPC fromClient> Unknown message (code = " << msg <<
")" << endl ;
406 void RooRealMPFE::calculate()
const
410 if (_state==Initialize) {
412 const_cast<RooRealMPFE*
>(
this)->initialize() ;
416 if (_state==Inline) {
424 if (_state==Client) {
427 RooFIter viter = _vars.fwdIterator() ;
428 RooFIter siter = _saveVars.fwdIterator() ;
431 RooAbsArg *var, *saveVar ;
432 while((var = viter.next())) {
433 saveVar = siter.next() ;
436 Bool_t valChanged,constChanged ;
437 if (!_updateMaster) {
438 valChanged = !var->isIdentical(*saveVar,kTRUE) ;
439 constChanged = (var->isConstant() != saveVar->isConstant()) ;
440 _valueChanged[i] = valChanged ;
441 _constChanged[i] = constChanged ;
443 valChanged = _updateMaster->_valueChanged[i] ;
444 constChanged = _updateMaster->_constChanged[i] ;
447 if ( valChanged || constChanged || _forceCalc) {
449 if (_verboseClient) cout <<
"RooRealMPFE::calculate(" << GetName()
450 <<
") variable " << _vars.at(i)->GetName() <<
" changed" << endl ;
452 ((RooRealVar*)saveVar)->setConstant(var->isConstant()) ;
454 saveVar->copyCache(var) ;
457 if (dynamic_cast<RooAbsReal*>(var)) {
459 Double_t val = ((RooAbsReal*)var)->getVal() ;
460 Bool_t isC = var->isConstant() ;
461 *_pipe << msg << i << val << isC;
463 if (_verboseServer) cout <<
"RooRealMPFE::calculate(" << GetName()
464 <<
") IPC toServer> SendReal [" << i <<
"]=" << val << (isC?
" (Constant)":
"") << endl ;
465 }
else if (dynamic_cast<RooAbsCategory*>(var)) {
467 UInt_t idx = ((RooAbsCategory*)var)->getIndex() ;
468 *_pipe << msg << i << idx;
469 if (_verboseServer) cout <<
"RooRealMPFE::calculate(" << GetName()
470 <<
") IPC toServer> SendCat [" << i <<
"]=" << idx << endl ;
476 int msg = hideOffset() ? Calculate : CalculateNoOffset;
478 if (_verboseServer) cout <<
"RooRealMPFE::calculate(" << GetName()
479 <<
") IPC toServer> Calculate " << endl ;
483 _calcInProgress = kTRUE ;
484 _forceCalc = kFALSE ;
487 *_pipe << msg << BidirMMapPipe::flush;
488 if (_verboseServer) cout <<
"RooRealMPFE::evaluate(" << GetName()
489 <<
") IPC toServer> Retrieve " << endl ;
490 _retrieveDispatched = kTRUE ;
492 }
else if (_state!=Inline) {
493 cout <<
"RooRealMPFE::calculate(" << GetName()
494 <<
") ERROR not in Client or Inline mode" << endl ;
510 Double_t RooRealMPFE::getValV(
const RooArgSet* )
const
513 if (isValueDirty()) {
517 _value = evaluate() ;
518 }
else if (_calcInProgress) {
521 _value = evaluate() ;
538 Double_t RooRealMPFE::evaluate()
const
541 Double_t return_value = 0;
542 if (_state==Inline) {
543 return_value = _arg ;
544 }
else if (_state==Client) {
546 bool needflush =
false;
552 if (evalErrorLoggingMode() != _remoteEvalErrorLoggingState) {
554 RooAbsReal::ErrorLoggingMode flag = evalErrorLoggingMode() ;
555 *_pipe << msg << flag;
557 _remoteEvalErrorLoggingState = evalErrorLoggingMode() ;
560 if (!_retrieveDispatched) {
564 if (_verboseServer) cout <<
"RooRealMPFE::evaluate(" << GetName()
565 <<
") IPC toServer> Retrieve " << endl ;
567 if (needflush) *_pipe << BidirMMapPipe::flush;
568 _retrieveDispatched = kFALSE ;
573 *_pipe >> msg >> value >> _evalCarry >> numError;
575 if (msg!=ReturnValue) {
576 cout <<
"RooRealMPFE::evaluate(" << GetName()
577 <<
") ERROR: unexpected message from server process: " << msg << endl ;
580 if (_verboseServer) cout <<
"RooRealMPFE::evaluate(" << GetName()
581 <<
") IPC fromServer> ReturnValue " << value << endl ;
583 if (_verboseServer) cout <<
"RooRealMPFE::evaluate(" << GetName()
584 <<
") IPC fromServer> NumErrors " << numError << endl ;
587 char *msgbuf1 = 0, *msgbuf2 = 0, *msgbuf3 = 0;
592 *_pipe >> msgbuf1 >> msgbuf2 >> msgbuf3;
593 if (_verboseServer) cout <<
"RooRealMPFE::evaluate(" << GetName()
594 <<
") IPC fromServer> retrieving error log Arg " << ptr <<
" Msg " << msgbuf1 << endl ;
596 logEvalError(reinterpret_cast<RooAbsReal*>(ptr),msgbuf3,msgbuf1,msgbuf2) ;
604 _calcInProgress = kFALSE ;
605 return_value = value ;
619 void RooRealMPFE::standby()
622 if (_state==Client) {
625 if (_verboseServer) cout <<
"RooRealMPFE::standby(" << GetName()
626 <<
") IPC toServer> Terminate " << endl;
628 *_pipe << msg << BidirMMapPipe::flush;
632 if (Terminate != msg || 0 != _pipe->close()) {
633 std::cerr <<
"In " << __func__ <<
"(" << __FILE__
", " << __LINE__ <<
634 "): Server shutdown failed." << std::endl;
637 if (_verboseServer) {
638 std::cerr <<
"In " << __func__ <<
"(" << __FILE__
", " <<
639 __LINE__ <<
"): Pipe has already shut down, not sending "
640 "Terminate to server." << std::endl;
659 void RooRealMPFE::constOptimizeTestStatistic(ConstOpCode opcode, Bool_t doAlsoTracking)
662 if (_state==Client) {
666 *_pipe << msg << op << doAlsoTracking;
667 if (_verboseServer) cout <<
"RooRealMPFE::constOptimize(" << GetName()
668 <<
") IPC toServer> ConstOpt " << opcode << endl ;
674 if (_state==Inline) {
675 ((RooAbsReal&)_arg.arg()).constOptimizeTestStatistic(opcode,doAlsoTracking) ;
685 void RooRealMPFE::setVerbose(Bool_t clientFlag, Bool_t serverFlag)
688 if (_state==Client) {
690 *_pipe << msg << serverFlag;
691 if (_verboseServer) cout <<
"RooRealMPFE::setVerbose(" << GetName()
692 <<
") IPC toServer> Verbose " << (serverFlag?1:0) << endl ;
695 _verboseClient = clientFlag ; _verboseServer = serverFlag ;
703 void RooRealMPFE::applyNLLWeightSquared(Bool_t flag)
706 if (_state==Client) {
707 int msg = ApplyNLLW2 ;
708 *_pipe << msg << flag;
709 if (_verboseServer) cout <<
"RooRealMPFE::applyNLLWeightSquared(" << GetName()
710 <<
") IPC toServer> ApplyNLLW2 " << (flag?1:0) << endl ;
719 void RooRealMPFE::doApplyNLLW2(Bool_t flag)
721 RooNLLVar* nll =
dynamic_cast<RooNLLVar*
>(_arg.absArg()) ;
723 nll->applyWeightSquared(flag) ;
732 void RooRealMPFE::enableOffsetting(Bool_t flag)
735 if (_state==Client) {
736 int msg = EnableOffset ;
737 *_pipe << msg << flag;
738 if (_verboseServer) cout <<
"RooRealMPFE::enableOffsetting(" << GetName()
739 <<
") IPC toServer> EnableOffset " << (flag?1:0) << endl ;
742 ((RooAbsReal&)_arg.arg()).enableOffsetting(flag) ;