Skip to content

Commit

Permalink
adding a pedestals cond format, filler and tester
Browse files Browse the repository at this point in the history
  • Loading branch information
pfs committed Jun 1, 2023
1 parent 179ef61 commit f8ae0cb
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CalibCalorimetry/HGCalPlugins/plugins/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<use name="CondFormats/HGCalObjects"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="DataFormats/Math"/>
<use name="yaml-cpp"/>
<library file="HGCalConfigESSourceFromYAML.cc">
<flags EDM_PLUGIN="1"/>
</library>
<library file="HGCalPedestalsESSource.cc">
<flags EDM_PLUGIN="1"/>
</library>
82 changes: 82 additions & 0 deletions CalibCalorimetry/HGCalPlugins/plugins/HGCalPedestalsESSource.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#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/EventSetupRecordIntervalFinder.h"
#include "FWCore/Framework/interface/ESProducts.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"
#include "DataFormats/Math/interface/libminifloat.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "CondFormats/DataRecord/interface/HGCalCondSerializablePedestalsRcd.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializablePedestals.h"

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>

/**
@short simple plugin to parse a pedestals file and construct the pedestals object
*/
class HGCalPedestalsESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
public:
explicit HGCalPedestalsESSource(const edm::ParameterSet& iConfig)
: filename_(iConfig.getParameter<std::string>("filename")) {
setWhatProduced(this);
findingRecord<HGCalCondSerializablePedestalsRcd>();
}

std::unique_ptr<HGCalCondSerializablePedestals> produce(const HGCalCondSerializablePedestalsRcd&) { return parsePedestals(filename_); }

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<std::string>("filename", {});
descriptions.addWithDefaultLabel(desc);
}

private:
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
const edm::IOVSyncValue&,
edm::ValidityInterval& oValidity) override {
oValidity = edm::ValidityInterval(edm::IOVSyncValue::beginOfTime(), edm::IOVSyncValue::endOfTime());
}

std::unique_ptr<HGCalCondSerializablePedestals> parsePedestals(const std::string& filename);

const std::string filename_;
};


//
std::unique_ptr<HGCalCondSerializablePedestals> HGCalPedestalsESSource::parsePedestals(const std::string& filename)
{
auto cond = std::make_unique<HGCalCondSerializablePedestals>();

//open file
edm::FileInPath fip(filename);
std::ifstream file(fip.fullPath());
std::string line;
uint32_t id;
float ped,cm_slope,cm_off,kappa_bxm1;
while(std::getline(file, line))
{
if(line.find("Channel")!=std::string::npos || line.find("#")!=std::string::npos) continue;

std::istringstream stream(line);
stream >> id >> ped >> cm_slope >> cm_off >> kappa_bxm1;

//reduce to half-point float and fill the pedestals of this channel
HGCalPedestals m;
m.pedestal = MiniFloatConverter::float32to16(ped);
m.cm_slope = MiniFloatConverter::float32to16(cm_slope);
m.cm_offset = MiniFloatConverter::float32to16(cm_off);
m.kappa_bxm1 = MiniFloatConverter::float32to16(kappa_bxm1);
cond->addParameter(id,m);
}

//return the conditions
return cond;
}


DEFINE_FWK_EVENTSETUP_SOURCE(HGCalPedestalsESSource);
7 changes: 6 additions & 1 deletion CalibCalorimetry/HGCalPlugins/test/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<use name="CondFormats/HGCalObjects"/>
<use name="FWCore/Framework"/>
<use name="FWCore/ParameterSet"/>
<use name="DataFormats/Math"/>
<use name="DataFormats/HGCalDigi"/>
<use name="yaml-cpp"/>
<flags EDM_PLUGIN="1"/>
<library file="HGCalConfigESSourceFromYAMLAnalyzer.cc" name="HGCalConfigESSourceFromYAMLAnalyzer">
<flags EDM_PLUGIN="1"/>
</library>
<library file="HGCalPedestalsESSourceAnalyzer.cc" name="HGCalPedestalsESSourceAnalyzer">
<flags EDM_PLUGIN="1"/>
</library>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/Framework/interface/ESWatcher.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "DataFormats/Math/interface/libminifloat.h"
#include "DataFormats/HGCalDigi/interface/HGCalElectronicsId.h"
#include "CondFormats/DataRecord/interface/HGCalCondSerializablePedestalsRcd.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializablePedestals.h"

class HGCalPedestalsESSourceAnalyzer : public edm::one::EDAnalyzer<> {
public:
explicit HGCalPedestalsESSourceAnalyzer(const edm::ParameterSet& iConfig)
: tokenConds_(esConsumes<HGCalCondSerializablePedestals, HGCalCondSerializablePedestalsRcd>(
edm::ESInputTag(iConfig.getParameter<std::string>("label")))) {}

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<std::string>("label", {});
descriptions.addWithDefaultLabel(desc);
}

