LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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<sstream>
22 #include"TString.h"
23 #include"TBuffer.h"
24 
25 
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 }
76 
77 
78 void genf::GFBookkeeping::Streamer(TBuffer &R__b)
79 {
80 
81 
82  // Stream an object of class GFBookkeeping.
83  if (R__b.IsReading()) {
84 
85  // Version_t R__v = R__b.ReadVersion();
86  TObject::Streamer(R__b);
87 
88  clearAll();
89 
90  R__b >> fNhits;
91 
92  TString s;
93  std::string key;
94  unsigned int nkeys;
95  TMatrixT<Double_t> mat;
97 
98  {//reading matrices
99  R__b >> nkeys;
100  for(unsigned int i=0;i<nkeys;++i){
101  s.Streamer(R__b);
102  key = s.Data();
103  bookMatrices(key);
104  for(int j=0;j<fNhits;++j){
105  mat.Streamer(R__b);
106  setMatrix(key,j,mat);
107  }
108  }
109  }//done reading matrices
110  {//reading planes
111  R__b >> nkeys;
112  for(unsigned int i=0;i<nkeys;++i){
113  s.Streamer(R__b);
114  key = s.Data();
115  bookGFDetPlanes(key);
116  for(int j=0;j<fNhits;++j){
117  pl.Streamer(R__b);
118  setDetPlane(key,j,pl);
119  }
120  }
121  }//done reading planes
122  {//reading numbers
123  R__b >> nkeys;
124  for(unsigned int i=0;i<nkeys;++i){
125  s.Streamer(R__b);
126  key = s.Data();
127  bookNumbers(key);
128  for(int j=0;j<fNhits;++j){
129  mat.Streamer(R__b);
130  setNumber(key,j,mat[0][0]);
131  }
132  }
133  }//done reading numbers
134  {//read failed hits
135  clearFailedHits();
136  unsigned int nFailedHits;
137  R__b >> nFailedHits;
138  unsigned int aFailedHit;
139  for(unsigned int i=0;i<nFailedHits;++i){
140  R__b >> aFailedHit;
141  fFailedHits.push_back(aFailedHit);
142  }
143  }//done reading failed hits
144  } else {
145  // R__b.WriteVersion(genf::GFBookkeeping::IsA());
146  TObject::Streamer(R__b);
147 
148  //write number of hits
149  R__b << fNhits;
150 
151  std::vector<std::string> keys;
152  {//save matrices
153  keys = getMatrixKeys();
154  R__b << (unsigned int)(keys.size());
155  for(unsigned int i=0;i<keys.size();++i){
156  TString s(keys.at(i));
157  s.Streamer(R__b);
158  for(int j=0;j<fNhits;++j){
159  ((fMatrices[keys.at(i)])[j]).Streamer(R__b);
160  }
161  }
162  }
163  keys.clear();
164  {//save GFDetPlanes
165  keys = getGFDetPlaneKeys();
166  R__b << (unsigned int)(keys.size());
167  for(unsigned int i=0;i<keys.size();++i){
168  TString s(keys.at(i));
169  s.Streamer(R__b);
170  for(int j=0;j<fNhits;++j){
171  ((fPlanes[keys.at(i)])[j]).Streamer(R__b);
172  }
173  }
174  }//done saving GFDetPlanes
175  keys.clear();
176  {//save numbers
177  keys = getNumberKeys();
178  R__b << (unsigned int)(keys.size());
179  for(unsigned int i=0;i<keys.size();++i){
180  TString s(keys.at(i));
181  s.Streamer(R__b);
182  for(int j=0;j<fNhits;++j){
183  ((fNumbers[keys.at(i)])[j]).Streamer(R__b);
184  }
185  }
186  }//done saving numbers
187  {//save failedHits
188  R__b << ((unsigned int) fFailedHits.size());
189  for(unsigned int i=0;i<fFailedHits.size();++i){
190  R__b << fFailedHits.at(i);
191  }
192  }//done saving failed Hits
193  }
194 
195 }
196 
197 void genf::GFBookkeeping::bookMatrices(std::string key){
198  if(fNhits<0){
199  GFException exc("fNhits not defined",__LINE__,__FILE__);
200  throw exc;
201  }
202  if(fMatrices[key] != NULL){
203  std::ostringstream ostr;
204  ostr << "The key " << key
205  << " is already occupied in genf::GFBookkeeping::bookMatrices()";
206  GFException exc(ostr.str(),__LINE__,__FILE__);
207  throw exc;
208  }
209  fMatrices[key] = new TMatrixT<Double_t>[fNhits];
210 }
211 
213  if(fNhits<0){
214  GFException exc("fNhits not defined",__LINE__,__FILE__);
215  throw exc;
216  }
217  if(fPlanes[key] != NULL){
218  std::ostringstream ostr;
219  ostr << "The key " << key
220  << " is already occupied in genf::GFBookkeeping::bookGFDetPlanes()";
221  GFException exc(ostr.str(),__LINE__,__FILE__);
222  throw exc;
223  }
224  fPlanes[key] = new genf::GFDetPlane[fNhits];
225 }
226 
227 //val is default set to 0.
228 void genf::GFBookkeeping::bookNumbers(std::string key,double val){
229  if(fNhits<0){
230  GFException exc("fNhits not defined",__LINE__,__FILE__);
231  throw exc;
232  }
233  if(fPlanes[key] != NULL){
234  std::ostringstream ostr;
235  ostr << "The key " << key
236  << " is already occupied in genf::GFBookkeeping::bookNumbers()";
237  GFException exc(ostr.str(),__LINE__,__FILE__);
238  throw exc;
239  }
240  fNumbers[key] = new TMatrixT<Double_t>[fNhits];
241  for(int i=0;i<fNhits;++i){
242  ((fNumbers[key])[i]).ResizeTo(1,1);
243  ((fNumbers[key])[i])[0][0] = val;
244  }
245 
246 }
247 
248 void genf::GFBookkeeping::setMatrix(std::string key, unsigned int index,
249  const TMatrixT<Double_t>& mat){
250  if(fMatrices[key] == NULL){
251  std::ostringstream ostr;
252  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setMatrix()";
253  GFException exc(ostr.str(),__LINE__,__FILE__);
254  throw exc;
255  }
256  if(index>=(unsigned int)fNhits){
257  std::ostringstream ostr;
258  ostr << "The index " << index
259  << " is out of range in genf::GFBookkeeping::setMatrix()";
260  GFException exc(ostr.str(),__LINE__,__FILE__);
261  throw exc;
262  }
263  (fMatrices[key])[index].ResizeTo(mat);
264  (fMatrices[key])[index] = mat;
265 }
266 void genf::GFBookkeeping::setDetPlane(std::string key, unsigned int index,
267  const genf::GFDetPlane& pl){
268  if(fPlanes[key] == NULL){
269  std::ostringstream ostr;
270  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setGFDetPlane()";
271  GFException exc(ostr.str(),__LINE__,__FILE__);
272  throw exc;
273  }
274  if(index>=(unsigned int)fNhits){
275  std::ostringstream ostr;
276  ostr << "The index " << index
277  << " is out of range in genf::GFBookkeeping::setGFDetPlane()";
278  GFException exc(ostr.str(),__LINE__,__FILE__);
279  throw exc;
280  }
281  (fPlanes[key])[index] = pl;
282 }
283 void genf::GFBookkeeping::setNumber(std::string key, unsigned int index,
284  const double& num){
285  if(fNumbers[key] == NULL){
286  std::ostringstream ostr;
287  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::setNumber()";
288  GFException exc(ostr.str(),__LINE__,__FILE__);
289  throw exc;
290  }
291  if(index>=(unsigned int)fNhits){
292  std::ostringstream ostr;
293  ostr << "The index " << index
294  << " is out of range in genf::GFBookkeeping::setNumber()";
295  GFException exc(ostr.str(),__LINE__,__FILE__);
296  throw exc;
297  }
298  ((fNumbers[key])[index])[0][0] = num;
299 }
300 
301 bool genf::GFBookkeeping::getMatrix(std::string key,
302  unsigned int index,
303  TMatrixT<Double_t>& mat) const {
304  auto iMatrix = fMatrices.find(key);
305  if(iMatrix == fMatrices.end()){
306  std::ostringstream ostr;
307  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getMatrix()";
308  GFException exc(ostr.str(),__LINE__,__FILE__);
309  throw exc;
310  }
311  if(index>=(unsigned int)fNhits){
312  std::ostringstream ostr;
313  ostr << "The index " << index
314  << " is out of range in genf::GFBookkeeping::getMatrix()";
315  GFException exc(ostr.str(),__LINE__,__FILE__);
316  throw exc;
317  }
318  mat.ResizeTo(iMatrix->second[index]);
319  mat = iMatrix->second[index];
320  return true;
321 }
322 bool genf::GFBookkeeping::getDetPlane(std::string key,
323  unsigned int index,
324  genf::GFDetPlane& pl) const {
325 
326  auto iPlane = fPlanes.find(key);
327  if(iPlane == fPlanes.end()){
328  std::ostringstream ostr;
329  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getGFDetPlane()";
330  GFException exc(ostr.str(),__LINE__,__FILE__);
331  throw exc;
332  }
333  if(index>=(unsigned int)fNhits){
334  std::ostringstream ostr;
335  ostr << "The index " << index
336  << " is out of range in genf::GFBookkeeping::getGFDetPlane()";
337  GFException exc(ostr.str(),__LINE__,__FILE__);
338  throw exc;
339  }
340  pl = iPlane->second[index];
341  return true;
342 }
343 bool genf::GFBookkeeping::getNumber(std::string key,
344  unsigned int index,
345  double& num) const {
346  auto iNumber = fNumbers.find(key);
347  if(iNumber == fNumbers.end()){
348  std::ostringstream ostr;
349  ostr << "The key " << key << " is unknown in genf::GFBookkeeping::getNumber()";
350  GFException exc(ostr.str(),__LINE__,__FILE__);
351  throw exc;
352  }
353  if(index>=(unsigned int)fNhits){
354  std::ostringstream ostr;
355  ostr << "The index " << index
356  << " is out of range in genf::GFBookkeeping::getNumber()";
357  GFException exc(ostr.str(),__LINE__,__FILE__);
358  throw exc;
359  }
360  num = iNumber->second[index][0][0];
361  return true;
362 }
363 
364 void genf::GFBookkeeping::addFailedHit(unsigned int id){
365  fFailedHits.push_back( id );
366 }
367 
369  return fFailedHits.size();
370 }
371 
372 unsigned int genf::GFBookkeeping::hitFailed(unsigned int id){
373  unsigned int retVal = 0;
374  for(unsigned int i=0;i<fFailedHits.size();++i){
375  if(fFailedHits.at(i) == id){
376  ++retVal;
377  }
378  }
379  return retVal;
380 }
381 
383  fFailedHits.clear();
384 }
385 
387  std::vector<std::string> matKeys = getMatrixKeys();
388  std::vector<std::string> planeKeys = getGFDetPlaneKeys();
389  std::vector<std::string> numKeys = getNumberKeys();
390 
391  clearAll();
392  clearFailedHits();
393 
394  for(unsigned int i=0;i<matKeys.size();++i){
395  bookMatrices(matKeys.at(i));
396  }
397  for(unsigned int i=0;i<planeKeys.size();++i){
398  bookGFDetPlanes(planeKeys.at(i));
399  }
400  for(unsigned int i=0;i<numKeys.size();++i){
401  bookNumbers(numKeys.at(i));
402  }
403 
404 }
405 
407  std::map<std::string, TMatrixT<Double_t>* >::iterator itMat;
408  for(itMat=fMatrices.begin();itMat!=fMatrices.end();itMat++){
409  if(itMat->second!=NULL) delete [] itMat->second;
410  }
412  for(itPl=fPlanes.begin();itPl!=fPlanes.end();itPl++){
413  if(itPl->second!=NULL) delete [] itPl->second;
414  }
415  std::map<std::string, TMatrixT<Double_t>* >::iterator itNum;
416  for(itNum=fNumbers.begin();itNum!=fNumbers.end();itNum++){
417  if(itNum->second!=NULL) delete [] itNum->second;
418  }
419  fMatrices.clear();
420  fPlanes.clear();
421  fNumbers.clear();
422 }
423 
424 std::vector< std::string > genf::GFBookkeeping::getMatrixKeys() const {
425  std::vector< std::string > keys;
426  std::map<std::string, TMatrixT<Double_t>* >::const_iterator it;
427  for(it=fMatrices.begin();it!=fMatrices.end();it++){
428  if(it->second!=NULL) keys.push_back(it->first);
429  }
430  return keys;
431 }
432 std::vector< std::string > genf::GFBookkeeping::getGFDetPlaneKeys() const {
433  std::vector< std::string > keys;
435  for(it=fPlanes.begin();it!=fPlanes.end();it++){
436  if(it->second!=NULL) keys.push_back(it->first);
437  }
438  return keys;
439 }
440 std::vector< std::string > genf::GFBookkeeping::getNumberKeys() const {
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 
450 void genf::GFBookkeeping::Print(std::ostream& out /* = std::cout */) const {
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
456  << " entries:" << std::endl;
457  for(int j=0;j<fNhits;++j){
458  TMatrixT<Double_t> m;
459  getMatrix(keys.at(i),j,m);
460  m.Print(); // TODO print the matrix outself
461  }
462  }
463  out << "-----printing all GFDetPlanes:------" << std::endl;
464  keys = getGFDetPlaneKeys();
465  for(unsigned int i=0;i<keys.size();++i){
466  out << "key " << keys.at(i) << " has " << fNhits
467  << " entries:" << std::endl;
468  for(int j=0;j<fNhits;++j){
470  getDetPlane(keys.at(i),j,p);
471  p.Print(out);
472  }
473  }
474  out << "-----printing all numbers:------" << std::endl;
475  keys = getNumberKeys();
476  for(unsigned int i=0;i<keys.size();++i){
477  out << "key " << keys.at(i) << " has " << fNhits
478  << " entries:" << std::endl;
479  for(int j=0;j<fNhits;++j){
480  double n(-1111.);
481  getNumber(keys.at(i),j,n);
482  out << n << std::endl;
483  }
484  }
485  out << "-----failed hits:------" << std::endl;
486  for(unsigned int i=0;i<fFailedHits.size();++i){
487  out << fFailedHits.at(i) << " ";
488  }
489  out << std::endl;
490 }
491 
492 
493 //ClassImp(GFBookkeeping)
void setDetPlane(std::string key, unsigned int index, const GFDetPlane &pl)
Float_t s
Definition: plot.C:23
std::map< std::string, TMatrixT< Double_t > * > fNumbers
Definition: GFBookkeeping.h:44
intermediate_table::iterator iterator
void Print(std::ostream &out=std::cout) const
std::map< std::string, TMatrixT< Double_t > * > fMatrices
Definition: GFBookkeeping.h:39
void setNumber(std::string key, unsigned int index, const double &num)
void Print(std::ostream &out=std::cout) const
Definition: GFDetPlane.cxx:242
void setMatrix(std::string key, unsigned int index, const TMatrixT< Double_t > &mat)
void addFailedHit(unsigned int)
intermediate_table::const_iterator const_iterator
void bookGFDetPlanes(std::string key)
Float_t mat
Definition: plot.C:40
unsigned int hitFailed(unsigned int)
std::vector< unsigned int > fFailedHits
Definition: GFBookkeeping.h:45
bool getNumber(std::string key, unsigned int index, double &num) const
void Streamer(TBuffer &)
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:50
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
std::map< std::string, GFDetPlane * > fPlanes
Definition: GFBookkeeping.h:40
bool getDetPlane(std::string key, unsigned int index, GFDetPlane &pl) const
unsigned int getNumFailed()