CMS 3D CMS Logo

GenFilterEfficiencyProducer.cc
Go to the documentation of this file.
1 // F. Cossutti
2 //
3 
4 // producer of a summary information product on filter efficiency for a user specified path
5 // meant for the generator filter efficiency calculation
6 
7 // system include files
8 #include <memory>
9 #include <string>
10 #include <atomic>
11 
12 // user include files
22 
25 
27 
31 
32 namespace genFilterEff {
33  struct Sums {
34  mutable std::atomic<unsigned int> numEventsPassPos_ ={0};
35  mutable std::atomic<unsigned int> numEventsPassNeg_ ={0};
36  mutable std::atomic<unsigned int> numEventsTotalPos_ ={0};
37  mutable std::atomic<unsigned int> numEventsTotalNeg_ ={0};
38  mutable std::atomic<double> sumpass_w_ ={0};
39  mutable std::atomic<double> sumpass_w2_ ={0};
40  mutable std::atomic<double> sumtotal_w_ ={0};
41  mutable std::atomic<double> sumtotal_w2_ ={0};
42  };
43 }
44 
45 namespace {
46  void atomic_sum_double(std::atomic<double>& oValue, double element) {
47  double v = oValue.load();
48  double sum = v+element;
49  while( not oValue.compare_exchange_strong(v, sum) ) {
50  //some other thread updated oValue
51  sum = v+element;
52  }
53  }
54 }
55 using namespace genFilterEff;
56 
57 class GenFilterEfficiencyProducer : public edm::global::EDProducer<edm::EndLuminosityBlockProducer,
58  edm::LuminosityBlockCache<Sums>> {
59 public:
61  ~GenFilterEfficiencyProducer() override;
62 
63 
64 private:
65  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
66 
67  void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const override;
68  void globalEndLuminosityBlockProduce(edm::LuminosityBlock &, const edm::EventSetup &) const override;
69 
70  std::shared_ptr<Sums> globalBeginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const override;
71  // ----------member data ---------------------------
72 
75 
77 
79 
81  unsigned int pathIndex;
82 
83 };
84 
85 
87  filterPath(iConfig.getParameter<std::string>("filterPath")),
88  tns_(),
89  thisProcess(),pathIndex(100000)
90 {
91  //now do what ever initialization is needed
93  // get tns pointer
95  if (tns_!=nullptr) {
97  std::vector<std::string> theNames = tns_->getTrigPaths();
98  for ( unsigned int i = 0; i < theNames.size(); i++ ) {
99  if ( theNames[i] == filterPath ) { pathIndex = i; continue; }
100  }
101  }
102  else
103  edm::LogError("ServiceNotAvailable") << "TriggerNamesServive not available, no filter information stored";
104  }
105 
106  triggerResultsToken_ = consumes<edm::TriggerResults>(edm::InputTag("TriggerResults","",thisProcess));
107  genEventInfoToken_ = consumes<GenEventInfoProduct>(edm::InputTag("generator",""));
108  produces<GenFilterInfo, edm::Transition::EndLuminosityBlock>();
109 
110 
111 }
112 
113 
115 {
116 
117 }
118 
119 
120 //
121 // member functions
122 //
123 
124 // ------------ method called to for each event ------------
125 void
127 {
128 
130  iEvent.getByToken(triggerResultsToken_,trigR);
131  edm::Handle<GenEventInfoProduct> genEventScale;
132  iEvent.getByToken(genEventInfoToken_,genEventScale);
133  if (!genEventScale.isValid()) return;
134  double weight = genEventScale->weight();
135 
136  auto sums = luminosityBlockCache(iEvent.getLuminosityBlock().index());
137 
138 
139  unsigned int nSize = (*trigR).size();
140  // std::cout << "Number of paths in TriggerResults = " << nSize << std::endl;
141  if ( nSize >= pathIndex ) {
142 
143  if (!trigR->wasrun(pathIndex))return;
144  if ( trigR->accept(pathIndex) ) {
145  atomic_sum_double(sums->sumpass_w_, weight);
146  atomic_sum_double(sums->sumpass_w2_, weight*weight);
147 
148  atomic_sum_double(sums->sumtotal_w_, weight);
149  atomic_sum_double(sums->sumtotal_w2_, weight*weight);
150 
151  if(weight > 0)
152  {
153  sums->numEventsPassPos_++;
154  sums->numEventsTotalPos_++;
155  }
156  else
157  {
158  sums->numEventsPassNeg_++;
159  sums->numEventsTotalNeg_++;
160  }
161 
162  }
163  else // if fail the filter
164  {
165  atomic_sum_double(sums->sumtotal_w_,weight);
166  atomic_sum_double(sums->sumtotal_w2_, weight*weight);
167 
168  if(weight > 0)
169  sums->numEventsTotalPos_++;
170  else
171  sums->numEventsTotalNeg_++;
172  }
173  // std::cout << "Total events = " << numEventsTotal << " passed = " << numEventsPassed << std::endl;
174 
175  }
176 
177 }
178 
179 std::shared_ptr<Sums>
181  return std::make_shared<Sums>();
182 }
183 
184 void
186 }
187 
188 void
190  auto sums = luminosityBlockCache(iLumi.index());
191  std::unique_ptr<GenFilterInfo> thisProduct(new GenFilterInfo(
192  sums->numEventsPassPos_,
193  sums->numEventsPassNeg_,
194  sums->numEventsTotalPos_,
195  sums->numEventsTotalNeg_,
196  sums->sumpass_w_,
197  sums->sumpass_w2_,
198  sums->sumtotal_w_,
199  sums->sumtotal_w2_
200  ));
201  iLumi.put(std::move(thisProduct));
202 }
203 
205 
bool wasrun() const
Was at least one path run?
std::string const & getProcessName() const
std::atomic< unsigned int > numEventsPassNeg_
LuminosityBlockIndex index() const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
std::atomic< unsigned int > numEventsTotalPos_
bool accept() const
Has at least one path accepted the event?
std::shared_ptr< Sums > globalBeginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const override
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
std::atomic< unsigned int > numEventsPassPos_
Definition: weight.py:1
double weight() const
edm::EDGetTokenT< GenEventInfoProduct > genEventInfoToken_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::atomic< double > sumpass_w2_
void put(std::unique_ptr< PROD > product)
Put a new product.
std::atomic< double > sumpass_w_
void globalEndLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &) const override
std::atomic< unsigned int > numEventsTotalNeg_
LuminosityBlock const & getLuminosityBlock() const
Definition: Event.h:97
bool isValid() const
Definition: HandleBase.h:74
std::atomic< double > sumtotal_w_
edm::EDGetTokenT< edm::TriggerResults > triggerResultsToken_
GenFilterEfficiencyProducer(const edm::ParameterSet &)
Strings const & getTrigPaths() const
void globalEndLuminosityBlockProduce(edm::LuminosityBlock &, const edm::EventSetup &) const override
std::atomic< double > sumtotal_w2_
edm::service::TriggerNamesService * tns_
def move(src, dest)
Definition: eostools.py:511