CMS 3D CMS Logo

CTPPSIncludeAlignments.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 
21 
22 #include <vector>
23 #include <string>
24 #include <map>
25 #include <set>
26 
27 
32 {
33  public:
35  ~CTPPSIncludeAlignments() override;
36 
37  std::unique_ptr<RPAlignmentCorrectionsData> produceMeasured(const RPMeasuredAlignmentRecord &);
38  std::unique_ptr<RPAlignmentCorrectionsData> produceReal(const RPRealAlignmentRecord &);
39  std::unique_ptr<RPAlignmentCorrectionsData> produceMisaligned(const RPMisalignedAlignmentRecord &);
40 
41  protected:
42  unsigned int verbosity;
45 
47 
49  RPAlignmentCorrectionsDataSequence Merge(const std::vector<RPAlignmentCorrectionsDataSequence>&) const;
50 
52  void PrepareSequence(const std::string &label, RPAlignmentCorrectionsDataSequence &seq, const std::vector<std::string> &files) const;
53 };
54 
55 //----------------------------------------------------------------------------------------------------
56 
57 using namespace std;
58 using namespace edm;
59 
60 //----------------------------------------------------------------------------------------------------
61 //----------------------------------------------------------------------------------------------------
62 
64  verbosity(pSet.getUntrackedParameter<unsigned int>("verbosity", 1))
65 {
66  std::vector<std::string> realFiles;
67  for (auto rFile: pSet.getParameter< vector<string> >("RealFiles"))
68  realFiles.push_back(edm::FileInPath(rFile).fullPath());
69  PrepareSequence("Measured", acsMeasured, pSet.getParameter< vector<string> >("MeasuredFiles"));
70  PrepareSequence("Real", acsReal, realFiles);
71  PrepareSequence("Misaligned", acsMisaligned, pSet.getParameter< vector<string> >("MisalignedFiles"));
72 
76 
77  findingRecord<RPMeasuredAlignmentRecord>();
78  findingRecord<RPRealAlignmentRecord>();
79  findingRecord<RPMisalignedAlignmentRecord>();
80 }
81 
82 //----------------------------------------------------------------------------------------------------
83 
85 {
86 }
87 
88 //----------------------------------------------------------------------------------------------------
89 
90 RPAlignmentCorrectionsDataSequence CTPPSIncludeAlignments::Merge(const vector<RPAlignmentCorrectionsDataSequence>& files) const
91 {
92  // find interval boundaries
93  map< TimeValue_t, vector< pair<bool, const RPAlignmentCorrectionsData*> > > bounds;
94 
95  for (const auto & file : files)
96  {
97  for (RPAlignmentCorrectionsDataSequence::const_iterator iit = file.begin(); iit != file.end(); ++iit)
98  {
99  const TimeValidityInterval &tvi = iit->first;
100  const RPAlignmentCorrectionsData *corr = & iit->second;
101 
102  bounds[tvi.first].emplace_back( pair<bool, const RPAlignmentCorrectionsData*>(true, corr) );
103 
104  TimeValue_t delta = (tvi.last != TimeValidityInterval::EndOfTime()) ? (1ULL << 32) : 0; // input resolution is 1s
105  bounds[tvi.last + delta].emplace_back( pair<bool, const RPAlignmentCorrectionsData*>(false, corr) );
106  }
107  }
108 
109  // build correction sums per interval
110  set<const RPAlignmentCorrectionsData*> accumulator;
112  // bool gap_found = false;
113  for (map< TimeValue_t, vector< pair<bool, const RPAlignmentCorrectionsData*> > >::const_iterator tit = bounds.begin(); tit != bounds.end(); ++tit)
114  {
115  for (const auto & cit : tit->second)
116  {
117  bool add = cit.first;
118  const RPAlignmentCorrectionsData *corr = cit.second;
119 
120  if (add)
121  accumulator.insert(corr);
122  else
123  accumulator.erase(corr);
124  }
125 
126  map< TimeValue_t, vector< pair<bool, const RPAlignmentCorrectionsData*> > >::const_iterator tit_next = tit;
127  tit_next++;
128  if (tit_next == bounds.end())
129  break;
130 
131  TimeValue_t delta = (tit_next->first != TimeValidityInterval::EndOfTime()) ? 1 : 0; // minimal step
132  TimeValidityInterval tvi(tit->first, tit_next->first - delta);
133 
134  if (verbosity)
135  {
136  printf("\tfirst=%10s, last=%10s: alignment blocks=%li\n",
139  accumulator.size()
140  );
141  }
142 
143  for (auto sit : accumulator)
144  result[tvi].addCorrections(*sit);
145  }
146 
147  return result;
148 }
149 
150 //----------------------------------------------------------------------------------------------------
151 
152 void CTPPSIncludeAlignments::PrepareSequence(const string &label, RPAlignmentCorrectionsDataSequence &seq, const vector<string> &files) const
153 {
154  if (verbosity)
155  printf(">> CTPPSIncludeAlignments::PrepareSequence(%s)\n", label.c_str());
156 
157  vector<RPAlignmentCorrectionsDataSequence> sequences;
158  for (const auto & file : files)
159  sequences.emplace_back(RPAlignmentCorrectionsDataSequence(file));
160 
161  seq = Merge(sequences);
162 }
163 
164 //----------------------------------------------------------------------------------------------------
165 
166 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignments::produceMeasured(const RPMeasuredAlignmentRecord &iRecord)
167 {
168  return std::make_unique<RPAlignmentCorrectionsData>(acMeasured);
169 }
170 
171 //----------------------------------------------------------------------------------------------------
172 
173 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignments::produceReal(const RPRealAlignmentRecord &iRecord)
174 {
175  return std::make_unique<RPAlignmentCorrectionsData>(acReal);
176 }
177 
178 //----------------------------------------------------------------------------------------------------
179 
180 std::unique_ptr<RPAlignmentCorrectionsData> CTPPSIncludeAlignments::produceMisaligned(const RPMisalignedAlignmentRecord &iRecord)
181 {
182  return std::make_unique<RPAlignmentCorrectionsData>(acMisaligned);
183 }
184 
185 //----------------------------------------------------------------------------------------------------
186 
188  const IOVSyncValue& iosv, ValidityInterval& valInt)
189 {
190  if (verbosity)
191  {
192  LogVerbatim("CTPPSIncludeAlignments")
193  << ">> CTPPSIncludeAlignments::setIntervalFor(" << key.name() << ")";
194 
195  time_t unixTime = iosv.time().unixTime();
196  char timeStr[50];
197  strftime(timeStr, 50, "%F %T", localtime(&unixTime));
198 
199  LogVerbatim("CTPPSIncludeAlignments")
200  << " run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event() << ", UNIX timestamp=" << unixTime << " (" << timeStr << ")";
201  }
202 
203  // determine what sequence and corrections should be used
204  RPAlignmentCorrectionsDataSequence *seq = nullptr;
205  RPAlignmentCorrectionsData *corr = nullptr;
206 
207  if (strcmp(key.name(), "RPMeasuredAlignmentRecord") == 0)
208  {
209  seq = &acsMeasured;
210  corr = &acMeasured;
211  }
212 
213  if (strcmp(key.name(), "RPRealAlignmentRecord") == 0)
214  {
215  seq = &acsReal;
216  corr = &acReal;
217  }
218 
219  if (strcmp(key.name(), "RPMisalignedAlignmentRecord") == 0)
220  {
221  seq = &acsMisaligned;
222  corr = &acMisaligned;
223  }
224 
225  if (seq == nullptr)
226  throw cms::Exception("CTPPSIncludeAlignments::setIntervalFor") << "Unknown record " << key.name();
227 
228  // find the corresponding time interval
229  bool next_exists = false;
230  TimeValue_t t = iosv.time().value(), next_start = TimeValidityInterval::EndOfTime();
231 
232  for (auto & it : *seq)
233  {
234  if (it.first.first <= t && it.first.last >= t)
235  {
236  valInt = ValidityInterval(IOVSyncValue(Timestamp(it.first.first)), IOVSyncValue(Timestamp(it.first.last)));
237  *corr = it.second;
238 
239  if (verbosity)
240  {
241  LogVerbatim("CTPPSIncludeAlignments")
242  << " setting validity interval [" << TimeValidityInterval::ValueToUNIXString(valInt.first().time().value())
243  << ", " << TimeValidityInterval::ValueToUNIXString(valInt.last().time().value()) << "]";
244  }
245 
246  return;
247  }
248 
249  if (t <= it.first.first)
250  {
251  next_exists = true;
252  next_start = min(next_start, it.first.first);
253  }
254  }
255 
256  // no interval found, set empty corrections
257  *corr = RPAlignmentCorrectionsData();
258 
259  if (!next_exists)
260  valInt = ValidityInterval(iosv, iosv.endOfTime());
261  else
262  valInt = ValidityInterval(iosv, IOVSyncValue(Timestamp(next_start - 1)));
263 
264  if (verbosity)
265  {
266  LogVerbatim("CTPPSIncludeAlignments")
267  << " setting validity interval [" << TimeValidityInterval::ValueToUNIXString(valInt.first().time().value())
268  << ", " << TimeValidityInterval::ValueToUNIXString(valInt.last().time().value()) << "]";
269  }
270 }
271 
272 //----------------------------------------------------------------------------------------------------
273 
RunNumber_t run() const
Definition: EventID.h:39
dbl * delta
Definition: mlp_gen.cc:36
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
RPAlignmentCorrectionsDataSequence Merge(const std::vector< RPAlignmentCorrectionsDataSequence > &) const
merges an array of sequences to one
RPAlignmentCorrectionsDataSequence acsMeasured
Time sequence of alignment corrections.
RPAlignmentCorrectionsDataSequence acsMisaligned
const EventID & eventID() const
Definition: IOVSyncValue.h:42
Validity interval in timestamps.
RPAlignmentCorrectionsData acMisaligned
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:97
RPAlignmentCorrectionsDataSequence acsReal
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
std::unique_ptr< RPAlignmentCorrectionsData > produceReal(const RPRealAlignmentRecord &)
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
const IOVSyncValue & last() const
std::unique_ptr< RPAlignmentCorrectionsData > produceMeasured(const RPMeasuredAlignmentRecord &)
RPAlignmentCorrectionsData acMeasured
CTPPSIncludeAlignments(const edm::ParameterSet &p)
static std::string ValueToUNIXString(const edm::TimeValue_t &v)
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
T min(T a, T b)
Definition: MathUtil.h:58
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
unsigned long long TimeValue_t
Definition: Timestamp.h:28
JetCorrectorParameters corr
Definition: classes.h:5
RPAlignmentCorrectionsData acReal
edm::TimeValue_t first
the boundaries (included) of the interval expressed as UNIX timestamps
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
Container for RP alignment corrections. The corrections are stored on two levels - RP and sensor...
HLT enums.
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &) override
const Timestamp & time() const
Definition: IOVSyncValue.h:44
const IOVSyncValue & first() const
static const edm::TimeValue_t EndOfTime()
TimeValue_t value() const
Definition: Timestamp.h:56
std::unique_ptr< RPAlignmentCorrectionsData > produceMisaligned(const RPMisalignedAlignmentRecord &)