CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ESTrendTask.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <math.h>
5 
18 
19 using namespace cms;
20 using namespace edm;
21 using namespace std;
22 
24 
25  init_ = false;
26 
27  dqmStore_ = Service<DQMStore>().operator->();
28 
29  prefixME_ = ps.getUntrackedParameter<string>("prefixME", "EcalPreshower");
30  enableCleanup_ = ps.getUntrackedParameter<bool>("enableCleanup", false);
31  mergeRuns_ = ps.getUntrackedParameter<bool>("mergeRuns", false);
32  rechittoken_ = consumes<ESRecHitCollection>(ps.getParameter<InputTag>("RecHitLabel"));
33  dccCollections_ = consumes<ESRawDataCollection>(ps.getParameter<InputTag>("ESDCCCollections"));
34 
35  for (int i=0; i<2; ++i)
36  for (int j=0; j<2; ++j) {
37  hESRecHitTrend_[i][j] = 0;
38  hESRecHitTrendHr_[i][j] = 0;
39  }
40 
41  hESSLinkErrTrend_ = 0;
42  hESFiberErrTrend_ = 0;
43  hESSLinkErrTrendHr_ = 0;
44  hESFiberErrTrendHr_ = 0;
45 
46  start_time_ = 0;
47  current_time_ = 0;
48  last_time_ = 0;
49 
50 }
51 
53 }
54 
56 
57  ievt_ = 0;
58 
59  if ( dqmStore_ ) {
60  dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
61  dqmStore_->rmdir(prefixME_ + "/ESTrendTask");
62  }
63 
64 }
65 
66 void ESTrendTask::beginRun(const Run& r, const EventSetup& c) {
67 
68  if ( ! mergeRuns_ ) this->reset();
69 
70  start_time_ = (r.beginTime()).unixTime();
71 
72  //std::cout << "start time : " << start_time_ << std::endl;
73 
74 }
75 
76 void ESTrendTask::endRun(const Run& r, const EventSetup& c) {
77 
78 }
79 
80 void ESTrendTask::reset(void) {
81 
82  for (int i=0 ; i<2; ++i)
83  for (int j=0 ; j<2; ++j) {
84  if (hESRecHitTrend_[i][j]) hESRecHitTrend_[i][j]->Reset();
85  if (hESRecHitTrendHr_[i][j]) hESRecHitTrendHr_[i][j]->Reset();
86  }
87 
88  if (hESSLinkErrTrend_) hESSLinkErrTrend_->Reset();
89  if (hESFiberErrTrend_) hESFiberErrTrend_->Reset();
90  if (hESSLinkErrTrendHr_) hESSLinkErrTrendHr_->Reset();
91  if (hESFiberErrTrendHr_) hESFiberErrTrendHr_->Reset();
92 
93 }
94 
95 void ESTrendTask::setup(void) {
96 
97  init_ = true;
98 
99  char histo[200];
100 
101  if ( dqmStore_ ) {
102  dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
103 
104  for (int i=0 ; i<2; ++i)
105  for (int j=0 ; j<2; ++j) {
106  int iz = (i==0)? 1:-1;
107  sprintf(histo, "ES Trending RH Occ per 5 mins Z %d P %d", iz, j+1);
108  hESRecHitTrend_[i][j] = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
109  hESRecHitTrend_[i][j]->setAxisTitle("Elapse time (Minutes)", 1);
110  hESRecHitTrend_[i][j]->setAxisTitle("ES RecHit Occupancy / 5 minutes", 2);
111 
112  sprintf(histo, "ES Trending RH Occ per hour Z %d P %d", iz, j+1);
113  hESRecHitTrendHr_[i][j] = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
114  hESRecHitTrendHr_[i][j]->setAxisTitle("Elapse time (Hours)", 1);
115  hESRecHitTrendHr_[i][j]->setAxisTitle("ES RecHit Occupancy / hour", 2);
116  }
117 
118  sprintf(histo, "ES Trending SLink CRC Error per 5 mins");
119  hESSLinkErrTrend_ = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
120  hESSLinkErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
121  hESSLinkErrTrend_->setAxisTitle("ES SLink CRC Err / 5 minutes", 2);
122 
123  sprintf(histo, "ES Trending Fiber Error per 5 mins");
124  hESFiberErrTrend_ = dqmStore_->bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
125  hESFiberErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
126  hESFiberErrTrend_->setAxisTitle("ES Fiber Err / 5 minutes", 2);
127 
128  sprintf(histo, "ES Trending SLink CRC Error per hour");
129  hESSLinkErrTrendHr_ = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
130  hESSLinkErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
131  hESSLinkErrTrendHr_->setAxisTitle("ES SLink CRC Err / hour", 2);
132 
133  sprintf(histo, "ES Trending Fiber Error per hour");
134  hESFiberErrTrendHr_ = dqmStore_->bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
135  hESFiberErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
136  hESFiberErrTrendHr_->setAxisTitle("ES Fiber Err / hour", 2);
137  }
138 
139 }
140 
142 
143  if ( ! init_ ) return;
144 
145  if ( dqmStore_ ) {
146  dqmStore_->setCurrentFolder(prefixME_ + "/ESTrendTask");
147 
148  for (int i=0 ; i<2; ++i)
149  for (int j=0 ; j<2; ++j) {
150  if (hESRecHitTrend_[i][j]) dqmStore_->removeElement(hESRecHitTrend_[i][j]->getName());
151  hESRecHitTrend_[i][j] = 0;
152  if (hESRecHitTrendHr_[i][j]) dqmStore_->removeElement(hESRecHitTrendHr_[i][j]->getName());
153  hESRecHitTrendHr_[i][j] = 0;
154  }
155 
156  if (hESSLinkErrTrend_) dqmStore_->removeElement(hESSLinkErrTrend_->getName());
157  hESSLinkErrTrend_ = 0;
158  if (hESFiberErrTrend_) dqmStore_->removeElement(hESFiberErrTrend_->getName());
159  hESFiberErrTrend_ = 0;
160  if (hESSLinkErrTrendHr_) dqmStore_->removeElement(hESSLinkErrTrendHr_->getName());
161  hESSLinkErrTrendHr_ = 0;
162  if (hESFiberErrTrendHr_) dqmStore_->removeElement(hESFiberErrTrendHr_->getName());
163  hESFiberErrTrendHr_ = 0;
164  }
165 
166  init_ = false;
167 
168 }
169 
171 
172  LogInfo("ESTrendTask") << "analyzed " << ievt_ << " events";
173 
174  if ( enableCleanup_ ) this->cleanup();
175 
176 }
177 
178 void ESTrendTask::analyze(const Event& e, const EventSetup& c) {
179 
180  if ( ! init_ ) this->setup();
181 
182  ievt_++;
183 
184  // Collect time information
185  updateTime(e);
186 
187  long int diff_current_start = current_time_ - start_time_;
188  long int diff_last_start = last_time_ - start_time_;
189  //LogInfo("ESTrendTask") << "time difference is negative in " << ievt_ << " events\n"
190  //<< "\tcurrent - start time = " << diff_current_start
191  //<< ", \tlast - start time = " << diff_last_start << endl;
192 
193  // std::cout << "current_time : " << current_time_ << ", diff : " << diff_current_start << std::endl;
194 
195  // Calculate time interval and bin width
196  // int minuteBinWidth = int(nBasicClusterMinutely_->getTProfile()->GetXaxis()->GetBinWidth(1));
197  int minuteBinWidth = 5;
198  long int minuteBinDiff = diff_current_start/60/minuteBinWidth - diff_last_start/60/minuteBinWidth;
199  long int minuteDiff = (current_time_ - last_time_)/60;
200 
201  // int hourBinWidth = int(nBasicClusterHourly_->getTProfile()->GetXaxis()->GetBinWidth(1));
202  int hourBinWidth = 1;
203  long int hourBinDiff = diff_current_start/3600/hourBinWidth - diff_last_start/3600/hourBinWidth;
204  long int hourDiff = (current_time_ - last_time_)/3600;
205 
206  if (minuteDiff >= minuteBinWidth) {
207  while (minuteDiff >= minuteBinWidth) minuteDiff -= minuteBinWidth;
208  }
209  if (hourDiff >= hourBinWidth) {
210  while (hourDiff >= hourBinWidth) hourDiff -= hourBinWidth;
211  }
212 
213  // ES DCC
214  Int_t slinkCRCErr = 0;
215  Int_t fiberErr = 0;
216  vector<int> fiberStatus;
218  if ( e.getByToken(dccCollections_, dccs) ) {
219  for (ESRawDataCollection::const_iterator dccItr = dccs->begin(); dccItr != dccs->end(); ++dccItr) {
220  ESDCCHeaderBlock dcc = (*dccItr);
221 
222  if (dcc.getDCCErrors() == 101) slinkCRCErr++;
223 
224  fiberStatus = dcc.getFEChannelStatus();
225  for (unsigned int i=0; i<fiberStatus.size(); ++i) {
226  if (fiberStatus[i]==4 || fiberStatus[i]==8 || fiberStatus[i]==10 || fiberStatus[i]==11 || fiberStatus[i]==12)
227  fiberErr++;
228  }
229 
230  }
231  }
232 
233  shift2Right(hESSLinkErrTrend_->getTProfile(), minuteBinDiff);
234  hESSLinkErrTrend_->Fill(minuteDiff, slinkCRCErr);
235 
236  shift2Right(hESFiberErrTrend_->getTProfile(), minuteBinDiff);
237  hESFiberErrTrend_->Fill(minuteDiff, fiberErr);
238 
239  shift2Right(hESSLinkErrTrendHr_->getTProfile(), hourBinDiff);
240  hESSLinkErrTrendHr_->Fill(hourDiff, slinkCRCErr);
241 
242  shift2Right(hESFiberErrTrendHr_->getTProfile(), hourBinDiff);
243  hESFiberErrTrendHr_->Fill(hourDiff, fiberErr);
244 
245  // ES RecHits
246  int zside, plane;
247  int nrh[2][2];
248  for (int i = 0; i < 2; i++ )
249  for( int j = 0; j < 2; j++) {
250  nrh[i][j] = 0;
251  }
252 
254  if ( e.getByToken(rechittoken_, ESRecHit) ) {
255 
256  for (ESRecHitCollection::const_iterator hitItr = ESRecHit->begin(); hitItr != ESRecHit->end(); ++hitItr) {
257 
258  ESDetId id = ESDetId(hitItr->id());
259 
260  zside = id.zside();
261  plane = id.plane();
262 
263  int i = (zside==1)? 0:1;
264  int j = plane-1;
265 
266  nrh[i][j]++;
267  }
268  } else {
269  LogWarning("ESTrendTask") << "RecHitCollection not available";
270  }
271 
272  for (int i=0; i<2; ++i)
273  for (int j=0; j<2; ++j) {
274  shift2Right(hESRecHitTrend_[i][j]->getTProfile(), minuteBinDiff);
275  hESRecHitTrend_[i][j]->Fill(minuteDiff, nrh[i][j]/(1072*32.));
276 
277  shift2Right(hESRecHitTrendHr_[i][j]->getTProfile(), hourBinDiff);
278  hESRecHitTrendHr_[i][j]->Fill(hourDiff, nrh[i][j]/(1072*32.));
279  }
280 
281 }
282 
284 
285  last_time_ = current_time_;
286  current_time_ = e.time().unixTime();
287 
288 }
289 
290 void ESTrendTask::shift2Right(TProfile* p, int bins) {
291 
292  if(bins <= 0) return;
293 
294  if(!p->GetSumw2()) p->Sumw2();
295  int nBins = p->GetXaxis()->GetNbins();
296 
297  // by shifting n bin to the right, the number of entries are
298  // reduced by the number in n bins including the overflow bin.
299  double nentries = p->GetEntries();
300  for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(nBins+1-bins);
301  p->SetEntries(nentries);
302 
303  // the last bin goes to overflow
304  // each bin moves to the right
305 
306  TArrayD* sumw2 = p->GetSumw2();
307 
308  for(int i=nBins+1; i>bins; i--) {
309  // GetBinContent return binContent/binEntries
310  p->SetBinContent(i, p->GetBinContent(i-bins)*p->GetBinEntries(i-bins));
311  p->SetBinEntries(i,p->GetBinEntries(i-bins));
312  sumw2->SetAt(sumw2->GetAt(i-bins),i);
313  }
314 
315 }
316 
317 void ESTrendTask::shift2Left(TProfile* p, int bins) {
318 
319  if(bins <= 0) return;
320 
321  if(!p->GetSumw2()) p->Sumw2();
322  int nBins = p->GetXaxis()->GetNbins();
323 
324  // by shifting n bin to the left, the number of entries are
325  // reduced by the number in n bins including the underflow bin.
326  double nentries = p->GetEntries();
327  for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(i);
328  p->SetEntries(nentries);
329 
330  // the first bin goes to underflow
331  // each bin moves to the right
332 
333  TArrayD* sumw2 = p->GetSumw2();
334 
335  for(int i=0; i<=nBins+1-bins; i++) {
336  // GetBinContent return binContent/binEntries
337  p->SetBinContent(i, p->GetBinContent(i+bins)*p->GetBinEntries(i+bins));
338  p->SetBinEntries(i,p->GetBinEntries(i+bins));
339  sumw2->SetAt(sumw2->GetAt(i+bins),i);
340  }
341 
342 }
343 
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
TProfile * getTProfile(std::string name, std::string process, std::string rootfolder, DQMStore *dbe_, bool verb, bool clone)
virtual ~ESTrendTask()
Definition: ESTrendTask.cc:52
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:434
void beginJob(void)
Definition: ESTrendTask.cc:55
void endJob(void)
Definition: ESTrendTask.cc:170
std::vector< ESDCCHeaderBlock >::const_iterator const_iterator
static void cleanup(const Factory::MakerMap::value_type &v)
Definition: Factory.cc:12
void shift2Right(TProfile *p, int bins=1)
Definition: ESTrendTask.cc:290
const std::vector< int > & getFEChannelStatus() const
void shift2Left(TProfile *p, int bins=1)
Definition: ESTrendTask.cc:317
void setup(void)
Definition: ESTrendTask.cc:95
void beginRun(const edm::Run &r, const edm::EventSetup &c)
Definition: ESTrendTask.cc:66
void analyze(const edm::Event &e, const edm::EventSetup &c)
Definition: ESTrendTask.cc:178
int j
Definition: DBlmapReader.cc:9
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
int zside() const
Definition: ESDetId.h:44
void reset(void)
Definition: ESTrendTask.cc:80
DEFINE_FWK_MODULE(CaloMETProducer)
void endRun(const edm::Run &r, const edm::EventSetup &c)
Definition: ESTrendTask.cc:76
ESTrendTask(const edm::ParameterSet &ps)
Definition: ESTrendTask.cc:23
Timestamp const & beginTime() const
Definition: RunBase.h:43
int getDCCErrors() const
void shift2Right(TH1 *h, int bins)
void cleanup(void)
Definition: ESTrendTask.cc:141
void reset(double vett[256])
Definition: TPedValues.cc:11
void updateTime(const edm::Event &)
Definition: ESTrendTask.cc:283
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
edm::Timestamp time() const
Definition: EventBase.h:57
Definition: Run.h:41