diff --git a/CondCore/CTPPSPlugins/src/plugin.cc b/CondCore/CTPPSPlugins/src/plugin.cc index 1c8892df4218e..f2eb460050b5e 100644 --- a/CondCore/CTPPSPlugins/src/plugin.cc +++ b/CondCore/CTPPSPlugins/src/plugin.cc @@ -26,6 +26,12 @@ #include "CondFormats/PPSObjects/interface/PPSAssociationCuts.h" #include "CondFormats/DataRecord/interface/PPSAssociationCutsRcd.h" +namespace { + struct InitAssociationCuts { + void operator()(PPSAssociationCuts &cuts) { cuts.initialize(); } + }; +} // namespace + REGISTER_PLUGIN(CTPPSBeamParametersRcd, CTPPSBeamParameters); REGISTER_PLUGIN(CTPPSPixelDAQMappingRcd, CTPPSPixelDAQMapping); REGISTER_PLUGIN(CTPPSPixelAnalysisMaskRcd, CTPPSPixelAnalysisMask); @@ -39,4 +45,5 @@ REGISTER_PLUGIN(PPSDirectSimulationDataRcd, PPSDirectSimulationData); REGISTER_PLUGIN(PPSPixelTopologyRcd, PPSPixelTopology); REGISTER_PLUGIN(PPSAlignmentConfigRcd, PPSAlignmentConfig); REGISTER_PLUGIN(PPSAlignmentConfigurationRcd, PPSAlignmentConfiguration); -REGISTER_PLUGIN(PPSAssociationCutsRcd, PPSAssociationCuts); + +REGISTER_PLUGIN_INIT(PPSAssociationCutsRcd, PPSAssociationCuts, InitAssociationCuts); diff --git a/CondFormats/PPSObjects/interface/PPSAssociationCuts.h b/CondFormats/PPSObjects/interface/PPSAssociationCuts.h index 59a35e5372c05..486fe22eff03d 100644 --- a/CondFormats/PPSObjects/interface/PPSAssociationCuts.h +++ b/CondFormats/PPSObjects/interface/PPSAssociationCuts.h @@ -12,6 +12,7 @@ struct TF1; #include "CondFormats/Serialization/interface/Serializable.h" #include "FWCore/ParameterSet/interface/ParameterSet.h" #include "FWCore/ParameterSet/interface/ParameterSetDescription.h" +#include "FWCore/Utilities/interface/Exception.h" #include #include @@ -37,8 +38,7 @@ class PPSAssociationCuts { double getTiTrMax() const { return ti_tr_max_; } // build TF1 representations of the mean and threshold functions - // NB: declared as const as it only modifies mutable class fields - void buildFunctions() const; + void buildFunctions(); // returns whether the specified cut is applied bool isApplied(Quantities quantity) const; @@ -52,8 +52,8 @@ class PPSAssociationCuts { std::vector s_thresholds_; // TF1 representation of the cut parameters - for run time evaluations - mutable std::vector > f_means_ COND_TRANSIENT; - mutable std::vector > f_thresholds_ COND_TRANSIENT; + std::vector > f_means_ COND_TRANSIENT; + std::vector > f_thresholds_ COND_TRANSIENT; // timing-tracking cuts double ti_tr_min_; @@ -70,7 +70,13 @@ class PPSAssociationCuts { ~PPSAssociationCuts() {} - const CutsPerArm &getAssociationCuts(const int sector) const { return association_cuts_.at(sector); } + // checks if the data have a valid structure + bool isValid() const; + + // builds run-time data members, useful e.g. after loading data from DB + void initialize(); + + const CutsPerArm &getAssociationCuts(const int sector) const { return association_cuts_.find(sector)->second; } static edm::ParameterSetDescription getDefaultParameters(); diff --git a/CondFormats/PPSObjects/src/PPSAssociationCuts.cc b/CondFormats/PPSObjects/src/PPSAssociationCuts.cc index 294829d2179fe..f486947af6aae 100644 --- a/CondFormats/PPSObjects/src/PPSAssociationCuts.cc +++ b/CondFormats/PPSObjects/src/PPSAssociationCuts.cc @@ -27,13 +27,11 @@ PPSAssociationCuts::CutsPerArm::CutsPerArm(const edm::ParameterSet &iConfig, int ti_tr_min_ = association_cuts.getParameter("ti_tr_min"); ti_tr_max_ = association_cuts.getParameter("ti_tr_max"); - - buildFunctions(); } //---------------------------------------------------------------------------------------------------- -void PPSAssociationCuts::CutsPerArm::buildFunctions() const { +void PPSAssociationCuts::CutsPerArm::buildFunctions() { f_means_.clear(); for (const auto &s : s_means_) f_means_.push_back(std::make_shared("f", s.c_str())); @@ -46,7 +44,7 @@ void PPSAssociationCuts::CutsPerArm::buildFunctions() const { //---------------------------------------------------------------------------------------------------- bool PPSAssociationCuts::CutsPerArm::isApplied(Quantities quantity) const { - return (!s_thresholds_.at(quantity).empty()) && (!s_means_.at(quantity).empty()); + return (!s_thresholds_[quantity].empty()) && (!s_means_[quantity].empty()); } //---------------------------------------------------------------------------------------------------- @@ -57,14 +55,9 @@ bool PPSAssociationCuts::CutsPerArm::isSatisfied( if (!isApplied(quantity)) return true; - // build functions if not already done - // (this may happen if data (string representation) are loaded from DB and the constructor is not executed) - if (f_means_.size() < s_means_.size()) - buildFunctions(); - // evaluate mean and threshold - const double mean = evaluateExpression(f_means_.at(quantity), x_near, y_near, xangle); - const double threshold = evaluateExpression(f_thresholds_.at(quantity), x_near, y_near, xangle); + const double mean = evaluateExpression(f_means_[quantity], x_near, y_near, xangle); + const double threshold = evaluateExpression(f_thresholds_[quantity], x_near, y_near, xangle); // make comparison return fabs(q_NF_diff - mean) < threshold; @@ -88,6 +81,8 @@ PPSAssociationCuts::PPSAssociationCuts(const edm::ParameterSet &iConfig) { unsigned int i = 0; for (const int §or : {45, 56}) association_cuts_[i++] = CutsPerArm(iConfig, sector); + + initialize(); } //---------------------------------------------------------------------------------------------------- @@ -115,6 +110,31 @@ edm::ParameterSetDescription PPSAssociationCuts::getDefaultParameters() { //---------------------------------------------------------------------------------------------------- +bool PPSAssociationCuts::isValid() const { + // valid if association_cuts_ have two entries, with keys 0 and 1 + if (association_cuts_.size() != 2) + return false; + + if (association_cuts_.find(0) == association_cuts_.end()) + return false; + if (association_cuts_.find(1) == association_cuts_.end()) + return false; + + return true; +} + +//---------------------------------------------------------------------------------------------------- + +void PPSAssociationCuts::initialize() { + if (!isValid()) + throw cms::Exception("PPS") << "Invalid structure of PPSAssociationCuts."; + + for (auto &p : association_cuts_) + p.second.buildFunctions(); +} + +//---------------------------------------------------------------------------------------------------- + std::ostream &operator<<(std::ostream &os, const PPSAssociationCuts::CutsPerArm &cutsPerArm) { os << "CutsPerArm {" << std::endl;