CMS 3D CMS Logo

APVCyclePhaseProducerFromL1TS.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiStripTools
4 // Class: APVCyclePhaseProducerFromL1TS
5 //
13 //
14 // Original Author: Andrea Venturi
15 // Created: Mon Jan 12 09:05:45 CET 2009
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
29 
33 
35 
37 
40 
41 #include <map>
42 #include <vector>
43 #include <utility>
44 #include <string>
45 #include <iostream>
46 
49 
53 
54 //
55 // class declaration
56 //
57 
59 public:
62 
63 private:
64  void beginRun(const edm::Run&, const edm::EventSetup&) override;
65  void produce(edm::Event&, const edm::EventSetup&) override;
66 
67  bool isBadRun(const unsigned int) const;
68  void printConfiguration(std::stringstream& ss) const;
69 
70  // ----------member data ---------------------------
71 
72  const bool m_ignoreDB;
78  std::vector<std::string> _defpartnames;
79  std::vector<int> _defphases;
80  bool _useEC0;
82  bool m_badRun;
83  const bool _forceSCAL;
84 
85  std::vector<std::pair<unsigned int, unsigned int> > m_badruns;
86 
87  long long _lastResync;
88  long long _lastHardReset;
89  long long _lastStart;
90  long long _lastEventCounter0;
91  long long _lastOrbitCounter0;
92  long long _lastTestEnable;
93 };
94 
95 //
96 // constants, enums and typedefs
97 //
98 
99 //
100 // static data member definitions
101 //
102 
103 //
104 // constructors and destructor
105 //
107  : m_ignoreDB(iConfig.getUntrackedParameter<bool>("ignoreDB", false)),
108  m_eswatcher(),
109  _l1tscollectionToken(
110  consumes<Level1TriggerScalersCollection>(iConfig.getParameter<edm::InputTag>("l1TSCollection"))),
111  _tcdsRecordToken(consumes<TCDSRecord>(iConfig.getParameter<edm::InputTag>("tcdsRecordLabel"))),
112  _confObjectToken((!m_ignoreDB)
113  ? decltype(_confObjectToken){esConsumes<edm::Transition::BeginRun>(edm::ESInputTag{
114  "", iConfig.getUntrackedParameter<std::string>("recordLabel", "apvphaseoffsets")})}
115  : decltype(_confObjectToken){}),
116  _tTopoToken((!m_ignoreDB) ? decltype(_tTopoToken){esConsumes<edm::Transition::BeginRun>()}
117  : decltype(_tTopoToken){}),
118  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
119  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
120  _useEC0(iConfig.getUntrackedParameter<bool>("useEC0", false)),
121  _magicOffset(iConfig.getUntrackedParameter<int>("magicOffset", 8)),
122  m_badRun(false),
123  _forceSCAL(iConfig.getParameter<bool>("forceSCAL")),
124  m_badruns(),
125  _lastResync(-1),
126  _lastHardReset(-1),
127  _lastStart(-1),
128  _lastEventCounter0(-1),
129  _lastOrbitCounter0(-1),
130  _lastTestEnable(-1) {
131  std::stringstream ss;
132  printConfiguration(ss);
133  edm::LogInfo("ConfigurationAtConstruction") << ss.str();
134 
135  produces<APVCyclePhaseCollection, edm::InEvent>();
136 
137  m_badruns.push_back(std::pair<unsigned int, unsigned int>(0, 131767));
138  m_badruns.push_back(std::pair<unsigned int, unsigned int>(193150, 193733));
139 
140  //now do what ever other initialization is needed
141 }
142 
144  // do anything here that needs to be done at desctruction time
145  // (e.g. close files, deallocate resources etc.)
146 }
147 
148 //
149 // member functions
150 //
151 
152 // ------------ method called to produce the data ------------
154 
155 {
156  // update the parameters from DB
157 
158  if (!m_ignoreDB && m_eswatcher.check(iSetup)) {
159  const auto& confObj = iSetup.getData(_confObjectToken);
160 
161  std::stringstream summary;
162  confObj.printDebug(summary, &iSetup.getData(_tTopoToken));
163  LogDebug("SiStripConfObjectSummary") << summary.str();
164 
165  _defpartnames = confObj.get<std::vector<std::string> >("defaultPartitionNames");
166  _defphases = confObj.get<std::vector<int> >("defaultPhases");
167  _useEC0 = confObj.get<bool>("useEC0");
168  m_badRun = confObj.get<bool>("badRun");
169  _magicOffset = confObj.get<int>("magicOffset");
170 
171  std::stringstream ss;
173  edm::LogInfo("UpdatedConfiguration") << ss.str();
174  }
175 
176  if (isBadRun(iRun.run())) {
177  LogDebug("UnreliableMissingL1TriggerScalers") << "In this run L1TriggerScalers is missing or unreliable for phase "
178  "determination: invlid phase will be returned";
179  }
180 }
181 
183  using namespace edm;
184 
185  std::unique_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection());
186 
187  std::vector<int> phases(_defphases.size(), APVCyclePhaseCollection::invalid);
188 
189  const std::vector<std::string>& partnames = _defpartnames;
190 
191  int phasechange = 0;
192 
194  iEvent.getByToken(_l1tscollectionToken, l1ts);
195  Handle<TCDSRecord> tcds_pIn;
196  iEvent.getByToken(_tcdsRecordToken, tcds_pIn);
197  bool useTCDS(tcds_pIn.isValid() && !_forceSCAL);
198  const auto* tcdsRecord = useTCDS ? tcds_pIn.product() : nullptr;
199  // offset computation
200 
201  long long orbitoffset = 0;
202 
203  if (useTCDS && iEvent.eventAuxiliary().isRealData()) {
204  // l1ts->empty() always retuns false (last commit as of today: https://github.com/cms-sw/cmssw/commit/f4694d795d4b268d541c633dfb68283d889264b0 ), so the check is likely not necessary---and TCDSRecord hasn't anything similar
205  if (tcdsRecord->getLastResync() != 0) {
206  orbitoffset =
207  _useEC0 ? tcdsRecord->getLastEventCounter0() + _magicOffset : tcdsRecord->getLastResync() + _magicOffset;
208  }
209 
210  if (_lastResync != tcdsRecord->getLastResync()) {
211  _lastResync = tcdsRecord->getLastResync();
212  LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync;
213  }
214  if (_lastHardReset != tcdsRecord->getLastHardReset()) {
215  _lastHardReset = tcdsRecord->getLastHardReset();
216  LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset;
217  }
218  if (_lastTestEnable != tcdsRecord->getLastTestEnable()) {
219  _lastTestEnable = tcdsRecord->getLastTestEnable();
220  // LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
221  }
222  if (_lastOrbitCounter0 != tcdsRecord->getLastOrbitCounter0()) {
223  _lastOrbitCounter0 = tcdsRecord->getLastOrbitCounter0();
224  LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0;
225  }
226  if (_lastEventCounter0 != tcdsRecord->getLastEventCounter0()) {
227  _lastEventCounter0 = tcdsRecord->getLastEventCounter0();
228  LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0;
229  }
230  if (_lastStart != tcdsRecord->getLastStart()) {
231  _lastStart = tcdsRecord->getLastStart();
232  LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart;
233  }
234 
235  if (!isBadRun(iEvent.run())) {
236  phasechange = ((long long)(orbitoffset * 3564)) % 70;
237 
238  for (unsigned int ipart = 0; ipart < phases.size(); ++ipart) {
239  phases[ipart] = (_defphases[ipart] + phasechange) % 70;
240  }
241  }
242 
243  } else {
244  if (!l1ts->empty()) {
245  if ((*l1ts)[0].lastResync() != 0) {
246  orbitoffset = _useEC0 ? (*l1ts)[0].lastEventCounter0() + _magicOffset : (*l1ts)[0].lastResync() + _magicOffset;
247  }
248 
249  if (_lastResync != (*l1ts)[0].lastResync()) {
250  _lastResync = (*l1ts)[0].lastResync();
251  LogDebug("TTCSignalReceived") << "New Resync at orbit " << _lastResync;
252  }
253  if (_lastHardReset != (*l1ts)[0].lastHardReset()) {
254  _lastHardReset = (*l1ts)[0].lastHardReset();
255  LogDebug("TTCSignalReceived") << "New HardReset at orbit " << _lastHardReset;
256  }
257  if (_lastTestEnable != (*l1ts)[0].lastTestEnable()) {
258  _lastTestEnable = (*l1ts)[0].lastTestEnable();
259  // LogDebug("TTCSignalReceived") << "New TestEnable at orbit " << _lastTestEnable ;
260  }
261  if (_lastOrbitCounter0 != (*l1ts)[0].lastOrbitCounter0()) {
262  _lastOrbitCounter0 = (*l1ts)[0].lastOrbitCounter0();
263  LogDebug("TTCSignalReceived") << "New OrbitCounter0 at orbit " << _lastOrbitCounter0;
264  }
265  if (_lastEventCounter0 != (*l1ts)[0].lastEventCounter0()) {
266  _lastEventCounter0 = (*l1ts)[0].lastEventCounter0();
267  LogDebug("TTCSignalReceived") << "New EventCounter0 at orbit " << _lastEventCounter0;
268  }
269  if (_lastStart != (*l1ts)[0].lastStart()) {
270  _lastStart = (*l1ts)[0].lastStart();
271  LogDebug("TTCSignalReceived") << "New Start at orbit " << _lastStart;
272  }
273 
274  if (!isBadRun(iEvent.run())) {
275  phasechange = ((long long)(orbitoffset * 3564)) % 70;
276 
277  for (unsigned int ipart = 0; ipart < phases.size(); ++ipart) {
278  phases[ipart] = (_defphases[ipart] + phasechange) % 70;
279  }
280  }
281  }
282  }
283 
284  if (phases.size() < partnames.size()) {
285  // throw exception
286  throw cms::Exception("InvalidAPVCyclePhases")
287  << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
288  }
289 
290  for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
291  // if(phases[ipart]>=0) {
292  // apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
293  apvphases->get()[partnames[ipart]] = phases[ipart];
294 
295  // }
296  }
297 
298  iEvent.put(std::move(apvphases));
299 }
300 
301 bool APVCyclePhaseProducerFromL1TS::isBadRun(const unsigned int run) const {
302  for (std::vector<std::pair<unsigned int, unsigned int> >::const_iterator runpair = m_badruns.begin();
303  runpair != m_badruns.end();
304  ++runpair) {
305  if (run >= runpair->first && run <= runpair->second)
306  return true;
307  }
308 
309  return m_badRun;
310 }
311 
313  ss << _defpartnames.size() << " default partition names: ";
314  for (std::vector<std::string>::const_iterator part = _defpartnames.begin(); part != _defpartnames.end(); ++part) {
315  ss << *part << " ";
316  }
317  ss << std::endl;
318  ss << _defphases.size() << " default phases: ";
319  for (std::vector<int>::const_iterator phase = _defphases.begin(); phase != _defphases.end(); ++phase) {
320  ss << *phase << " ";
321  }
322  ss << std::endl;
323  ss << " Magic offset: " << _magicOffset << std::endl;
324  ss << " use ECO: " << _useEC0 << std::endl;
325  ss << " bad run: " << m_badRun << std::endl;
326 }
327 //define this as a plug-in
edm::EDGetTokenT< TCDSRecord > _tcdsRecordToken
T const * product() const
Definition: Handle.h:70
Class to contain information from TCDS FED.
Definition: TCDSRecord.h:19
edm::ESWatcher< SiStripConfObjectRcd > m_eswatcher
void beginRun(const edm::Run &, const edm::EventSetup &) override
T getUntrackedParameter(std::string const &, T const &) const
U second(std::pair< T, U > const &p)
int iEvent
Definition: GenABIO.cc:224
RunNumber_t run() const
Definition: RunBase.h:40
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
bool isBadRun(const unsigned int) const
Log< level::Info, false > LogInfo
void produce(edm::Event &, const edm::EventSetup &) override
std::vector< int > phases
part
Definition: HCALResponse.h:20
void printConfiguration(std::stringstream &ss) const
std::vector< Level1TriggerScalers > Level1TriggerScalersCollection
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
std::vector< std::pair< unsigned int, unsigned int > > m_badruns
bool isValid() const
Definition: HandleBase.h:70
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > _tTopoToken
HLT enums.
APVCyclePhaseProducerFromL1TS(const edm::ParameterSet &)
edm::ESGetToken< SiStripConfObject, SiStripConfObjectRcd > _confObjectToken
edm::EDGetTokenT< Level1TriggerScalersCollection > _l1tscollectionToken
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
#define LogDebug(id)