CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
20 // system include files
21 #include <memory>
22 
23 // user include files
26 
30 
32 
35 
37 
40 
41 #include <map>
42 #include <vector>
43 #include <string>
44 
45 #include "TH1F.h"
46 #include "TProfile.h"
47 
50 
51 //
52 // class decleration
53 //
54 
56  public:
59 
60 private:
61  virtual void beginJob() ;
62  virtual void beginRun(edm::Run&, const edm::EventSetup&);
63  virtual void endRun(edm::Run&, const edm::EventSetup&);
64  virtual void produce(edm::Event&, const edm::EventSetup&);
65  virtual void endJob() ;
66 
67  // ----------member data ---------------------------
68 
70  const std::vector<std::string> _defpartnames;
71  const std::vector<int> _defphases;
72  const int _orbitoffsetSOR;
73  const bool _wantHistos;
74  TH1F* _hbx;
75  TH1F* _hdbx;
76  TH1F* _hdorbit;
77  const unsigned int _firstgoodrun;
78  std::map<unsigned int, long long> _offsets;
79  long long _curroffset;
80  unsigned int _curroffevent;
81 
82 };
83 
84 //
85 // constants, enums and typedefs
86 //
87 
88 
89 //
90 // static data member definitions
91 //
92 
93 //
94 // constructors and destructor
95 //
97  _l1abccollection(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)),
102  _hbx(0),_hdbx(0),_hdorbit(0),_firstgoodrun(110878),
103  _offsets(), _curroffset(0), _curroffevent(0)
104 {
105 
106  produces<APVCyclePhaseCollection,edm::InEvent>();
107 
108  //now do what ever other initialization is needed
109 
110 }
111 
112 
114 {
115 
116  // do anything here that needs to be done at desctruction time
117  // (e.g. close files, deallocate resources etc.)
118 
119 }
120 
121 
122 //
123 // member functions
124 //
125 
126 // ------------ method called to produce the data ------------
127 void
129 
130 {
131 
132  // reset offset vector
133 
134  _offsets.clear();
135  edm::LogInfo("AbsoluteBXOffsetReset") << "Absolute BX offset map reset";
136 
137 
138  if(_wantHistos) {
139 
141 
142  char dirname[300];
143  sprintf(dirname,"run_%d",iRun.run());
144  TFileDirectory subrun = tfserv->mkdir(dirname);
145 
146  _hbx = subrun.make<TH1F>("l1abcbx","BX number from L1ABC",4096,-0.5,4095.5);
147  _hbx->GetXaxis()->SetTitle("BX"); _hbx->GetYaxis()->SetTitle("Events");
148 
149  _hdbx = subrun.make<TH1F>("dbx","BX number difference",4096*2-1,-4095.5,4095.5);
150  _hdbx->GetXaxis()->SetTitle("#DeltaBX"); _hdbx->GetYaxis()->SetTitle("Events");
151 
152  _hdorbit = subrun.make<TH1F>("dorbit","Orbit Number difference",9999,-4999.5,4999.5);
153  _hdorbit->GetXaxis()->SetTitle("#Deltaorbit"); _hdorbit->GetYaxis()->SetTitle("Events");
154 
155  }
156 
157  if(iRun.run() < _firstgoodrun) {
158  edm::LogInfo("UnreliableMissingL1AcceptBunchCrossingCollection") <<
159  "In this run L1AcceptBunchCrossingCollection is missing or unreliable: default phases will be used";
160  }
161 
162 }
163 
164 void
166 {
167  // summary of absolute bx offset vector
168 
169  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << "Absolute BX offset summary:";
170  for(std::map<unsigned int, long long>::const_iterator offset=_offsets.begin();offset!=_offsets.end();++offset) {
171  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetSummary") << offset->first << " " << offset->second;
172  }
173 
174 }
175 
176 
177 void
179 
180  using namespace edm;
181 
182  std::auto_ptr<APVCyclePhaseCollection> apvphases(new APVCyclePhaseCollection() );
183 
184 
185  const std::vector<int>& phases = _defphases;
186  const std::vector<std::string>& partnames = _defpartnames;
187 
188  // Look for the L1AcceptBunchCrossingCollection
189 
190  int phasechange = 0;
191 
192  if(iEvent.run() >= _firstgoodrun ) {
193 
195  iEvent.getByLabel(_l1abccollection,pIn);
196 
197  // offset computation
198 
199  long long orbitoffset = _orbitoffsetSOR;
200  int bxoffset = 0;
201 
202  for(L1AcceptBunchCrossingCollection::const_iterator l1abc=pIn->begin();l1abc!=pIn->end();++l1abc) {
203  if(l1abc->l1AcceptOffset()==0) {
204  if(l1abc->eventType()!=0) {
205  orbitoffset = (long long)iEvent.orbitNumber() - (long long)l1abc->orbitNumber() ;
206  bxoffset = iEvent.bunchCrossing() - l1abc->bunchCrossing();
207 
208  if(_wantHistos) {
209  _hbx->Fill(l1abc->bunchCrossing());
210  _hdbx->Fill(bxoffset);
211  _hdorbit->Fill(orbitoffset);
212  }
213  }
214  else {
215  edm::LogWarning("L1AcceptBunchCrossingNoType") << "L1AcceptBunchCrossing with no type found: ";
216  for(L1AcceptBunchCrossingCollection::const_iterator debu=pIn->begin();debu!=pIn->end();++debu) {
217  edm::LogPrint("L1AcceptBunchCrossingNoType") << *debu;
218  }
219  }
220  }
221  }
222 
223  long long absbxoffset = orbitoffset*3564 + bxoffset;
224 
225  if(orbitoffset != _orbitoffsetSOR) phasechange = (orbitoffset*3564)%70;
226 
227  if(_offsets.size()==0) {
228  _curroffset = absbxoffset;
229  _curroffevent = iEvent.id().event();
230  _offsets[iEvent.id().event()] = absbxoffset;
231  }
232  else {
233  if(_curroffset != absbxoffset || iEvent.id().event() < _curroffevent ) {
234 
235  if( _curroffset != absbxoffset) {
236  edm::LogInfo("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << "Absolute BX offset changed from "
237  << _curroffset << " to "
238  << absbxoffset << " at orbit "
239  << iEvent.orbitNumber() << " and BX "
240  << iEvent.bunchCrossing();
241  for(L1AcceptBunchCrossingCollection::const_iterator l1abc=pIn->begin();l1abc!=pIn->end();++l1abc) {
242  edm::LogVerbatim("L1AcceptBunchCrossingAbsoluteBXOffsetChanged") << *l1abc;
243  }
244  }
245 
246  _curroffset = absbxoffset;
247  _curroffevent = iEvent.id().event();
248  _offsets[iEvent.id().event()] = absbxoffset;
249  }
250  }
251 
252 
253  }
254 
255 
256 
257  if(phases.size() < partnames.size() ) {
258  // throw exception
259  throw cms::Exception("InvalidAPVCyclePhases") << " Inconsistent phases/partitions vector sizes: "
260  << phases.size() << " "
261  << partnames.size();
262  }
263 
264  for(unsigned int ipart=0;ipart<partnames.size();++ipart) {
265  if(phases[ipart]>=0) {
266  apvphases->get()[partnames[ipart]] = (phases[ipart]+phasechange)%70;
267 
268  }
269  }
270 
271 
272  iEvent.put(apvphases);
273 
274 }
275 
276 // ------------ method called once each job just before starting event loop ------------
277 void
279 {
280 }
281 
282 // ------------ method called once each job just after ending the event loop ------------
283 void
285 }
286 
287 //define this as a plug-in
std::map< unsigned int, long long > _offsets
EventNumber_t event() const
Definition: EventID.h:44
const std::vector< std::string > _defpartnames
RunNumber_t run() const
Definition: RunBase.h:44
APVCyclePhaseProducerFromL1ABC(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
int bunchCrossing() const
Definition: EventBase.h:62
virtual void endRun(edm::Run &, const edm::EventSetup &)
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:84
RunNumber_t run() const
Definition: Event.h:66
int orbitNumber() const
Definition: EventBase.h:63
virtual void produce(edm::Event &, const edm::EventSetup &)
unsigned int offset(bool)
virtual void beginRun(edm::Run &, const edm::EventSetup &)
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:359
TFileDirectory mkdir(const std::string &dir, const std::string &descr="")
create a new subdirectory
edm::EventID id() const
Definition: EventBase.h:56
Definition: Run.h:31