File indexing completed on 2024-04-06 12:09:55
0001 #include "DQMOffline/Trigger/interface/HLTTauPostProcessor.h"
0002 #include "DQMServices/Core/interface/DQMStore.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004
0005 #include "TEfficiency.h"
0006 #include "TProfile.h"
0007
0008 #include <tuple>
0009
0010 namespace {
0011 std::tuple<float, float> calcEfficiency(float num, float denom) {
0012 if (denom == 0.0f)
0013 return std::make_tuple(0.0f, 0.0f);
0014
0015
0016 constexpr double cl = 0.683f;
0017 const float eff = num / denom;
0018 const float errDown = TEfficiency::ClopperPearson(denom, num, cl, false);
0019 const float errUp = TEfficiency::ClopperPearson(denom, num, cl, true);
0020
0021
0022 return std::make_tuple(eff, std::max(eff - errDown, errUp - eff));
0023 }
0024 }
0025
0026 HLTTauPostProcessor::HLTTauPostProcessor(const edm::ParameterSet& ps)
0027 : dqmBaseFolder_(ps.getUntrackedParameter<std::string>("DQMBaseFolder")) {}
0028
0029 HLTTauPostProcessor::~HLTTauPostProcessor() = default;
0030
0031 void HLTTauPostProcessor::dqmEndJob(DQMStore::IBooker& iBooker, DQMStore::IGetter& iGetter) {
0032 if (!iGetter.dirExists(dqmBaseFolder_)) {
0033 LogDebug("HLTTauDQMOffline") << "Folder " << dqmBaseFolder_ << " does not exist";
0034 return;
0035 }
0036 iGetter.setCurrentFolder(dqmBaseFolder_);
0037 for (const std::string& subfolder : iGetter.getSubdirs()) {
0038 std::size_t pos = subfolder.rfind('/');
0039 if (pos == std::string::npos)
0040 continue;
0041 ++pos;
0042 if (subfolder.compare(pos, 4, "HLT_") == 0) {
0043 LogDebug("HLTTauDQMOffline") << "Processing path " << subfolder.substr(pos);
0044 plotFilterEfficiencies(iBooker, iGetter, subfolder);
0045 }
0046 }
0047 }
0048
0049 void HLTTauPostProcessor::plotFilterEfficiencies(DQMStore::IBooker& iBooker,
0050 DQMStore::IGetter& iGetter,
0051 const std::string& folder) const {
0052
0053 const MonitorElement* eventsPerFilter = iGetter.get(folder + "/EventsPerFilter");
0054 if (!eventsPerFilter) {
0055 LogDebug("HLTTauDQMOffline") << "ME " << folder << "/EventsPerFilter not found";
0056 return;
0057 }
0058
0059
0060 iBooker.setCurrentFolder(folder);
0061 MonitorElement* efficiency = iBooker.bookProfile("EfficiencyRefPrevious",
0062 "Efficiency to previous filter",
0063 eventsPerFilter->getNbinsX() - 1,
0064 0,
0065 eventsPerFilter->getNbinsX() - 1,
0066 100,
0067 0,
0068 1);
0069 efficiency->setAxisTitle("Efficiency", 2);
0070 const TAxis* xaxis = eventsPerFilter->getTH1F()->GetXaxis();
0071 for (int bin = 1; bin < eventsPerFilter->getNbinsX(); ++bin) {
0072 efficiency->setBinLabel(bin, xaxis->GetBinLabel(bin + 1));
0073 }
0074
0075
0076 TProfile* prev = efficiency->getTProfile();
0077 for (int i = 2; i <= eventsPerFilter->getNbinsX(); ++i) {
0078 if (eventsPerFilter->getBinContent(i - 1) < eventsPerFilter->getBinContent(i)) {
0079 LogDebug("HLTTauDQMOffline") << "HLTTauPostProcessor: Encountered denominator < numerator with efficiency plot "
0080 "EfficiencyRefPrevious in folder "
0081 << folder << ", bin " << i << " numerator " << eventsPerFilter->getBinContent(i)
0082 << " denominator " << eventsPerFilter->getBinContent(i - 1);
0083 continue;
0084 }
0085 const std::tuple<float, float> effErr =
0086 calcEfficiency(eventsPerFilter->getBinContent(i), eventsPerFilter->getBinContent(i - 1));
0087 const float efficiency = std::get<0>(effErr);
0088 const float err = std::get<1>(effErr);
0089
0090 prev->SetBinContent(i - 1, efficiency);
0091 prev->SetBinEntries(i - 1, 1);
0092 prev->SetBinError(i - 1, std::sqrt(efficiency * efficiency + err * err));
0093 }
0094 }
0095
0096 #include "FWCore/Framework/interface/MakerMacros.h"
0097 DEFINE_FWK_MODULE(HLTTauPostProcessor);