CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Operations.cc
Go to the documentation of this file.
1 /*
2  * Operations.cc
3  *
4  * Created on: Sep 24, 2012
5  * Author: aspataru
6  */
7 
8 #include "../interface/Operations.h"
9 #include "../interface/Utils.h"
10 #include <sstream>
11 
12 using namespace jsoncollector;
13 using std::vector;
14 using std::string;
15 using std::stringstream;
16 
17 const string Operations::SUM = "sum";
18 const string Operations::AVG = "avg";
19 const string Operations::SAME = "same";
20 const string Operations::HISTO = "histo";
21 const string Operations::CAT = "cat";
22 
23 double Operations::sum(vector<double> elems) {
24  double added = elems.at(0);
25  for (unsigned int i = 1; i < elems.size(); i++)
26  added += elems.at(i);
27  return added;
28 }
29 
30 double Operations::avg(vector<double> elems) {
31  return sum(elems) / elems.size();
32 }
33 
34 string Operations::same(const vector<string>& elems) {
35  for (unsigned int i = 0; i < elems.size() - 1; i++)
36  if (!Utils::matchExactly(elems.at(i), elems.at(i + 1)) || elems.at(i).length()
37  == 0)
38  return "N/A";
39  return elems.at(0);
40 }
41 
42 string Operations::histo(const vector<string>& elems) {
43  vector<vector<int> > inputHistos;
44 
45  for (unsigned int i = 0; i < elems.size(); i++) {
46  vector<int> currentHisto;
47  string currentHistoAsString = elems.at(i);
48  Utils::stringToIntArray(currentHisto, currentHistoAsString);
49  inputHistos.push_back(currentHisto);
50  }
51 
52  if (inputHistos.size() > 0) {
53  // initialize resulting histo to largest size
54  unsigned int maxSize = 0;
55  for (unsigned int i = 0; i < inputHistos.size(); i++) {
56  if (inputHistos[i].size() > maxSize)
57  maxSize = inputHistos[i].size();
58  }
59 
60  // initialize the resulting histo to the size
61  vector<int> resultingHisto(maxSize);
62  for (unsigned int i = 0; i < resultingHisto.size(); i++)
63  resultingHisto[i] = 0;
64 
65  for (unsigned int i = 0; i < inputHistos.size(); i++) {
66  vector<int> currentHisto = inputHistos[i];
67  for (unsigned int j = 0; j < currentHisto.size(); j++) {
68  resultingHisto[j] += currentHisto[j];
69  }
70  }
71 
72  string resHistoAsString;
73  Utils::intArrayToString(resultingHisto, resHistoAsString);
74  return resHistoAsString;
75 
76  } else
77  return "Cannot load input histos";
78 }
79 
80 string Operations::cat(const vector<string>& elems) {
81  stringstream ss;
82  for (unsigned int i = 0; i < elems.size(); i++) {
83  ss << elems.at(i);
84  if (i != elems.size() - 1) {
85  ss << ", ";
86  }
87  }
88  return ss.str();
89 }
int i
Definition: DBlmapReader.cc:9
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 void stringToIntArray(std::vector< int > &theVector, const std::string &theString)
Definition: Utils.cc:52
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
tuple maxSize
&#39;/store/data/Commissioning08/BeamHalo/RECO/StuffAlmostToP5_v1/000/061/642/10A0FE34-A67D-DD11-AD05-000...
static bool matchExactly(std::string s1, std::string s2)
Definition: Utils.cc:37
static double avg(std::vector< double >)
Definition: Operations.cc:30
int j
Definition: DBlmapReader.cc:9
static const std::string SUM
Definition: Operations.h:31
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
tuple size
Write out results.
static std::string same(const std::vector< std::string > &)
Definition: Operations.cc:34