CMS 3D CMS Logo

CTPPSIncludeAlignmentsFromXML.cc
Go to the documentation of this file.
1 /****************************************************************************
2 *
3 * This is a part of TOTEM offline software.
4 * Authors:
5 * Jan Kaspar (jan.kaspar@gmail.com)
6 *
7 ****************************************************************************/
8 
17 
19 
21 
25 
26 #include <vector>
27 #include <string>
28 #include <map>
29 #include <set>
30 
31 
36 {
37  public:
40 
41  std::unique_ptr<RPAlignmentCorrectionsData> produceMeasured(const RPMeasuredAlignmentRecord &);
42  std::unique_ptr<RPAlignmentCorrectionsData> produceReal(const RPRealAlignmentRecord &);
43  std::unique_ptr<RPAlignmentCorrectionsData> produceMisaligned(const RPMisalignedAlignmentRecord &);
44 
45  protected:
46  unsigned int verbosity;
49 
51 
53  {
55  return src;
56 
57  if (src.luminosityBlock() == 0)
59 
60  return edm::EventID(src.run(), src.luminosityBlock() - 1, src.event());
61  }
62 
64  {
66  {
67  if (src.run() == edm::EventID::maxRunNumber())
68  return src;
69 
70  return edm::EventID(src.run() + 1, 0, src.event());
71  }
72 
73  return edm::EventID(src.run(), src.luminosityBlock() + 1, src.event());
74  }
75 
77  RPAlignmentCorrectionsDataSequence Merge(const std::vector<RPAlignmentCorrectionsDataSequence>&) const;
78 
80  void PrepareSequence(const std::string &label, RPAlignmentCorrectionsDataSequence &seq, const std::vector<std::string> &files) const;
81 };
82 
83 //----------------------------------------------------------------------------------------------------
84 
85 using namespace std;
86 using namespace edm;
87 
88 //----------------------------------------------------------------------------------------------------
89 //----------------------------------------------------------------------------------------------------
90 
92  verbosity(pSet.getUntrackedParameter<unsigned int>("verbosity", 0))
93 {
94  std::vector<std::string> measuredFiles;
95  for (const auto &f: pSet.getParameter< vector<string> >("MeasuredFiles"))
96  measuredFiles.push_back(edm::FileInPath(f).fullPath());
97  PrepareSequence("Measured", acsMeasured, measuredFiles);
98 
99  std::vector<std::string> realFiles;
100  for (const auto &f: pSet.getParameter< vector<string> >("RealFiles"))
101  realFiles.push_back(edm::FileInPath(f).fullPath());
102  PrepareSequence("Real", acsReal, realFiles);
103 
104  std::vector<std::string> misalignedFiles;
105  for (const auto &f: pSet.getParameter< vector<string> >("MisalignedFiles"))
106  misalignedFiles.push_back(edm::FileInPath(f).fullPath());
107  PrepareSequence("Misaligned", acsMisaligned, misalignedFiles);
108 
112 
113  findingRecord<RPMeasuredAlignmentRecord>();
114  findingRecord<RPRealAlignmentRecord>();
115  findingRecord<RPMisalignedAlignmentRecord>();
116 }
117 
118 //----------------------------------------------------------------------------------------------------
119 
121 {
122 }
123 
124 //----------------------------------------------------------------------------------------------------
125 
126 RPAlignmentCorrectionsDataSequence CTPPSIncludeAlignmentsFromXML::Merge(const vector<RPAlignmentCorrectionsDataSequence>& seqs) const
127 {
128  // find interval boundaries
129  map< edm::EventID, vector< pair<bool, const RPAlignmentCorrectionsData*> > > bounds;
130 
131  for (const auto & seq : seqs)
132  {
133  for (const auto &p : seq)
134  {
135  const ValidityInterval &iov = p.first;
136  const RPAlignmentCorrectionsData *corr = & p.second;
137 
138  const EventID &event_first = iov.first().eventID();
139  bounds[event_first].emplace_back( pair<bool, const RPAlignmentCorrectionsData*>(true, corr) );
140 
141  const EventID &event_after = nextLS(iov.last().eventID());
142  bounds[event_after].emplace_back( pair<bool, const RPAlignmentCorrectionsData*>(false, corr) );
143  }
144  }
145 
146  // build correction sums per interval
147  set<const RPAlignmentCorrectionsData*> accumulator;
149  for (map< EventID, vector< pair<bool, const RPAlignmentCorrectionsData*> > >::const_iterator tit = bounds.begin(); tit != bounds.end(); ++tit)
150  {
151  for (const auto & cit : tit->second)
152  {
153  bool add = cit.first;
154  const RPAlignmentCorrectionsData *corr = cit.second;
155 
156  if (add)
157  accumulator.insert(corr);
158  else
159  accumulator.erase(corr);
160  }
161 
162  auto tit_next = tit;
163  tit_next++;
164  if (tit_next == bounds.end())
165  break;
166 
167  const EventID &event_first = tit->first;
168  const EventID &event_last = previousLS(tit_next->first);
169 
170  if (verbosity)
171  {
172  LogVerbatim("CTPPSIncludeAlignmentsFromXML")
175  << ": alignment blocks " << accumulator.size();
176  }
177 
179  for (auto sit : accumulator)
180  corr_sum.addCorrections(*sit);
181 
182  result.insert(edm::ValidityInterval(edm::IOVSyncValue(event_first), edm::IOVSyncValue(event_last)), corr_sum);
183  }
184 
185  return result;
186 }
187 
188 //----------------------------------------------------------------------------------------------------
189 
191 {
192  if (verbosity)
193  LogVerbatim(">> CTPPSIncludeAlignmentsFromXML") << "CTPPSIncludeAlignmentsFromXML::PrepareSequence(" << label << ")";
194 
195  vector<RPAlignmentCorrectionsDataSequence> sequences;
196  for (const auto & file : files)
197  sequences.emplace_back(RPAlignmentCorrectionsMethods::loadFromXML(file));
198 
199  seq = Merge(sequences);
200 }
201 
202 //----------------------------------------------------------------------------------------------------
203 
204 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignmentsFromXML::produceMeasured(const RPMeasuredAlignmentRecord &iRecord)
205 {
206  return std::make_unique<RPAlignmentCorrectionsData>(acMeasured);
207 }
208 
209 //----------------------------------------------------------------------------------------------------
210 
211 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignmentsFromXML::produceReal(const RPRealAlignmentRecord &iRecord)
212 {
213  return std::make_unique<RPAlignmentCorrectionsData>(acReal);
214 }
215 
216 //----------------------------------------------------------------------------------------------------
217 
218 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignmentsFromXML::produceMisaligned(const RPMisalignedAlignmentRecord &iRecord)
219 {
220  return std::make_unique<RPAlignmentCorrectionsData>(acMisaligned);
221 }
222 
223 //----------------------------------------------------------------------------------------------------
224 
226  const IOVSyncValue& iosv, ValidityInterval& valInt)
227 {
228  if (verbosity)
229  {
230  time_t unixTime = iosv.time().unixTime();
231  char timeStr[50];
232  strftime(timeStr, 50, "%F %T", localtime(&unixTime));
233 
234  LogVerbatim("CTPPSIncludeAlignmentsFromXML")
235  << ">> CTPPSIncludeAlignmentsFromXML::setIntervalFor(" << key.name() << ")";
236 
237  LogVerbatim("CTPPSIncludeAlignmentsFromXML")
238  << " event=" << iosv.eventID() << ", UNIX timestamp=" << unixTime << " (" << timeStr << ")";
239  }
240 
241  // determine what sequence and corrections should be used
242  RPAlignmentCorrectionsDataSequence *p_seq = nullptr;
243  RPAlignmentCorrectionsData *p_corr = nullptr;
244 
245  if (strcmp(key.name(), "RPMeasuredAlignmentRecord") == 0)
246  {
247  p_seq = &acsMeasured;
248  p_corr = &acMeasured;
249  }
250 
251  if (strcmp(key.name(), "RPRealAlignmentRecord") == 0)
252  {
253  p_seq = &acsReal;
254  p_corr = &acReal;
255  }
256 
257  if (strcmp(key.name(), "RPMisalignedAlignmentRecord") == 0)
258  {
259  p_seq = &acsMisaligned;
260  p_corr = &acMisaligned;
261  }
262 
263  if (p_seq == nullptr)
264  throw cms::Exception("CTPPSIncludeAlignmentsFromXML::setIntervalFor") << "Unknown record " << key.name();
265 
266  // find the corresponding interval
267  bool next_exists = false;
268  const edm::EventID &event_curr = iosv.eventID();
270 
271  for (const auto &it: *p_seq)
272  {
273  const auto &it_event_first = it.first.first().eventID();
274  const auto &it_event_last = it.first.last().eventID();
275 
276  bool it_contained_lo = ( (it_event_first.run() < event_curr.run()) ||
277  ((it_event_first.run() == event_curr.run()) && (it_event_first.luminosityBlock() <= event_curr.luminosityBlock())) );
278 
279  bool it_contained_up = ( (it_event_last.run() > event_curr.run()) ||
280  ((it_event_last.run() == event_curr.run()) && (it_event_last.luminosityBlock() >= event_curr.luminosityBlock())) );
281 
282  if (it_contained_lo && it_contained_up)
283  {
284  valInt = it.first;
285  *p_corr = it.second;
286 
287  if (verbosity)
288  {
289  LogVerbatim("CTPPSIncludeAlignmentsFromXML")
290  << " setting validity interval ["
292  << ", " << RPAlignmentCorrectionsMethods::iovValueToString(valInt.last()) << "]";
293  }
294 
295  return;
296  }
297 
298  bool it_in_future = ( (it_event_first.run() > event_curr.run()) ||
299  ((it_event_first.run() == event_curr.run() && (it_event_first.luminosityBlock() > event_curr.luminosityBlock()))) );
300 
301  if (it_in_future)
302  {
303  next_exists = true;
304  if (event_next_start > it_event_first)
305  event_next_start = it_event_first;
306  }
307  }
308 
309  // no interval found, set empty corrections
310  *p_corr = RPAlignmentCorrectionsData();
311 
312  if (!next_exists)
313  {
314  valInt = ValidityInterval(iosv, iosv.endOfTime());
315  } else {
316  const EventID &event_last = previousLS(event_next_start);
317  valInt = ValidityInterval(iosv, IOVSyncValue(event_last));
318  }
319 
320  if (verbosity)
321  {
322  LogVerbatim("CTPPSIncludeAlignmentsFromXML")
323  << " setting validity interval ["
325  << ", " << RPAlignmentCorrectionsMethods::iovValueToString(valInt.last()) << "] (empty alignment corrections)";
326  }
327 }
328 
329 //----------------------------------------------------------------------------------------------------
330 
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
Time sequence of alignment corrections. I/O methods have been factored out to: Geometry/VeryForwardGe...
static RunNumber_t maxRunNumber()
Definition: EventID.h:103
const EventID & eventID() const
Definition: IOVSyncValue.h:42
std::unique_ptr< RPAlignmentCorrectionsData > produceMeasured(const RPMeasuredAlignmentRecord &)
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:97
static RPAlignmentCorrectionsDataSequence loadFromXML(const std::string &fileName)
loads sequence of alignment corrections from XML file
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
void insert(const edm::ValidityInterval &iov, const RPAlignmentCorrectionsData &data)
LuminosityBlockNumber_t luminosityBlock() const
Definition: EventID.h:40
RPAlignmentCorrectionsDataSequence acsMisaligned
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
const IOVSyncValue & last() const
static edm::EventID previousLS(const edm::EventID &src)
static LuminosityBlockNumber_t maxLuminosityBlockNumber()
Definition: EventID.h:107
void PrepareSequence(const std::string &label, RPAlignmentCorrectionsDataSequence &seq, const std::vector< std::string > &files) const
builds a sequence of corrections from provided sources and runs a few checks
std::unique_ptr< RPAlignmentCorrectionsData > produceMisaligned(const RPMisalignedAlignmentRecord &)
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
double f[11][100]
void addCorrections(const RPAlignmentCorrectionsData &, bool sumErrors=true, bool addSh=true, bool addRot=true)
adds (merges) corrections on top of the current values
JetCorrectorParameters corr
Definition: classes.h:5
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
CTPPSIncludeAlignmentsFromXML(const edm::ParameterSet &p)
Container for CTPPS RP alignment corrections. The corrections are stored on two levels - RP and senso...
RPAlignmentCorrectionsDataSequence acsReal
std::unique_ptr< RPAlignmentCorrectionsData > produceReal(const RPRealAlignmentRecord &)
RPAlignmentCorrectionsDataSequence Merge(const std::vector< RPAlignmentCorrectionsDataSequence > &) const
merges an array of sequences to one
HLT enums.
static edm::EventID nextLS(const edm::EventID &src)
RPAlignmentCorrectionsDataSequence acsMeasured
const Timestamp & time() const
Definition: IOVSyncValue.h:44
const IOVSyncValue & first() const
static std::string iovValueToString(const edm::IOVSyncValue &)