CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TotemRPIncludeAlignments.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 
16 
21 
22 #include <vector>
23 #include <string>
24 #include <map>
25 #include <set>
26 
27 
35 {
36  public:
38  virtual ~TotemRPIncludeAlignments();
39 
40  std::auto_ptr<RPAlignmentCorrectionsData> produceMeasured(const RPMeasuredAlignmentRecord &);
41  std::auto_ptr<RPAlignmentCorrectionsData> produceReal(const RPRealAlignmentRecord &);
42  std::auto_ptr<RPAlignmentCorrectionsData> produceMisaligned(const RPMisalignedAlignmentRecord &);
43 
44  protected:
45  unsigned int verbosity;
48 
50 
52  RPAlignmentCorrectionsDataSequence Merge(const std::vector<RPAlignmentCorrectionsDataSequence>) const;
53 
55  void PrepareSequence(const std::string &label, RPAlignmentCorrectionsDataSequence &seq, const std::vector<std::string> &files) const;
56 };
57 
58 //----------------------------------------------------------------------------------------------------
59 
60 using namespace std;
61 using namespace edm;
62 
63 //----------------------------------------------------------------------------------------------------
64 //----------------------------------------------------------------------------------------------------
65 
67  verbosity(pSet.getUntrackedParameter<unsigned int>("verbosity", 1))
68 {
69  PrepareSequence("Measured", acsMeasured, pSet.getParameter< vector<string> >("MeasuredFiles"));
70  PrepareSequence("Real", acsReal, pSet.getParameter< vector<string> >("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 TotemRPIncludeAlignments::Merge(const vector<RPAlignmentCorrectionsDataSequence> files) const
91 {
92  // find interval boundaries
93  map< TimeValue_t, vector< pair<bool, const RPAlignmentCorrectionsData*> > > bounds;
94 
95  for (vector<RPAlignmentCorrectionsDataSequence>::const_iterator fit = files.begin(); fit != files.end(); ++fit)
96  {
97  for (RPAlignmentCorrectionsDataSequence::const_iterator iit = fit->begin(); iit != fit->end(); ++iit)
98  {
99  const TimeValidityInterval &tvi = iit->first;
100  const RPAlignmentCorrectionsData *corr = & iit->second;
101 
102  bounds[tvi.first].push_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].push_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 (vector< pair<bool, const RPAlignmentCorrectionsData*> >::const_iterator cit = tit->second.begin(); cit != tit->second.end(); ++cit)
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 (set<const RPAlignmentCorrectionsData*>::iterator sit = accumulator.begin(); sit != accumulator.end(); ++sit)
144  result[tvi].AddCorrections(*(*sit));
145  }
146 
147  return result;
148 }
149 
150 //----------------------------------------------------------------------------------------------------
151 
152 void TotemRPIncludeAlignments::PrepareSequence(const string &label, RPAlignmentCorrectionsDataSequence &seq, const vector<string> &files) const
153 {
154  if (verbosity)
155  printf(">> TotemRPIncludeAlignments::PrepareSequence(%s)\n", label.c_str());
156 
157  vector<RPAlignmentCorrectionsDataSequence> sequences;
158  for (unsigned int i = 0; i < files.size(); i++)
159  sequences.push_back(RPAlignmentCorrectionsDataSequence(files[i]));
160 
161  seq = Merge(sequences);
162 }
163 
164 //----------------------------------------------------------------------------------------------------
165 
166 std::auto_ptr<RPAlignmentCorrectionsData> TotemRPIncludeAlignments::produceMeasured(const RPMeasuredAlignmentRecord &iRecord)
167 {
168  std::auto_ptr<RPAlignmentCorrectionsData> alignments(new RPAlignmentCorrectionsData(acMeasured));
169  return alignments;
170 }
171 
172 //----------------------------------------------------------------------------------------------------
173 
174 std::auto_ptr<RPAlignmentCorrectionsData> TotemRPIncludeAlignments::produceReal(const RPRealAlignmentRecord &iRecord)
175 {
176  std::auto_ptr<RPAlignmentCorrectionsData> alignments(new RPAlignmentCorrectionsData(acReal));
177  return alignments;
178 }
179 
180 //----------------------------------------------------------------------------------------------------
181 
182 std::auto_ptr<RPAlignmentCorrectionsData> TotemRPIncludeAlignments::produceMisaligned(const RPMisalignedAlignmentRecord &iRecord)
183 {
184  std::auto_ptr<RPAlignmentCorrectionsData> alignments(new RPAlignmentCorrectionsData(acMisaligned));
185  return alignments;
186 }
187 
188 //----------------------------------------------------------------------------------------------------
189 
191  const IOVSyncValue& iosv, ValidityInterval& valInt)
192 {
193  if (verbosity)
194  {
195  LogVerbatim("TotemRPIncludeAlignments")
196  << ">> TotemRPIncludeAlignments::setIntervalFor(" << key.name() << ")";
197 
198  time_t unixTime = iosv.time().unixTime();
199  char timeStr[50];
200  strftime(timeStr, 50, "%F %T", localtime(&unixTime));
201 
202  LogVerbatim("TotemRPIncludeAlignments")
203  << " run=" << iosv.eventID().run() << ", event=" << iosv.eventID().event() << ", UNIX timestamp=" << unixTime << " (" << timeStr << ")";
204  }
205 
206  // determine what sequence and corrections should be used
209 
210  if (strcmp(key.name(), "RPMeasuredAlignmentRecord") == 0)
211  {
212  seq = &acsMeasured;
213  corr = &acMeasured;
214  }
215 
216  if (strcmp(key.name(), "RPRealAlignmentRecord") == 0)
217  {
218  seq = &acsReal;
219  corr = &acReal;
220  }
221 
222  if (strcmp(key.name(), "RPMisalignedAlignmentRecord") == 0)
223  {
224  seq = &acsMisaligned;
225  corr = &acMisaligned;
226  }
227 
228  if (seq == NULL)
229  throw cms::Exception("TotemRPIncludeAlignments::setIntervalFor") << "Unknown record " << key.name();
230 
231  // find the corresponding time interval
232  bool next_exists = false;
233  TimeValue_t t = iosv.time().value(), next_start = TimeValidityInterval::EndOfTime();
234 
235  for (RPAlignmentCorrectionsDataSequence::iterator it = seq->begin(); it != seq->end(); ++it)
236  {
237  if (it->first.first <= t && it->first.last >= t)
238  {
239  valInt = ValidityInterval(IOVSyncValue(Timestamp(it->first.first)), IOVSyncValue(Timestamp(it->first.last)));
240  *corr = it->second;
241 
242  if (verbosity)
243  {
244  LogVerbatim("TotemRPIncludeAlignments")
245  << " setting validity interval [" << TimeValidityInterval::ValueToUNIXString(valInt.first().time().value())
246  << ", " << TimeValidityInterval::ValueToUNIXString(valInt.last().time().value()) << "]";
247  }
248 
249  return;
250  }
251 
252  if (t <= it->first.first)
253  {
254  next_exists = true;
255  next_start = min(next_start, it->first.first);
256  }
257  }
258 
259  // no interval found, set empty corrections
260  *corr = RPAlignmentCorrectionsData();
261 
262  if (!next_exists)
263  valInt = ValidityInterval(iosv, iosv.endOfTime());
264  else
265  valInt = ValidityInterval(iosv, IOVSyncValue(Timestamp(next_start - 1)));
266 
267  if (verbosity)
268  {
269  LogVerbatim("TotemRPIncludeAlignments")
270  << " setting validity interval [" << TimeValidityInterval::ValueToUNIXString(valInt.first().time().value())
271  << ", " << TimeValidityInterval::ValueToUNIXString(valInt.last().time().value()) << "]";
272  }
273 }
274 
RunNumber_t run() const
Definition: EventID.h:39
dbl * delta
Definition: mlp_gen.cc:36
TotemRPIncludeAlignments(const edm::ParameterSet &p)
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
int i
Definition: DBlmapReader.cc:9
Time sequence of alignment corrections.
RPAlignmentCorrectionsDataSequence Merge(const std::vector< RPAlignmentCorrectionsDataSequence >) const
merges an array of sequences to one
const EventID & eventID() const
Definition: IOVSyncValue.h:42
Validity interval in timestamps.
RPAlignmentCorrectionsData acMisaligned
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:97
std::auto_ptr< RPAlignmentCorrectionsData > produceReal(const RPRealAlignmentRecord &)
A class adding (mis)alignments to geometry (both real and misaligned).
std::pair< Time_t, Time_t > ValidityInterval
Definition: Time.h:19
void add(const std::vector< const T * > &source, std::vector< const T * > &dest)
#define NULL
Definition: scimark2.h:8
RPAlignmentCorrectionsDataSequence acsReal
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
RPAlignmentCorrectionsDataSequence acsMeasured
tuple result
Definition: mps_fire.py:84
const IOVSyncValue & last() const
RPAlignmentCorrectionsData acReal
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
RPAlignmentCorrectionsData acMeasured
static std::string ValueToUNIXString(const edm::TimeValue_t &v)
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
std::auto_ptr< RPAlignmentCorrectionsData > produceMeasured(const RPMeasuredAlignmentRecord &)
T min(T a, T b)
Definition: MathUtil.h:58
RPAlignmentCorrectionsDataSequence acsMisaligned
unsigned long long TimeValue_t
Definition: Timestamp.h:28
string key
FastSim: produces sample of signal events, overlayed with premixed minbias events.
JetCorrectorParameters corr
Definition: classes.h:5
edm::TimeValue_t first
the boundaries (included) of the interval expressed as UNIX timestamps
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
Container for RP alignment corrections. The corrections are stored on two levels - RP and sensor...
std::auto_ptr< RPAlignmentCorrectionsData > produceMisaligned(const RPMisalignedAlignmentRecord &)
const Timestamp & time() const
Definition: IOVSyncValue.h:44
const IOVSyncValue & first() const
static const edm::TimeValue_t EndOfTime()
virtual void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &, edm::ValidityInterval &)
TimeValue_t value() const
Definition: Timestamp.h:56