Skip to content

Commit

Permalink
add HGCal configuration ESProducers + record + tester
Browse files Browse the repository at this point in the history
  • Loading branch information
IzaakWN authored and pfs committed Jul 24, 2024
1 parent 3907071 commit 394aade
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 71 deletions.
28 changes: 28 additions & 0 deletions CondFormats/DataRecord/interface/HGCalModuleConfigurationRcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CondFormats_HGCalModuleConfigurationRcd_h
#define CondFormats_HGCalModuleConfigurationRcd_h
// -*- C++ -*-
//
// Package: CondFormats/DataRecord
// Class : HGCalModuleConfigurationRcd
//
/**\class HGCalModuleConfigurationRcd HGCalModuleConfigurationRcd.h CondFormats/DataRecord/interface/HGCalModuleConfigurationRcd.h
*
* Description:
* This record is used for passing the configuration parameters to the calibration step in RAW -> RECO,
* This record depends on the HGCalMappingModuleIndexerRcd.
*
*/
//
// Author: Pedro Da Silva, Izaak Neutelings
// Created: Mon, 29 May 2023 09:13:07 GMT
//

#include "FWCore/Framework/interface/DependentRecordImplementation.h"
#include "FWCore/Utilities/interface/mplVector.h"
#include "CondFormats/DataRecord/interface/HGCalMappingModuleIndexerRcd.h"

class HGCalModuleConfigurationRcd
: public edm::eventsetup::DependentRecordImplementation<HGCalModuleConfigurationRcd,
edm::mpl::Vector<HGCalMappingModuleIndexerRcd> > {};

#endif
15 changes: 15 additions & 0 deletions CondFormats/DataRecord/src/HGCalModuleConfigurationRcd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -*- C++ -*-
//
// Package: CondFormats/DataRecord
// Class : HGCalModuleConfigurationRcd
//
// Implementation:
// [Notes on implementation]
//
// Author: Pedro Da Silva, Izaak Neutelings
// Created: Mon, 29 May 2023 09:13:07 GMT

#include "CondFormats/DataRecord/interface/HGCalModuleConfigurationRcd.h"
#include "FWCore/Framework/interface/eventsetuprecord_registration_macro.h"

