CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectMerger.cc
Go to the documentation of this file.
1 /*
2  * ObjectMerger.cc
3  *
4  * Created on: Sep 24, 2012
5  * Author: aspataru
6  */
7 
8 #include "../interface/ObjectMerger.h"
9 #include "../interface/Operations.h"
10 #include "../interface/Utils.h"
11 #include "../interface/FileIO.h"
12 #include "../interface/JSONSerializer.h"
13 #include <sstream>
14 #include <iostream>
15 
16 using namespace jsoncollector;
17 using std::vector;
18 using std::string;
19 using std::stringstream;
20 using std::cout;
21 using std::endl;
22 
23 DataPoint* ObjectMerger::merge(const vector<DataPoint*>& objectsToMerge,
24  string& outcomeMessage, bool onlyHistos) {
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 }
102 
104  DataPointDefinition& dpd) {
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 }
116 
118  string defPath) {
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 }
148 
149 string ObjectMerger::applyOperation(const std::vector<string>& dataVector,
150  std::string operationName) {
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 }
189 
190 bool ObjectMerger::checkConsistency(const std::vector<DataPoint*>& objectsToMerge,
191  std::string& outcomeMessage) {
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
static std::vector< double > vectorStringToDouble(const std::vector< std::string > &stringVector)
Definition: Utils.cc:30
static bool readStringFromFile(std::string &filename, std::string &content)
Definition: FileIO.cc:33
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
#define NULL
Definition: scimark2.h:8
static const std::string SAME
Definition: Operations.h:33
static const std::string HISTO
Definition: Operations.h:34
void addToData(std::string data)
Definition: DataPoint.h:50
tuple field
Definition: statics.py:62
static double sum(std::vector< double >)
Definition: Operations.cc:23
void setDefinition(std::string definition)
Definition: DataPoint.h:47
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 double avg(std::vector< double >)
Definition: Operations.cc:30
static const std::string SUM
Definition: Operations.h:31
static DataPoint * csvToJson(std::string &olCSV, DataPointDefinition *dpd, std::string defPath)
static bool checkConsistency(const std::vector< DataPoint * > &objectsToMerge, std::string &outcomeMessage)
static DataPoint * merge(const std::vector< DataPoint * > &objectsToMerge, std::string &outcomeMessage, bool onlyHistos)
Definition: ObjectMerger.cc:23
static bool deserialize(JsonSerializable *pObj, std::string &input)
static std::string cat(const std::vector< std::string > &)
Definition: Operations.cc:80
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
static bool getDataPointDefinitionFor(std::string defFilePath, DataPointDefinition &def)
tuple cout
Definition: gather_cfg.py:121
LegendItem getLegendFor(unsigned int index) const
static std::string const source
Definition: EdmProvDump.cc:43
static std::string same(const std::vector< std::string > &)
Definition: Operations.cc:34
std::vector< LegendItem > getLegend() const