Diana Software
QAverageVectorC.cc
Go to the documentation of this file.
1 #include "QAverageVectorC.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  : QVectorC(orig),
19  fNumEvents(0)
20 {
23 }
24 QAverageVectorC::QAverageVectorC(const QVectorC &orig)
25  : QVectorC(orig),
26  fNumEvents(0)
27 {}
29 {
31  fSourceRuns.clear();
32  fNumEvents = 0;
33 }
34 
35 QError QAverageVectorC::WriteOnStream (std::ostream &o) const
36 {
38 
39  // QVectorC dump
40  const QVectorC 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 QVectorC
64 
65  QVectorC vec;
66 
67  err = vec.ReadFromStream(ob);
68  if(err != QERR_SUCCESS) return err;
69  this->QVectorC::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 QAverageVectorC &QAverageVectorC::operator+=(const QAverageVectorC &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 QAverageVectorC QAverageVectorC::operator+(const QAverageVectorC &alt) const
149 {
150  QAverageVectorC ret(*this);
151  ret+=alt;
152  return ret;
153 }
154 */
155 const QAverageVectorC& QAverageVectorC::operator=(const QVectorC& orig)
156 {
157  Clear();
158  QVectorC::operator=(orig);
159  return *this;
160 }
162 {
163  Clear();
164  QVectorC::operator=(orig);
165  fNumEvents=orig.fNumEvents;
167  return *this;
168 }
err
Definition: CheckOF.C:114
QVector vec(3)
QObjectImp(QAverageVectorC)
@ QERR_UNKNOWN_ERR
Definition: QError.hh:108
@ QERR_SUCCESS
Definition: QError.hh:27
average vector object
void Clear()
Clear the attributes.
const QAverageVectorC & operator=(const Diana::QVectorC &orig)
Assignment operator.
QError WriteOnStream(std::ostream &o) const
Write the object to a stream.
QAverageVectorC()
Default ctor.
QError ReadFromStream(std::istream &ob)
Read the object from a stream.
std::vector< int > fSourceRuns
Add two average vectors.
int fNumEvents
The number of events contributing to this average.
error class with error type and description
Definition: QError.hh:115
const QVectorC & operator=(const QVectorC &orig)
copy the content of another vector
Definition: QVectorC.cc:294
void Clear()
clear the vector
Definition: QVectorC.hh:75
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...