EVENTSETUP_RECORD_REG(HGCalModuleConfigurationRcd);
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ namespace hgcalrechit {
// SoA with channel-level calibration parameters in host memory:
// pedestal, CM_slope, CM_ped, BXm1_kappa
using HGCalCalibParamHostCollection = PortableHostCollection<HGCalCalibParamSoA>;

// SoA with channel-level configuration parameters in host memory:
// ...
//using HGCalChannelConfigParamHostCollection = PortableHostCollection<HGCalChannelConfigParamSoA>;


// SoA with ROC-level configuration parameters in host memory:
// gain
using HGCalConfigParamHostCollection = PortableHostCollection<HGCalConfigParamSoA>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ namespace hgcalrechit {

// Generate structure of ROC-level arrays (SoA) layout with RecHit dataformat
GENERATE_SOA_LAYOUT(HGCalConfigParamSoALayout,
SOA_SCALAR(HGCalMappingModuleIndexer, map),
SOA_COLUMN(uint8_t, gain)
SOA_SCALAR(HGCalMappingModuleIndexer, map), // map elecId <-> dense idx
SOA_COLUMN(uint8_t, gain) // for ADC to charge (fC) conversion (80, 160, 320 fC)
)
using HGCalConfigParamSoA = HGCalConfigParamSoALayout<>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/ESProducts.h"
#include "FWCore/Framework/interface/ESTransientHandle.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"
Expand All @@ -17,16 +16,9 @@
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"

#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
//#include "CondFormats/DataRecord/interface/HGCalCondSerializableModuleInfoRcd.h"
//#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableModuleInfo.h"
//#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
#include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
#include "CondFormats/DataRecord/interface/HGCalMappingModuleIndexerRcd.h"

//#include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
#include "CondFormats/HGCalObjects/interface/HGCalMappingParameterHostCollection.h"
#include "CondFormats/HGCalObjects/interface/alpaka/HGCalMappingParameterDeviceCollection.h"

#include "RecoLocalCalo/HGCalRecAlgos/interface/HGCalCalibrationParameterHostCollection.h"
#include "RecoLocalCalo/HGCalRecAlgos/interface/alpaka/HGCalCalibrationParameterDeviceCollection.h"

Expand All @@ -38,7 +30,6 @@
#include <nlohmann/json.hpp>
using json = nlohmann::json;


namespace ALPAKA_ACCELERATOR_NAMESPACE {

namespace hgcalrechit {
Expand All @@ -57,7 +48,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<std::string>("filename", "/home/hgcdaq00/DPG/test/hackathon_2024Mar/ineuteli/calibration_parameters_v2.json");
desc.add<edm::ESInputTag>("moduleIndexerSource",edm::ESInputTag(""))->setComment("Label for module info to calculate size");
desc.add<edm::ESInputTag>("moduleIndexerSource",edm::ESInputTag(""))->setComment("Label for module info to set SoA size");
descriptions.addWithDefaultLabel(desc);
}

Expand All @@ -66,7 +57,7 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
// fill SoA column with data from JSON list for any type
const std::vector<T> values = col_json.get<std::vector<T>>();
const int nrows = values.size();
if(nrows_exp>=0 and nrows_exp!=nrows) { // sanity check
if (nrows_exp>=0 and nrows_exp!=nrows) { // sanity check
edm::LogWarning("HGCalCalibrationESProducer") << " Found two columns with different number or rows: "
<< nrows_exp << " vs. " << nrows << "!";
}
Expand All @@ -81,34 +72,34 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
const uint32_t size = moduleMap.getMaxDataSize(); // channel-level size
hgcalrechit::HGCalCalibParamHostCollection product(size, cms::alpakatools::host());
product.view().map() = moduleMap; // set dense indexing in SoA
std::cout << "HGCalCalibrationESProducer: moduleMap.getMaxDataSize()=" << size
std::cout << "HGCalCalibrationESProducer::produce: moduleMap.getMaxDataSize()=" << size
<< ", moduleMap.getMaxERxSize()=" << moduleMap.getMaxERxSize() << std::endl;

// load calib parameters from JSON
std::cout << "HGCalCalibrationESProducer: filename_=" << filename_ << std::endl;
std::cout << "HGCalCalibrationESProducer::produce: filename_=" << filename_ << std::endl;
//std::ifstream infile("/home/hgcdaq00/DPG/test/hackathon_2024Mar/ineuteli/level0_calib_params.json");
std::ifstream infile(filename_);
json calib_data = json::parse(infile);
for(const auto& it: calib_data.items()){
for (const auto& it: calib_data.items()) {
std::string module = it.key(); // module typecode, e.g. "ML-F3PT-TX-0003"
uint32_t offset = moduleMap.getIndexForModuleData(module); // convert electronics ID to dense index for this module
int nrows = calib_data[module]["Channel"].size();
std::cout << "HGCalCalibrationESProducer: calib_data[\"" << module << "\"][\"Channel\"].size() = "
std::cout << "HGCalCalibrationESProducer::produce: calib_data[\"" << module << "\"][\"Channel\"].size() = "
<< nrows << ", offset=" << offset << std::endl;
fill_SoA_column<float>(product.view().ADC_ped(), calib_data[module]["ADC_ped"], offset,nrows);
fill_SoA_column<float>(product.view().CM_slope(), calib_data[module]["CM_slope"], offset,nrows);
fill_SoA_column<float>(product.view().CM_ped(), calib_data[module]["CM_ped"], offset,nrows);
fill_SoA_column<float>(product.view().ADCtofC(), calib_data[module]["ADCtofC"], offset,nrows);
fill_SoA_column<float>(product.view().ADCtofC(), calib_data[module]["ADCtofC"], offset,nrows); // should switch depending on gain from config
fill_SoA_column<float>(product.view().BXm1_slope(),calib_data[module]["BXm1_slope"],offset,nrows);
fill_SoA_column<float>(product.view().TOTtofC(), calib_data[module]["TOTtofC"], offset,nrows);
fill_SoA_column<float>(product.view().TOTtofC(), calib_data[module]["TOTtofC"], offset,nrows); // should switch depending on gain from config
fill_SoA_column<float>(product.view().TOT_ped(), calib_data[module]["TOT_ped"], offset,nrows);
fill_SoA_column<float>(product.view().TOT_lin(), calib_data[module]["TOT_lin"], offset,nrows);
fill_SoA_column<float>(product.view().TOT_P0(), calib_data[module]["TOT_P0"], offset,nrows);
fill_SoA_column<float>(product.view().TOT_P1(), calib_data[module]["TOT_P1"], offset,nrows);
fill_SoA_column<float>(product.view().TOT_P2(), calib_data[module]["TOT_P2"], offset,nrows);
fill_SoA_column<float>(product.view().TOAtops(), calib_data[module]["TOAtops"], offset,nrows);
fill_SoA_column<mybool>(product.view().valid(), calib_data[module]["Valid"], offset,nrows); // mybool (=std::byte) defined in HGCalCalibrationParameterSoA.h
std::cout << "HGCalCalibrationESProducer: memcpied all columns !" << std::endl;
std::cout << "HGCalCalibrationESProducer::produce: memcpied all columns !" << std::endl;
}

//// OLD: load calib parameters from txt
Expand All @@ -120,8 +111,8 @@ namespace ALPAKA_ACCELERATOR_NAMESPACE {
//std::string line;
//uint32_t id;
//float ped, noise, cm_slope, cm_offset, bxm1_slope, bxm1_offset;
//while(std::getline(file, line)) {
// if(line.find("Channel")!=std::string::npos || line.find("#")!=std::string::npos) continue;
//while (std::getline(file, line)) {
// if (line.find("Channel")!=std::string::npos || line.find("#")!=std::string::npos) continue;
//
// std::istringstream stream(line);
// //stream >> std::hex >> id >> std::dec >> ped >> noise >> cm_slope >> cm_offset >> bxm1_slope >> bxm1_offset;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/SourceFactory.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESProducer.h"
#include "FWCore/Framework/interface/ESTransientHandle.h"
#include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/ESGetToken.h"
#include "DataFormats/Math/interface/libminifloat.h"

#include "HeterogeneousCore/AlpakaCore/interface/alpaka/ESGetToken.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/ESProducer.h"
#include "HeterogeneousCore/AlpakaCore/interface/alpaka/ModuleFactory.h"
#include "HeterogeneousCore/AlpakaInterface/interface/config.h"
#include "HeterogeneousCore/AlpakaInterface/interface/host.h"
#include "HeterogeneousCore/AlpakaInterface/interface/memory.h"

//#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
#include "CondFormats/HGCalObjects/interface/HGCalMappingModuleIndexer.h"
#include "CondFormats/DataRecord/interface/HGCalMappingModuleIndexerRcd.h"
#include "CondFormats/DataRecord/interface/HGCalModuleConfigurationRcd.h"
#include "RecoLocalCalo/HGCalRecAlgos/interface/HGCalCalibrationParameterHostCollection.h" // for HGCalConfigParamHostCollection
#include "RecoLocalCalo/HGCalRecAlgos/interface/alpaka/HGCalCalibrationParameterDeviceCollection.h"

#include <string>
#include <iostream> // for std::cout
#include <iomanip> // for std::setw

namespace ALPAKA_ACCELERATOR_NAMESPACE {

namespace hgcalrechit {

class HGCalConfigurationESProducer : public ESProducer {
public:

HGCalConfigurationESProducer(const edm::ParameterSet& iConfig)
: ESProducer(iConfig),
charMode_(iConfig.getParameter<int>("charMode")),
gain_(iConfig.getParameter<int>("gain")) {
auto cc = setWhatProduced(this);
//findingRecord<HGCalModuleConfigurationRcd>();
//configToken_ = cc.consumes(iConfig.getParameter<edm::ESInputTag>("configSource"));
moduleIndexerToken_ = cc.consumes(iConfig.getParameter<edm::ESInputTag>("moduleIndexerSource"));
}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<int>("charMode",0)->setComment("Manual override for characterization mode to unpack raw data");
desc.add<int>("gain",2)->setComment("Manual override for gain (1: 80 fC, 2: 160 fC, 4: 320 fC)");
desc.add<edm::ESInputTag>("moduleIndexerSource",edm::ESInputTag(""))->setComment("Label for module info to set SoA size");
//desc.add<edm::ESInputTag>("configSource",edm::ESInputTag(""))->setComment("Label for ROC configuration parameters");
descriptions.addWithDefaultLabel(desc);
}

std::optional<hgcalrechit::HGCalConfigParamHostCollection> produce(const HGCalModuleConfigurationRcd& iRecord) {
//std::cout << "HGCalConfigurationESProducer::produce" << std::endl;
//const auto& config = iRecord.get(configToken_);
auto const& moduleMap = iRecord.get(moduleIndexerToken_);

// load dense indexing
const uint32_t nmod = moduleMap.getMaxERxSize(); // ROC-level size
hgcalrechit::HGCalConfigParamHostCollection product(nmod, cms::alpakatools::host());
product.view().map() = moduleMap; // set dense indexing in SoA (causes segfault !?)
std::cout << "HGCalConfigurationESProducer::produce: moduleMap.getMaxDataSize()=" << moduleMap.getMaxDataSize()
<< ", moduleMap.getMaxERxSize()=" << nmod
<< ", moduleMap.getMaxModuleSize()=" << moduleMap.getMaxModuleSize() << std::endl;

// NEW: fill SoA with default placeholders
for (uint32_t imod = 0; imod < nmod; imod++) {
uint32_t charMode = charMode_;
uint8_t gain = gain_; // allow manual override
//std::cout << "Module imod=" << std::setw(3) << imod
// << ", charMode=" << charMode << ", gain=" << uint32_t(gain) << std::endl;
product.view()[imod].gain() = gain;
}

//// OLD: fill SoA from YAML
//// TODO: Use typecode to assign dense index
//size_t nmods = config.moduleConfigs.size();
////LogDebug("HGCalRecHitCalibrationAlgorithms") << "Configuration retrieved for " << nmods << " modules: " << config << std::endl;
//std::cout << "HGCalRecHitCalibrationAlgorithms" << "Configuration retrieved for " << nmods << " modules: " << config << std::endl;
//for(auto it : config.moduleConfigs) { // loop over map module electronicsId -> HGCalModuleConfig
// HGCalModuleConfig moduleConfig(it.second);
// LogDebug("HGCalRecHitCalibrationAlgorithms")
// << "Module " << it.first << std::hex << " (0x" << it.first << std::dec
// << ") charMode=" << moduleConfig.charMode
// << ", ngains=" << moduleConfig.gains.size(); //<< std::endl;
// for(auto rocit : moduleConfig.gains) {
// uint32_t rocid = rocit.first;
// uint8_t gain = (gain_>=1 ? gain_ : rocit.second); // allow manual override
// product.view()[cpi.denseROCMap(rocid)].gain() = gain;
// LogDebug("HGCalRecHitCalibrationAlgorithms")
// << " ROC " << std::setw(4) << rocid << std::hex << " (0x" << rocid << std::dec
// << "): gain=" << (unsigned int) gain << " (override: " << gain_ << ")"; //std::endl;
// }
//}

return product;
} // end of produce()

private:
edm::ESGetToken<HGCalMappingModuleIndexer, HGCalMappingModuleIndexerRcd> moduleIndexerToken_;
//edm::ESGetToken<HGCalCondSerializableConfig, HGCalModuleConfigurationRcd> configToken_;
const int32_t charMode_; // manual override of YAML files
const int32_t gain_; // manual override of YAML files

};

} // namespace hgcalrechit

} // namespace ALPAKA_ACCELERATOR_NAMESPACE

DEFINE_FWK_EVENTSETUP_ALPAKA_MODULE(hgcalrechit::HGCalConfigurationESProducer);
Loading

0 comments on commit 394aade

Please sign in to comment.