Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HGCAL Electronics mapping (v2) #44729

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CondFormats/DataRecord/interface/HGCalElectronicsMappingRcd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CondFormatsModuleRcd_HGCalElectronicsMappingRcd_h
#define CondFormatsModuleRcd_HGCalElectronicsMappingRcd_h
// -*- C++ -*-
//
// Package: CondFormats/DataRecords
// Class : HGCalElectronicsMappingRcd
//
/**\class HGCalElectronicsMappingRcd HGCalElectronicsMappingRcd.h CondFormats/DataRecords/interface/HGCalElectronicsMappingRcd.h
Description: This record *is temporary* and it is used to store the parameters which are used to describe
- dense indexing of HGCAL modules and corresponding cells in the readound sequence
- module and cell information in the electronics mapping
The record will change to its final format(s) once the we settle on the final formats to be used in CondDB
*/
//
// Author: Pedro Vieira De Castro Ferreira Da Silva
// Created: Mon, 13 Nov 2023 12:02:11 GMT
//

#include "FWCore/Framework/interface/EventSetupRecordImplementation.h"

class HGCalElectronicsMappingRcd : public edm::eventsetup::EventSetupRecordImplementation<HGCalElectronicsMappingRcd> {
};

#endif
13 changes: 13 additions & 0 deletions CondFormats/DataRecord/src/HGCalElectronicsMappingRcd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// -*- C++ -*-
//
// Package: CondFormats/DataRecord
// Class : HGCalElectronicsMappingRcd
//
//
// Author: Pedro Vieira De Castro Ferreira Da Silva
// Created: Mon, 13 Nov 2023 12:02:11 GMT

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

EVENTSETUP_RECORD_REG(HGCalElectronicsMappingRcd);
20 changes: 13 additions & 7 deletions CondFormats/HGCalObjects/BuildFile.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<use name="FWCore/Utilities"/>
<use name="FWCore/MessageLogger"/>
<use name="HeterogeneousCore/CUDACore"/>
<use name="CUDADataFormats/HGCal"/>
<use name="Geometry/HGCalCommonData"/>
<use name="CondFormats/Serialization"/>
<use name="Geometry/HGCalCommonData"/>
<use name="boost_serialization"/>
<use name="DataFormats/HGCalDigi"/>
<use name="DataFormats/Portable"/>
<use name="DataFormats/SoATemplate"/>
<use name="FWCore/MessageLogger"/>
<use name="FWCore/Utilities"/>
<use name="HeterogeneousCore/AlpakaInterface"/>
<use name="HeterogeneousCore/CUDACore"/>
<flags ALPAKA_BACKENDS="1"/>
<export>
<lib name="1"/>
</export>
<lib name="1"/>
</export>
66 changes: 66 additions & 0 deletions CondFormats/HGCalObjects/interface/HGCalDenseIndexerBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef CondFormats_HGCalObjects_interface_HGCalDenseIndexerBase_h
#define CondFormats_HGCalObjects_interface_HGCalDenseIndexerBase_h

#include "CondFormats/Serialization/interface/Serializable.h"
#include "FWCore/Utilities/interface/Exception.h"
#include <array>
#include <numeric>

/**
@short this is a simple class that takes care of building a dense index for a set of categories
the maximum number of items expected in each category is encoded in the IndexRanges_t
the class is templated for the number of categories to use
*/
class HGCalDenseIndexerBase {
public:
HGCalDenseIndexerBase() : HGCalDenseIndexerBase(0) {}

HGCalDenseIndexerBase(int n) : n_(n), maxIdx_(0), vmax_(n, 0) {}

HGCalDenseIndexerBase(std::vector<uint32_t> const &o) : n_(o.size()) { updateRanges(o); }

void updateRanges(std::vector<uint32_t> const &o) {
check(o.size());
vmax_ = o;
maxIdx_ = std::accumulate(vmax_.begin(), vmax_.end(), 1, std::multiplies<uint32_t>());
}

uint32_t denseIndex(std::vector<uint32_t> v) const {
uint32_t rtn = v[0];
for (size_t i = 1; i < n_; i++)
rtn = rtn * vmax_[i] + v[i];
return rtn;
}

std::vector<uint32_t> unpackDenseIndex(uint32_t rtn) const {
std::vector<uint32_t> codes(n_, 0);

const auto rend = vmax_.rend();
for (auto rit = vmax_.rbegin(); rit != rend; ++rit) {
size_t i = rend - rit - 1;
codes[i] = rtn % (*rit);
rtn = rtn / (*rit);
}

return codes;
}

uint32_t getMaxIndex() const { return maxIdx_; }

~HGCalDenseIndexerBase() = default;

private:
void check(size_t osize) const {
if (osize != n_)
throw cms::Exception("ValueError") << " unable to update indexer max values. Expected " << n_ << " received "
<< osize;
}

uint32_t n_;
uint32_t maxIdx_;
std::vector<uint32_t> vmax_;

COND_SERIALIZABLE;
};

#endif
164 changes: 164 additions & 0 deletions CondFormats/HGCalObjects/interface/HGCalMappingCellIndexer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#ifndef CondFormats_HGCalObjects_interface_HGCalMappingCellParameterIndex_h
#define CondFormats_HGCalObjects_interface_HGCalMappingCellParameterIndex_h

#include <iostream>
#include <cstdint>
#include <vector>
#include <numeric>
#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
#include "CondFormats/Serialization/interface/Serializable.h"
#include "CondFormats/HGCalObjects/interface/HGCalDenseIndexerBase.h"

