Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-07-28 03:09:51

0001 /*
0002  * DataPointDefinition.cc
0003  *
0004  *  Created on: Sep 24, 2012
0005  *      Author: aspataru
0006  */
0007 
0008 #include "EventFilter/Utilities/interface/DataPointDefinition.h"
0009 #include "EventFilter/Utilities/interface/JsonMonitorable.h"
0010 #include "EventFilter/Utilities/interface/FileIO.h"
0011 #include "EventFilter/Utilities/interface/JSONSerializer.h"
0012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0013 
0014 using namespace jsoncollector;
0015 
0016 const std::string DataPointDefinition::SUM = "sum";
0017 const std::string DataPointDefinition::AVG = "avg";
0018 const std::string DataPointDefinition::SAME = "same";
0019 const std::string DataPointDefinition::HISTO = "histo";
0020 const std::string DataPointDefinition::CAT = "cat";
0021 const std::string DataPointDefinition::BINARYOR = "binaryOr";
0022 const std::string DataPointDefinition::MERGE = "merge";
0023 const std::string DataPointDefinition::ADLER32 = "adler32";
0024 
0025 const std::string DataPointDefinition::LEGEND = "legend";
0026 const std::string DataPointDefinition::DATA = "data";
0027 const std::string DataPointDefinition::PARAM_NAME = "name";
0028 const std::string DataPointDefinition::OPERATION = "operation";
0029 const std::string DataPointDefinition::TYPE = "type";
0030 
0031 //static member implementation
0032 bool DataPointDefinition::getDataPointDefinitionFor(std::string& defFilePath,
0033                                                     DataPointDefinition* dpd,
0034                                                     const std::string* defaultGroup) {
0035   std::string dpdString;
0036   bool readOK = FileIO::readStringFromFile(defFilePath, dpdString);
0037   // data point definition is missing!
0038   if (!readOK) {
0039     edm::LogWarning("DataPointDefinition") << "Cannot read from JSON definition path -: " << defFilePath;
0040     return false;
0041   }
0042   if (!defaultGroup)
0043     dpd->setDefaultGroup(LEGEND);
0044   else
0045     dpd->setDefaultGroup(*defaultGroup);
0046   JSONSerializer::deserialize(dpd, dpdString);
0047   return true;
0048 }
0049 
0050 void DataPointDefinition::serialize(Json::Value& root) const {
0051   for (unsigned int i = 0; i < varNames_.size(); i++) {
0052     Json::Value currentDef;
0053     currentDef[PARAM_NAME] = varNames_[i];
0054     currentDef[OPERATION] = opNames_[i];
0055     if (!typeNames_[i].empty())  //only if it was found
0056       currentDef[TYPE] = typeNames_[i];
0057     root[defaultGroup_].append(currentDef);
0058   }
0059 }
0060 
0061 void DataPointDefinition::deserialize(Json::Value& root) {
0062   if (root.get(defaultGroup_, "").isArray()) {
0063     unsigned int size = root.get(defaultGroup_, "").size();
0064     for (unsigned int i = 0; i < size; i++) {
0065       varNames_.push_back(root.get(defaultGroup_, "")[i].get(PARAM_NAME, "").asString());
0066       opNames_.push_back(root.get(defaultGroup_, "")[i].get(OPERATION, "").asString());
0067       typeNames_.push_back(root.get(defaultGroup_, "")[i].get(TYPE, "").asString());
0068     }
0069   }
0070 }
0071 
0072 bool DataPointDefinition::isPopulated() const {
0073   if (!varNames_.empty())
0074     return true;
0075   return false;
0076 }
0077 
0078 OperationType DataPointDefinition::getOperationFor(unsigned int index) const {
0079   OperationType opType = OPUNKNOWN;
0080   if (opNames_.at(index) == DataPointDefinition::SUM)
0081     opType = OPSUM;
0082   if (opNames_.at(index) == DataPointDefinition::AVG)
0083     opType = OPAVG;
0084   if (opNames_.at(index) == DataPointDefinition::SAME)
0085     opType = OPSAME;
0086   if (opNames_.at(index) == DataPointDefinition::HISTO)
0087     opType = OPHISTO;
0088   if (opNames_.at(index) == DataPointDefinition::CAT)
0089     opType = OPCAT;
0090   return opType;
0091 }
0092 
0093 void DataPointDefinition::addLegendItem(std::string const& name,
0094                                         std::string const& type,
0095                                         std::string const& operation) {
0096   varNames_.push_back(name);
0097   typeNames_.push_back(type);
0098   opNames_.push_back(operation);
0099 }