6262
6363#include < TInterpreter.h>
6464
65- namespace RooFit {
66- namespace Experimental {
65+ namespace RooFit ::Experimental {
6766
6867namespace {
6968
@@ -103,7 +102,7 @@ void rooHistTranslateImpl(RooAbsArg const &arg, CodegenContext &ctx, int intOrde
103102}
104103
105104std::string realSumPdfTranslateImpl (CodegenContext &ctx, RooAbsArg const &arg, RooArgList const &funcList,
106- RooArgList const &coefList, bool normalize)
105+ RooArgList const &coefList, bool normalize, bool forceScopeIndependent )
107106{
108107 bool noLastCoeff = funcList.size () != coefList.size ();
109108
@@ -113,7 +112,12 @@ std::string realSumPdfTranslateImpl(CodegenContext &ctx, RooAbsArg const &arg, R
113112
114113 std::string sum = ctx.getTmpVarName ();
115114 std::string coeffSum = ctx.getTmpVarName ();
116- ctx.addToCodeBody (&arg, " double " + sum + " = 0;\n double " + coeffSum + " = 0;\n " );
115+ std::string code1 = " double " + sum + " = 0;\n double " + coeffSum + " = 0;\n " ;
116+
117+ if (forceScopeIndependent)
118+ ctx.addToCodeBody (code1, true );
119+ else
120+ ctx.addToCodeBody (&arg, code1);
117121
118122 std::string iterator = " i_" + ctx.getTmpVarName ();
119123 std::string subscriptExpr = " [" + iterator + " ]" ;
@@ -128,7 +132,10 @@ std::string realSumPdfTranslateImpl(CodegenContext &ctx, RooAbsArg const &arg, R
128132 } else if (normalize) {
129133 code += sum + " /= " + coeffSum + " ;\n " ;
130134 }
131- ctx.addToCodeBody (&arg, code);
135+ if (forceScopeIndependent)
136+ ctx.addToCodeBody (code, true );
137+ else
138+ ctx.addToCodeBody (&arg, code);
132139
133140 return sum;
134141}
@@ -240,7 +247,7 @@ void codegenImpl(RooAbsArg &arg, CodegenContext &ctx)
240247
241248void codegenImpl (RooAddPdf &arg, CodegenContext &ctx)
242249{
243- ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.pdfList (), arg.coefList (), true ));
250+ ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.pdfList (), arg.coefList (), true , false ));
244251}
245252
246253void codegenImpl (RooMultiVarGaussian &arg, CodegenContext &ctx)
@@ -261,30 +268,25 @@ void codegenImpl(RooMultiPdf &arg, CodegenContext &ctx)
261268 // indices MathFunc call becomes more efficient.
262269 if (numPdfs > 2 ) {
263270 ctx.addResult (&arg, ctx.buildCall (mathFunc (" multipdf" ), arg.indexCategory (), arg.getPdfList ()));
271+ return ;
272+ }
273+ // Ternary nested expression
274+ std::string indexExpr = ctx.getResult (arg.indexCategory ());
264275
265- std::cout << " MathFunc call used\n " ;
266-
267- } else {
268-
269- // Ternary nested expression
270- std::string indexExpr = ctx.getResult (arg.indexCategory ());
271-
272- // int numPdfs = arg.getNumPdfs();
273- std::string expr;
276+ // int numPdfs = arg.getNumPdfs();
277+ std::string expr;
274278
275- for (int i = 0 ; i < numPdfs; ++i) {
276- RooAbsPdf *pdf = arg.getPdf (i);
277- std::string pdfExpr = ctx.getResult (*pdf);
279+ for (int i = 0 ; i < numPdfs; ++i) {
280+ RooAbsPdf *pdf = arg.getPdf (i);
281+ std::string pdfExpr = ctx.getResult (*pdf);
278282
279- expr += " (" + indexExpr + " == " + std::to_string (i) + " ? (" + pdfExpr + " ) : " ;
280- }
283+ expr += " (" + indexExpr + " == " + std::to_string (i) + " ? (" + pdfExpr + " ) : " ;
284+ }
281285
282- expr += " 0.0" ;
283- expr += std::string (numPdfs, ' )' ); // Close all ternary operators
286+ expr += " 0.0" ;
287+ expr += std::string (numPdfs, ' )' ); // Close all ternary operators
284288
285- ctx.addResult (&arg, expr);
286- std::cout << " Ternary expression call used \n " ;
287- }
289+ ctx.addResult (&arg, expr);
288290}
289291
290292// RooCategory index added.
@@ -294,7 +296,7 @@ void codegenImpl(RooCategory &arg, CodegenContext &ctx)
294296 if (idx < 0 ) {
295297
296298 idx = 1 ;
297- ctx.addVecObs (arg.GetName (), idx);
299+ ctx.addVecObs (arg.GetName (), idx, 1 );
298300 }
299301
300302 std::string result = std::to_string (arg.getCurrentIndex ());
@@ -305,6 +307,7 @@ void codegenImpl(RooAddition &arg, CodegenContext &ctx)
305307{
306308 if (arg.list ().empty ()) {
307309 ctx.addResult (&arg, " 0.0" );
310+ return ;
308311 }
309312 std::string result;
310313 if (arg.list ().size () > 1 )
@@ -469,7 +472,6 @@ void codegenImpl(RooFit::Detail::RooNLLVarNew &arg, CodegenContext &ctx)
469472
470473 std::string weightSumName = RooFit::Detail::makeValidVarName (arg.GetName ()) + " WeightSum" ;
471474 std::string resName = RooFit::Detail::makeValidVarName (arg.GetName ()) + " Result" ;
472- ctx.addResult (&arg, resName);
473475 ctx.addToGlobalScope (" double " + weightSumName + " = 0.0;\n " );
474476 ctx.addToGlobalScope (" double " + resName + " = 0.0;\n " );
475477
@@ -496,6 +498,8 @@ void codegenImpl(RooFit::Detail::RooNLLVarNew &arg, CodegenContext &ctx)
496498 std::string expected = ctx.getResult (*arg.expectedEvents ());
497499 ctx.addToCodeBody (resName + " += " + expected + " - " + weightSumName + " * std::log(" + expected + " );\n " );
498500 }
501+
502+ ctx.addResult (&arg, resName);
499503}
500504
501505void codegenImpl (RooFit::Detail::RooNormalizedPdf &arg, CodegenContext &ctx)
@@ -609,17 +613,17 @@ void codegenImpl(RooRealIntegral &arg, CodegenContext &ctx)
609613 auto &intVar = static_cast <RooAbsRealLValue &>(*arg.numIntRealVars ()[0 ]);
610614
611615 std::string obsName = ctx.getTmpVarName ();
612- std::string oldIntVarResult = ctx.getResult (intVar);
613- ctx.addResult (&intVar, " obs[0]" );
614616
617+ auto oldVecObsInfo = ctx._vecObsIndices [intVar.namePtr ()];
618+ ctx.addVecObs (intVar.GetName (), 0 , 1 );
615619 std::string funcName = ctx.buildFunction (arg.integrand (), {});
620+ ctx._vecObsIndices [intVar.namePtr ()] = oldVecObsInfo;
616621
617622 std::stringstream ss;
618623
619624 ss << " double " << obsName << " [1];\n " ;
620625
621626 std::string resName = RooFit::Detail::makeValidVarName (arg.GetName ()) + " Result" ;
622- ctx.addResult (&arg, resName);
623627 ctx.addToGlobalScope (" double " + resName + " = 0.0;\n " );
624628
625629 // TODO: once Clad has support for higher-order functions (follow also the
@@ -640,24 +644,21 @@ void codegenImpl(RooRealIntegral &arg, CodegenContext &ctx)
640644
641645 ctx.addToGlobalScope (ss.str ());
642646
643- ctx.addResult (&intVar, oldIntVarResult );
647+ ctx.addResult (&arg, resName );
644648}
645649
646650void codegenImpl (RooRealSumFunc &arg, CodegenContext &ctx)
647651{
648- ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.funcList (), arg.coefList (), false ));
652+ ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.funcList (), arg.coefList (), false , false ));
649653}
650654
651655void codegenImpl (RooRealSumPdf &arg, CodegenContext &ctx)
652656{
653- ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.funcList (), arg.coefList (), false ));
657+ ctx.addResult (&arg, realSumPdfTranslateImpl (ctx, arg, arg.funcList (), arg.coefList (), false , false ));
654658}
655659
656660void codegenImpl (RooRealVar &arg, CodegenContext &ctx)
657661{
658- if (!arg.isConstant ()) {
659- ctx.addResult (&arg, arg.GetName ());
660- }
661662 ctx.addResult (&arg, doubleToString (arg.getVal ()));
662663}
663664
@@ -898,7 +899,7 @@ std::string codegenIntegralImpl(RooPolynomial &arg, int, const char *rangeName,
898899std::string codegenIntegralImpl (RooRealSumPdf &arg, int code, const char *rangeName, CodegenContext &ctx)
899900{
900901 // Re-use translate, since integration is linear.
901- return realSumPdfTranslateImpl (ctx, arg, arg.funcIntListFromCache (code, rangeName), arg.coefList (), false );
902+ return realSumPdfTranslateImpl (ctx, arg, arg.funcIntListFromCache (code, rangeName), arg.coefList (), false , true );
902903}
903904
904905std::string codegenIntegralImpl (RooUniform &arg, int code, const char *rangeName, CodegenContext &)
@@ -908,5 +909,4 @@ std::string codegenIntegralImpl(RooUniform &arg, int code, const char *rangeName
908909 return doubleToString (arg.analyticalIntegral (code, rangeName));
909910}
910911
911- } // namespace Experimental
912- } // namespace RooFit
912+ } // namespace RooFit::Experimental
0 commit comments