/**
@short utility class to assign dense readout cell indexing
*/
class HGCalMappingCellIndexer {
public:
// typedef HGCalDenseIndexerBase WaferDenseIndexerBase;

HGCalMappingCellIndexer() = default;

/**
adds to map of type codes (= module types) to handle and updatest the max. number of eRx
*/
void processNewCell(std::string typecode, uint16_t chip, uint16_t half) {
//assign index to this typecode and resize the max e-Rx vector
if (typeCodeIndexer_.count(typecode) == 0) {
typeCodeIndexer_[typecode] = typeCodeIndexer_.size();
maxErx_.resize(typeCodeIndexer_.size(), 0);
}

size_t idx = typeCodeIndexer_[typecode];
uint16_t erx = chip * 2 + half + 1; //use the number not the index here
maxErx_[idx] = std::max(maxErx_[idx], erx);
}

/**
@short process the current list of type codes handled and updates the dense indexers
*/
void update() {
uint32_t n = typeCodeIndexer_.size();
offsets_ = std::vector<uint32_t>(n, 0);
di_ = std::vector<HGCalDenseIndexerBase>(n, HGCalDenseIndexerBase(2));
for (uint32_t idx = 0; idx < n; idx++) {
uint16_t nerx = maxErx_[idx];
di_[idx].updateRanges({{nerx, maxChPerErx_}});
if (idx < n - 1)
offsets_[idx + 1] = di_[idx].getMaxIndex();
}

//accumulate the offsets in the array
std::partial_sum(offsets_.begin(), offsets_.end(), offsets_.begin());
}

/**
@short gets index given typecode string
*/
size_t getEnumFromTypecode(std::string typecode) const {
auto it = typeCodeIndexer_.find(typecode);
if (it == typeCodeIndexer_.end())
throw cms::Exception("ValueError") << " unable to find typecode=" << typecode << " in cell indexer";
return it->second;
}

/**
@short checks if there is a typecode corresponding to an index
*/
std::string getTypecodeFromEnum(size_t idx) const {
for (const auto& it : typeCodeIndexer_)
if (it.second == idx)
return it.first;
throw cms::Exception("ValueError") << " unable to find typecode corresponding to idx=" << idx;
}

/**
@short returns the dense indexer for a typecode
*/
HGCalDenseIndexerBase getDenseIndexFor(std::string typecode) const {
return getDenseIndexerFor(getEnumFromTypecode(typecode));
}

/**
@short returns the dense indexer for a given internal index
*/
HGCalDenseIndexerBase getDenseIndexerFor(size_t idx) const {
if (idx >= di_.size())
throw cms::Exception("ValueError") << " index requested for cell dense indexer (i=" << idx
<< ") is larger than allocated";
return di_[idx];
}

/**
@short builders for the dense index
*/
uint32_t denseIndex(std::string typecode, uint32_t chip, uint32_t half, uint32_t seq) const {
return denseIndex(getEnumFromTypecode(typecode), chip, half, seq);
}
uint32_t denseIndex(std::string typecode, uint32_t erx, uint32_t seq) const {
return denseIndex(getEnumFromTypecode(typecode), erx, seq);
}
uint32_t denseIndex(size_t idx, uint32_t chip, uint32_t half, uint32_t seq) const {
uint16_t erx = chip * maxHalfPerROC_ + half;
return denseIndex(idx, erx, seq);
}
uint32_t denseIndex(size_t idx, uint32_t erx, uint32_t seq) const {
return di_[idx].denseIndex({{erx, seq}}) + offsets_[idx];
}

/**
@short decodes the dense index code
*/
uint32_t elecIdFromIndex(uint32_t rtn, std::string typecode) const {
return elecIdFromIndex(rtn, getEnumFromTypecode(typecode));
}
uint32_t elecIdFromIndex(uint32_t rtn, size_t idx) const {
if (idx >= di_.size())
throw cms::Exception("ValueError") << " index requested for cell dense indexer (i=" << idx
<< ") is larger than allocated";
rtn -= offsets_[idx];
auto rtn_codes = di_[idx].unpackDenseIndex(rtn);
return HGCalElectronicsId(false, 0, 0, 0, rtn_codes[0], rtn_codes[1]).raw();
}

/**
@short returns the max. dense index expected
*/
uint32_t maxDenseIndex() const {
size_t i = maxErx_.size();
if (i == 0)
return 0;
return offsets_.back() + maxErx_.back() * maxChPerErx_;
}

/**
@short gets the number of words for a given typecode
*/
size_t getNWordsExpectedFor(std::string typecode) const {
auto it = getEnumFromTypecode(typecode);
return getNWordsExpectedFor(it);
}
size_t getNWordsExpectedFor(size_t typecodeidx) const { return maxErx_[typecodeidx] * maxChPerErx_; }

/**
@short gets the number of e-Rx for a given typecode
*/
size_t getNErxExpectedFor(std::string typecode) const {
auto it = getEnumFromTypecode(typecode);
return getNErxExpectedFor(it);
}
size_t getNErxExpectedFor(size_t typecodeidx) const { return maxErx_[typecodeidx]; }

constexpr static char maxHalfPerROC_ = 2;
constexpr static uint16_t maxChPerErx_ = 37; //36 channels + 1 calib

std::map<std::string, size_t> typeCodeIndexer_;
std::vector<uint16_t> maxErx_;
std::vector<uint32_t> offsets_;
std::vector<HGCalDenseIndexerBase> di_;

~HGCalMappingCellIndexer() {}

COND_SERIALIZABLE;
};

#endif
Loading