private:

void analyze(const edm::Event&, const edm::EventSetup& iSetup) override {

//check if there are new conditions and read them
if (!cfgWatcher_.check(iSetup)) return;
auto conds=iSetup.getData(tokenConds_);
size_t nconds=conds.params_.size();
edm::LogInfo("HGCalPedestalsESSourceAnalyzer") << "Conditions retrieved:\n" << nconds;

//print out all conditions readout
std::cout << "ID\teRx\tROC\tChannel\tIs CM?\tPedestal\tCM slope\tCM offset\tkappa(BX-1)" << std::endl;
for(auto it : conds.params_) {

HGCalElectronicsId id(it.first);
bool cmflag=id.isCM();
uint32_t eRx=(uint32_t) id.econdeRx();
uint32_t roc=(uint32_t) eRx/2;
uint32_t seqch=id.sequentialHalfrocChannel();

HGCalPedestals table(it.second);
float pedestal = MiniFloatConverter::float16to32(table.pedestal);
float cm_slope = MiniFloatConverter::float16to32(table.cm_slope);
float cm_offset = MiniFloatConverter::float16to32(table.cm_offset);
float kappa_bxm1 = MiniFloatConverter::float16to32(table.kappa_bxm1);

std::cout << std::hex << id.raw() << " "
<< std::dec << eRx << " " << roc << " " << seqch << " " << cmflag << " "
<< std::setprecision(3) << " " << pedestal << " " << cm_slope << " " << cm_offset << " " << kappa_bxm1 << std::endl;
}

}

edm::ESWatcher<HGCalCondSerializablePedestalsRcd> cfgWatcher_;
edm::ESGetToken<HGCalCondSerializablePedestals, HGCalCondSerializablePedestalsRcd> tokenConds_;
};

DEFINE_FWK_MODULE(HGCalPedestalsESSourceAnalyzer);
62 changes: 62 additions & 0 deletions CalibCalorimetry/HGCalPlugins/test/hgcal_pedestals_reader_cfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import FWCore.ParameterSet.Config as cms
process = cms.Process("Calib")

process.MessageLogger = cms.Service("MessageLogger",
cerr = cms.untracked.PSet(
enable = cms.untracked.bool(False)
),
cout = cms.untracked.PSet(
enable = cms.untracked.bool(True),
threshold = cms.untracked.string('INFO')
)
)

process.source = cms.Source('EmptyIOVSource',
timetype = cms.string('runnumber'),
firstValue = cms.uint64(1),
lastValue = cms.uint64(1),
interval = cms.uint64(1)
)

process.load('CalibCalorimetry.HGCalPlugins.hgCalPedestalsESSource_cfi')
process.hgCalPedestalsESSource.filename = 'CalibCalorimetry/HGCalPlugins/test/pedestals_test.txt'

#generate values for a test file
import numpy as np
import itertools
import scipy.constants as k
consts=np.array([k.pi,k.golden,k.c/1e8,k.g,k.h/1e-34,k.alpha/1e-2,np.exp(1)])
rows=[]
for erx,ich in itertools.product(range(6),range(39)):

#identifier
cmflag=(ich>36)
ch=(ich-37)%2 if cmflag else ich
id=(cmflag << 28) | ((erx & 0xf)<<6) | (ch & 0x3f)

#random values
np.random.shuffle(consts)
vals=consts[0:4]
rows.append( [id] + vals.tolist() )

#write the test file
import pandas as pd
from pathlib import Path
import os
test_dir = os.path.dirname(Path( __file__ ).absolute())
df=pd.DataFrame(rows,columns=['Channel','Pedestal','CM_slope','CM_offset','kappa_BXm1'])
df.to_csv(os.path.join(test_dir,'pedestals_test.txt'),index=False,sep=' ',header=True, mode='w')

#instantiate the analyzer
process.analyzer = cms.EDAnalyzer("HGCalPedestalsESSourceAnalyzer",
label = cms.string(''))

process.source = cms.Source('EmptySource')

process.maxEvents = cms.untracked.PSet(
input = cms.untracked.int32(1)
)

process.p = cms.Path(
process.analyzer
)
3 changes: 3 additions & 0 deletions CondCore/HGCalPlugins/src/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableSiPMTileInfo.h"
#include "CondFormats/DataRecord/interface/HGCalCondSerializableModuleInfoRcd.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableModuleInfo.h"
#include "CondFormats/DataRecord/interface/HGCalCondSerializablePedestalsRcd.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializablePedestals.h"

