8 #define DB_WRITE_SUPPORT
24 #ifdef DB_WRITE_SUPPORT
27 #endif //DB_WRITE_SUPPORT defined
39 : dcc1stSample_(ps.getParameter<int>(
"dcc1stSample")),
40 sampleToSkip_(ps.getParameter<int>(
"sampleToSkip")),
41 nDccWeights_(ps.getParameter<int>(
"nDccWeights")),
42 inputWeights_(ps.getParameter<
vector<double> >(
"inputWeights")),
43 mode_(ps.getParameter<
string>(
"mode")),
44 dccWeightsWithIntercalib_(ps.getParameter<bool>(
"dccWeightsWithIntercalib")),
45 writeToDB_(ps.getParameter<bool>(
"writeToDB")),
46 writeToAsciiFile_(ps.getParameter<bool>(
"writeToAsciiFile")),
47 writeToRootFile_(ps.getParameter<bool>(
"writeToRootFile")),
48 asciiOutputFileName_(ps.getParameter<
string>(
"asciiOutputFileName")),
49 rootOutputFileName_(ps.getParameter<
string>(
"rootOutputFileName")),
50 dbSid_(ps.getParameter<
string>(
"dbSid")),
51 dbUser_(ps.getParameter<
string>(
"dbUser")),
52 dbPassword_(ps.getUntrackedParameter<
string>(
"dbPassword",
"")),
53 dbTag_(ps.getParameter<
string>(
"dbTag")),
54 dbVersion_(ps.getParameter<int>(
"dbVersion")),
55 sqlMode_(ps.getParameter<bool>(
"sqlMode")),
59 calibMap_(emptyCalibMap_),
60 ebShape_(consumesCollector()),
61 eeShape_(consumesCollector()) {
62 if (
mode_ ==
"weightsFromConfig") {
65 throw cms::Exception(
"Config") <<
"Inconsistent configuration. 'nDccWeights' parameters indicated "
66 <<
nDccWeights_ <<
" weights while parameter 'inputWeights_' contains "
69 }
else if (
mode_ ==
"computeWeights") {
73 <<
"Valid values are: 'weightsFromConfig' and 'computeWeights'\n";
110 vector<DetId> detIds(ebDetIds.size() + eeDetIds.size());
111 copy(ebDetIds.begin(), ebDetIds.end(), detIds.begin());
112 copy(eeDetIds.begin(), eeDetIds.end(), detIds.begin() + ebDetIds.size());
114 vector<double> baseWeights(nw);
115 vector<double>
w(nw);
124 for (vector<DetId>::const_iterator it = detIds.begin(); it != detIds.end(); ++it) {
130 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": ";
133 <<
" iphi = " << setw(4) << ((
EBDetId)(*it)).iphi() <<
" ";
136 <<
" iy = " << setw(3) << ((
EEDetId)(*it)).iy()
137 <<
" iz = " << setw(1) << ((
EEDetId)(*it)).iy() <<
" ";
140 <<
"Bug found in " << __FILE__ <<
":" << __LINE__ <<
": "
141 <<
"Got a detId which is neither tagged as ECAL Barrel "
142 <<
"not ECAL endcap while looping on ECAL cell detIds\n";
145 edm::LogVerbatim(
"EcalDccWeightBuilder") <<
" -> binOfMax: " << binOfMax <<
"\n";
156 throw cms::Exception(
"EcalDccWeightBuilder") <<
"Bug found in " << __FILE__ <<
":" << __LINE__ <<
": "
157 <<
"Got a detId which is neither tagged as ECAL Barrel "
158 <<
"not ECAL endcap while looping on ECAL cell detIds\n";
161 if (phase != prevPhase) {
169 for (
int i = 0;
i < nw; ++
i) {
170 w[
i] = baseWeights[
i];
177 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": ";
180 <<
" iphi = " << setw(4) << ((
EBDetId)(*it)).iphi() <<
" ";
183 <<
"ix = " << setw(3) << ((
EEDetId)(*it)).ix() <<
" iy = " << setw(3) << ((
EEDetId)(*it)).iy()
184 <<
" iz = " << setw(1) << ((
EEDetId)(*it)).iy() <<
" ";
203 result.resize(nWeights);
205 int nActualWeights = 0;
207 const double tzero = -(binOfMax - 1) * 25 + timePhase + shape.
timeToRise();
209 for (
int i = 0;
i < nWeights; ++
i) {
210 double t_ns = tzero + (iFirst +
i) * 25;
211 double s = shape(t_ns);
220 for (
int i = 0;
i < nWeights; ++
i) {
224 result[
i] = (result[
i] - sum / nActualWeights) / (sum2 - sum * sum / nActualWeights);
237 bool changed =
false;
239 for (
unsigned i = 0;
i < a.size(); ++
i)
245 for (
unsigned i = 0;
i < a.size() - 1; ++
i) {
247 const int nextj = s[
i + 1];
248 if ((decreasingOrder && (a[j] < a[nextj])) || (!decreasingOrder && (a[
j] > a[nextj]))) {
257 const unsigned nw = weights.size();
260 vector<double> dw(nw);
263 for (
unsigned i = 0;
i < nw; ++
i) {
279 int wsumSign = wsum > 0 ? 1 : -1;
280 int i = wsum > 0 ? 0 : (nw - 1);
282 W[iw[
i]] -= wsumSign;
285 if (i < 0 || i >= (
int)nw) {
287 for (
unsigned i = 0; i < nw; ++
i) {
299 if (encodedWeights !=
nullptr)
300 encodedWeights->resize(nw);
301 for (
unsigned i = 0; i < nw; ++
i) {
304 (*encodedWeights)[
i] = W[
i];
316 edm::LogVerbatim(
"EcalDccWeightBuilder") << (uint32_t)detId <<
" not found in EcalIntercalibConstantMap";
319 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": ";
322 <<
" iphi = " << ((
EBDetId)detId).iphi();
325 <<
" iy = " << ((
EEDetId)detId).iy()
326 <<
" iz = " << ((
EEDetId)detId).zside();
335 ofstream
file(fName.c_str());
337 throw cms::Exception(
"Output") <<
"Failed to open file '" << fName <<
"'for writing DCC weights\n";
340 const char* comment =
sqlMode_ ?
"-- " :
"# ";
342 file << comment <<
"List of weights for amplitude estimation to be used in DCC for\n"
343 << comment <<
"zero suppresssion.\n\n";
345 file << comment <<
"Note: RU: trigger tower in EB, supercrystal in EE\n"
346 << comment <<
" xtl: crystal electronic channel id in RU, from 1 to 25\n\n"
347 << comment <<
" DetId SM FED RU xtl weights[0..5]...\n";
351 file <<
"variable recid number;\n"
352 "exec select COND2CONF_INFO_SQ.NextVal into :recid from DUAL;\n"
353 "insert into weights_info (rec_id,tag,version) values (:recid,'"
357 <<
"index of first sample used in the weighting sum\n"
362 " insert into dcc_weightsample_dat (rec_id, logic_id, sample_id, \n"
364 " values(:recid,fedid,"
374 file <<
"\n" << comment <<
"list of weights per crystal channel\n";
378 const DetId& detId = it->first;
386 dbId(detId, fedId, smId, ruId, xtalId);
391 file <<
"-- detId " << detId.
rawId() <<
"\n"
392 <<
"insert into dcc_weights_dat(rec_id,sm_id,fed_id,"
394 "weight_0,weight_1,weight_2,weight_3,weight_4,weight_5) \n"
398 const vector<int>&
weights = it->second;
401 file << delim << setw(2) << smId;
403 file << delim << setw(2) << ruId;
404 file << delim << setw(2) << xtalId;
406 for (
unsigned i = 0;
i < weights.size(); ++
i) {
407 file << delim << setw(5) << weights[
i];
414 throw cms::Exception(
"Output") <<
"Error while writing DCC weights to '" << fName <<
"' file.";
419 TFile
file(fName.c_str(),
"RECREATE");
420 if (
file.IsZombie()) {
421 throw cms::Exception(
"Output") <<
"Failed to open file '" << fName <<
"'for writing DCC weights\n";
423 TTree
t(
"dccWeights",
"Weights for DCC ZS filter");
424 const int nWeightMax = 20;
443 "weights[n_weights]/I");
446 buf.detId = it->first.rawId();
447 buf.n_weights = it->second.size();
452 if (
buf.n_weights > nWeightMax) {
453 throw cms::Exception(
"EcalDccWeight") <<
"Number of weights (" <<
buf.n_weights <<
") for DetId " <<
buf.detId
454 <<
" exceeded maximum limit (" << nWeightMax <<
") of root output format. ";
456 copy(it->second.begin(), it->second.end(),
buf.weights);
463 #ifndef DB_WRITE_SUPPORT
465 throw cms::Exception(
"DccWeight") <<
"Code was compiled without support for writing dcc weights directly "
466 " into configuration DB. Configurable writeToDB must be set to False. "
467 "sqlMode can be used to produce an SQL*PLUS script to fill the DB\n";
469 #else //DB_WRITE_SUPPORT defined
476 edm::LogVerbatim(
"EcalDccWeightBuilder") <<
"Making connection..." << flush;
487 }
catch (runtime_error&
e) {
499 int weight_id = weight_info.
getId();
500 edm::LogVerbatim(
"EcalDccWeightBuilder") <<
"WeightInfo inserted with ID " << weight_id;
502 vector<ODWeightsDat> datadel;
505 vector<ODWeightsSamplesDat> dcc1stSampleConfig(
nDccs);
507 dcc1stSampleConfig[
i].setId(weight_id);
508 dcc1stSampleConfig[
i].setFedId(601 +
i);
510 dcc1stSampleConfig[
i].setWeightNumber(-1);
516 const DetId& detId = it->first;
517 const unsigned nWeights = 6;
520 for (
unsigned i = 0;
i < weights.size(); ++
i) {
523 const vector<int>&
w = it->second;
524 weights[
i] =
i < w.size() ? w[
i] : 0;
528 one_dat.
setId(weight_id);
536 dbId(detId, fedId, smId, ruId, xtalId);
550 datadel.push_back(one_dat);
555 edm::LogVerbatim(
"EcalDccWeightBuilder") <<
"closed DB connection ... done";
557 #endif //DB_WRITE_SUPPORT not defined
562 fedId = 600 + elecId.
dccId();
568 smId = 10000 -
fedId;
571 const int stripLength = 5;
572 xtalId = (elecId.
stripId() - 1) * stripLength + elecId.
xtalId();
575 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": FED ID "
578 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": SM logical ID "
581 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": RU ID (TT or SC): "
584 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": strip:"
587 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": xtal in strip: "
588 << elecId.
xtalId() <<
"\n";
590 edm::LogVerbatim(
"EcalDccWeightBuilder") << __FILE__ <<
":" << __LINE__ <<
": xtalId in RU: "
Log< level::Info, true > LogVerbatim
void writeWeightToAsciiFile()
int xtalId() const
get the channel id
std::map< DetId, std::vector< int > > encodedWeights_
double decodeWeight(int W)
void analyze(const edm::Event &event, const edm::EventSetup &es) override
int stripId() const
get the tower id
const edm::ESGetToken< EcalElectronicsMapping, EcalMappingRcd > mappingToken_
const self & getMap() const
double intercalib(const DetId &detId)
Ecal readout channel identification [32:20] Unused (so far) [19:13] DCC id [12:6] tower [5:3] strip [...
std::vector< double > inputWeights_
const CaloSimParameters & simParameters(const DetId &id) const override
return the sim parameters relative to the right subdet
constexpr uint32_t rawId() const
get the raw id
edm::ESHandle< CaloGeometry > geom_
static const double weightScale_
void dbId(const DetId &detId, int &fedId, int &smId, int &ruId, int &xtalId) const
int towerId() const
get the tower id
double timeToRise() const override
Log< level::Error, false > LogError
const edm::ESGetToken< EcalIntercalibConstants, EcalIntercalibConstantsRcd > intercalibConstToken_
bool dccWeightsWithIntercalib_
void computeWeights(const EcalShapeBase &shape, int binOfMax, double timePhase, int iFirst0, int nWeights, int iSkip0, std::vector< double > &result)
bool getData(T &iHolder) const
double timePhase() const
the adjustment you need to apply to get the signal where you want it
void sort(const std::vector< T > &a, std::vector< int > &s, bool decreasingOrder=false)
EcalElectronicsId getElectronicsId(const DetId &id) const
Get the electronics id for this det id.
std::string asciiOutputFileName_
void insertConfigDataArraySet(const std::vector< DATT > &data, ICONF *iconf) noexcept(false)
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
EcalIntercalibConstantMap & calibMap_
void writeWeightToRootFile()
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector's numbering enum) ...
void insertConfigSet(ICONF *iconf) noexcept(false)
void setCrystalId(int dac)
int dccId() const
get the DCC (Ecal Local DCC value not global one) id
std::string rootOutputFileName_
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geometryToken_
void readPassword(const std::string &fileName, const std::string &user, std::string &password)
std::vector< Item >::const_iterator const_iterator
void unbiasWeights(std::vector< double > &weights, std::vector< int32_t > *encodedWeigths)
EcalDccWeightBuilder(edm::ParameterSet const &ps)
int encodeWeight(double w)
void setConfigTag(std::string x)
void computeAllWeights(bool withIntercalib, const edm::EventSetup &es)
const_iterator find(uint32_t rawId) const
static const double tzero[3]
const_iterator end() const
static const int ecalDccFedIdMax
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
const EcalElectronicsMapping * ecalElectronicsMap_
static const int ecalDccFedIdMin