@@ -2216,7 +2216,7 @@ public:
22162216 // / \param[in] model The returned histogram will be constructed using this as a model.
22172217 // / \param[in] columnList
22182218 // / A list containing the names of the columns that will be passed when calling `Fill`.
2219- // / (N columns for unweighted filling, or N+1 columns for weighted filling)
2219+ // / \param[in] wName The name of the column that will provide the weights.
22202220 // / \return the N-dimensional histogram wrapped in a RResultPtr.
22212221 // /
22222222 // / This action is *lazy*: upon invocation of this method the calculation is
@@ -2229,19 +2229,42 @@ public:
22292229 // / {"col0", "col1", "col2", "col3"});
22302230 // / ~~~
22312231 // /
2232+ // / \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
2233+ // / argument `wName`: `HistoND(model, cols, weightCol)`.
2234+ // /
22322235 template <typename FirstColumn, typename ... OtherColumns> // need FirstColumn to disambiguate overloads
2233- RResultPtr<::THnD> HistoND (const THnDModel &model, const ColumnNames_t &columnList)
2236+ RResultPtr<::THnD> HistoND (const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName = " " )
22342237 {
22352238 std::shared_ptr<::THnD> h (nullptr );
22362239 {
22372240 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel (kError );
22382241 h = model.GetHistogram ();
2242+ const auto hDims = h->GetNdimensions ();
2243+ decltype (hDims) nCols = columnList.size ();
2244+
2245+ if (!wName.empty () && nCols == hDims + 1 )
2246+ throw std::invalid_argument (" The weight column was passed as an argument and at the same time the list of "
2247+ " input columns contains one column more than the number of dimensions of the "
2248+ " histogram. Call as 'HistoND(model, cols, weightCol)'." );
2249+
2250+ if (nCols == hDims + 1 )
2251+ Warning (" HistoND" , " Passing the column with the weights as the last column in the list is deprecated. "
2252+ " Instead, pass it as a separate argument, e.g. 'HistoND(model, cols, weightCol)'." );
22392253
2240- if (int (columnList. size ()) == (h-> GetNdimensions () + 1 )) {
2254+ if (!wName. empty () || nCols == hDims + 1 )
22412255 h->Sumw2 ();
2242- } else if (int (columnList.size ()) != h->GetNdimensions ()) {
2243- throw std::runtime_error (" Wrong number of columns for the specified number of histogram axes." );
2244- }
2256+
2257+ if (nCols != hDims + 1 && nCols != hDims)
2258+ throw std::invalid_argument (" Wrong number of columns for the specified number of histogram axes." );
2259+ }
2260+
2261+ if (!wName.empty ()) {
2262+ // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
2263+ // passed arguments is one more the number of dimensions of the histogram.
2264+ ColumnNames_t userColumns = columnList;
2265+ userColumns.push_back (std::string{wName});
2266+ return CreateAction<RDFInternal::ActionTags::HistoND, FirstColumn, OtherColumns...>(userColumns, h, h,
2267+ fProxiedPtr );
22452268 }
22462269 return CreateAction<RDFInternal::ActionTags::HistoND, FirstColumn, OtherColumns...>(columnList, h, h,
22472270 fProxiedPtr );
@@ -2251,7 +2274,7 @@ public:
22512274 // / \brief Fill and return an N-dimensional histogram (*lazy action*).
22522275 // / \param[in] model The returned histogram will be constructed using this as a model.
22532276 // / \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2254- // / (N columns for unweighted filling, or N+1 columns for weighted filling)
2277+ // / \param[in] wName The name of the column that will provide the weights.
22552278 // / \return the N-dimensional histogram wrapped in a RResultPtr.
22562279 // /
22572280 // / This action is *lazy*: upon invocation of this method the calculation is
@@ -2264,18 +2287,41 @@ public:
22642287 // / {"col0", "col1", "col2", "col3"});
22652288 // / ~~~
22662289 // /
2267- RResultPtr<::THnD> HistoND (const THnDModel &model, const ColumnNames_t &columnList)
2290+ // / \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
2291+ // / argument `wName`: `HistoND(model, cols, weightCol)`.
2292+ // /
2293+ RResultPtr<::THnD> HistoND (const THnDModel &model, const ColumnNames_t &columnList, std::string_view wName = " " )
22682294 {
22692295 std::shared_ptr<::THnD> h (nullptr );
22702296 {
22712297 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel (kError );
22722298 h = model.GetHistogram ();
2299+ const auto hDims = h->GetNdimensions ();
2300+ decltype (hDims) nCols = columnList.size ();
2301+
2302+ if (!wName.empty () && nCols == hDims + 1 )
2303+ throw std::invalid_argument (" The weight column was passed as an argument and at the same time the list of "
2304+ " input columns contains one column more than the number of dimensions of the "
2305+ " histogram. Call as 'HistoND(model, cols, weightCol)'." );
2306+
2307+ if (nCols == hDims + 1 )
2308+ Warning (" HistoND" , " Passing the column with the weights as the last column in the list is deprecated. "
2309+ " Instead, pass it as a separate argument, e.g. 'HistoND(model, cols, weightCol)'." );
22732310
2274- if (int (columnList. size ()) == (h-> GetNdimensions () + 1 )) {
2311+ if (!wName. empty () || nCols == hDims + 1 )
22752312 h->Sumw2 ();
2276- } else if (int (columnList.size ()) != h->GetNdimensions ()) {
2277- throw std::runtime_error (" Wrong number of columns for the specified number of histogram axes." );
2278- }
2313+
2314+ if (nCols != hDims + 1 && nCols != hDims)
2315+ throw std::invalid_argument (" Wrong number of columns for the specified number of histogram axes." );
2316+ }
2317+
2318+ if (!wName.empty ()) {
2319+ // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
2320+ // passed arguments is one more the number of dimensions of the histogram.
2321+ ColumnNames_t userColumns = columnList;
2322+ userColumns.push_back (std::string{wName});
2323+ return CreateAction<RDFInternal::ActionTags::HistoND, RDFDetail::RInferredType>(userColumns, h, h, fProxiedPtr ,
2324+ userColumns.size ());
22792325 }
22802326 return CreateAction<RDFInternal::ActionTags::HistoND, RDFDetail::RInferredType>(columnList, h, h, fProxiedPtr ,
22812327 columnList.size ());
@@ -2290,7 +2336,7 @@ public:
22902336 // / \param[in] model The returned histogram will be constructed using this as a model.
22912337 // / \param[in] columnList
22922338 // / A list containing the names of the columns that will be passed when calling `Fill`.
2293- // / (N columns for unweighted filling, or N+1 columns for weighted filling)
2339+ // / \param[in] wName The name of the column that will provide the weights.
22942340 // / \return the N-dimensional histogram wrapped in a RResultPtr.
22952341 // /
22962342 // / This action is *lazy*: upon invocation of this method the calculation is
@@ -2303,19 +2349,44 @@ public:
23032349 // / {"col0", "col1", "col2", "col3"});
23042350 // / ~~~
23052351 // /
2352+ // / \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
2353+ // / argument `wName`: `HistoND(model, cols, weightCol)`.
2354+ // /
23062355 template <typename FirstColumn, typename ... OtherColumns> // need FirstColumn to disambiguate overloads
2307- RResultPtr<::THnSparseD> HistoNSparseD (const THnSparseDModel &model, const ColumnNames_t &columnList)
2356+ RResultPtr<::THnSparseD>
2357+ HistoNSparseD (const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName = " " )
23082358 {
23092359 std::shared_ptr<::THnSparseD> h (nullptr );
23102360 {
23112361 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel (kError );
23122362 h = model.GetHistogram ();
2363+ const auto hDims = h->GetNdimensions ();
2364+ decltype (hDims) nCols = columnList.size ();
2365+
2366+ if (!wName.empty () && nCols == hDims + 1 )
2367+ throw std::invalid_argument (" The weight column was passed as an argument and at the same time the list of "
2368+ " input columns contains one column more than the number of dimensions of the "
2369+ " histogram. Call as 'HistoNSparseD(model, cols, weightCol)'." );
2370+
2371+ if (nCols == hDims + 1 )
2372+ Warning (" HistoNSparseD" ,
2373+ " Passing the column with the weights as the last column in the list is deprecated. "
2374+ " Instead, pass it as a separate argument, e.g. 'HistoNSparseD(model, cols, weightCol)'." );
23132375
2314- if (int (columnList. size ()) == (h-> GetNdimensions () + 1 )) {
2376+ if (!wName. empty () || nCols == hDims + 1 )
23152377 h->Sumw2 ();
2316- } else if (int (columnList.size ()) != h->GetNdimensions ()) {
2317- throw std::runtime_error (" Wrong number of columns for the specified number of histogram axes." );
2318- }
2378+
2379+ if (nCols != hDims + 1 && nCols != hDims)
2380+ throw std::invalid_argument (" Wrong number of columns for the specified number of histogram axes." );
2381+ }
2382+
2383+ if (!wName.empty ()) {
2384+ // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
2385+ // passed arguments is one more the number of dimensions of the histogram.
2386+ ColumnNames_t userColumns = columnList;
2387+ userColumns.push_back (std::string{wName});
2388+ return CreateAction<RDFInternal::ActionTags::HistoNSparseD, FirstColumn, OtherColumns...>(userColumns, h, h,
2389+ fProxiedPtr );
23192390 }
23202391 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, FirstColumn, OtherColumns...>(columnList, h, h,
23212392 fProxiedPtr );
@@ -2325,7 +2396,7 @@ public:
23252396 // / \brief Fill and return a sparse N-dimensional histogram (*lazy action*).
23262397 // / \param[in] model The returned histogram will be constructed using this as a model.
23272398 // / \param[in] columnList A list containing the names of the columns that will be passed when calling `Fill`
2328- // / (N columns for unweighted filling, or N+1 columns for weighted filling)
2399+ // / \param[in] wName The name of the column that will provide the weights.
23292400 // / \return the N-dimensional histogram wrapped in a RResultPtr.
23302401 // /
23312402 // / This action is *lazy*: upon invocation of this method the calculation is
@@ -2338,18 +2409,43 @@ public:
23382409 // / {"col0", "col1", "col2", "col3"});
23392410 // / ~~~
23402411 // /
2341- RResultPtr<::THnSparseD> HistoNSparseD (const THnSparseDModel &model, const ColumnNames_t &columnList)
2412+ // / \note A column with event weights should not be passed as part of `columnList`, but instead be passed in the new
2413+ // / argument `wName`: `HistoND(model, cols, weightCol)`.
2414+ // /
2415+ RResultPtr<::THnSparseD>
2416+ HistoNSparseD (const THnSparseDModel &model, const ColumnNames_t &columnList, std::string_view wName = " " )
23422417 {
23432418 std::shared_ptr<::THnSparseD> h (nullptr );
23442419 {
23452420 ROOT::Internal::RDF::RIgnoreErrorLevelRAII iel (kError );
23462421 h = model.GetHistogram ();
2422+ const auto hDims = h->GetNdimensions ();
2423+ decltype (hDims) nCols = columnList.size ();
2424+
2425+ if (!wName.empty () && nCols == hDims + 1 )
2426+ throw std::invalid_argument (" The weight column was passed as an argument and at the same time the list of "
2427+ " input columns contains one column more than the number of dimensions of the "
2428+ " histogram. Call as 'HistoNSparseD(model, cols, weightCol)'." );
2429+
2430+ if (nCols == hDims + 1 )
2431+ Warning (" HistoNSparseD" ,
2432+ " Passing the column with the weights as the last column in the list is deprecated. "
2433+ " Instead, pass it as a separate argument, e.g. 'HistoNSparseD(model, cols, weightCol)'." );
23472434
2348- if (int (columnList. size ()) == (h-> GetNdimensions () + 1 )) {
2435+ if (!wName. empty () || nCols == hDims + 1 )
23492436 h->Sumw2 ();
2350- } else if (int (columnList.size ()) != h->GetNdimensions ()) {
2351- throw std::runtime_error (" Wrong number of columns for the specified number of histogram axes." );
2352- }
2437+
2438+ if (nCols != hDims + 1 && nCols != hDims)
2439+ throw std::invalid_argument (" Wrong number of columns for the specified number of histogram axes." );
2440+ }
2441+
2442+ if (!wName.empty ()) {
2443+ // The action helper will invoke THnBase::Fill overload that performs weighted filling in case the number of
2444+ // passed arguments is one more the number of dimensions of the histogram.
2445+ ColumnNames_t userColumns = columnList;
2446+ userColumns.push_back (std::string{wName});
2447+ return CreateAction<RDFInternal::ActionTags::HistoNSparseD, RDFDetail::RInferredType>(
2448+ userColumns, h, h, fProxiedPtr , userColumns.size ());
23532449 }
23542450 return CreateAction<RDFInternal::ActionTags::HistoNSparseD, RDFDetail::RInferredType>(
23552451 columnList, h, h, fProxiedPtr , columnList.size ());
0 commit comments