CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalLaserEventFilter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HcalLaserEventFilter
4 // Class: HcalLaserEventFilter
5 //
15 //
16 // Original Author: Jeff Temple (temple@cern.ch)
17 // Created: Thu Nov 17 12:44:22 EST 2011
18 //
19 //
20 
21 
22 // system include files
23 #include <memory>
24 #include <sstream>
25 #include <iostream>
26 #include <string>
27 
28 
29 // user include files
33 
36 
38 
39 // Use for HBHERecHitCollection
41 
43 
45 //
46 // class declaration
47 //
48 
50  public:
51  explicit HcalLaserEventFilter(const edm::ParameterSet&);
53 
54  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
55 
56  private:
57  virtual void beginJob() override;
58  virtual bool filter(edm::Event&, const edm::EventSetup&) override;
59  virtual void endJob() override;
60 
61  std::vector<int> GetCMSSWVersion(std::string const&);
62  bool IsGreaterThanMinCMSSWVersion(std::vector<int> const&);
63 
64  // ----------member data ---------------------------
65 
66  // Filter option 1: veto events by run, event number
68  std::vector<std::pair<edm::RunNumber_t,edm::EventNumber_t> > RunEventData_;
69 
70  // Filter option 2: veto events by HBHE occupancy
72  const unsigned int minOccupiedHBHE_;
73 
74  // Allow for debugging information to be printed
75  const bool debug_;
76  // Reverse the filter decision (so instead of selecting only good events, it
77  // will select only events that fail the filter conditions -- useful for studying
78  // bad events.)
79  const bool reverseFilter_;
80 
81  // InputTag for HBHE rechits
86 
87  const bool taggingMode_;
88 
89  // Control maximum number of error messages to display
92 
93  // Decide whether to use the HcalNoiseSummary to get the RecHit info, or to use the RecHit Collection itself
97  std::vector<int> minVersion_;
98 };
99 
100 //
101 // constants, enums and typedefs
102 //
103 
104 //
105 // static data member definitions
106 //
107 
108 //
109 // constructors and destructor
110 //
112 
113  // Get values from python cfg file
114  : vetoByRunEventNumber_ (iConfig.getUntrackedParameter<bool>("vetoByRunEventNumber",true))
115  , vetoByHBHEOccupancy_ (iConfig.getUntrackedParameter<bool>("vetoByHBHEOccupancy",false))
116  , minOccupiedHBHE_ (iConfig.getUntrackedParameter<unsigned int>("minOccupiedHBHE",5000))
117  , debug_ (iConfig.getUntrackedParameter<bool>("debug",false))
118  , reverseFilter_ (iConfig.getUntrackedParameter<bool>("reverseFilter",false))
119  , hbheInputLabel_ (iConfig.getUntrackedParameter<edm::InputTag>("hbheInputLabel",edm::InputTag("hbhereco")))
120  , hbheToken_ (mayConsume<HBHERecHitCollection>(hbheInputLabel_))
121 
122  , hcalNoiseSummaryLabel_ (iConfig.getUntrackedParameter<edm::InputTag>("hcalNoiseSummaryLabel",edm::InputTag("hcalnoise")))
123  , hcalNoiseSummaryToken_ (mayConsume<HcalNoiseSummary>(hcalNoiseSummaryLabel_))
124  , taggingMode_ (iConfig.getParameter<bool>("taggingMode"))
125  , maxerrormessage_ (iConfig.getUntrackedParameter<int>("maxerrormessage",1))
126  , forceUseRecHitCollection_ (iConfig.getUntrackedParameter<bool>("forceUseRecHitCollection",false))
127  , forceUseHcalNoiseSummary_ (iConfig.getUntrackedParameter<bool>("forceUseHcalNoiseSummary",false))
128 
129 {
130  std::vector<unsigned int> dummy; // dummy empty vector
131  dummy.clear();
132 
133  std::vector<unsigned int> temprunevt = iConfig.getUntrackedParameter<std::vector<unsigned int> >("BadRunEventNumbers",dummy);
134 
135  // Make (run,evt) pairs for storing bad events
136  // Make this a map for better search performance?
137  for (unsigned int i=0;i+1<temprunevt.size();i+=2)
138  {
139  edm::RunNumber_t run=temprunevt[i];
140  edm::EventNumber_t evt=temprunevt[i+1];
141  RunEventData_.push_back(std::make_pair(run,evt));
142  }
143  errorcount=0;
144  produces<bool>();
145 }
146 
147 
149 {
150 
151  // do anything here that needs to be done at desctruction time
152  // (e.g. close files, deallocate resources etc.)
153 
154 }
155 
156 
157 //
158 // member functions
159 //
160 
161 // ------------ method called on each new Event ------------
162 bool
164 {
165  using namespace edm;
166 
167  bool filterDecision=true;
168 
169  if (debug_) std::cout <<"<HcalLaserEventFilter> Run = "<<iEvent.id().run()<<" Event = "<<iEvent.id().event()<<std::endl;
170 
171  // Veto events by run/event numbers
173  {
174  for (unsigned int i=0;i<RunEventData_.size();++i)
175  {
176  if (iEvent.id().run()==RunEventData_[i].first &&
177  iEvent.id().event()==RunEventData_[i].second)
178  {
179  if (debug_) std::cout<<"\t<HcalLaserEventFilter> Filtering bad event; Run "<<iEvent.id().run()<<" Event = "<<iEvent.id().event()<<std::endl;
180  filterDecision=false;
181  break;
182  }
183  }
184  } // if (vetoByRunEventNumber_)
185 
186  //Veto events by HBHE rechit collection size
188  {
189  // The decision on whether or not to use the noise summary is made based on the CMSSW version.
190  // As of CMSSW_5_2_0, the HcalNoiseSummary contains the total number of HBHE hits, as well as number of hits > 1.5 GeV and some other info.
191  // The boolean 'forceUseRecHitCollection_' can be used to override this automatic behavior, and to use the RecHit collection itself, regardless of CMSSW version.
192 
193 
195  //
196  // Apply Filtering based on RecHit information in HBHERecHitcollection
197  //
199 
200 
202  {
204  if (iEvent.getByToken(hbheToken_,hbheRecHits))
205  {
206  if (debug_) std::cout <<"Rechit size = "<<hbheRecHits->size()<<" threshold = "<<minOccupiedHBHE_<<std::endl;
207  if (hbheRecHits->size()>=minOccupiedHBHE_)
208  {
209  if (debug_) std::cout <<"<HcalLaserEventFilter> Filtering because of large HBHE rechit size; "<<hbheRecHits->size()<<" rechits is greater than or equal to the allowed maximum of "<<minOccupiedHBHE_<<std::endl;
210  filterDecision=false;
211  }
212  }
213  else
214  {
216  std::cout <<"<HcalLaserEventFilter::Error> No valid HBHERecHitCollection with label '"<<hbheInputLabel_<<"' found"<<std::endl;
217  ++errorcount;
218  }
219  }
220 
222  //
223  // Apply Filtering based on RecHit information in HcalNoiseSummary object
224  //
226  else if (useHcalNoiseSummary_==true || forceUseHcalNoiseSummary_==true)
227  {
228  Handle<HcalNoiseSummary> hSummary;
229  if (iEvent.getByToken(hcalNoiseSummaryToken_,hSummary)) // get by label, usually with label 'hcalnoise'
230  {
231  if (debug_) std::cout << " RECHIT SIZE (from HcalNoiseSummary) = "<<hSummary->GetRecHitCount()<<" threshold = "<<minOccupiedHBHE_<<std::endl;
232  if (hSummary->GetRecHitCount() >= (int)minOccupiedHBHE_)
233  {
234  if (debug_) std::cout <<"<HcalLaserEventFilter> Filtering because of large HBHE rechit size in HcalNoiseSummary; "<<hSummary->GetRecHitCount()<<" rechits is greater than or equal to the allowed maximum of "<<minOccupiedHBHE_<<std::endl;
235  filterDecision=false;
236  }
237  }
238  else
239  {
241  std::cout <<"<HcalLaserEventFilter::Error> No valid HcalNoiseSummary with label '"<<hcalNoiseSummaryLabel_<<"' found"<<std::endl;
242  ++errorcount;
243  }
244  }
245  }// if (vetoByHBHEOccupancy_)
246 
247  // Reverse decision, if specified by user
248  if (reverseFilter_)
249  filterDecision=!filterDecision;
250 
251  iEvent.put( std::auto_ptr<bool>(new bool(filterDecision)) );
252 
253  return taggingMode_ || filterDecision;
254 }
255 
256 // ------------ method called once each job just before starting event loop ------------
257 void
259 {
260  // Specify the minimum release that has the rechit counts in the HcalNoiseSummary object.
261  // If current release >= that release, then HcalNoiseSummary will be used. Otherwise, Rechit collection will be used.
262  std::string minRelease="\"CMSSW_5_2_0\"";
263 
264  minVersion_=GetCMSSWVersion(minRelease);
265  std::vector <int> currentVersion=GetCMSSWVersion(edm::getReleaseVersion());
266 
267  if (IsGreaterThanMinCMSSWVersion(currentVersion)) // current Version is >= minVersion_
269  else
270  useHcalNoiseSummary_=false;
271 }
272 
273 // ------------ method called once each job just after ending the event loop ------------
274 void
276 }
277 
278 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
279 void
281  //The following says we do not know what parameters are allowed so do no validation
282  // Please change this to state exactly what you do use, even if it is no parameters
284  desc.setUnknown();
285  descriptions.addDefault(desc);
286 }
287 
288 std::vector<int> HcalLaserEventFilter::GetCMSSWVersion(std::string const& instring)
289 {
290  std::vector <int> temp;
291 
292 
294  std::string v1, v2, v3;
295 
296  std::istringstream oss(instring);
297  getline(oss, prefix,'_');
298  getline(oss, v1,'_');
299  getline(oss, v2,'_');
300  getline(oss, v3,'_');
301 
302  std::stringstream buffer(v1);
303  int t;
304  buffer>>t;
305  temp.push_back(t);
306  buffer.str();
307  buffer<<v2;
308  buffer>>t;
309  temp.push_back(t);
310  buffer.str();
311  buffer<<v3;
312  buffer>>t;
313  temp.push_back(t);
314 
315  //std::cout <<"PREFIX = "<<prefix<<" "<<temp[0]<<" "<<temp[1]<<" "<<temp[2]<<std::endl;
316  //( ex: PREFIX = "CMSSW 5 5 5 )
317  return temp;
318 }
319 
320 bool HcalLaserEventFilter::IsGreaterThanMinCMSSWVersion(std::vector<int> const& currentVersion)
321 {
322  // Returns false if current version is less than min version
323  // Otherwise, returns true
324  // Assumes CMSSW versioning X_Y_Z
325 
326 
327 
328  // Compare X
329  if (currentVersion[0]<minVersion_[0]) return false;
330  if (currentVersion[0]>minVersion_[0]) return true;
331  // If neither is true, first value of CMSSW versions are the same
332 
333  // Compare Y
334  if (currentVersion[1]<minVersion_[1]) return false;
335  if (currentVersion[1]>minVersion_[1]) return true;
336 
337  // Compare Z
338  if (currentVersion[2]<minVersion_[2]) return false;
339  if (currentVersion[2]>minVersion_[2]) return true;
340 
341  return true; // versions are identical
342 }
343 
344 
345 //define this as a plug-in
edm::EDGetTokenT< HBHERecHitCollection > hbheToken_
RunNumber_t run() const
Definition: EventID.h:39
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< int > GetCMSSWVersion(std::string const &)
edm::EDGetTokenT< HcalNoiseSummary > hcalNoiseSummaryToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:464
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
const edm::InputTag hcalNoiseSummaryLabel_
const edm::InputTag hbheInputLabel_
HcalLaserEventFilter(const edm::ParameterSet &)
unsigned long long EventNumber_t
virtual bool filter(edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:120
virtual void endJob() override
std::string getReleaseVersion()
std::vector< int > minVersion_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool IsGreaterThanMinCMSSWVersion(std::vector< int > const &)
edm::EventID id() const
Definition: EventBase.h:60
std::vector< std::pair< edm::RunNumber_t, edm::EventNumber_t > > RunEventData_
const unsigned int minOccupiedHBHE_
tuple cout
Definition: gather_cfg.py:121
unsigned int RunNumber_t
volatile std::atomic< bool > shutdown_flag false
virtual void beginJob() override