Diana Software
QAverageVector.cc
Go to the documentation of this file.
1 #include "QAverageVector.hh"
2 #include <sstream>
3 #include <algorithm>
4 
5 using std::stringstream;
6 using std::vector;
7 using std::endl;
8 using std::cout;
9 using std::string;
10 
12 using namespace Diana;
13 
15  : fNumEvents(0)
16 {}
18  : QVector(orig),
19  fNumEvents(0)
20 {
23 }
24 QAverageVector::QAverageVector(const QVector &orig)
25  : QVector(orig),
26  fNumEvents(0)
27 {}
29 {
31  fSourceRuns.clear();
32  fNumEvents = 0;
33 }
34 
35 QError QAverageVector::WriteOnStream (std::ostream &o) const
36 {
38 
39  // QVector dump
40  const QVector vec = *this;
41  err = vec.WriteOnStream(o);
42  if(err != QERR_SUCCESS) return err;
43  o<<"fNumEvents"<<"\t"<<fNumEvents<<endl;
44  // source runs
45  o<<"fSourceRuns.size"<<"\t"<<fSourceRuns.size()<<endl;
46 
47  if(fSourceRuns.empty()) return err;
48 
49  for(size_t j=0; j<fSourceRuns.size(); j++) {
50  o<<"fSourceRuns["<<j<<"]"<<"\t"<<fSourceRuns[j]<<"\t"<< endl;
51  }
52 
53  return err;
54 }
55 
57 {
59 
60  std::string tag;
61  int numev, size, run;
62  stringstream str;
63  // read QVector
64 
65  QVector vec;
66 
67  err = vec.ReadFromStream(ob);
68  if(err != QERR_SUCCESS) return err;
69  this->QVector::operator=(vec);
70 
71  // restart from the beginning of ob
72  ob.clear();
73  ob.seekg(std::ios::beg);
74  // read fNumEvents
75  string line;
76  do {
77  getline(ob,line);
78  stringstream sline;
79  sline<<line;
80  sline>> tag;
81  if(tag == "fNumEvents") sline >> numev;
82  } while(tag != "fNumEvents");
83 
84  if(tag != "fNumEvents") {
86  err.SetDescription("Expected tag fNumEvents");
87  return err;
88  }
89  fNumEvents = numev;
90 
91  // read fSource Runs
92  ob >> tag >> size;
93  if(tag != "fSourceRuns.size") {
95  err.SetDescription("Expected tag fSourceRuns.size");
96  return err;
97  }
98 
99  char s[50];
100 
101  for(Int_t i=0; i<size; i++) {
102  ob >> tag >> run;
103  snprintf(s,50,"fSourceRuns[%d]",i);
104  if(tag != s) {
106  err.SetDescription("Expected tag fSourceRuns");
107  return err;
108  }
109  fSourceRuns.push_back(run);
110  }
111 
112 
113  return err;
114 }
115 /*
116 const QAverageVector &QAverageVector::operator+=(const QAverageVector &alt)
117 {
118  // Make sure the sizes match
119  if(Size()!=alt.Size()){
120  std::stringstream msg;
121  msg << "Can not add two average vectors of different size. ("
122  << Size() << "!=" << alt.Size() << ").";
123  DianaThrow(QError(QERR_SIZE_NOT_MATCH,__FILE__,__LINE__,
124  msg.str().c_str()));
125  }
126 
127  // Get the new weighted average
128  for(size_t i=0;i<Size();i++)
129  (*this)[i]=(fNumEvents*(*this)[i]+alt.fNumEvents*alt[i])
130  *1./(fNumEvents+alt.fNumEvents);
131 
132  // Set the new number of Events
133  fNumEvents+=alt.fNumEvents;
134 
135  // Update the list of source runs
136  for(size_t i=0;i<alt.fSourceRuns.size();i++)
137  fSourceRuns.push_back(alt.fSourceRuns[i]);
138 
139  // Make sure the list is unique
140  std::sort(fSourceRuns.begin(),fSourceRuns.end());
141  std::vector<int>::iterator it=
142  std::unique(fSourceRuns.begin(),fSourceRuns.end());
143  fSourceRuns.resize(std::distance(fSourceRuns.begin(),it));
144 
145  return (*this);
146 }
147 
148 QAverageVector QAverageVector::operator+(const QAverageVector &alt) const
149 {
150  QAverageVector ret(*this);
151  ret+=alt;
152  return ret;
153 }
154 */
155 const QAverageVector& QAverageVector::operator=(const QVector& orig)
156 {
157  Clear();
158  QVector::operator=(orig);
159  return *this;
160 }
162 {
163  Clear();
164  QVector::operator=(orig);
165  fNumEvents=orig.fNumEvents;
167  return *this;
168 }
err
Definition: CheckOF.C:114
QVector vec(3)
QObjectImp(QAverageVector)
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_SUCCESS
Definition: QError.hh:27
average vector object
int fNumEvents
The number of events contributing to this average.
const QAverageVector & operator=(const Diana::QVector &orig)
Assignment operator.
QError WriteOnStream(std::ostream &o) const
Write the object to a stream.
QError ReadFromStream(std::istream &ob)
Read the object from a stream.
QAverageVector()
Default ctor.
void Clear()
Clear the attributes.
std::vector< int > fSourceRuns
Add two average vectors.
error class with error type and description
Definition: QError.hh:115
virtual void Clear()
clear the vector
Definition: QVector.hh:406
const QVector & operator=(const QVector &orig)
copy the content from another QVector
Definition: QVector.cc:188
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...