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 
46 
48 
49 //
50 // class decleration
51 //
52 
54 public:
57 
58 private:
59  void beginJob() override;
60  void beginRun(const edm::Run&, const edm::EventSetup&) override;
61  void endRun(const edm::Run&, const edm::EventSetup&) override;
62  void produce(edm::Event&, const edm::EventSetup&) override;
63  void endJob() override;
64 
65  // ----------member data ---------------------------
66 
68  const std::vector<std::string> _defpartnames;
69  const std::vector<int> _defphases;
70  const int _orbitoffsetSOR;
71  const bool _wantHistos;
72 
74 
75  TH1F** _hbx;
76  TH1F** _hdbx;
77  TH1F** _hdorbit;
78  const unsigned int _firstgoodrun;
79  std::map<edm::EventNumber_t, long long> _offsets;
80  long long _curroffset;
82 };
83 
84 //
85 // constants, enums and typedefs
86 //
87 
88 //
89 // static data member definitions
90 //
91 
92 //
93 // constructors and destructor
94 //
97  mayConsume<L1AcceptBunchCrossingCollection>(iConfig.getParameter<edm::InputTag>("l1ABCCollection"))),
98  _defpartnames(iConfig.getParameter<std::vector<std::string> >("defaultPartitionNames")),
99  _defphases(iConfig.getParameter<std::vector<int> >("defaultPhases")),
100  _orbitoffsetSOR(iConfig.getParameter<int>("StartOfRunOrbitOffset")),
101  _wantHistos(iConfig.getUntrackedParameter<bool>("wantHistos", false)),
103  _hbx(nullptr),
104  _hdbx(nullptr),
105  _hdorbit(nullptr),
106  _firstgoodrun(110878),
107  _offsets(),
108  _curroffset(0),
109  _curroffevent(0) {
110  produces<APVCyclePhaseCollection, edm::InEvent>();
111 
112  //now do what ever other initialization is needed
113 
114  if (_wantHistos) {
115  _hbx = m_rhm.makeTH1F("l1abcbx", "BX number from L1ABC", 4096, -0.5, 4095.5);
116  _hdbx = m_rhm.makeTH1F("dbx", "BX number difference", 4096 * 2 - 1, -4095.5, 4095.5);
117  _hdorbit = m_rhm.makeTH1F("dorbit", "Orbit Number difference", 9999, -4999.5, 4999.5);
118  }
119 }
120 
122  // do anything here that needs to be done at desctruction time
123  // (e.g. close files, deallocate resources etc.)
124 }
125 
126 //
127 // member functions
128 //
129 
130 // ------------ method called to produce the data ------------
132 
133 {
134  // reset offset vector
135 
136  _offsets.clear();
137  edm::LogInfo("AbsoluteBXOffsetReset") << "Absolute BX offset map reset";
138 
139  if (_wantHistos) {
140  m_rhm.beginRun(iRun);
141 
142  if (_hbx && *_hbx) {
143  (*_hbx)->GetXaxis()->SetTitle("BX");
144  (*_hbx)->GetYaxis()->SetTitle("Events");
145  }
146 
147  if (_hdbx && *_hdbx) {
148  (*_hdbx)->GetXaxis()->SetTitle("#DeltaBX");
149  (*_hdbx)->GetYaxis()->SetTitle("Events");
150  }
151 
152  if (_hdorbit && *_hdorbit) {
153  (*_hdorbit)->GetXaxis()->SetTitle("#Deltaorbit");
154  (*_hdorbit)->GetYaxis()->SetTitle("Events");
155  }
156  }
157 
158  if (iRun.run() < _firstgoodrun) {
159  edm::LogInfo("UnreliableMissingL1AcceptBunchCrossingCollection")
160  << "In this run L1AcceptBunchCrossingCollection is missing or unreliable: default phases will be used";
161  }
162 }
163 
165  // summary of absolute bx offset vector
166 
167  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << "Absolute BX offset summary:";
168  for (std::map<edm::EventNumber_t, long long>::const_iterator offset = _offsets.begin(); offset != _offsets.end();
169  ++offset) {
170  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << offset->first << " " << offset->second;
171  }
172 }
173 
175  using namespace edm;
176 
177  std::unique_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection());
178 
179  const std::vector<int>& phases = _defphases;
180  const std::vector<std::string>& partnames = _defpartnames;
181 
182  // Look for the L1AcceptBunchCrossingCollection
183 
184  int phasechange = 0;
185 
186  if (iEvent.run() >= _firstgoodrun) {
188  iEvent.getByToken(_l1abccollectionToken, pIn);
189 
190  // offset computation
191 
192  long long orbitoffset = _orbitoffsetSOR;
193  int bxoffset = 0;
194 
195  for (L1AcceptBunchCrossingCollection::const_iterator l1abc = pIn->begin(); l1abc != pIn->end(); ++l1abc) {
196  if (l1abc->l1AcceptOffset() == 0) {
197  if (l1abc->eventType() != 0) {
198  orbitoffset = (long long)iEvent.orbitNumber() - (long long)l1abc->orbitNumber();
199  bxoffset = iEvent.bunchCrossing() - l1abc->bunchCrossing();
200 
201  if (_wantHistos) {
202  if (_hbx && *_hbx)
203  (*_hbx)->Fill(l1abc->bunchCrossing());
204  if (_hdbx && *_hdbx)
205  (*_hdbx)->Fill(bxoffset);
206  if (_hdorbit && *_hdorbit)
207  (*_hdorbit)->Fill(orbitoffset);
208  }
209  } else {
210  edm::LogWarning("L1AcceptBunchCrossingNoType") << "L1AcceptBunchCrossing with no type found: ";
211  for (L1AcceptBunchCrossingCollection::const_iterator debu = pIn->begin(); debu != pIn->end(); ++debu) {
212  edm::LogPrint("L1AcceptBunchCrossingNoType") << *debu;
213  }
214  }
215  }
216  }
217 
218  long long absbxoffset = orbitoffset * 3564 + bxoffset;
219 
220  if (orbitoffset != _orbitoffsetSOR)
221  phasechange = (orbitoffset * 3564) % 70;
222 
223  if (_offsets.empty()) {
224  _curroffset = absbxoffset;
225  _curroffevent = iEvent.id().event();
226  _offsets[iEvent.id().event()] = absbxoffset;
227  } else {
228  if (_curroffset != absbxoffset || iEvent.id().event() < _curroffevent) {
229  if (_curroffset != absbxoffset) {
230  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetChanged")
231  << "Absolute BX offset changed from " << _curroffset << " to " << absbxoffset << " at orbit "
232  << iEvent.orbitNumber() << " and BX " << iEvent.bunchCrossing();
233  for (L1AcceptBunchCrossingCollection::const_iterator l1abc = pIn->begin(); l1abc != pIn->end(); ++l1abc) {
234  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << *l1abc;
235  }
236  }
237 
238  _curroffset = absbxoffset;
239  _curroffevent = iEvent.id().event();
240  _offsets[iEvent.id().event()] = absbxoffset;
241  }
242  }
243  }
244 
245  if (phases.size() < partnames.size()) {
246  // throw exception
247  throw cms::Exception("InvalidAPVCyclePhases")
248  << " Inconsistent phases/partitions vector sizes: " << phases.size() << " " << partnames.size();
249  }
250 
251  for (unsigned int ipart = 0; ipart < partnames.size(); ++ipart) {
252  if (phases[ipart] >= 0) {
253  apvphases->get()[partnames[ipart]] = (phases[ipart] + phasechange) % 70;
254  }
255  }
256 
257  iEvent.put(std::move(apvphases));
258 }
259 
260 // ------------ method called once each job just before starting event loop ------------
262 
263 // ------------ method called once each job just after ending the event loop ------------
265 
266 //define this as a plug-in
EventNumber_t event() const
Definition: EventID.h:40
std::map< edm::EventNumber_t, long long > _offsets
const std::vector< std::string > _defpartnames
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
RunNumber_t run() const
Definition: RunBase.h:40
APVCyclePhaseProducerFromL1ABC(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
void produce(edm::Event &, const edm::EventSetup &) override
#define nullptr
int bunchCrossing() const
Definition: EventBase.h:64
unsigned long long EventNumber_t
edm::EDGetTokenT< L1AcceptBunchCrossingCollection > _l1abccollectionToken
TH1F ** makeTH1F(const char *name, const char *title, const unsigned int nbinx, const double xmin, const double xmax)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void endRun(const edm::Run &, const edm::EventSetup &) override
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
RunNumber_t run() const
Definition: Event.h:107
std::vector< L1AcceptBunchCrossing > L1AcceptBunchCrossingCollection
int orbitNumber() const
Definition: EventBase.h:65
void beginRun(const edm::Run &, const edm::EventSetup &) override
std::vector< int > phases
void beginRun(const edm::Run &iRun)
edm::EventID id() const
Definition: EventBase.h:59
HLT enums.
EDGetTokenT< ProductType > mayConsume(edm::InputTag const &tag)
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45