CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Static Public Member Functions | Static Private Member Functions
jsoncollector::ObjectMerger Class Reference

#include <ObjectMerger.h>

Static Public Member Functions

static DataPointcsvToJson (std::string &olCSV, DataPointDefinition *dpd, std::string defPath)
 
static bool getDataPointDefinitionFor (std::string defFilePath, DataPointDefinition &def)
 
static DataPointmerge (const std::vector< DataPoint * > &objectsToMerge, std::string &outcomeMessage, bool onlyHistos)
 

Static Private Member Functions

static std::string applyOperation (const std::vector< std::string > &dataVector, std::string operationName)
 
static bool checkConsistency (const std::vector< DataPoint * > &objectsToMerge, std::string &outcomeMessage)
 

Detailed Description

Definition at line 16 of file ObjectMerger.h.

Member Function Documentation

string ObjectMerger::applyOperation ( const std::vector< std::string > &  dataVector,
std::string  operationName 
)
staticprivate

Definition at line 149 of file ObjectMerger.cc.

References jsoncollector::Operations::avg(), jsoncollector::Operations::AVG, jsoncollector::Operations::cat(), jsoncollector::Operations::CAT, gather_cfg::cout, jsoncollector::Operations::histo(), jsoncollector::Operations::HISTO, jsoncollector::Operations::same(), jsoncollector::Operations::SAME, jsoncollector::Operations::sum(), jsoncollector::Operations::SUM, and jsoncollector::Utils::vectorStringToDouble().

Referenced by merge().

150  {
151  string opResultString = "N/A";
152 
153  if (operationName.compare(Operations::SUM) == 0) {
154  stringstream ss;
155  double opResult = Operations::sum(
156  Utils::vectorStringToDouble(dataVector));
157  ss << opResult;
158  opResultString = ss.str();
159 
160  } else if (operationName.compare(Operations::AVG) == 0) {
161  stringstream ss;
162  double opResult = Operations::avg(
163  Utils::vectorStringToDouble(dataVector));
164  ss << opResult;
165  opResultString = ss.str();
166  }
167  /*
168  * ADD MORE OPERATIONS HERE
169  */
170  else if (operationName.compare(Operations::SAME) == 0) {
171  opResultString = Operations::same(dataVector);
172  }
173 
174  else if (operationName.compare(Operations::HISTO) == 0) {
175  opResultString = Operations::histo(dataVector);
176  }
177 
178  else if (operationName.compare(Operations::CAT) == 0) {
179  opResultString = Operations::cat(dataVector);
180  }
181 
182  // OPERATION WAS NOT DEFINED
183  else {
184  cout << "Operation " << operationName << " is NOT DEFINED!" << endl;
185  }
186 
187  return opResultString;
188 }
static std::vector< double > vectorStringToDouble(const std::vector< std::string > &stringVector)
Definition: Utils.cc:30
static const std::string CAT
Definition: Operations.h:35
static const std::string AVG
Definition: Operations.h:32
static std::string histo(const std::vector< std::string > &)
Definition: Operations.cc:42
static const std::string SAME
Definition: Operations.h:33
static const std::string HISTO
Definition: Operations.h:34
static double sum(std::vector< double >)
Definition: Operations.cc:23
static double avg(std::vector< double >)
Definition: Operations.cc:30
static const std::string SUM
Definition: Operations.h:31
static std::string cat(const std::vector< std::string > &)
Definition: Operations.cc:80
tuple cout
Definition: gather_cfg.py:121
static std::string same(const std::vector< std::string > &)
Definition: Operations.cc:34
bool ObjectMerger::checkConsistency ( const std::vector< DataPoint * > &  objectsToMerge,
std::string &  outcomeMessage 
)
staticprivate

Definition at line 190 of file ObjectMerger.cc.

References AlCaHLTBitMon_QueryRunRegistry::getData(), and i.

Referenced by merge().

