Skip to content

Commit

Permalink
Merge pull request #21870 from mmusich/10X_SiStripGainsTools
Browse files Browse the repository at this point in the history
[10_1_X] SiStrip Gains conditions DB manipulation tools
  • Loading branch information
cmsbuild committed Jan 27, 2018
2 parents a2d8ced + 5b4733b commit bb10759
Show file tree
Hide file tree
Showing 5 changed files with 878 additions and 3 deletions.
Expand Up @@ -1107,7 +1107,7 @@ namespace {
std::vector<std::string> parts = {"TEC","TOB","TIB","TID"};

for ( const auto &part : parts){
ratios[part] = std::make_shared<TH1F>(Form("hRatio_%s",part.c_str()),Form("Gains ratio IOV: %s/ IOV: %s ;New Gain (%s) / Previous Gain (%s);Number of APV",lastIOVsince.c_str(),firstIOVsince.c_str(),lastIOVsince.c_str(),firstIOVsince.c_str()),200,0.,2.);
ratios[part] = std::make_shared<TH1F>(Form("hRatio_%s",part.c_str()),Form("Gains ratio IOV: %s/ IOV: %s ;Previous Gain (%s) / New Gain (%s);Number of APV",firstIOVsince.c_str(),lastIOVsince.c_str(),firstIOVsince.c_str(),lastIOVsince.c_str()),200,0.,2.);
scatters[part] = std::make_shared<TH2F>(Form("hScatter_%s",part.c_str()),Form("new Gain (%s) vs previous Gain (%s);Previous Gain (%s);New Gain (%s)",lastIOVsince.c_str(),firstIOVsince.c_str(),firstIOVsince.c_str(),lastIOVsince.c_str()),100,0.5,1.8,100,0.5,1.8);
}

Expand Down Expand Up @@ -1152,6 +1152,7 @@ namespace {

for (const auto &part : parts){
SiStripPI::makeNicePlotStyle(ratios[part].get());
ratios[part]->SetMinimum(1.);
ratios[part]->SetStats(false);
ratios[part]->SetLineWidth(2);
ratios[part]->SetLineColor(colormap[part]);
Expand Down Expand Up @@ -1301,8 +1302,8 @@ namespace {
h_firstGains->SetMarkerSize(1.);
h_lastGains->SetMarkerSize(1.);

h_firstGains->SetLineWidth(1.5);
h_lastGains->SetLineWidth(1.5);
h_firstGains->SetLineWidth(1);
h_lastGains->SetLineWidth(1);

h_firstGains->SetMarkerStyle(20);
h_lastGains->SetMarkerStyle(21);
Expand Down
208 changes: 208 additions & 0 deletions CondTools/SiStrip/plugins/SiStripApvGainRescaler.cc
@@ -0,0 +1,208 @@
// -*- C++ -*-
//
// Package: CondTools/SiStrip
// Class: SiStripApvGainRescaler
//
/**\class SiStripApvGainRescaler SiStripApvGainRescaler.cc CondTools/SiStrip/plugins/SiStripApvGainRescaler.cc
Description: Utility class to rescale the values of SiStrip G2 by the ratio of G1_old/G1_new: this is useful in the case in which a Gain2 payload needs to recycled after a G1 update to keep the G1*G2 product constant
Implementation:
[Notes on implementation]
*/
//
// Original Author: Marco Musich
// Created: Tue, 03 Oct 2017 12:57:34 GMT
//
//


// system include files
#include <memory>
#include <iostream>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/ESHandle.h"

#include "CondFormats/SiStripObjects/interface/SiStripApvGain.h"
#include "CondFormats/DataRecord/interface/SiStripApvGainRcd.h"
#include "CalibFormats/SiStripObjects/interface/SiStripGain.h"
#include "CalibTracker/Records/interface/SiStripGainRcd.h"

#include "FWCore/ServiceRegistry/interface/Service.h"
#include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
#include "FWCore/Framework/interface/EventSetup.h"

//
// class declaration
//

class SiStripApvGainRescaler : public edm::one::EDAnalyzer<> {
public:
explicit SiStripApvGainRescaler(const edm::ParameterSet&);
~SiStripApvGainRescaler();

static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);

private:
virtual void beginJob() override;
virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap);
virtual void endJob() override;

// ----------member data ---------------------------
const std::string m_Record;

};

//
// constructors and destructor
//
SiStripApvGainRescaler::SiStripApvGainRescaler(const edm::ParameterSet& iConfig):
m_Record(iConfig.getParameter<std::string> ("Record"))
{
//now do what ever initialization is needed
}

SiStripApvGainRescaler::~SiStripApvGainRescaler()
{

// do anything here that needs to be done at desctruction time
// (e.g. close files, deallocate resources etc.)
}


//
// member functions
//

// ------------ method called for each event ------------
void
SiStripApvGainRescaler::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
{
using namespace edm;

// take G2_old and G1_old from the regular gain handle
edm::ESHandle<SiStripGain> g1g2Handle_;
iSetup.get<SiStripGainRcd>().get(g1g2Handle_);

// take the additional G1_new from the Gain3Rcd (dirty trick)
edm::ESHandle<SiStripApvGain> g3Handle_;
iSetup.get<SiStripApvGain3Rcd>().get(g3Handle_);

std::map<std::pair<uint32_t,int>,float> theMap;

std::vector<uint32_t> detid;
g1g2Handle_->getDetIds(detid);
for (const auto & d : detid) {

SiStripApvGain::Range rangeG1_old = g1g2Handle_->getRange(d,0);
SiStripApvGain::Range rangeG2_old = g1g2Handle_->getRange(d,1);
SiStripApvGain::Range rangeG1_new = g3Handle_->getRange(d);

int nAPV=0;
for(int it=0;it<rangeG1_old.second-rangeG1_old.first;it++){
nAPV++;

std::pair<uint32_t,int> index = std::make_pair(d,nAPV);

float G1_old = g1g2Handle_->getApvGain(it,rangeG1_old);
float G2_old = g1g2Handle_->getApvGain(it,rangeG2_old);
float G1G2_old = G1_old*G2_old;
float G1_new = g3Handle_->getApvGain(it,rangeG1_new);

// this is based on G1_old*G2_old = G1_new * G2_new ==> G2_new = (G1_old*G2_old)/G1_new

float NewGain = G1G2_old/G1_new;

// DO NOT RESCALE APVs set to the default value
if(G2_old!=1.){
theMap[index]=NewGain;
} else {
theMap[index]=1.;
}

} // loop over APVs
} // loop over DetIds

std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);

// write out the APVGains record
edm::Service<cond::service::PoolDBOutputService> poolDbService;

if( poolDbService.isAvailable() )
poolDbService->writeOne(theAPVGains.get(),poolDbService->currentTime(),m_Record);
else
throw std::runtime_error("PoolDBService required.");

}


// ------------ method called once each job just before starting event loop ------------
void
SiStripApvGainRescaler::beginJob()
{
}

// ------------ method called once each job just after ending the event loop ------------
void
SiStripApvGainRescaler::endJob()
{
}

//********************************************************************************//
std::unique_ptr<SiStripApvGain>
SiStripApvGainRescaler::getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap)
{
std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());

std::vector<float> theSiStripVector;
uint32_t PreviousDetId = 0;
for(const auto &element : theMap){
uint32_t DetId = element.first.first;
if(DetId != PreviousDetId){
if(!theSiStripVector.empty()){
SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
}
theSiStripVector.clear();
PreviousDetId = DetId;
}
theSiStripVector.push_back(element.second);

edm::LogInfo("SiStripApvGainRescaler")<<" DetId: "<<DetId
<<" APV: "<<element.first.second
<<" Gain: "<<element.second
<<std::endl;
}

if(!theSiStripVector.empty()){
SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
}

return obj;
}


// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
void
SiStripApvGainRescaler::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;

desc.setComment(" Utility class to rescale the values of SiStrip G2 by the ratio of G1_old/G1_new: this is useful in the case in which a Gain2 payload needs to recycled after a G1 update to keep the G1*G2 product constant."
"PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");

desc.add<std::string>("Record","SiStripApvGainRcd");
descriptions.add("rescaleGain2byGain1", desc);
}

//define this as a plug-in
DEFINE_FWK_MODULE(SiStripApvGainRescaler);

0 comments on commit bb10759

Please sign in to comment.