CMS 3D CMS Logo

APVCyclePhaseProducerFromL1ABC.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiStripTools
4 // Class: APVCyclePhaseProducerFromL1ABC
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 
31 
33 
36 
37 #include <map>
38 #include <vector>
39 #include <string>
40 
41 #include "TH1F.h"
42 #include "TProfile.h"
43 
47 
49 
50 //
51 // class decleration
52 //
53 
54 class APVCyclePhaseProducerFromL1ABC : public edm::one::EDProducer<edm::one::WatchRuns> {
55 public:
58 
59 private:
60  void beginJob() override;
61  void beginRun(const edm::Run&, const edm::EventSetup&) override;
62  void endRun(const edm::Run&, const edm::EventSetup&) override;
63  void produce(edm::Event&, const edm::EventSetup&) override;
64  void endJob() override;
65 
66  // ----------member data ---------------------------
67 
70  const std::vector<std::string> _defpartnames;
71  const std::vector<int> _defphases;
72  const int _orbitoffsetSOR;
73  const bool _wantHistos;
74  const bool _forceSCAL;
76 
77  TH1F** _hbx;
78  TH1F** _hdbx;
79  TH1F** _hdorbit;
80  const unsigned int _firstgoodrun;
81  std::map<edm::EventNumber_t, long long> _offsets;
82  long long _curroffset;
84 };
85 
86 //
87 // constants, enums and typedefs
88 //
89 
90 //
91 // static data member definitions
92 //
93 
94 //
95 // constructors and destructor
96 //
98  : _l1abccollectionToken(
99  mayConsume<L1AcceptBunchCrossingCollection>(iConfig.getParameter<edm::InputTag>("l1ABCCollection"))),
100  _tcdsRecordToken(mayConsume<TCDSRecord>(iConfig.getParameter<edm::InputTag>("tcdsRecordLabel"))),
101  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
102  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
103  _orbitoffsetSOR(iConfig.getParameter<int>("StartOfRunOrbitOffset")),
104  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos", false)),
105  _forceSCAL(iConfig.getParameter<bool>("forceSCAL")),
106  m_rhm(consumesCollector()),
107  _hbx(nullptr),
108  _hdbx(nullptr),
109  _hdorbit(nullptr),
110  _firstgoodrun(110878),
111  _offsets(),
112  _curroffset(0),
113  _curroffevent(0) {
114  produces<APVCyclePhaseCollection, edm::InEvent>();
115 
116  //now do what ever other initialization is needed
117 
118  if (_wantHistos) {
119  _hbx = m_rhm.makeTH1F("l1abcbx", "BX number from TCDS (or L1ABC as fallback)", 4096, -0.5, 4095.5);
120  _hdbx = m_rhm.makeTH1F("dbx", "BX number difference", 4096 * 2 - 1, -4095.5, 4095.5);
121  _hdorbit = m_rhm.makeTH1F("dorbit", "Orbit Number difference", 9999, -4999.5, 4999.5);
122  }
123 }
124 
126  // do anything here that needs to be done at desctruction time
127  // (e.g. close files, deallocate resources etc.)
128 }
129 
130 //
131 // member functions
132 //
133 
134 // ------------ method called to produce the data ------------
136 
137 {
138  // reset offset vector
139 
140  _offsets.clear();
141  edm::LogInfo("AbsoluteBXOffsetReset") << "Absolute BX offset map reset";
142 
143  if (_wantHistos) {
144  m_rhm.beginRun(iRun);
145 
146  if (_hbx && *_hbx) {
147  (*_hbx)->GetXaxis()->SetTitle("BX");
148  (*_hbx)->GetYaxis()->SetTitle("Events");
149  }
150 
151  if (_hdbx && *_hdbx) {
152  (*_hdbx)->GetXaxis()->SetTitle("#DeltaBX");
153  (*_hdbx)->GetYaxis()->SetTitle("Events");
154  }
155 
156  if (_hdorbit && *_hdorbit) {
157  (*_hdorbit)->GetXaxis()->SetTitle("#Deltaorbit");
158  (*_hdorbit)->GetYaxis()->SetTitle("Events");
159  }
160  }
161 
162  if (iRun.run() < _firstgoodrun) {
163  edm::LogInfo("UnreliableMissingL1AcceptBunchCrossingCollection")
164  << "In this run L1AcceptBunchCrossingCollection is missing or unreliable: default phases will be used";
165  }
166 }
167 
169  // summary of absolute bx offset vector
170 
171  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << "Absolute BX offset summary:";
172  for (std::map<edm::EventNumber_t, long long>::const_iterator offset = _offsets.begin(); offset != _offsets.end();
173  ++offset) {
174  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << offset->first << " " << offset->second;
175  }
176 }
177 
179  using namespace edm;
180 
181  std::unique_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection());
182 
183  const std::vector<int>& phases = _defphases;
184  const std::vector<std::string>& partnames = _defpartnames;
185 
186  // Look for the L1AcceptBunchCrossingCollection
187 
188  int phasechange = 0;
189 
190  if (iEvent.run() >= _firstgoodrun) {
192  iEvent.getByToken(_l1abccollectionToken, pIn);
193  Handle<TCDSRecord> tcds_pIn;
194  iEvent.getByToken(_tcdsRecordToken, tcds_pIn);
195  bool useTCDS(tcds_pIn.isValid() && !_forceSCAL);
196  const auto* tcdsRecord = useTCDS ? tcds_pIn.product() : nullptr;
197  // offset computation
198 
199  long long orbitoffset = _orbitoffsetSOR;
200  int bxoffset = 0;
201 
202  if (useTCDS) {
203  if (tcdsRecord->getL1aHistoryEntry(0).getIndex() == 0) {
204  if (tcdsRecord->getEventType() != 0) {
205  orbitoffset = (long long)tcdsRecord->getOrbitNr() - (long long)iEvent.orbitNumber();
206  // In EventWithHistoryProducerFromL1ABC, it's -1\times this, following the corresponding L1ABC line below. I keep this difference here, because I am not sure if it should be kept or not
207  bxoffset = iEvent.bunchCrossing() - tcdsRecord->getBXID();
208  // If I understand correctly, tcdsRecord has no l1AcceptOffset thing, so the control done for l1abc can be safely skipped
209  if (_wantHistos) {
210  if (_hbx && *_hbx)
211  (*_hbx)->Fill(tcdsRecord->getBXID());
212  if (_hdbx && *_hdbx)
213  (*_hdbx)->Fill(bxoffset);
214  if (_hdorbit && *_hdorbit)
215  (*_hdorbit)->Fill(orbitoffset);
216  }
217  }
218  } else {
219  edm::LogWarning("L1AcceptBunchCrossingNoType") << "L1AcceptBunchCrossing with no type found: ";
220  edm::LogPrint("L1AcceptBunchCrossingNoType") << *tcdsRecord;
221  }
222  } else {
223  for (L1AcceptBunchCrossingCollection::const_iterator l1abc = pIn->begin(); l1abc != pIn->end(); ++l1abc) {
224  if (l1abc->l1AcceptOffset() == 0) {
225  if (l1abc->eventType() != 0) {
226  orbitoffset = (long long)iEvent.orbitNumber() - (long long)l1abc->orbitNumber();
227  bxoffset =
228  iEvent.bunchCrossing() -
229  l1abc->bunchCrossing(); // In EventWithHistoryProducerFromL1ABC, it's l1abc - iEvent. I keep this difference here, because I am not sure if it should be kept or not
230 
231  if (_wantHistos) {
232  if (_hbx && *_hbx)
233  (*_hbx)->Fill(l1abc->bunchCrossing());
234  if (_hdbx && *_hdbx)
235  (*_hdbx)->Fill(bxoffset);
236  if (_hdorbit && *_hdorbit)
237  (*_hdorbit)->Fill(orbitoffset);
238  }
239  } else {
240  edm::LogWarning("L1AcceptBunchCrossingNoType") << "L1AcceptBunchCrossing with no type found: ";
241  for (L1AcceptBunchCrossingCollection::const_iterator debu = pIn->begin(); debu != pIn->end(); ++debu) {
242  edm::LogPrint("L1AcceptBunchCrossingNoType") << *debu;
243  }
244  }
245  }
246  }
247  }
248 
249  long long absbxoffset = orbitoffset * 3564 + bxoffset;
250 
251  if (orbitoffset != _orbitoffsetSOR)
252  phasechange = (orbitoffset * 3564) % 70;
253 
254  if (_offsets.empty()) {
255  _curroffset = absbxoffset;
256  _curroffevent = iEvent.id().event();
257  _offsets[iEvent.id().event()] = absbxoffset;
258  } else {
259  if (_curroffset != absbxoffset || iEvent.id().event() < _curroffevent) {
260  if (_curroffset != absbxoffset) {
261  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetChanged")
262  << "Absolute BX offset changed from " << _curroffset << " to " << absbxoffset << " at orbit "
263  << iEvent.orbitNumber() << " and BX " << iEvent.bunchCrossing();
264  if (useTCDS) {
265  edm::LogVerbatim("AbsoluteBXOffsetChanged") << *tcdsRecord; // Not sure about this
266  } else {
267  for (L1AcceptBunchCrossingCollection::const_iterator l1abc = pIn->begin(); l1abc != pIn->end(); ++l1abc) {
268  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << *l1abc;
269  }
270  }
271  }
272 
273  _curroffset = absbxoffset;
274  _curroffevent = iEvent.id().event();
275  _offsets[iEvent.id().event()] = absbxoffset;
276  }
277  }
278  }
279 
280  if (phases.size() < partnames.size()) {
281  // throw exception
282  throw cms::Exception("InvalidAPVCyclePhases")
283  << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
284  }
285 
286  for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
287  if (phases[ipart] >= 0) {
288  apvphases->get()[partnames[ipart]] = (phases[ipart] + phasechange) % 70;
289  }
290  }
291 
292  iEvent.put(std::move(apvphases));
293 }
294 
295 // ------------ method called once each job just before starting event loop ------------
297 
298 // ------------ method called once each job just after ending the event loop ------------
300 
301 //define this as a plug-in
Log< level::Info, true > LogVerbatim
std::map< edm::EventNumber_t, long long > _offsets
const std::vector< std::string > _defpartnames
APVCyclePhaseProducerFromL1ABC(const edm::ParameterSet &)
void produce(edm::Event &, const edm::EventSetup &) override
T const * product() const
Definition: Handle.h:70
unsigned long long EventNumber_t
edm::EDGetTokenT< L1AcceptBunchCrossingCollection > _l1abccollectionToken
edm::EDGetTokenT< TCDSRecord > _tcdsRecordToken
Class to contain information from TCDS FED.
Definition: TCDSRecord.h:19
TH1F ** makeTH1F(const char *name, const char *title, const unsigned int nbinx, const double xmin, const double xmax)
int iEvent
Definition: GenABIO.cc:224
RunNumber_t run() const
Definition: RunBase.h:40
void endRun(const edm::Run &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< L1AcceptBunchCrossing > L1AcceptBunchCrossingCollection
void beginRun(const edm::Run &, const edm::EventSetup &) override
Log< level::Warning, true > LogPrint
Log< level::Info, false > LogInfo
void beginRun(const edm::Run &iRun)
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
Log< level::Warning, false > LogWarning
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45