191  {
192 
193  for (unsigned int i = 0; i < objectsToMerge.size() - 1; i++) {
194  // 1. Check if all have the same definition
195  if (objectsToMerge[i]->getDefinition().compare(
196  objectsToMerge.at(i + 1)->getDefinition()) != 0) {
197  outcomeMessage = "JSON files have inconsistent definitions!";
198  return false;
199  }
200  // 2. Check if objects to merge have the same number of elements in data vector
201  if (objectsToMerge[i]->getData().size()
202  != objectsToMerge.at(i + 1)->getData().size()) {
203  outcomeMessage
204  = "JSON files have inconsistent number of elements in the data vector!";
205  return false;
206  }
207  }
208  return true;
209 }
int i
Definition: DBlmapReader.cc:9
DataPoint * ObjectMerger::csvToJson ( std::string &  olCSV,
DataPointDefinition dpd,
std::string  defPath 
)
static

Transforms the CSV string into a DataPoint object using the definition

Definition at line 117 of file ObjectMerger.cc.

References jsoncollector::DataPoint::addToData(), jsoncollector::Utils::bumpIndex(), reco::dp, statics::field, jsoncollector::DataPointDefinition::getLegendFor(), jsoncollector::LegendItem::getOperation(), jsoncollector::Operations::HISTO, timingPdfMaker::histo, i, getHLTprescales::index, jsoncollector::Utils::intArrayToString(), jsoncollector::DataPoint::resetData(), and jsoncollector::DataPoint::setDefinition().

Referenced by jsoncollector::FastMonitor::outputFullHistoDataPoint().

118  {
119 
120  DataPoint* dp = new DataPoint();
121  dp->setDefinition(defPath);
122 
123  vector<string> tokens;
124  std::istringstream ss(olCSV);
125  while (!ss.eof()) {
126  string field;
127  getline(ss, field, ',');
128  tokens.push_back(field);
129  }
130 
131  dp->resetData();
132 
133  for (unsigned int i = 0; i < tokens.size(); i++) {
134  string currentOpName = dpd->getLegendFor(i).getOperation();
135  int index = atoi(tokens[i].c_str());
136  if (currentOpName.compare(Operations::HISTO) == 0) {
137  vector<int> histo;
138  Utils::bumpIndex(histo, index);
139  string histoStr;
140  Utils::intArrayToString(histo, histoStr);
141  dp->addToData(histoStr);
142  } else
143  dp->addToData(tokens[i]);
144  }
145 
146  return dp;
147 }
int i
Definition: DBlmapReader.cc:9
static const std::string HISTO
Definition: Operations.h:34
void addToData(std::string data)
Definition: DataPoint.h:50
tuple field
Definition: statics.py:62
void setDefinition(std::string definition)
Definition: DataPoint.h:47
std::string getOperation() const
Definition: LegendItem.h:23
static void intArrayToString(std::vector< int > &theVector, std::string &theString)
Definition: Utils.cc:72
auto dp
Definition: deltaR.h:24
static void bumpIndex(std::vector< int > &theVector, unsigned int index)
Definition: Utils.cc:85
LegendItem getLegendFor(unsigned int index) const
bool ObjectMerger::getDataPointDefinitionFor ( std::string  defFilePath,
DataPointDefinition def 
)
static

Loads DataPointDefinition into the specified reference

Definition at line 103 of file ObjectMerger.cc.

References gather_cfg::cout, jsoncollector::JSONSerializer::deserialize(), and jsoncollector::FileIO::readStringFromFile().

Referenced by jsoncollector::DataPointMonitor::DataPointMonitor(), jsoncollector::FastMonitor::FastMonitor(), and merge().

104  {
105  string dpdString;
106  bool readOK = FileIO::readStringFromFile(defFilePath, dpdString);
107  // data point definition is bad!
108  if (!readOK) {
109  cout << "Cannot read from JSON definition path: " << defFilePath
110  << endl;
111  return false;
112  }
113  JSONSerializer::deserialize(&dpd, dpdString);
114  return true;
115 }
static bool readStringFromFile(std::string &filename, std::string &content)
Definition: FileIO.cc:33
static bool deserialize(JsonSerializable *pObj, std::string &input)
tuple cout
Definition: gather_cfg.py:121
DataPoint * ObjectMerger::merge ( const std::vector< DataPoint * > &  objectsToMerge,
std::string &  outcomeMessage,
bool  onlyHistos 
)
static

