CMS 3D CMS Logo

AlignPCLThresholdsWriter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondFormats/PCLConfig
4 // Class: AlignPCLThresholdsWriter
5 //
11 //
12 // Original Author: Marco Musich
13 // Created: Wed, 22 Feb 2017 12:04:36 GMT
14 //
15 //
16 
17 // system include files
18 #include <memory>
19 
20 // user include files
23 
26 
32 
33 //
34 // class declaration
35 //
36 
37 namespace DOFs {
38  enum dof { X, Y, Z, thetaX, thetaY, thetaZ, extraDOF };
39 }
40 
41 template <typename T>
43 public:
45  ~AlignPCLThresholdsWriter() override = default;
46 
47  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
48 
49 private:
50  void analyze(const edm::Event&, const edm::EventSetup&) override;
52 
53  void writePayload(T& myThresholds);
54  void storeHGthresholds(AlignPCLThresholdsHG& myThresholds, const std::vector<std::string>& alignables);
55 
56  // ----------member data ---------------------------
58  const unsigned int m_minNrecords;
59  const std::vector<edm::ParameterSet> m_parameters;
60 };
61 
62 //
63 // constructors and destructor
64 //
65 template <typename T>
67  : m_record(iConfig.getParameter<std::string>("record")),
68  m_minNrecords(iConfig.getParameter<unsigned int>("minNRecords")),
69  m_parameters(iConfig.getParameter<std::vector<edm::ParameterSet> >("thresholds")) {}
70 
71 //
72 // member functions
73 //
74 
75 // ------------ method called for each event ------------
76 template <typename T>
78  // detect if new payload is used
79  bool newClass = false;
80  for (auto& thePSet : m_parameters) {
81  if (thePSet.exists("fractionCut")) {
82  newClass = true;
83  break;
84  }
85  }
86 
87  T myThresholds{};
88  if constexpr (std::is_same_v<T, AlignPCLThresholdsHG>) {
89  if (newClass) {
90  this->writePayload(myThresholds);
91  } else {
92  throw cms::Exception("AlignPCLThresholdsWriter") << "mismatched configuration";
93  }
94  } else {
95  if (!newClass) {
96  this->writePayload(myThresholds);
97  } else {
98  throw cms::Exception("AlignPCLThresholdsWriter") << "mismatched configuration";
99  }
100  }
101 }
102 
103 template <typename T>
105  if (coord == "X") {
106  return DOFs::X;
107  } else if (coord == "Y") {
108  return DOFs::Y;
109  } else if (coord == "Z") {
110  return DOFs::Z;
111  } else if (coord == "thetaX") {
112  return DOFs::thetaX;
113  } else if (coord == "thetaY") {
114  return DOFs::thetaY;
115  } else if (coord == "thetaZ") {
116  return DOFs::thetaZ;
117  } else {
118  return DOFs::extraDOF;
119  }
120 }
121 
122 // ------------ templated method to write the payload ------------
123 template <typename T>
125  using namespace edm;
126 
127  edm::LogInfo("AlignPCLThresholdsWriter") << "Size of AlignPCLThresholds object " << myThresholds.size() << std::endl;
128 
129  // loop on the PSet and insert the conditions
130  std::array<std::string, 6> mandatories = {{"X", "Y", "Z", "thetaX", "thetaY", "thetaZ"}};
131  std::vector<std::string> alignables;
132 
133  // fill the list of alignables
134  for (auto& thePSet : m_parameters) {
135  const std::string alignableId(thePSet.getParameter<std::string>("alignableId"));
136  // only if it is not yet in the list
137  if (std::find(alignables.begin(), alignables.end(), alignableId) == alignables.end()) {
138  alignables.push_back(alignableId);
139  }
140  }
141 
142  for (auto& alignable : alignables) {
149 
150  std::vector<std::string> presentDOF;
151 
152  // extra degrees of freedom
153  std::vector<AlignPCLThreshold::coordThresholds> extraDOFs = std::vector<AlignPCLThreshold::coordThresholds>();
154 
155  for (auto& thePSet : m_parameters) {
156  const std::string alignableId(thePSet.getParameter<std::string>("alignableId"));
157  const std::string DOF(thePSet.getParameter<std::string>("DOF"));
158 
159  const double cutoff(thePSet.getParameter<double>("cut"));
160  const double sigCut(thePSet.getParameter<double>("sigCut"));
161  const double maxMoveCut(thePSet.getParameter<double>("maxMoveCut"));
162  const double maxErrorCut(thePSet.getParameter<double>("maxErrorCut"));
163 
164  if (alignableId == alignable) {
165  presentDOF.push_back(DOF);
166  // create the objects
167 
168  switch (mapOntoEnum(DOF)) {
169  case DOFs::X:
170  my_X.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
171  break;
172  case DOFs::Y:
173  my_Y.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
174  break;
175  case DOFs::Z:
176  my_Z.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
177  break;
178  case DOFs::thetaX:
179  my_tX.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
180  break;
181  case DOFs::thetaY:
182  my_tY.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
183  break;
184  case DOFs::thetaZ:
185  my_tZ.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
186  break;
187  default:
188  edm::LogInfo("AlignPCLThresholdsWriter")
189  << "Appending Extra degree of freeedom: " << DOF << " " << mapOntoEnum(DOF) << std::endl;
191  ExtraDOF.setThresholds(cutoff, sigCut, maxErrorCut, maxMoveCut, DOF);
192  extraDOFs.push_back(ExtraDOF);
193  }
194 
195  AlignPCLThreshold a(my_X, my_tX, my_Y, my_tY, my_Z, my_tZ, extraDOFs);
196  myThresholds.setAlignPCLThreshold(alignableId, a);
197 
198  } // if alignable is found in the PSet
199  } // loop on the PSets
200 
201  // checks if all mandatories are present
202  edm::LogInfo("AlignPCLThresholdsWriter")
203  << "Size of AlignPCLThresholds object " << myThresholds.size() << std::endl;
204  for (auto& mandatory : mandatories) {
205  if (std::find(presentDOF.begin(), presentDOF.end(), mandatory) == presentDOF.end()) {
206  edm::LogWarning("AlignPCLThresholdsWriter")
207  << "Configuration for DOF: " << mandatory << " for alignable " << alignable << "is not present \n"
208  << "Will build object with defaults!" << std::endl;
209  }
210  }
211 
212  } // ends loop on the alignable units
213 
214  // set the minimum number of records to be used in pede
215  myThresholds.setNRecords(m_minNrecords);
216  edm::LogInfo("AlignPCLThresholdsWriter") << "Content of AlignPCLThresholds " << std::endl;
217 
218  // additional thresholds for AlignPCLThresholdsHG
219  if constexpr (std::is_same_v<T, AlignPCLThresholdsHG>) {
220  storeHGthresholds(myThresholds, alignables);
221  }
222 
223  // use built-in method in the CondFormat
224  myThresholds.printAll();
225 
226  // Form the data here
228  if (poolDbService.isAvailable()) {
229  cond::Time_t valid_time = poolDbService->currentTime();
230  // this writes the payload to begin in current run defined in cfg
231  poolDbService->writeOneIOV(myThresholds, valid_time, m_record);
232  }
233 }
234 
235 // ------------ method to store additional HG thresholds ------------
236 template <typename T>
238  const std::vector<std::string>& alignables) {
239  edm::LogInfo("AlignPCLThresholdsWriter")
240  << "Found type AlignPCLThresholdsHG, additional thresholds are written" << std::endl;
241 
242  for (auto& alignable : alignables) {
243  for (auto& thePSet : m_parameters) {
244  const std::string alignableId(thePSet.getParameter<std::string>("alignableId"));
245  const std::string DOF(thePSet.getParameter<std::string>("DOF"));
246 
247  // Get coordType from DOF
248  AlignPCLThresholds::coordType type = static_cast<AlignPCLThresholds::coordType>(mapOntoEnum(DOF));
249 
250  if (alignableId == alignable) {
251  if (thePSet.exists("fractionCut")) {
252  const double fractionCut(thePSet.getParameter<double>("fractionCut"));
253  myThresholds.setFractionCut(alignableId, type, fractionCut);
254  }
255  }
256  }
257  }
258 }
259 
260 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
261 template <typename T>
264  desc.setComment("Plugin to write payloads of type AlignPCLThresholds");
265  desc.add<unsigned int>("minNRecords", 25000);
266  edm::ParameterSetDescription desc_thresholds;
267 
268  desc_thresholds.add<std::string>("alignableId");
269  desc_thresholds.add<std::string>("DOF");
270  desc_thresholds.add<double>("cut");
271  desc_thresholds.add<double>("sigCut");
272  desc_thresholds.add<double>("maxMoveCut");
273  desc_thresholds.add<double>("maxErrorCut");
274  if constexpr (std::is_same_v<T, AlignPCLThresholdsHG>) {
275  desc.add<std::string>("record", "AlignPCLThresholdsHGRcd");
276  //optional thresholds from new payload version (not for all the alignables)
277  desc_thresholds.addOptional<double>("fractionCut");
278  } else {
279  desc.add<std::string>("record", "AlignPCLThresholdsRcd");
280  }
281 
282  std::vector<edm::ParameterSet> default_thresholds(1);
283  desc.addVPSet("thresholds", desc_thresholds, default_thresholds);
284  descriptions.addWithDefaultLabel(desc);
285 }
286 
289 
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
~AlignPCLThresholdsWriter() override=default
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &, const edm::EventSetup &) override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void storeHGthresholds(AlignPCLThresholdsHG &myThresholds, const std::vector< std::string > &alignables)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
AlignPCLThresholdsWriter< AlignPCLThresholds > AlignPCLThresholdsLGWriter
int iEvent
Definition: GenABIO.cc:224
unsigned long long Time_t
Definition: Time.h:14
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
AlignPCLThresholdsWriter(const edm::ParameterSet &)
AlignPCLThresholdsWriter< AlignPCLThresholdsHG > AlignPCLThresholdsHGWriter
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Log< level::Info, false > LogInfo
DOFs::dof mapOntoEnum(std::string coord)
HLT enums.
double a
Definition: hdecay.h:119
void writePayload(T &myThresholds)
bool isAvailable() const
Definition: Service.h:40
Log< level::Warning, false > LogWarning
void setFractionCut(const std::string &AlignableId, const coordType &type, const float &cut)
void setThresholds(float theCut, float theSigCut, float theErrorCut, float theMaxMoveCut, const std::string &theLabel)
long double T
const std::vector< edm::ParameterSet > m_parameters