LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
GFBookkeeping.cxx
Go to the documentation of this file.
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert
3 
4  This file is part of GENFIT.
5 
6  GENFIT is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  GENFIT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "GFBookkeeping.h"
20 #include "GFException.h"
21 #include "TBuffer.h"
22 #include "TString.h"
23 #include <sstream>
24 
26 {
27  fNhits = bk.fNhits;
28  fMatrices = bk.fMatrices; //implicit copy constructor call
29  fNumbers = bk.fNumbers;
30  fPlanes = bk.fPlanes;
32 
33  //deep copy
34 
35  std::map<std::string, TMatrixT<Double_t>*>::const_iterator it;
36  std::map<std::string, TMatrixT<Double_t>*>::iterator it_here;
37 
38  it = bk.fMatrices.begin();
39  it_here = fMatrices.begin();
40  while (it != bk.fMatrices.end()) {
41  it_here->second = new TMatrixT<Double_t>[fNhits];
42  for (int i = 0; i < fNhits; ++i) {
43  (it_here->second)[i] = (it->second)[i];
44  }
45  it++;
46  it_here++;
47  }
48 
49  it = bk.fNumbers.begin();
50  it_here = fNumbers.begin();
51  while (it != bk.fNumbers.end()) {
52  it_here->second = new TMatrixT<Double_t>[fNhits];
53  for (int i = 0; i < fNhits; ++i) {
54  (it_here->second)[i] = (it->second)[i];
55  }
56  it++;
57  it_here++;
58  }
59 
62 
63  ip = bk.fPlanes.begin();
64  ip_here = fPlanes.begin();
65  while (ip != bk.fPlanes.end()) {
66  ip_here->second = new genf::GFDetPlane[fNhits];
67  for (int i = 0; i < fNhits; ++i) {
68  (ip_here->second)[i] = ((ip->second)[i]);
69  }
70  ip++;
71  ip_here++;
72  }
73 }
74 
75 void genf::GFBookkeeping::Streamer(TBuffer& R__b)
76 {
77 
78  // Stream an object of class GFBookkeeping.
79  if (R__b.IsReading()) {
80 
81  // Version_t R__v = R__b.ReadVersion();
82  TObject::Streamer(R__b);
83 
84  clearAll();
85 
86  R__b >> fNhits;
87 
88  TString s;
89  std::string key;
90  unsigned int nkeys;
91  TMatrixT<Double_t> mat;
93 
94  { //reading matrices
95  R__b >> nkeys;
96  for (unsigned int i = 0; i < nkeys; ++i) {
97  s.Streamer(R__b);
98  key = s.Data();
99  bookMatrices(key);
100  for (int j = 0; j < fNhits; ++j) {
101  mat.Streamer(R__b);
102  setMatrix(key, j, mat);
103  }
104  }
105  } //done reading matrices
106  { //reading planes
107  R__b >> nkeys;
108  for (unsigned int i = 0; i < nkeys; ++i) {
109  s.Streamer(R__b);
110  key = s.Data();
111  bookGFDetPlanes(key);
112  for (int j = 0; j < fNhits; ++j) {
113  pl.Streamer(R__b);
114  setDetPlane(key, j, pl);
115  }
116  }
117  } //done reading planes
118  { //reading numbers
119  R__b >> nkeys;
120  for (unsigned int i = 0; i < nkeys; ++i) {
121  s.Streamer(R__b);
122  key = s.Data();
123  bookNumbers(key);
124  for (int j = 0; j < fNhits; ++j) {
125  mat.Streamer(R__b);
126  setNumber(key, j, mat[0][0]);
127  }
128  }
129  } //done reading numbers
130  { //read failed hits
131  clearFailedHits();
132  unsigned int nFailedHits;
133  R__b >> nFailedHits;
134  unsigned int aFailedHit;
135  for (unsigned int i = 0; i < nFailedHits; ++i) {
136  R__b >> aFailedHit;
137  fFailedHits.push_back(aFailedHit);
138  }
139  } //done reading failed hits
140  }
141  else {
142  // R__b.WriteVersion(genf::GFBookkeeping::IsA());
143  TObject::Streamer(R__b);
144 
145  //write number of hits
146  R__b << fNhits;
147 
148  std::vector<std::string> keys;
149  { //save matrices
150  keys = getMatrixKeys();
151  R__b << (unsigned int)(keys.size());
152  for (unsigned int i = 0; i < keys.size(); ++i) {
153  TString s(keys.at(i));
154  s.Streamer(R__b);
155  for (int j = 0; j < fNhits; ++j) {
156  ((fMatrices[keys.at(i)])[j]).Streamer(R__b);
157  }
158  }
159  }
160  keys.clear();
161  { //save GFDetPlanes
162  keys = getGFDetPlaneKeys();
163  R__b << (unsigned int)(keys.size());
164  for (unsigned int i = 0; i < keys.size(); ++i) {
165  TString s(keys.at(i));
166  s.Streamer(R__b);
167  for (int j = 0; j < fNhits; ++j) {
168  ((fPlanes[keys.at(i)])[j]).Streamer(R__b);
169  }
170  }
171  } //done saving GFDetPlanes
172  keys.clear();
173  { //save numbers
174  keys = getNumberKeys();
175  R__b << (unsigned int)(keys.size());
176  for (unsigned int i = 0; i < keys.size(); ++i) {
177  TString s(keys.at(i));
178  s.Streamer(R__b);
179  for (int j = 0; j < fNhits; ++j) {
180  ((fNumbers[keys.at(i)])[j]).Streamer(R__b);
181  }
182  }
183  } //done saving numbers
184  { //save failedHits
185  R__b << ((unsigned int)fFailedHits.size());
186  for (unsigned int i = 0; i < fFailedHits.size(); ++i) {
187  R__b << fFailedHits.at(i);
188  }
189  } //done saving failed Hits
190  }
191 }
192 
194 {
195  if (fNhits < 0) {
196  GFException exc("fNhits not defined", __LINE__, __FILE__);
197  throw exc;
198  }
199  if (fMatrices[key] != NULL) {
200  std::ostringstream ostr;
201  ostr << "The key " << key << " is already occupied in genf::GFBookkeeping::bookMatrices()";
202  GFException exc(ostr.str(), __LINE__, __FILE__);
203  throw exc;
204  }
205  fMatrices[key] = new TMatrixT<Double_t>[fNhits];
206 }
207 
209 {
210  if (fNhits < 0) {
211  GFException exc("fNhits not defined", __LINE__, __FILE__);
212  throw exc;
213  }
214  if (fPlanes[key] != NULL) {
215  std::ostringstream ostr;
216  ostr << "The key " << key << " is already occupied in genf::GFBookkeeping::bookGFDetPlanes()";
217  GFException exc(ostr.str(), __LINE__, __FILE__);
218  throw exc;
219  }
220  fPlanes[key] = new genf::GFDetPlane[fNhits];
221 }
222 
223 //val is default set to 0.
224 void genf::GFBookkeeping::bookNumbers(std::string key, double val)
225 {
226  if (fNhits < 0) {
227  GFException exc("fNhits not defined", __LINE__, __FILE__);
228  throw exc;
229  }
230  if (fPlanes[key] != NULL) {
231  std::ostringstream ostr;
232  ostr << "The key " << key << " is already occupied in genf::GFBookkeeping::bookNumbers()";
233  GFException exc(ostr.str(), __LINE__, __FILE__);
234  throw exc;
235  }
236  fNumbers[key] = new TMatrixT<Double_t>[fNhits];
237  for (int i = 0; i < fNhits; ++i) {
238  ((fNumbers[key])[i]).ResizeTo(1, 1);
239  ((fNumbers[key])[i])[0][0] = val;
240  }
241 }
242 
243 void genf::GFBookkeeping::setMatrix(std::string key,
244  unsigned int index,
245  const TMatrixT<Double_t>& mat)
246 {
247  if (fMatrices[key] == NULL) {
248  std::ostringstream ostr;
249  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setMatrix()";
250  GFException exc(ostr.str(), __LINE__, __FILE__);
251  throw exc;
252  }
253  if (index >= (unsigned int)fNhits) {
254  std::ostringstream ostr;
255  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::setMatrix()";
256  GFException exc(ostr.str(), __LINE__, __FILE__);
257  throw exc;
258  }
259  (fMatrices[key])[index].ResizeTo(mat);
260  (fMatrices[key])[index] = mat;
261 }
262 void genf::GFBookkeeping::setDetPlane(std::string key,
263  unsigned int index,
264  const genf::GFDetPlane& pl)
265 {
266  if (fPlanes[key] == NULL) {
267  std::ostringstream ostr;
268  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setGFDetPlane()";
269  GFException exc(ostr.str(), __LINE__, __FILE__);
270  throw exc;
271  }
272  if (index >= (unsigned int)fNhits) {
273  std::ostringstream ostr;
274  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::setGFDetPlane()";
275  GFException exc(ostr.str(), __LINE__, __FILE__);
276  throw exc;
277  }
278  (fPlanes[key])[index] = pl;
279 }
280 void genf::GFBookkeeping::setNumber(std::string key, unsigned int index, const double& num)
281 {
282  if (fNumbers[key] == NULL) {
283  std::ostringstream ostr;
284  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setNumber()";
285  GFException exc(ostr.str(), __LINE__, __FILE__);
286  throw exc;
287  }
288  if (index >= (unsigned int)fNhits) {
289  std::ostringstream ostr;
290  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::setNumber()";
291  GFException exc(ostr.str(), __LINE__, __FILE__);
292  throw exc;
293  }
294  ((fNumbers[key])[index])[0][0] = num;
295 }
296 
297 bool genf::GFBookkeeping::getMatrix(std::string key,
298  unsigned int index,
299  TMatrixT<Double_t>& mat) const
300 {
301  auto iMatrix = fMatrices.find(key);
302  if (iMatrix == fMatrices.end()) {
303  std::ostringstream ostr;
304  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getMatrix()";
305  GFException exc(ostr.str(), __LINE__, __FILE__);
306  throw exc;
307  }
308  if (index >= (unsigned int)fNhits) {
309  std::ostringstream ostr;
310  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::getMatrix()";
311  GFException exc(ostr.str(), __LINE__, __FILE__);
312  throw exc;
313  }
314  mat.ResizeTo(iMatrix->second[index]);
315  mat = iMatrix->second[index];
316  return true;
317 }
318 bool genf::GFBookkeeping::getDetPlane(std::string key,
319  unsigned int index,
320  genf::GFDetPlane& pl) const
321 {
322 
323  auto iPlane = fPlanes.find(key);
324  if (iPlane == fPlanes.end()) {
325  std::ostringstream ostr;
326  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getGFDetPlane()";
327  GFException exc(ostr.str(), __LINE__, __FILE__);
328  throw exc;
329  }
330  if (index >= (unsigned int)fNhits) {
331  std::ostringstream ostr;
332  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::getGFDetPlane()";
333  GFException exc(ostr.str(), __LINE__, __FILE__);
334  throw exc;
335  }
336  pl = iPlane->second[index];
337  return true;
338 }
339 bool genf::GFBookkeeping::getNumber(std::string key, unsigned int index, double& num) const
340 {
341  auto iNumber = fNumbers.find(key);
342  if (iNumber == fNumbers.end()) {
343  std::ostringstream ostr;
344  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getNumber()";
345  GFException exc(ostr.str(), __LINE__, __FILE__);
346  throw exc;
347  }
348  if (index >= (unsigned int)fNhits) {
349  std::ostringstream ostr;
350  ostr << "The index " << index << " is out of range in genf::GFBookkeeping::getNumber()";
351  GFException exc(ostr.str(), __LINE__, __FILE__);
352  throw exc;
353  }
354  num = iNumber->second[index][0][0];
355  return true;
356 }
357 
359 {
360  fFailedHits.push_back(id);
361 }
362 
364 {
365  return fFailedHits.size();
366 }
367 
368 unsigned int genf::GFBookkeeping::hitFailed(unsigned int id)
369 {
370  unsigned int retVal = 0;
371  for (unsigned int i = 0; i < fFailedHits.size(); ++i) {
372  if (fFailedHits.at(i) == id) { ++retVal; }
373  }
374  return retVal;
375 }
376 
378 {
379  fFailedHits.clear();
380 }
381 
383 {
384  std::vector<std::string> matKeys = getMatrixKeys();
385  std::vector<std::string> planeKeys = getGFDetPlaneKeys();
386  std::vector<std::string> numKeys = getNumberKeys();
387 
388  clearAll();
389  clearFailedHits();
390 
391  for (unsigned int i = 0; i < matKeys.size(); ++i) {
392  bookMatrices(matKeys.at(i));
393  }
394  for (unsigned int i = 0; i < planeKeys.size(); ++i) {
395  bookGFDetPlanes(planeKeys.at(i));
396  }
397  for (unsigned int i = 0; i < numKeys.size(); ++i) {
398  bookNumbers(numKeys.at(i));
399  }
400 }
401 
403 {
404  std::map<std::string, TMatrixT<Double_t>*>::iterator itMat;
405  for (itMat = fMatrices.begin(); itMat != fMatrices.end(); itMat++) {
406  if (itMat->second != NULL) delete[] itMat->second;
407  }
409  for (itPl = fPlanes.begin(); itPl != fPlanes.end(); itPl++) {
410  if (itPl->second != NULL) delete[] itPl->second;
411  }
412  std::map<std::string, TMatrixT<Double_t>*>::iterator itNum;
413  for (itNum = fNumbers.begin(); itNum != fNumbers.end(); itNum++) {
414  if (itNum->second != NULL) delete[] itNum->second;
415  }
416  fMatrices.clear();
417  fPlanes.clear();
418  fNumbers.clear();
419 }
420 
421 std::vector<std::string> genf::GFBookkeeping::getMatrixKeys() const
422 {
423  std::vector<std::string> keys;
424  std::map<std::string, TMatrixT<Double_t>*>::const_iterator it;
425  for (it = fMatrices.begin(); it != fMatrices.end(); it++) {
426  if (it->second != NULL) keys.push_back(it->first);
427  }
428  return keys;
429 }
430 std::vector<std::string> genf::GFBookkeeping::getGFDetPlaneKeys() const
431 {
432  std::vector<std::string> keys;
434  for (it = fPlanes.begin(); it != fPlanes.end(); it++) {
435  if (it->second != NULL) keys.push_back(it->first);
436  }
437  return keys;
438 }
439 std::vector<std::string> genf::GFBookkeeping::getNumberKeys() const
440 {
441  std::vector<std::string> keys;
442  std::map<std::string, TMatrixT<Double_t>*>::const_iterator it;
443  for (it = fNumbers.begin(); it != fNumbers.end(); it++) {
444  if (it->second != NULL) keys.push_back(it->first);
445  }
446  return keys;
447 }
448 
449 void genf::GFBookkeeping::Print(std::ostream& out /* = std::cout */) const
450 {
451  out << "=============genf::GFBookkeeping::print()==============" << std::endl;
452  out << "-----printing all matrices:------" << std::endl;
453  std::vector<std::string> keys = getMatrixKeys();
454  for (unsigned int i = 0; i < keys.size(); ++i) {
455  out << "key " << keys.at(i) << " has " << fNhits << " entries:" << std::endl;
456  for (int j = 0; j < fNhits; ++j) {
457  TMatrixT<Double_t> m;
458  getMatrix(keys.at(i), j, m);
459  m.Print(); // TODO print the matrix outself
460  }
461  }
462  out << "-----printing all GFDetPlanes:------" << std::endl;
463  keys = getGFDetPlaneKeys();
464  for (unsigned int i = 0; i < keys.size(); ++i) {
465  out << "key " << keys.at(i) << " has " << fNhits << " entries:" << std::endl;
466  for (int j = 0; j < fNhits; ++j) {
468  getDetPlane(keys.at(i), j, p);
469  p.Print(out);
470  }
471  }
472  out << "-----printing all numbers:------" << std::endl;
473  keys = getNumberKeys();
474  for (unsigned int i = 0; i < keys.size(); ++i) {
475  out << "key " << keys.at(i) << " has " << fNhits << " entries:" << std::endl;
476  for (int j = 0; j < fNhits; ++j) {
477  double n(-1111.);
478  getNumber(keys.at(i), j, n);
479  out << n << std::endl;
480  }
481  }
482  out << "-----failed hits:------" << std::endl;
483  for (unsigned int i = 0; i < fFailedHits.size(); ++i) {
484  out << fFailedHits.at(i) << " ";
485  }
486  out << std::endl;
487 }
488 
489 //ClassImp(GFBookkeeping)
void setDetPlane(std::string key, unsigned int index, const GFDetPlane &pl)
intermediate_table::iterator iterator
void Print(std::ostream &out=std::cout) const
std::map< std::string, GFDetPlane * > fPlanes
Definition: GFBookkeeping.h:37
intermediate_table::const_iterator const_iterator
void setNumber(std::string key, unsigned int index, const double &num)
void Print(std::ostream &out=std::cout) const
Definition: GFDetPlane.cxx:228
void setMatrix(std::string key, unsigned int index, const TMatrixT< Double_t > &mat)
void addFailedHit(unsigned int)
void bookGFDetPlanes(std::string key)
Float_t mat
Definition: plot.C:38
unsigned int hitFailed(unsigned int)
bool getNumber(std::string key, unsigned int index, double &num) const
std::map< std::string, TMatrixT< Double_t > * > fNumbers
Definition: GFBookkeeping.h:41
void Streamer(TBuffer &)
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:47
void bookMatrices(std::string key)
std::vector< std::string > getMatrixKeys() const
std::vector< std::string > getGFDetPlaneKeys() const
bool getMatrix(std::string key, unsigned int index, TMatrixT< Double_t > &mat) const
void bookNumbers(std::string key, double val=0.)
Char_t n[5]
std::vector< std::string > getNumberKeys() const
bool getDetPlane(std::string key, unsigned int index, GFDetPlane &pl) const
std::vector< unsigned int > fFailedHits
Definition: GFBookkeeping.h:42
std::map< std::string, TMatrixT< Double_t > * > fMatrices
Definition: GFBookkeeping.h:36
unsigned int getNumFailed()