GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TCalManager.cxx
Go to the documentation of this file.
1#include "TCalManager.h"
2
3#include <stdexcept>
4
5TCalManager::TCalManager(const char* classname)
6{
7 SetClass(classname);
8}
9
11{
12 for(auto& iter : fCalMap) {
13 delete iter.second;
14 }
15}
16
17void TCalManager::RemoveCal(UInt_t channum, Option_t*)
18{
19 if(fCalMap.count(channum) == 1) { // if this cal exists
20 TCal* cal = GetCal(channum);
21 delete cal;
22 fCalMap.erase(channum);
23 }
24}
25
26void TCalManager::SetClass(const char* className)
27{
28 /// Sets the Derived class of the TCal being held in the TCalManager
29 SetClass(TClass::GetClass(className));
30}
31
32void TCalManager::SetClass(TClass* cls)
33{
34 /// Sets the Derived class of the TCal being held in the TCalManager
35 if(fClass != nullptr) {
36 std::cout << "TCalManager type already set to " << fClass->ClassName() << std::endl;
37 return;
38 }
39
40 fClass = cls;
41 if(fClass == nullptr) {
42 MakeZombie();
43 Error("SetClass", "called with a null pointer");
44 return;
45 }
46 const char* className = fClass->GetName();
47 if(!fClass->InheritsFrom(TCal::Class())) {
48 MakeZombie();
49 Error("SetClass", "%s does not inherit from TCal", className);
50 return;
51 }
52 if(fClass->GetBaseClassOffset(TObject::Class()) != 0) {
53 MakeZombie();
54 Error("SetClass", "%s must inherit from TObject as the left most base class.", className);
55 return;
56 }
57 std::cout << "Changing TCalManager to type: " << className << std::endl;
58 auto nch = strlen(className) + 2;
59 auto* name = new char[nch];
60 snprintf(name, nch, "%ss", className);
61 SetName(name);
62 delete[] name;
63}
64
65TCal* TCalManager::GetCal(UInt_t chanNum)
66{
67 /// Gets the TCal for the channel number chanNum
68 TCal* cal = nullptr;
69 try {
70 cal = fCalMap.at(chanNum);
71 } catch(const std::out_of_range& oor) {
72 Error("GetCal", "Channel %u is empty", chanNum);
73 return nullptr;
74 }
75 return cal;
76}
77
78Bool_t TCalManager::AddToManager(TCal* cal, Option_t* opt)
79{
80 /// Makes a Deep copy of cal and adds it to the CalManager Map.
81
82 // Check to see if the channel number has been set. If not, the user must supply one in the function call.
83 if(cal->GetChannel() != nullptr) {
84 return AddToManager(cal, cal->GetChannel()->GetNumber(), opt);
85 }
86 Error("AddToManager", "Channel has not been set");
87 return false;
88
89 // Might have to do other checks
90}
91
92Bool_t TCalManager::AddToManager(TCal* cal, UInt_t chanNum, Option_t* opt)
93{
94 /// Makes a Deep copy of cal and adds it to the CalManager Map for channel number
95 /// chanNum.
96 if(cal == nullptr) {
97 return false;
98 }
99
100 if(fClass == nullptr) {
101 SetClass(cal->ClassName());
102 } else if(fClass->GetName() != cal->ClassName()) {
103 Error("AddToManager", "Trying to put a %s in a TCalManager of type %s", cal->ClassName(), fClass->GetName());
104 return false;
105 }
106
107 if((cal->GetChannel()) == nullptr) {
108 if(!(cal->SetChannel(chanNum))) {
109 return false; // TCal does the Error for us.
110 }
111 }
112
113 if(fCalMap.count(cal->GetChannel()->GetNumber()) == 1) { // if this cal already exists
114 if(strcmp(opt, "overwrite") == 0) {
115 TCal* oldCal = GetCal(chanNum);
116 // delete the old calibration for this channel number
117 delete oldCal;
118 oldCal = static_cast<TCal*>(cal->Clone(cal->GetName()));
119 fCalMap.at(chanNum) = oldCal;
120 return true;
121 }
122 Error("AddToManager", "Trying to add a channel that already exists!");
123 return false;
124 }
125 TCal* newCal = static_cast<TCal*>(cal->Clone(cal->GetName()));
126 // In order to construct a new derived class you need to know the type at compile time.
127 // Clone lets us get around this. There are other ways to do this using "virtual constructor idioms"
128 // but the basically do what clone does anyway.
129 // Clone uses ROOT streamers. We have made the TChannel part of the TCal a TRef.
130 // This has the effect of making it persistent as far as the ROOT streamer
131 // facility is concerned. All of the other "pointer members" of the TCal
132 // Get Deep copied into the TCal Manager.
133 std::cout << "newCal: " << newCal->GetChannel() << ", cal: " << cal->GetChannel() << std::endl;
134 ;
135 fCalMap.insert(std::make_pair(chanNum, newCal));
136
137 return true;
138}
139
141{
142 /// Writes all of the TCals to TChannel based on the method WriteToChannel
143 /// defined in the TCal held by TCalManager.
144 for(const auto& iter : fCalMap) {
145 if(iter.second != nullptr) {
146 iter.second->WriteToChannel();
147 }
148 }
149}
150
151void TCalManager::Clear(Option_t*)
152{
153 /// This deletes all of the current TCal's. It also resets the class
154 /// type to 0.
155 for(auto& iter : fCalMap) {
156 delete iter.second;
157 }
158 fCalMap.clear();
159 fClass = nullptr;
160}
161
162void TCalManager::Print(Option_t*) const
163{
164 if(fClass != nullptr) {
165 std::cout << "Type: " << fClass->GetName() << std::endl;
166 }
167 std::cout << "Size: " << fCalMap.size() << std::endl; // Printing this way due to size_type return
168}
Definition TCal.h:44
Bool_t SetChannel(TChannel *chan)
Definition TCal.cxx:51
TChannel * GetChannel() const
Definition TCal.cxx:121
void WriteToChannel() const
Bool_t AddToManager(TCal *cal, UInt_t chanNum, Option_t *opt="")
void Clear(Option_t *opt="") override
std::map< UInt_t, TCal * > fCalMap
Definition TCalManager.h:37
void SetClass(const char *className)
void Print(Option_t *opt="") const override
TCalManager()=default
void RemoveCal(UInt_t channum, Option_t *opt="")
TCal * GetCal(UInt_t chanNum)
TClass * fClass
Definition TCalManager.h:38
int GetNumber() const
Definition TChannel.h:169