Skip to content

Commit

Permalink
Merge pull request #43020 from mmusich/dev_SiPixelFEDChannelContainer…
Browse files Browse the repository at this point in the history
…MapWeigthed

SiPixel Payload Inspector: introduce utility to display probability-weighted `SiPixelFEDChannelContainer` contents
  • Loading branch information
cmsbuild authored Oct 17, 2023
2 parents eb6caad + db7d86e commit b3c51d5
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

// the data format of the condition to be inspected
#include "CondFormats/SiPixelObjects/interface/SiPixelFEDChannelContainer.h"
#include "CondFormats/SiPixelObjects/interface/SiPixelQualityProbabilities.h" // to display aggregate probability
#include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
Expand Down Expand Up @@ -449,6 +450,218 @@ namespace {
using SiPixelFPixFEDChannelContainerMap = SiPixelFEDChannelContainerMapSimple<SiPixelPI::t_forward>;
using SiPixelFullFEDChannelContainerMap = SiPixelFEDChannelContainerMapSimple<SiPixelPI::t_all>;

/*
Produces an aggregate map of the masked components for all scenarios,
weighted on the probability per PU unit from SiPixelQualityProbabilities
assuming a flat PU profile in the range encoded in SiPixelQualityProbabilities
The SiPixelQualityProbabilities tag comes from user input
*/
template <SiPixelPI::DetType myType>
class SiPixelFEDChannelContainerMapWeigthed : public PlotImage<SiPixelFEDChannelContainer, SINGLE_IOV> {
public:
SiPixelFEDChannelContainerMapWeigthed()
: PlotImage<SiPixelFEDChannelContainer, SINGLE_IOV>(
"SiPixelFEDChannelContainer Pixel Track Map of one (or more scenarios)") {
// for inputs
PlotBase::addInputParam("SiPixelQualityProbabilitiesTag");

// hardcoded connection to the SiPixelQualityProbability tag, though luck
m_condSiPixelProb = "frontier://FrontierProd/CMS_CONDITIONS";
m_connectionPool.setParameters(m_connectionPset);
m_connectionPool.configure();
}

bool fill() override {
auto paramValues = PlotBase::inputParamValues();
auto ip = paramValues.find("SiPixelQualityProbabilitiesTag");
if (ip != paramValues.end()) {
m_SiPixelProbTagName = ip->second;
} else {
edm::LogWarning(k_ClassName) << "\n WARNING!!!! \n The needed parameter SiPixelQualityProbabilitiesTag was not "
"inputed from the user \n Display will be aborted \n\n";
return false;
}

Phase1PixelROCMaps theROCMap("", "Masking Probability [%]");

auto tag = PlotBase::getTag<0>();
auto tagname = tag.name;
auto iov = tag.iovs.front();

// open db session for the cabling map
edm::LogPrint(k_ClassName) << "[SiPixelFEDChannelContainerTest::" << __func__ << "] "
<< "Query the condition database " << m_condSiPixelProb;

cond::persistency::Session condDbSession = m_connectionPool.createSession(m_condSiPixelProb);
condDbSession.transaction().start(true);

// query the database
edm::LogPrint(k_ClassName) << "[SiPixelFEDChannelContainerTest::" << __func__ << "] "
<< "Reading IOVs from tag " << m_SiPixelProbTagName;

const auto MIN_VAL = cond::timeTypeSpecs[cond::runnumber].beginValue;
const auto MAX_VAL = cond::timeTypeSpecs[cond::runnumber].endValue;

// get the list of payloads for the Cabling Map
std::vector<std::tuple<cond::Time_t, cond::Hash>> m_pixelProb_iovs;
condDbSession.readIov(m_SiPixelProbTagName).selectRange(MIN_VAL, MAX_VAL, m_pixelProb_iovs);

// in MC there should be only 1 IOV, oh well...
edm::LogPrint(k_ClassName) << " using the SiPixelQualityProbabilities with hash: "
<< std::get<1>(m_pixelProb_iovs.front()) << std::endl;

auto probabilitiesPayload =
condDbSession.fetchPayload<SiPixelQualityProbabilities>(std::get<1>(m_pixelProb_iovs.front()));

const auto& PUbins = probabilitiesPayload->getPileUpBins();

SiPixelQualityProbabilities::probabilityMap m_probabilities = probabilitiesPayload->getProbability_Map();

// find the PU-averaged (assuming flat PU in the range) probabilities for each scenario
std::map<std::string, float> puAvgedProbabilities;
for (const auto& [PUbin, ProbMap] : m_probabilities) {
float totProbInPUbin = 0.f;
for (const auto& [scenName, prob] : ProbMap) {
totProbInPUbin += prob;
if (puAvgedProbabilities.find(scenName) == puAvgedProbabilities.end()) {
puAvgedProbabilities[scenName] += prob;
} else {
puAvgedProbabilities.insert({scenName, prob});
}
}
LogDebug(k_ClassName) << "PU bin: " << PUbin << " tot probability " << totProbInPUbin << std::endl;
}

std::shared_ptr<SiPixelFEDChannelContainer> payload = fetchPayload(std::get<1>(iov));
const auto& scenarioMap = payload->getScenarioMap();

float totProb{0.f};
for (const auto& [scenName, prob] : puAvgedProbabilities) {
// only sum up the scenarios that are in the SiPixelFEDChannelContainer payload!
if (scenarioMap.find(scenName) != scenarioMap.end()) {
LogDebug(k_ClassName) << scenName << " : " << prob << std::endl;
totProb += prob;
}
}

LogDebug(k_ClassName) << "Total probability to normalize to: " << totProb << std::endl;

//normalize the probabilities per scenario to the toal probability
for (auto& pair : puAvgedProbabilities) {
pair.second /= totProb;
}

for (const auto& scenario : scenarioMap) {
std::string scenName = scenario.first;
LogDebug(k_ClassName) << "\t Found Scenario: " << scenName << " ==> dumping it";

// calculate the weight
float w_frac = 0.f;
if (puAvgedProbabilities.find(scenName) != puAvgedProbabilities.end()) {
w_frac = puAvgedProbabilities[scenName];
}

// if scenario is not in the probability payload, continue
if (w_frac == 0.f)
continue;

LogDebug(k_ClassName) << "scen: " << scenName << " weight: " << w_frac << " log(weight):" << log10(w_frac)
<< std::endl;

const auto& theDetSetBadPixelFedChannels = payload->getDetSetBadPixelFedChannels(scenName);
for (const auto& disabledChannels : *theDetSetBadPixelFedChannels) {
const auto t_detid = disabledChannels.detId();
int subid = DetId(t_detid).subdetId();
LogDebug(k_ClassName) << fmt::sprintf("DetId : %i \n", t_detid) << std::endl;

std::bitset<16> badRocsFromFEDChannels;

for (const auto& ch : disabledChannels) {
std::string toOut_ = fmt::sprintf("fed : %i | link : %2i | roc_first : %2i | roc_last: %2i \n",
ch.fed,
ch.link,
ch.roc_first,
ch.roc_last);

LogDebug(k_ClassName) << toOut_ << std::endl;
for (unsigned int i_roc = ch.roc_first; i_roc <= ch.roc_last; ++i_roc) {
badRocsFromFEDChannels.set(i_roc);
}
}

LogDebug(k_ClassName) << badRocsFromFEDChannels << std::endl;

const auto& myDetId = DetId(t_detid);

if (subid == PixelSubdetector::PixelBarrel) {
theROCMap.fillSelectedRocs(myDetId, badRocsFromFEDChannels, w_frac * 100);
} // if it's barrel
else if (subid == PixelSubdetector::PixelEndcap) {
theROCMap.fillSelectedRocs(myDetId, badRocsFromFEDChannels, w_frac * 100);
} // if it's endcap
else {
throw cms::Exception("LogicError") << "Unknown Pixel SubDet ID " << std::endl;
} // else nonsense
} // loop on the channels
} // loop on the scenarios

gStyle->SetOptStat(0);
//=========================
TCanvas canvas("Summary", "Summary", 1200, k_height[myType]);
canvas.cd();

auto unpacked = SiPixelPI::unpack(std::get<0>(iov));

std::string IOVstring = (unpacked.first == 0)
? std::to_string(unpacked.second)
: (std::to_string(unpacked.first) + "," + std::to_string(unpacked.second));

const auto headerText =
fmt::sprintf("#bf{#scale[0.6]{#color[2]{%s}, #color[4]{%s}}}", tagname, m_SiPixelProbTagName);

switch (myType) {
case SiPixelPI::t_barrel:
theROCMap.drawBarrelMaps(canvas, headerText);
break;
case SiPixelPI::t_forward:
theROCMap.drawForwardMaps(canvas, headerText);
break;
case SiPixelPI::t_all:
theROCMap.drawMaps(canvas, headerText);
break;
default:
throw cms::Exception("LogicError") << "\nERROR: unrecognized Pixel Detector part " << std::endl;
}

// add list of scenarios watermark
canvas.cd();
auto ltx = TLatex();
ltx.SetTextFont(62);
//ltx.SetTextColor(kMagenta);
ltx.SetTextSize(0.023);
ltx.DrawLatexNDC(gPad->GetLeftMargin() - 0.09, gPad->GetBottomMargin() - 0.09, "");

std::string fileName(m_imageFileName);
canvas.SaveAs(fileName.c_str());
return true;
}

private:
// graphics
static constexpr std::array<int, 3> k_height = {{1200, 600, 1600}};
static constexpr const char* k_ClassName = "SiPixelFEDChannelContainerMapWeigthed";

// parameters for auxilliary DB connection
edm::ParameterSet m_connectionPset;
cond::persistency::ConnectionPool m_connectionPool;
std::string m_SiPixelProbTagName;
std::string m_condSiPixelProb;
};

using SiPixelBPixFEDChannelContainerWeightedMap = SiPixelFEDChannelContainerMapWeigthed<SiPixelPI::t_barrel>;
using SiPixelFPixFEDChannelContainerWeightedMap = SiPixelFEDChannelContainerMapWeigthed<SiPixelPI::t_forward>;
using SiPixelFullFEDChannelContainerWeightedMap = SiPixelFEDChannelContainerMapWeigthed<SiPixelPI::t_all>;

/************************************************
1d histogram of number of SiPixelFEDChannelContainer scenarios
*************************************************/
Expand Down Expand Up @@ -547,5 +760,8 @@ PAYLOAD_INSPECTOR_MODULE(SiPixelFEDChannelContainer) {
PAYLOAD_INSPECTOR_CLASS(SiPixelBPixFEDChannelContainerMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelFPixFEDChannelContainerMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelFullFEDChannelContainerMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelBPixFEDChannelContainerWeightedMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelFPixFEDChannelContainerWeightedMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelFullFEDChannelContainerWeightedMap);
PAYLOAD_INSPECTOR_CLASS(SiPixelFEDChannelContainerScenarios);
}
49 changes: 49 additions & 0 deletions CondCore/SiPixelPlugins/test/testSiPixelFEDChannelContainer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# Save current working dir so img can be outputted there later
W_DIR=$(pwd);
source /afs/cern.ch/cms/cmsset_default.sh;
eval `scram run -sh`;
# Go back to original working directory
cd $W_DIR;
# Run get payload data script
if [ -d $W_DIR/plots_FEDChannelContainer ]; then
rm -fr $W_DIR/plots_FEDChannelContainer
fi

mkdir $W_DIR/plots_FEDChannelContainer

getPayloadData.py \
--plugin pluginSiPixelFEDChannelContainer_PayloadInspector \
--plot plot_SiPixelBPixFEDChannelContainerMap \
--tag SiPixelStatusScenarios_StuckTBM_2023_v1_mc \
--input_params '{"Scenarios": "370097_302"}' \
--time_type Run \
--iovs '{"start_iov": "1", "end_iov": "1"}' \
--db Prod \
--test ;

mv *.png $W_DIR/plots_FEDChannelContainer/SiPixelBPixFEDChannelContainerMap.png

getPayloadData.py \
--plugin pluginSiPixelFEDChannelContainer_PayloadInspector \
--plot plot_SiPixelBPixFEDChannelContainerWeightedMap \
--tag SiPixelStatusScenarios_StuckTBMandOther_2023_v2_mc \
--input_params '{"SiPixelQualityProbabilitiesTag": "SiPixelQualityProbabilities_2023_v2_mc"}' \
--time_type Run \
--iovs '{"start_iov": "1", "end_iov": "1"}' \
--db Prod \
--test ;

mv *.png $W_DIR/plots_FEDChannelContainer/SiPixelFEDChannelContainerMapWeigthed.png

getPayloadData.py \
--plugin pluginSiPixelFEDChannelContainer_PayloadInspector \
--plot plot_SiPixelBPixFEDChannelContainerWeightedMap \
--tag SiPixelStatusScenarios_StuckTBM_2023_v1_mc \
--input_params '{"SiPixelQualityProbabilitiesTag": "SiPixelQualityProbabilities_2023_for_eGamma_v1_mc"}' \
--time_type Run \
--iovs '{"start_iov": "1", "end_iov": "1"}' \
--db Prod \
--test ;

mv *.png $W_DIR/plots_FEDChannelContainer/SiPixelFEDChannelContainerMapWeigthed_v2.png
21 changes: 21 additions & 0 deletions CondCore/SiPixelPlugins/test/testSiPixelPayloadInspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "CondCore/SiPixelPlugins/plugins/SiPixelVCal_PayloadInspector.cc"
#include "CondCore/SiPixelPlugins/plugins/SiPixelQualityProbabilities_PayloadInspector.cc"
#include "CondCore/SiPixelPlugins/plugins/SiPixelDynamicInefficiency_PayloadInspector.cc"
#include "CondCore/SiPixelPlugins/plugins/SiPixelFEDChannelContainer_PayloadInspector.cc"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/PluginManager/interface/PluginManager.h"
#include "FWCore/PluginManager/interface/standard.h"
Expand Down Expand Up @@ -241,6 +242,26 @@ int main(int argc, char** argv) {
histo30.process(connectionString, PI::mk_input(tag, start, end, tag2, start2, start2));
edm::LogPrint("testSiPixelPayloadInspector") << histo30.data() << std::endl;

// SiPixelFEDChannelContainer
tag = "SiPixelStatusScenarios_StuckTBMandOther_2023_v2_mc";
tag2 = "SiPixelQualityProbabilities_2023_v2_mc";
start = static_cast<unsigned long long>(1);
end = static_cast<unsigned long long>(1);

edm::LogPrint("testSiPixelPayloadInspector") << "## Exercising SiPixelFEDChannelContainer plots " << std::endl;

inputs["SiPixelQualityProbabilitiesTag"] = tag2; // Quality Probabilities tag to use
SiPixelBPixFEDChannelContainerWeightedMap histo31;
histo31.setInputParamValues(inputs);
histo31.process(connectionString, PI::mk_input(tag, start, end));
edm::LogPrint("testSiPixelPayloadInspector") << histo31.data() << std::endl;

inputs["Scenarios"] = "370097_302";
SiPixelBPixFEDChannelContainerMap histo32;
histo32.setInputParamValues(inputs);
histo32.process(connectionString, PI::mk_input(tag, start, end));
edm::LogPrint("testSiPixelPayloadInspector") << histo32.data() << std::endl;

inputs.clear();
#if PY_MAJOR_VERSION >= 3
// TODO I don't know why this Py_INCREF is necessary...
Expand Down

0 comments on commit b3c51d5

Please sign in to comment.