CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalSelectiveReadoutProducer.cc
Go to the documentation of this file.
9 //#include "DataFormats/EcalDigi/interface/EcalMGPASample.h"
10 
12 
13 #include <memory>
14 #include <fstream>
15 
16 
17 using namespace std;
18 
20  : params_(params)
21 {
22  //settings:
23  // settings which are only in python config files:
24  digiProducer_ = params.getParameter<string>("digiProducer");
25  ebdigiCollection_ = params.getParameter<std::string>("EBdigiCollection");
26  eedigiCollection_ = params.getParameter<std::string>("EEdigiCollection");
27  ebSRPdigiCollection_ = params.getParameter<std::string>("EBSRPdigiCollection");
28  eeSRPdigiCollection_ = params.getParameter<std::string>("EESRPdigiCollection");
29  ebSrFlagCollection_ = params.getParameter<std::string>("EBSrFlagCollection");
30  eeSrFlagCollection_ = params.getParameter<std::string>("EESrFlagCollection");
31  trigPrimProducer_ = params.getParameter<string>("trigPrimProducer");
32  trigPrimCollection_ = params.getParameter<string>("trigPrimCollection");
33  trigPrimBypass_ = params.getParameter<bool>("trigPrimBypass");
34  trigPrimBypassMode_ = params.getParameter<int>("trigPrimBypassMode");
35  dumpFlags_ = params.getUntrackedParameter<int>("dumpFlags", 0);
36  writeSrFlags_ = params.getUntrackedParameter<bool>("writeSrFlags", false);
37  produceDigis_ = params.getUntrackedParameter<bool>("produceDigis", true);
38  // settings which can come from either condition database or python configuration file:
39  useCondDb_ = false;
40  try{
41  if(params.getParameter<bool>("configFromCondDB")){
42  useCondDb_ = true;
43  }
44  } catch(cms::Exception){
45  /* pameter not found */
46  edm::LogWarning("EcalSelectiveReadout") << "Parameter configFromCondDB of EcalSelectiveReadout module not found. "
47  "Selective readout configuration will be read from python file.";
48  }
49  if(!useCondDb_){
50  settingsFromFile_ = auto_ptr<EcalSRSettings>(new EcalSRSettings());
53  }
54 
55  //declares the products made by this producer:
56  if(produceDigis_){
57  produces<EBDigiCollection>(ebSRPdigiCollection_);
58  produces<EEDigiCollection>(eeSRPdigiCollection_);
59  }
60 
61  if (writeSrFlags_) {
62  produces<EBSrFlagCollection>(ebSrFlagCollection_);
63  produces<EESrFlagCollection>(eeSrFlagCollection_);
64  }
65 
66  theGeometry = 0;
68  theElecMap = 0;
69 }
70 
71 
72 
74 { }
75 
76 
77 void
79 {
80  //getting selective readout configuration:
82  eventSetup.get<EcalSRSettingsRcd>().get(hSr);
83  settings_ = hSr.product();
84 
85  //gets the trigger primitives:
86  EcalTrigPrimDigiCollection emptyTPColl;
87  const EcalTrigPrimDigiCollection* trigPrims =
88  (trigPrimBypass_ && trigPrimBypassMode_==0)?&emptyTPColl:getTrigPrims(event);
89 
90 
91  //gets the digis from the events:
92  EBDigiCollection dummyEbDigiColl;
93  EEDigiCollection dummyEeDigiColl;
94 
95  const EBDigiCollection* ebDigis = produceDigis_?getEBDigis(event)
96  :&dummyEbDigiColl;
97  const EEDigiCollection* eeDigis = produceDigis_?getEEDigis(event)
98  :&dummyEeDigiColl;
99 
100  //runs the selective readout algorithm:
101  auto_ptr<EBDigiCollection> selectedEBDigis;
102  auto_ptr<EEDigiCollection> selectedEEDigis;
103  auto_ptr<EBSrFlagCollection> ebSrFlags;
104  auto_ptr<EESrFlagCollection> eeSrFlags;
105 
106  if(produceDigis_){
107  selectedEBDigis = auto_ptr<EBDigiCollection>(new EBDigiCollection);
108  selectedEEDigis = auto_ptr<EEDigiCollection>(new EEDigiCollection);
109  }
110 
111  if(writeSrFlags_){
112  ebSrFlags = auto_ptr<EBSrFlagCollection>(new EBSrFlagCollection);
113  eeSrFlags = auto_ptr<EESrFlagCollection>(new EESrFlagCollection);
114  }
115 
116  if(suppressor_.get() == 0){
117  //Check the validity of EcalSRSettings
119 
120  //instantiates the selective readout algorithm:
121  suppressor_ = auto_ptr<EcalSelectiveReadoutSuppressor>(new EcalSelectiveReadoutSuppressor(params_, settings_));
122 
123  // check that everything is up-to-date
124  checkGeometry(eventSetup);
125  checkTriggerMap(eventSetup);
126  checkElecMap(eventSetup);
127  }
128 
129  suppressor_->run(eventSetup, *trigPrims, *ebDigis, *eeDigis,
130  selectedEBDigis.get(), selectedEEDigis.get(),
131  ebSrFlags.get(), eeSrFlags.get());
132 
133  static int iEvent = 1;
134  if(dumpFlags_>=iEvent){
135  ofstream ttfFile("TTF.txt", (iEvent==1?ios::trunc:ios::app));
136  suppressor_->printTTFlags(ttfFile, iEvent,
137  iEvent==1?true:false);
138 
139  ofstream srfFile("SRF.txt", (iEvent==1?ios::trunc:ios::app));
140  if(iEvent==1){
141  suppressor_->getEcalSelectiveReadout()->printHeader(srfFile);
142  }
143  srfFile << "# Event " << iEvent << "\n";
144  suppressor_->getEcalSelectiveReadout()->print(srfFile);
145  srfFile << "\n";
146 
147  ofstream afFile("AF.txt", (iEvent==1?ios::trunc:ios::app));
148  printSrFlags(afFile, *ebSrFlags, *eeSrFlags, iEvent,
149  iEvent==1?true:false);
150  }
151 
152  ++iEvent; //event counter
153 
154  if(produceDigis_){
155  //puts the selected digis into the event:
156  event.put(selectedEBDigis, ebSRPdigiCollection_);
157  event.put(selectedEEDigis, eeSRPdigiCollection_);
158  }
159 
160  //puts the SR flags into the event:
161  if(writeSrFlags_) {
162  event.put(ebSrFlags, ebSrFlagCollection_);
163  event.put(eeSrFlags, eeSrFlagCollection_);
164  }
165 }
166 
167 const EBDigiCollection*
169 {
171  event.getByLabel(digiProducer_, ebdigiCollection_, hEBDigis);
172  //product() method is called before id() in order to get an exception
173  //if the handle is not available (check not done by id() method).
174  const EBDigiCollection* result = hEBDigis.product();
175  static bool firstCall= true;
176  if(firstCall){
177  checkWeights(event, hEBDigis.id());
178  firstCall = false;
179  }
180  return result;
181 }
182 
183 const EEDigiCollection*
185 {
187  event.getByLabel(digiProducer_, eedigiCollection_, hEEDigis);
188  //product() method is called before id() in order to get an exception
189  //if the handle is not available (check not done by id() method).
190  const EEDigiCollection* result = hEEDigis.product();
191  static bool firstCall = true;
192  if(firstCall){
193  checkWeights(event, hEEDigis.id());
194  firstCall = false;
195  }
196  return result;
197 }
198 
201 {
203  event.getByLabel(trigPrimProducer_, trigPrimCollection_, hTPDigis);
204  return hTPDigis.product();
205 }
206 
207 
209 {
210  edm::ESHandle<CaloGeometry> hGeometry;
211  eventSetup.get<CaloGeometryRecord>().get(hGeometry);
212 
213  const CaloGeometry * pGeometry = &*hGeometry;
214 
215  // see if we need to update
216  if(pGeometry != theGeometry) {
217  theGeometry = pGeometry;
218  suppressor_->setGeometry(theGeometry);
219  }
220 }
221 
222 
224 {
225 
227  eventSetup.get<IdealGeometryRecord>().get(eTTmap);
228 
229  const EcalTrigTowerConstituentsMap * pMap = &*eTTmap;
230 
231  // see if we need to update
232  if(pMap!= theTriggerTowerMap) {
233  theTriggerTowerMap = pMap;
234  suppressor_->setTriggerMap(theTriggerTowerMap);
235  }
236 }
237 
238 
240 {
241 
243  eventSetup.get<EcalMappingRcd>().get(eElecmap);
244 
245  const EcalElectronicsMapping * pMap = &*eElecmap;
246 
247  // see if we need to update
248  if(pMap!= theElecMap) {
249  theElecMap = pMap;
250  suppressor_->setElecMap(theElecMap);
251  }
252 }
253 
254 
256  const char tccFlagMarker[] = { 'x', '.', 'S', '?', 'C', 'E', 'E', 'E', 'E'};
259 
260  //static bool firstCall=true;
261  // if(firstCall){
262  // firstCall=false;
263  os << "# TCC flag map\n#\n"
264  "# +-->Phi " << tccFlagMarker[0+1] << ": 000 (low interest)\n"
265  "# | " << tccFlagMarker[1+1] << ": 001 (mid interest)\n"
266  "# | " << tccFlagMarker[2+1] << ": 010 (not valid)\n"
267  "# V Eta " << tccFlagMarker[3+1] << ": 011 (high interest)\n"
268  "# " << tccFlagMarker[4+1] << ": 1xx forced readout (Hw error)\n"
269  "#\n";
270  //}
271 
272  vector<vector<int> > ttf(nEta, vector<int>(nPhi, -1));
274  it != tp.end(); ++it){
275  const EcalTriggerPrimitiveDigi& trigPrim = *it;
276  if(trigPrim.size()>0){
277  int iEta = trigPrim.id().ieta();
278  int iEta0 = iEta<0?iEta+nEta/2:iEta+nEta/2-1;
279  int iPhi0 = trigPrim.id().iphi() - 1;
280  ttf[iEta0][iPhi0] = trigPrim.ttFlag();
281  }
282  }
283  for(int iEta=0; iEta<nEta; ++iEta){
284  for(int iPhi=0; iPhi<nPhi; ++iPhi){
285  os << tccFlagMarker[ttf[iEta][iPhi]+1];
286  }
287  os << "\n";
288  }
289 }
290 
292  const edm::ProductID& noZsDigiId) const{
293  const vector<float> & weights = settings_->dccNormalizedWeights_[0]; //params_.getParameter<vector<double> >("dccNormalizedWeights");
295  static bool warnWeightCnt = true;
296  if((int)weights.size() > nFIRTaps && warnWeightCnt){
297  edm::LogWarning("Configuration") << "The list of DCC zero suppression FIR "
298  "weights given in parameter dccNormalizedWeights is longer "
299  "than the expected depth of the FIR filter :(" << nFIRTaps << "). "
300  "The last weights will be discarded.";
301  warnWeightCnt = false; //it's not needed to repeat the warning.
302  }
303 
304  if(weights.size()>0){
305  int iMaxWeight = 0;
306  double maxWeight = weights[iMaxWeight];
307  //looks for index of maximum weight
308  for(unsigned i=0; i<weights.size(); ++i){
309  if(weights[i]>maxWeight){
310  iMaxWeight = i;
311  maxWeight = weights[iMaxWeight];
312  }
313  }
314 
315  //position of time sample whose maximum weight is applied:
316  int maxWeightBin = settings_->ecalDccZs1stSample_[0] //params_.getParameter<int>("ecalDccZs1stSample")
317  + iMaxWeight;
318 
319  //gets the bin of maximum (in case of raw data it will not exist)
320  int binOfMax = 0;
321  bool rc = getBinOfMax(evt, noZsDigiId, binOfMax);
322 
323  if(rc && maxWeightBin!=binOfMax){
324  edm::LogWarning("Configuration")
325  << "The maximum weight of DCC zero suppression FIR filter is not "
326  "applied to the expected maximum sample(" << binOfMax
327  << (binOfMax==1?"st":(binOfMax==2?"nd":(binOfMax==3?"rd":"th")))
328  << " time sample). This may indicate faulty 'dccNormalizedWeights' "
329  "or 'ecalDccZs1sSample' parameters.";
330  }
331  }
332 }
333 
334 bool
336  const edm::ProductID& noZsDigiId,
337  int& binOfMax) const{
338  bool rc;
339  const edm::Provenance p=evt.getProvenance(noZsDigiId);
341  vector<string> ebDigiParamList = result.getParameterNames();
342  string bofm("binOfMaximum");
343  if(find(ebDigiParamList.begin(), ebDigiParamList.end(), bofm)
344  != ebDigiParamList.end()){//bofm found
345  binOfMax=result.getParameter<int>("binOfMaximum");
346  rc = true;
347  } else{
348  rc = false;
349  }
350  return rc;
351 }
352 
353 void
355  const EBSrFlagCollection& ebSrFlags,
356  const EESrFlagCollection& eeSrFlags,
357  int iEvent,
358  bool withHeader){
359  const char srpFlagMarker[] = {'.', 'z', 'Z', 'F', '4','5','6','7'};
360  if(withHeader){
361  time_t t;
362  time(&t);
363  const char* date = ctime(&t);
364  os << "#SRP flag map\n#\n"
365  "# Generatied on: " << date << "\n#\n"
366  "# +-->Phi/Y " << srpFlagMarker[0] << ": suppressed\n"
367  "# | " << srpFlagMarker[1] << ": ZS 1\n"
368  "# | " << srpFlagMarker[2] << ": ZS 2\n"
369  "# V Eta/X " << srpFlagMarker[3] << ": full readout\n"
370  "#\n";
371  }
372 
373  //EE-,EB,EE+ map wil be written onto file in following format:
374  //
375  // 72
376  // <-------------->
377  // 20
378  // <--->
379  // EEE A +-----> Y
380  // EEEEE | |
381  // EE EE | 20 EE- |
382  // EEEEE | |
383  // EEE V V X
384  // BBBBBBBBBBBBBBBBB A
385  // BBBBBBBBBBBBBBBBB | +-----> Phi
386  // BBBBBBBBBBBBBBBBB | |
387  // BBBBBBBBBBBBBBBBB | 34 EB |
388  // BBBBBBBBBBBBBBBBB | |
389  // BBBBBBBBBBBBBBBBB | V Eta
390  // BBBBBBBBBBBBBBBBB |
391  // BBBBBBBBBBBBBBBBB |
392  // BBBBBBBBBBBBBBBBB V
393  // EEE A +-----> Y
394  // EEEEE | |
395  // EE EE | 20 EE+ |
396  // EEEEE | |
397  // EEE V V X
398  //
399  //
400  //
401  //
402  //event header:
403  if(iEvent>=0){
404  os << "# Event " << iEvent << "\n";
405  }
406 
407  //retrieve flags:
408  const int nEndcaps = 2;
409  const int nScX = 20;
410  const int nScY = 20;
411  int eeSrf[nEndcaps][nScX][nScY];
412  for(size_t i=0; i < sizeof(eeSrf)/sizeof(int); ((int*)eeSrf)[i++] = -1){};
413  for(EESrFlagCollection::const_iterator it = eeSrFlags.begin();
414  it != eeSrFlags.end(); ++it){
415  const EESrFlag& flag = *it;
416  int iZ0 = flag.id().zside()>0?1:0;
417  int iX0 = flag.id().ix()-1;
418  int iY0 = flag.id().iy()-1;
419  assert(iZ0>=0 && iZ0<nEndcaps);
420  assert(iX0>=0 && iX0<nScX);
421  assert(iY0>=0 && iY0<nScY);
422  eeSrf[iZ0][iX0][iY0] = flag.value();
423  }
424  const int nEbTtEta = 34;
425  const int nEeTtEta = 11;
426  const int nTtEta = nEeTtEta*2+nEbTtEta;
427  const int nTtPhi = 72;
428  int ebSrf[nEbTtEta][nTtPhi];
429  for(size_t i=0; i<sizeof(ebSrf)/sizeof(int); ((int*)ebSrf)[i++] = -1){};
430  for(EBSrFlagCollection::const_iterator it = ebSrFlags.begin();
431  it != ebSrFlags.end(); ++it){
432 
433  const EBSrFlag& flag = *it;
434  int iEta = flag.id().ieta();
435  int iEta0 = iEta + nTtEta/2 - (iEta>=0?1:0); //0->55 from eta=-3 to eta=3
436  int iEbEta0 = iEta0 - nEeTtEta;//0->33 from eta=-1.48 to eta=1.48
437  int iPhi0 = flag.id().iphi() - 1;
438  assert(iEbEta0>=0 && iEbEta0<nEbTtEta);
439  assert(iPhi0>=0 && iPhi0<nTtPhi);
440 
441 // cout << __FILE__ << ":" << __LINE__ << ": "
442 // << iEta << "\t" << flag.id().iphi() << " -> "
443 // << iEbEta0 << "\t" << iPhi0
444 // << "... Flag: " << flag.value() << "\n";
445 
446 
447  ebSrf[iEbEta0][iPhi0] = flag.value();
448  }
449 
450 
451  //print flags:
452 
453  //EE-
454  for(int iX0=0; iX0<nScX; ++iX0){
455  for(int iY0=0; iY0<nScY; ++iY0){
456  int srFlag = eeSrf[0][iX0][iY0];
457  assert(srFlag>=-1
458  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
459  os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
460  }
461  os << "\n"; //one Y supercystal column per line
462  } //next supercrystal X-index
463 
464  //EB
465  for(int iEta0 = 0;
466  iEta0 < nEbTtEta;
467  ++iEta0){
468  for(int iPhi0 = 0; iPhi0 < nTtPhi; ++iPhi0){
469  int srFlag = ebSrf[iEta0][iPhi0];
470  assert(srFlag>=-1
471  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
472  os << (srFlag==-1?'?':srpFlagMarker[srFlag]);
473  }
474  os << "\n"; //one phi per line
475  }
476 
477  //EE+
478  for(int iX0=0; iX0<nScX; ++iX0){
479  for(int iY0=0; iY0<nScY; ++iY0){
480  int srFlag = eeSrf[1][iX0][iY0];
481  assert(srFlag>=-1
482  && srFlag<(int)(sizeof(srpFlagMarker)/sizeof(srpFlagMarker[0])));
483  os << (srFlag==-1?' ':srpFlagMarker[srFlag]);
484  }
485  os << "\n"; //one Y supercystal column per line
486  } //next supercrystal X-index
487 
488  //event trailer:
489  os << "\n";
490 }
491 
493  if(settings.dccNormalizedWeights_.size() != 1){
494  throw cms::Exception("Configuration") << "Selective readout emulator, EcalSelectiveReadout, supports only single set of ZS weights. "
495  "while the configuration contains " << settings.dccNormalizedWeights_.size() << " set(s)\n";
496  }
497 
498 // if(settings.dccNormalizedWeights_.size() != 1
499 // && settings.dccNormalizedWeights_.size() != 2
500 // && settings.dccNormalizedWeights_.size() != 54
501 // && settings.dccNormalizedWeights_.size() != 75848){
502 // throw cms::Exception("Configuration") << "Invalid number of DCC weight set (" << settings.dccNormalizedWeights_.size()
503 // << ") in condition object EcalSRSetting::dccNormalizedWeights_. "
504 // << "Valid counts are: 1 (single set), 2 (EB and EE), 54 (one per DCC) and 75848 "
505 // "(one per crystal)\n";
506 // }
507 
508  if(settings.dccNormalizedWeights_.size() != settings.ecalDccZs1stSample_.size()){
509  throw cms::Exception("Configuration") << "Inconsistency between number of weigth sets ("
510  << settings.dccNormalizedWeights_.size() << ") and "
511  << "number of ecalDccZs1Sample values ("
512  << settings.ecalDccZs1stSample_.size() << ").";
513  }
514 }
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
long int flag
Definition: mlp_lapack.h:47
int value() const
Definition: EcalSrFlag.h:45
const EEDigiCollection * getEEDigis(edm::Event &event) const
void checkTriggerMap(const edm::EventSetup &eventSetup)
ProductID id() const
Definition: HandleBase.cc:15
static void importParameterSet(EcalSRSettings &sr, const edm::ParameterSet &ps)
void checkGeometry(const edm::EventSetup &eventSetup)
ParameterSet const & getParameterSet(ParameterSetID const &id)
std::vector< T >::const_iterator const_iterator
std::vector< std::vector< float > > dccNormalizedWeights_
const char tccFlagMarker[]
Definition: GenABIO.cc:189
bool getBinOfMax(const edm::Event &evt, const edm::ProductID &noZsDigiId, int &binOfMax) const
void printTTFlags(const EcalTrigPrimDigiCollection &tp, std::ostream &os) const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
int ieta() const
get the tower ieta
virtual void produce(edm::Event &event, const edm::EventSetup &eventSetup)
ParameterSetID psetID() const
Definition: Provenance.cc:56
void checkElecMap(const edm::EventSetup &eventSetup)
int iEvent
Definition: GenABIO.cc:243
edm::SortedCollection< EBSrFlag > EBSrFlagCollection
int ix() const
Definition: EcalScDetId.h:72
const EcalTrigTowerConstituentsMap * theTriggerTowerMap
edm::SortedCollection< EESrFlag > EESrFlagCollection
std::vector< int > ecalDccZs1stSample_
tuple result
Definition: query.py:137
const EcalElectronicsMapping * theElecMap
std::vector< std::string > getParameterNames() const
std::auto_ptr< EcalSelectiveReadoutSuppressor > suppressor_
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void checkWeights(const edm::Event &evt, const edm::ProductID &noZSDigiId) const
int iy() const
Definition: EcalScDetId.h:78
const_iterator end() const
int iphi() const
get the tower iphi
static const int nEndcaps
Definition: GenABIO.cc:138
const EBDigiCollection * getEBDigis(edm::Event &event) const
const EcalTrigPrimDigiCollection * getTrigPrims(edm::Event &event) const
const EcalTrigTowerDetId & id() const
const EcalScDetId & id() const
Definition: EESrFlag.h:38
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
static const size_t nTriggerTowersInEta
int zside() const
Definition: EcalScDetId.h:66
static const size_t nTriggerTowersInPhi
T const * product() const
Definition: Handle.h:74
const EcalTrigTowerDetId & id() const
Definition: EBSrFlag.h:37
static void printSrFlags(std::ostream &os, const EBSrFlagCollection &ebSrFlags, const EESrFlagCollection &eeSrFlags, int iEvent=-1, bool withHeader=true)
EcalSelectiveReadoutProducer(const edm::ParameterSet &params)
Provenance getProvenance(BranchID const &theID) const
Definition: Event.cc:61
static void checkValidity(const EcalSRSettings &settings)
int ttFlag() const
get the Trigger tower Flag of interesting sample
const_iterator begin() const
const char srpFlagMarker[]
Definition: GenABIO.cc:188
std::auto_ptr< EcalSRSettings > settingsFromFile_