GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TDetector.cxx
Go to the documentation of this file.
1#include "TDetector.h"
2#include "TClass.h"
3
4TDetector::TDetector(const TDetector& rhs) : TObject(rhs)
5{
6 /// Default Copy constructor.
7 rhs.Copy(*this);
8}
9
10TDetector::TDetector(TDetector&& rhs) noexcept : TObject(rhs)
11{
12 /// Default Move constructor.
13 rhs.Copy(*this);
14}
15
17{
18 /// Default Destructor.
19 for(auto* hit : fHits) {
20 delete hit;
21 }
22}
23
24void TDetector::Copy(TObject& rhs) const
25{
26 TObject::Copy(rhs);
27 // to copy the hits without creating a memory leak we need to check
28 // if the right-hand side has more hits than this
29 // if so, we need to delete the hits pointed to by the right-hand side
30 auto& rhsHits = static_cast<TDetector&>(rhs).fHits;
31 if(rhsHits.size() > fHits.size()) {
32 for(size_t i = fHits.size(); i < rhsHits.size(); ++i) {
33 delete rhsHits[i];
34 }
35 rhsHits.resize(fHits.size());
36 } else if(rhsHits.size() < fHits.size()) {
37 size_t oldSize = rhsHits.size();
38 rhsHits.resize(fHits.size(), nullptr);
39 for(size_t i = oldSize; i < rhsHits.size(); ++i) {
40 // we need to use IsA()->New() to make a new hit of whatever derived type this actually is
41 rhsHits[i] = static_cast<TDetectorHit*>(fHits[i]->IsA()->New());
42 }
43 }
44 // we have now ensured that the size of the two vectors is the same, so we can copy the contents of the hits
45 for(size_t i = 0; i < fHits.size(); ++i) {
46 fHits[i]->Copy(*(rhsHits[i]), true);
47 }
48}
49
50void TDetector::Print(Option_t*) const
51{
52 /// Default print statement for TDetector.
53 Print(std::cout);
54}
55
56void TDetector::Print(std::ostream& out) const
57{
58 /// Print detector to stream out. Iterates over hits and prints them.
59 std::ostringstream str;
60 str << "TDetector " << this << ":" << std::endl;
61 for(auto* hit : fHits) {
62 hit->Print(str);
63 }
64 out << str.str();
65}
66
67void TDetector::Clear(Option_t* opt)
68{
69 if(strcmp(opt, "a") == 0) {
70 for(auto* hit : fHits) {
71 delete hit;
72 }
73 }
74 fHits.clear();
75}
76
78{
79 for(auto* hit : fHits) {
80 hit->ClearTransients();
81 }
82}
83
84TDetectorHit* TDetector::GetHit(const int& index) const
85{
86 try {
87 return fHits.at(index);
88 } catch(const std::out_of_range& oor) {
89 std::cerr << ClassName() << " is out of range: " << oor.what() << std::endl;
90 throw grsi::exit_exception(1);
91 }
92 return nullptr;
93}
std::vector< TDetectorHit * > fHits
Definition TDetector.h:84
void Print(Option_t *opt="") const override
!
Definition TDetector.cxx:50
virtual void ClearTransients()
!
Definition TDetector.cxx:77
void Copy(TObject &) const override
!
Definition TDetector.cxx:24
virtual TDetectorHit * GetHit(const int &index) const
Definition TDetector.cxx:84
void Clear(Option_t *="") override
!
Definition TDetector.cxx:67
TDetector()=default