REGISTER_PLUGIN(HGCalCondSerializableGenericConfigRcd,HGCalCondSerializableGenericConfig);
REGISTER_PLUGIN(HGCalCondSerializableSiCellChannelInfoRcd,HGCalCondSerializableSiCellChannelInfo);
REGISTER_PLUGIN(HGCalCondSerializableSiPMTileInfoRcd,HGCalCondSerializableSiPMTileInfo);
REGISTER_PLUGIN(HGCalCondSerializableModuleInfoRcd,HGCalCondSerializableModuleInfo);
REGISTER_PLUGIN(HGCalCondSerializablePedestalsRcd,HGCalCondSerializablePedestals);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef HGCalCondSerializablePedestalsRcd_HGCalCondSerializablePedestalsRcd_h
#define HGCalCondSerializablePedestalsRcd_HGCalCondSerializablePedestalsRcd_h
// -*- C++ -*-
//
// Package: CondFormats/DataRecord
// Class : HGCalCondSerializablePedestalsRcd
//
/**\class HGCalCondSerializablePedestalsRcd HGCalCondSerializablePedestalsRcd.h CondFormats/DataRecord/interface/HGCalCondSerializablePedestalsRcd.h
Description: [one line class summary]
Usage:
<usage>
*/
//
// Author: Pedro Vieira De Castro Ferreira Da Silva
// Created: Thu, 01 Jun 2023 09:12:22 GMT
//

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

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

#endif
15 changes: 15 additions & 0 deletions CondFormats/DataRecord/src/HGCalCondSerializablePedestalsRcd.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// -*- C++ -*-
//
// Package: CondFormats/DataRecord
// Class : HGCalCondSerializablePedestalsRcd
//
// Implementation:
// [Notes on implementation]
//
// Author: Pedro Vieira De Castro Ferreira Da Silva
// Created: Thu, 01 Jun 2023 09:12:22 GMT

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

EVENTSETUP_RECORD_REG(HGCalCondSerializablePedestalsRcd);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef CondFormats_HGCalObjects_HGCalCondSerializablePedestals_h
#define CondFormats_HGCalObjects_HGCalCondSerializablePedestals_h

#include <map>
#include <cstdint>

#include "CondFormats/Serialization/interface/Serializable.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/Utilities/interface/Exception.h"

/**
@short representation of a si-cell channel information read from a txt file or db
*/
struct HGCalPedestals {
uint16_t pedestal, cm_slope, cm_offset, kappa_bxm1;
COND_SERIALIZABLE;
};

/**
@holder for the si cell channel mappeing
*/
class HGCalCondSerializablePedestals {

public:

HGCalCondSerializablePedestals() {}
virtual ~HGCalCondSerializablePedestals() {}
inline HGCalCondSerializablePedestals& addParameter(uint32_t id,HGCalPedestals &p) {
if (params_.count(id) != 0)
throw cms::Exception("HGCalCondSerializablePedestals") << "Already existing parameter with id='" << id << "'.";
params_[id] = p;
return *this;
}
inline const HGCalPedestals& parameters(uint32_t id) {
if (params_.count(id) != 0)
throw cms::Exception("HGCalCondSerializablePedestals") << "Failed to retrieve parameter with id='" << id << "'.";
return params_[id];
}

std::map<uint32_t,HGCalPedestals> params_;

COND_SERIALIZABLE;
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializablePedestals.h"
#include "FWCore/Utilities/interface/typelookup.h"

TYPELOOKUP_DATA_REG(HGCalCondSerializablePedestals);
6 changes: 6 additions & 0 deletions CondFormats/HGCalObjects/src/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ namespace CondFormats_HGCalObjects {
std::vector<HGCalModuleInfo> v_hmi;
HGCalCondSerializableModuleInfo h_csmi();

HGCalPedestals hp;
std::pair<uint32_t,HGCalPedestals> p_hp;
std::map<uint32_t,HGCalPedestals> v_hp;
HGCalCondSerializablePedestals h_csp();


} // namespace CondFormats_HGCalObjects
8 changes: 8 additions & 0 deletions CondFormats/HGCalObjects/src/classes_def.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@
<class name="HGCalModuleInfo"/>
<class name="std::vector<HGCalModuleInfo>"/>

<class name="HGCalCondSerializablePedestals" class_version="0">
<field name="params_" mapping="blob"/>
</class>
<class name="HGCalPedestals"/>
<class name="std::map<uint32_t, HGCalPedestals>"/>
<class name="std::pair<uint32_t, HGCalPedestals>"/>


</lcgdict>
1 change: 1 addition & 0 deletions CondFormats/HGCalObjects/src/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableSiCellChannelInfo.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableSiPMTileInfo.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializableModuleInfo.h"
#include "CondFormats/HGCalObjects/interface/HGCalCondSerializablePedestals.h"
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ int main()
testSerialization<HGCalCondSerializableModuleInfo>();
testSerialization<std::vector<HGCalCondSerializableModuleInfo>>();

//pedestals
testSerialization<HGCalPedestals>();
testSerialization<HGCalCondSerializablePedestals>();
testSerialization<std::vector<HGCalCondSerializablePedestals>>();

return 0;
}
Loading

0 comments on commit f8ae0cb

Please sign in to comment.