CMS 3D CMS Logo

ESTrendTask.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <vector>
4 #include <cmath>
5 
18 
19 using namespace cms;
20 using namespace edm;
21 using namespace std;
22 
24 
25  prefixME_ = ps.getUntrackedParameter<string>("prefixME", "EcalPreshower");
26  rechittoken_ = consumes<ESRecHitCollection>(ps.getParameter<InputTag>("RecHitLabel"));
27  dccCollections_ = consumes<ESRawDataCollection>(ps.getParameter<InputTag>("ESDCCCollections"));
28 
29  for (int i=0; i<2; ++i)
30  for (int j=0; j<2; ++j) {
31  hESRecHitTrend_[i][j] = nullptr;
32  hESRecHitTrendHr_[i][j] = nullptr;
33  }
34 
35  hESSLinkErrTrend_ = nullptr;
36  hESFiberErrTrend_ = nullptr;
37  hESSLinkErrTrendHr_ = nullptr;
38  hESFiberErrTrendHr_ = nullptr;
39 
40  start_time_ = 0;
41  current_time_ = 0;
42  last_time_ = 0;
43 
44  ievt_ = 0;
45 }
46 
47 void ESTrendTask::dqmBeginRun(const Run& r, const EventSetup& c) {
48 
49  start_time_ = (r.beginTime()).unixTime();
50 
51  //std::cout << "start time : " << start_time_ << std::endl;
52 
53 }
54 
56 {
57  char histo[200];
58 
59  iBooker.setCurrentFolder(prefixME_ + "/ESTrendTask");
60 
61  for (int i=0 ; i<2; ++i)
62  for (int j=0 ; j<2; ++j) {
63  int iz = (i==0)? 1:-1;
64  sprintf(histo, "ES Trending RH Occ per 5 mins Z %d P %d", iz, j+1);
65  hESRecHitTrend_[i][j] = iBooker.bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
66  hESRecHitTrend_[i][j]->setAxisTitle("Elapse time (Minutes)", 1);
67  hESRecHitTrend_[i][j]->setAxisTitle("ES RecHit Occupancy / 5 minutes", 2);
68 
69  sprintf(histo, "ES Trending RH Occ per hour Z %d P %d", iz, j+1);
70  hESRecHitTrendHr_[i][j] = iBooker.bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
71  hESRecHitTrendHr_[i][j]->setAxisTitle("Elapse time (Hours)", 1);
72  hESRecHitTrendHr_[i][j]->setAxisTitle("ES RecHit Occupancy / hour", 2);
73  }
74 
75  sprintf(histo, "ES Trending SLink CRC Error per 5 mins");
76  hESSLinkErrTrend_ = iBooker.bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
77  hESSLinkErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
78  hESSLinkErrTrend_->setAxisTitle("ES SLink CRC Err / 5 minutes", 2);
79 
80  sprintf(histo, "ES Trending Fiber Error per 5 mins");
81  hESFiberErrTrend_ = iBooker.bookProfile(histo, histo, 36, 0.0, 180.0, 100, 0.0, 1.0e6, "s");
82  hESFiberErrTrend_->setAxisTitle("Elapse time (Minutes)", 1);
83  hESFiberErrTrend_->setAxisTitle("ES Fiber Err / 5 minutes", 2);
84 
85  sprintf(histo, "ES Trending SLink CRC Error per hour");
86  hESSLinkErrTrendHr_ = iBooker.bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
87  hESSLinkErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
88  hESSLinkErrTrendHr_->setAxisTitle("ES SLink CRC Err / hour", 2);
89 
90  sprintf(histo, "ES Trending Fiber Error per hour");
91  hESFiberErrTrendHr_ = iBooker.bookProfile(histo, histo, 24, 0.0, 24.0, 100, 0.0, 1.0e6, "s");
92  hESFiberErrTrendHr_->setAxisTitle("Elapse time (Hours)", 1);
93  hESFiberErrTrendHr_->setAxisTitle("ES Fiber Err / hour", 2);
94 }
95 
96 void ESTrendTask::endJob(void) {
97 
98  LogInfo("ESTrendTask") << "analyzed " << ievt_ << " events";
99 
100 }
101 
102 void ESTrendTask::analyze(const Event& e, const EventSetup& c) {
103 
104  ievt_++;
105 
106  // Collect time information
107  updateTime(e);
108 
109  long int diff_current_start = current_time_ - start_time_;
110  long int diff_last_start = last_time_ - start_time_;
111  //LogInfo("ESTrendTask") << "time difference is negative in " << ievt_ << " events\n"
112  //<< "\tcurrent - start time = " << diff_current_start
113  //<< ", \tlast - start time = " << diff_last_start << endl;
114 
115  // std::cout << "current_time : " << current_time_ << ", diff : " << diff_current_start << std::endl;
116 
117  // Calculate time interval and bin width
118  // int minuteBinWidth = int(nBasicClusterMinutely_->getTProfile()->GetXaxis()->GetBinWidth(1));
119  int minuteBinWidth = 5;
120  long int minuteBinDiff = diff_current_start/60/minuteBinWidth - diff_last_start/60/minuteBinWidth;
121  long int minuteDiff = (current_time_ - last_time_)/60;
122 
123  // int hourBinWidth = int(nBasicClusterHourly_->getTProfile()->GetXaxis()->GetBinWidth(1));
124  int hourBinWidth = 1;
125  long int hourBinDiff = diff_current_start/3600/hourBinWidth - diff_last_start/3600/hourBinWidth;
126  long int hourDiff = (current_time_ - last_time_)/3600;
127 
128  if (minuteDiff >= minuteBinWidth) {
129  while (minuteDiff >= minuteBinWidth) minuteDiff -= minuteBinWidth;
130  }
131  if (hourDiff >= hourBinWidth) {
132  while (hourDiff >= hourBinWidth) hourDiff -= hourBinWidth;
133  }
134 
135  // ES DCC
136  Int_t slinkCRCErr = 0;
137  Int_t fiberErr = 0;
138  vector<int> fiberStatus;
140  if ( e.getByToken(dccCollections_, dccs) ) {
141  for (ESRawDataCollection::const_iterator dccItr = dccs->begin(); dccItr != dccs->end(); ++dccItr) {
142  ESDCCHeaderBlock dcc = (*dccItr);
143 
144  if (dcc.getDCCErrors() == 101) slinkCRCErr++;
145 
146  fiberStatus = dcc.getFEChannelStatus();
147  for (unsigned int i=0; i<fiberStatus.size(); ++i) {
148  if (fiberStatus[i]==4 || fiberStatus[i]==8 || fiberStatus[i]==10 || fiberStatus[i]==11 || fiberStatus[i]==12)
149  fiberErr++;
150  }
151 
152  }
153  }
154 
155  shift2Right(hESSLinkErrTrend_->getTProfile(), minuteBinDiff);
156  hESSLinkErrTrend_->Fill(minuteDiff, slinkCRCErr);
157 
158  shift2Right(hESFiberErrTrend_->getTProfile(), minuteBinDiff);
159  hESFiberErrTrend_->Fill(minuteDiff, fiberErr);
160 
161  shift2Right(hESSLinkErrTrendHr_->getTProfile(), hourBinDiff);
162  hESSLinkErrTrendHr_->Fill(hourDiff, slinkCRCErr);
163 
164  shift2Right(hESFiberErrTrendHr_->getTProfile(), hourBinDiff);
165  hESFiberErrTrendHr_->Fill(hourDiff, fiberErr);
166 
167  // ES RecHits
168  int zside, plane;
169  int nrh[2][2];
170  for (int i = 0; i < 2; i++ )
171  for( int j = 0; j < 2; j++) {
172  nrh[i][j] = 0;
173  }
174 
176  if ( e.getByToken(rechittoken_, ESRecHit) ) {
177 
178  for (ESRecHitCollection::const_iterator hitItr = ESRecHit->begin(); hitItr != ESRecHit->end(); ++hitItr) {
179 
180  ESDetId id = ESDetId(hitItr->id());
181 
182  zside = id.zside();
183  plane = id.plane();
184 
185  int i = (zside==1)? 0:1;
186  int j = plane-1;
187 
188  nrh[i][j]++;
189  }
190  } else {
191  LogWarning("ESTrendTask") << "RecHitCollection not available";
192  }
193 
194  for (int i=0; i<2; ++i)
195  for (int j=0; j<2; ++j) {
196  shift2Right(hESRecHitTrend_[i][j]->getTProfile(), minuteBinDiff);
197  hESRecHitTrend_[i][j]->Fill(minuteDiff, nrh[i][j]/(1072*32.));
198 
199  shift2Right(hESRecHitTrendHr_[i][j]->getTProfile(), hourBinDiff);
200  hESRecHitTrendHr_[i][j]->Fill(hourDiff, nrh[i][j]/(1072*32.));
201  }
202 
203 }
204 
206 
207  last_time_ = current_time_;
208  current_time_ = e.time().unixTime();
209 
210 }
211 
212 void ESTrendTask::shift2Right(TProfile* p, int bins) {
213 
214  if(bins <= 0) return;
215 
216  if(!p->GetSumw2()) p->Sumw2();
217  int nBins = p->GetXaxis()->GetNbins();
218 
219  // by shifting n bin to the right, the number of entries are
220  // reduced by the number in n bins including the overflow bin.
221  double nentries = p->GetEntries();
222  for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(nBins+1-bins);
223  p->SetEntries(nentries);
224 
225  // the last bin goes to overflow
226  // each bin moves to the right
227 
228  TArrayD* sumw2 = p->GetSumw2();
229 
230  for(int i=nBins+1; i>bins; i--) {
231  // GetBinContent return binContent/binEntries
232  p->SetBinContent(i, p->GetBinContent(i-bins)*p->GetBinEntries(i-bins));
233  p->SetBinEntries(i,p->GetBinEntries(i-bins));
234  sumw2->SetAt(sumw2->GetAt(i-bins),i);
235  }
236 
237 }
238 
239 void ESTrendTask::shift2Left(TProfile* p, int bins) {
240 
241  if(bins <= 0) return;
242 
243  if(!p->GetSumw2()) p->Sumw2();
244  int nBins = p->GetXaxis()->GetNbins();
245 
246  // by shifting n bin to the left, the number of entries are
247  // reduced by the number in n bins including the underflow bin.
248  double nentries = p->GetEntries();
249  for(int i=0; i<bins; i++) nentries -= p->GetBinEntries(i);
250  p->SetEntries(nentries);
251 
252  // the first bin goes to underflow
253  // each bin moves to the right
254 
255  TArrayD* sumw2 = p->GetSumw2();
256 
257  for(int i=0; i<=nBins+1-bins; i++) {
258  // GetBinContent return binContent/binEntries
259  p->SetBinContent(i, p->GetBinContent(i+bins)*p->GetBinEntries(i+bins));
260  p->SetBinEntries(i,p->GetBinEntries(i+bins));
261  sumw2->SetAt(sumw2->GetAt(i+bins),i);
262  }
263 
264 }
265 
void dqmBeginRun(const edm::Run &r, const edm::EventSetup &c) override
Definition: ESTrendTask.cc:47
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
MonitorElement * bookProfile(Args &&...args)
Definition: DQMStore.h:113
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::vector< T >::const_iterator const_iterator
void shift2Right(TProfile *p, int bins=1)
Definition: ESTrendTask.cc:212
int zside(DetId const &)
const std::vector< int > & getFEChannelStatus() const
void shift2Left(TProfile *p, int bins=1)
Definition: ESTrendTask.cc:239
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
Definition: ESTrendTask.cc:55
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:268
unsigned int unixTime() const
Time in seconds since January 1, 1970.
Definition: Timestamp.h:46
int zside() const
Definition: ESDetId.h:39
Namespace of DDCMS conversion namespace.
const_iterator end() const
ESTrendTask(const edm::ParameterSet &ps)
Definition: ESTrendTask.cc:23
Timestamp const & beginTime() const
Definition: RunBase.h:41
HLT enums.
int getDCCErrors() const
void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)
void analyze(const edm::Event &e, const edm::EventSetup &c) override
Definition: ESTrendTask.cc:102
void updateTime(const edm::Event &)
Definition: ESTrendTask.cc:205
edm::Timestamp time() const
Definition: EventBase.h:60
const_iterator begin() const
Definition: Run.h:45
void endJob(void) override
Definition: ESTrendTask.cc:96