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