Merges the DataPoint objects in the vector by getting their definition and applying the required operations If the onlyHistos arg is set to true, only histograms will be merged, will for other params the latest value (@ objectsToMerge.size() - 1) will be taken

Definition at line 23 of file ObjectMerger.cc.

References applyOperation(), checkConsistency(), AlCaHLTBitMon_QueryRunRegistry::getData(), getDataPointDefinitionFor(), jsoncollector::DataPointDefinition::getLegend(), jsoncollector::DataPointDefinition::getLegendFor(), jsoncollector::LegendItem::getOperation(), jsoncollector::Operations::HISTO, i, jsoncollector::Utils::matchExactly(), NULL, and source.

Referenced by jsoncollector::FastMonitor::outputFullHistoDataPoint().

24  {
25  // vector of vectors containing all data in datapoints
26  vector<vector<string> > mergedData;
27 
28  // check consistency of input files
29  if (!checkConsistency(objectsToMerge, outcomeMessage))
30  return NULL;
31 
32  // 1. Get the definition of these data points
34  getDataPointDefinitionFor(objectsToMerge.at(0)->getDefinition(), dpd);
35 
36  // 1.1 Check if definition has exact no of elements specified
37  if (objectsToMerge[0]->getData().size() != dpd.getLegend().size()) {
38  outcomeMessage
39  = "JSON files and DEFINITION do not have the same number of elements in vectors!";
40  //delete dpd;
41  return NULL;
42  }
43 
44  // 2. Assemble merged vector
45  for (unsigned int nMetric = 0; nMetric
46  < objectsToMerge.at(0)->getData().size(); nMetric++) {
47  // 2.1. assemble vector of n-th data elements
48  vector<string> metricVector;
49  for (unsigned int nObj = 0; nObj < objectsToMerge.size(); nObj++)
50  metricVector.push_back(objectsToMerge.at(nObj)->getData()[nMetric]);
51  mergedData.push_back(metricVector);
52  }
53 
54  // 3. Apply defined operation for each measurement in all data points
55  vector<string> outputValues;
56  for (unsigned int i = 0; i < mergedData.size(); i++) {
57  string strVal = "";
58  if (onlyHistos) {
61  strVal = applyOperation(mergedData[i],
62  dpd.getLegendFor(i).getOperation());
63  } else {
64  strVal = mergedData[i][mergedData[i].size() - 1];
65  }
66 
67  } else {
68  strVal = applyOperation(mergedData[i],
69  dpd.getLegendFor(i).getOperation());
70 
71  }
72  outputValues.push_back(strVal);
73  }
74 
75  // 4. Assemble output DataPoint
76  /*
77  * Change @ 19.06.2013
78  * Merging no longer concatenates DataPoint sources.
79  * The merged object will contain as source all contributing files,
80  * added by the caller class.
81  *
82  */
83  /*
84  stringstream ss;
85  for (unsigned int i = 0; i < objectsToMerge.size(); i++) {
86  ss << objectsToMerge[i]->getSource();
87  if (i != objectsToMerge.size(objectsToMerge) - 1)
88  ss << ", ";
89  }
90  string source = ss.str();
91  */
92 
93  string source = "";
94  string definition = objectsToMerge[0]->getDefinition();
95  DataPoint* outputDP = new DataPoint(source, definition, outputValues);
96 
97  // delete dpd after no longer needed
98  //delete dpd;
99 
100  return outputDP;
101 }
int i
Definition: DBlmapReader.cc:9
#define NULL
Definition: scimark2.h:8
static const std::string HISTO
Definition: Operations.h:34
std::string getOperation() const
Definition: LegendItem.h:23
static std::string applyOperation(const std::vector< std::string > &dataVector, std::string operationName)
static bool matchExactly(std::string s1, std::string s2)
Definition: Utils.cc:37
static bool checkConsistency(const std::vector< DataPoint * > &objectsToMerge, std::string &outcomeMessage)
static bool getDataPointDefinitionFor(std::string defFilePath, DataPointDefinition &def)
LegendItem getLegendFor(unsigned int index) const
static std::string const source
Definition: EdmProvDump.cc:43
std::vector< LegendItem > getLegend() const