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