Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
Package.cxx
1/*
2 * HalPackage.cxx
3 *
4 * Created on: 11-07-2013
5 * Author: Daniel Wielanek
6 */
7
8#include "Package.h"
9
10#include "Cout.h"
11#include "Parameter.h"
12#include "Std.h"
13#include "StdString.h"
14
15#include <TBrowser.h>
16#include <TCollection.h>
17#include <TH1.h>
18#include <TList.h>
19#include <TObjArray.h>
20#include <TPRegexp.h>
21#include <TSeqCollection.h>
22
23namespace Hal {
25 fArrayObjects = new TObjArray(1);
26 fArrayObjects->SetName("ArrayObjects");
27 fArrayObjects->SetOwner(kTRUE);
28 fNo = 0;
29 fNameClass = ("Hal::Package");
30 fComment = " ";
31 fMerged = 1;
32 }
33
34 Package::Package(const TObject* object, Bool_t cut_template) {
35 fArrayObjects = new TObjArray(1);
36 fArrayObjects->SetName("ArrayObjects");
37 fArrayObjects->SetOwner(kTRUE);
38 fNo = 0;
39 fNameClass = (object->ClassName());
40 if (cut_template) {
41 TString temp = fNameClass.String();
42 TPRegexp regs("[A-z,0-9,:]+");
43 TString temp2 = temp(regs);
44 fNameClass.SetString(temp2);
45 }
46 fComment = " ";
47 fMerged = 1;
48 }
49
50 void Package::SetAdditionalCounter(Int_t counter) { fNameClass = (Form("%s_%i", fNameClass.String().Data(), counter)); }
51
52 TObject* Package::GetObject(Int_t i) const {
53 if (i < fNo && i >= 0)
54 return fArrayObjects->At(i);
55 else {
56 Cout::PrintInfo("No object at such index in Hal::Package", Hal::EInfo::kWarning);
57 return NULL;
58 }
59 }
60
61 Int_t Package::GetEntries() const { return fNo; }
62
63 TString Package::GetComment() const { return fComment.GetString(); }
64
65 void Package::SetComment(TString name) { fComment.SetString(name); }
66
67 TObject* Package::GetObjectByName(TString name, Int_t index, Bool_t quiet) const {
68 Int_t counter = 0;
69 for (Int_t i = 0; i < fNo; i++) {
70 TString temp_name = ((fArrayObjects->At(i))->GetName());
71 if (name.EqualTo(temp_name)) {
72 counter++;
73 if (counter > index) return fArrayObjects->At(i);
74 }
75 }
76 if (!quiet)
77 Cout::PrintInfo(Form("object named %s not found in package %s", name.Data(), (fNameClass.GetString()).Data()),
78 Hal::EInfo::kLowWarning);
79 return NULL;
80 }
81
82 void Package::PrintInfo(Int_t counter, Int_t deep) const {
83 if (deep == 0) Cout::Stars(kCyan);
84 Cout::ShiftInStars(deep, Form("Hal::Package Info"), kCyan);
85 Cout::ShiftInStars(deep, Form("ClassName : %s", (fNameClass.GetString()).Data()), kGreen);
86 Cout::ShiftText(deep, Form("Comment : %s", fComment.GetString().Data()), "L");
87 Cout::ShiftInStars(deep, "Main List", -1);
88 Cout::ShiftDatabase(deep, 3, "Index", "ObjectType", "ObjectName");
89 for (Int_t i = 0; i < fNo; i++) {
90 TString index = Hal::Std::RoundToString(i);
91 TString type = (fArrayObjects->At(i))->ClassName();
92 TString name = (fArrayObjects->At(i))->GetName();
93 Cout::ShiftDatabase(deep, 3, index.Data(), type.Data(), name.Data());
94 }
95 // print lists
96 if (counter >= deep) {
97 Cout::ShiftInStars(deep, "List of exotic objects", kGreen);
98 for (int i = 0; i < fNo; i++) {
99 TString index = Hal::Std::RoundToString(i);
100 TString type = (fArrayObjects->At(i))->ClassName();
101 TString name = (fArrayObjects->At(i))->GetName();
102 if (type == "TList") {
103 TList* list = (TList*) fArrayObjects->At(i);
104 // looop over TList
105 Cout::ShiftInStars(deep, Form("TList %s", list->GetName()), kBlue);
106 for (int j = 0; j <= list->LastIndex(); j++) {
107 if (list->At(j)->ClassName() == this->ClassName()) {
108 Cout::ShiftInStars(deep, "Hal::Packages in TList!", kCyan);
109 ((Package*) list->At(j))->PrintInfo(counter, deep + 1);
110 } else {
111 // other object
112 TString Index = Hal::Std::RoundToString(j);
113 TString Type = list->At(j)->ClassName();
114 TString Name = list->At(j)->GetName();
115 Cout::ShiftDatabase(deep, 3, Index.Data(), Type.Data(), Name.Data());
116 }
117 }
118 Cout::ShiftInStars(deep, Form("TList %s End", list->GetName()), kBlue);
119 delete list;
120 } else if (name == this->ClassName()) {
121 Cout::ShiftInStars(deep, Form("Ha::lPackage in Hal::Package! [No : %i]", i), kCyan);
122 ((Package*) fArrayObjects->At(i))->PrintInfo(counter, deep + 1);
123 }
124 }
125 }
126 Cout::ShiftInStars(deep, Form("End of %s package", fNameClass.GetString().Data()), kCyan);
127 }
128
130 Package* pack = new Package(this, kTRUE);
131 pack->AddObject(new ParameterString("ClassName", "Hal::Package"));
132 // pack->AddObject(new
133 // HalParameterString("SubClassName",fNameClass.String()));
134 for (Int_t i = 0; i < fNo; i++) {
135 pack->AddObject(fArrayObjects->At(i));
136 }
137 return pack;
138 }
139
140 Int_t Package::GetMerged() const { return fMerged; }
141
142 void Package::SumHalParameter(TObject* first, const TObject* second) {
143 TString name = first->ClassName();
144 ((Parameter*) first)->Add((Parameter*) second);
145 }
146
147 void Package::SumHistograms(TObject* first, const TObject* second) {
148 TString name = first->ClassName();
149 ((TH1*) first)->Add((TH1*) second);
150 }
151
152 void Package::SumOther(TObject* first, const TObject* second) {
153 Cout::PrintInfo("Not supported adding", Hal::EInfo::kLowWarning);
154 Cout::PrintInfo(Form("For class %s %s", second->ClassName(), first->ClassName()), Hal::EInfo::kLowWarning);
155 }
156
157 void Package::SumPackages(TObject* first, const TObject* second) { ((Package*) first)->Add((Package*) second); }
158
159 void Package::SumList(TObject* first, const TObject* second) {
160 TList* fst = (TList*) first;
161 TList* sec = (TList*) second;
162 for (int i = 0; i < fst->GetEntries(); i++) {
163 MergeObjects(fst->At(i), sec->At(i));
164 }
165 }
166
167 const char* Package::GetName() const { return fNameClass.GetName(); }
168
169 TObject* Package::GetObjectByClassName(TString name, Int_t index) const {
170 Int_t counter = 0;
171 for (Int_t i = 0; i < fNo; i++) {
172 TString namet = (fArrayObjects->At(i))->ClassName();
173 if (name.EqualTo(namet)) {
174 counter++;
175 if (counter > index) return fArrayObjects->At(i);
176 } else {
177 }
178 }
179 Cout::PrintInfo(Form("class %s not found in package %s", name.Data(), (fNameClass.GetString()).Data()), Hal::EInfo::kWarning);
180 return NULL;
181 }
182
183 void Package::SumPack(TObject* first, const TObject* second) { ((Object*) first)->Add((Object*) second); }
184
185 void Package::SumTObjString(TObject* first, const TObject* second) {
186 TString stringA = ((TObjString*) first)->GetString();
187 TString stringB = ((TObjString*) second)->GetString();
188 if (stringA.EqualTo(stringB)) {
189 // do nothing
190 } else {
191 Cout::PrintInfo(Form("TObjString not compatible (%s vs %s", stringA.Data(), stringB.Data()), Hal::EInfo::kLowWarning);
192 }
193 }
194
195 void Package::Browse(TBrowser* b) {
196 if (fArrayObjects) {
197 for (int i = 0; i < fArrayObjects->GetEntries(); i++) {
198 TObject* obj = fArrayObjects->At(i);
199 b->Add(obj, obj->GetName());
200 }
201 }
202 }
203
204 Package::~Package() {
205 fArrayObjects->Delete();
206 delete fArrayObjects;
207 }
208
209 void Package::AddObject(TObject* object) {
210 if (object != NULL) {
211 fArrayObjects->AddLast(object);
212 fNo++;
213 } else {
214 Cout::PrintInfo("Cannot add NULL object to Hal::Package", Hal::EInfo::kError);
215 }
216 }
217
219 if (this->fNameClass.GetString().EqualTo(rhs.fNameClass.GetString())) {
220 Cout::PrintInfo("Trying to merging incompatibile Hal::Packages ! This can result in "
221 "crash !",
222 Hal::EInfo::kLowWarning);
223 }
224 for (Int_t i = 0; i < fNo; i++) {
225 MergeObjects(this->fArrayObjects->At(i), rhs.fArrayObjects->At(i));
226 }
227 this->fNo += rhs.fNo;
228 return *this;
229 }
230
231 void Package::MergeObjects(TObject* first, const TObject* second) {
232 TString name = first->ClassName();
233 TString name2 = second->ClassName();
234 if (name != name2) {
235 Cout::PrintInfo(Form("No compatybile classes %s %s", first->ClassName(), second->ClassName()), Hal::EInfo::kCriticalError);
236 }
237 if (name == "Hal::Package") {
238 SumPackages(first, second);
239 } else if (first->InheritsFrom("TH1")) {
240 SumHistograms(first, second);
241 } else if ((first->InheritsFrom("Hal::Parameter"))) {
242 SumHalParameter(first, second);
243 } else if (name == "TList") {
244 SumList(first, second);
245 } else if (first->InheritsFrom("Hal::Object")) {
246 SumPack(first, second);
247 } else if (name.EqualTo("TObjString")) {
248 SumTObjString(first, second);
249 } else {
250 SumOther(first, second);
251 }
252 }
253
254 void Package::Add(const Package* pack) {
255 if (this->fNameClass.GetString().EqualTo(pack->fNameClass.GetString())) {
256 } else {
257 Cout::PrintInfo("Trying to merging incompatibile Hal::Packages ! This can result in "
258 "crash !",
259 Hal::EInfo::kLowWarning);
260 }
261 if (this->fNo != pack->fNo) {
262 Cout::PrintInfo("Different number of object in Hal::PackageArray this may result in "
263 "crash!",
264 Hal::EInfo::kLowWarning);
265 }
266 for (Int_t i = 0; i < fNo; i++) {
267 TString name = (fArrayObjects->At(i)->ClassName());
268 MergeObjects(this->fArrayObjects->At(i), pack->fArrayObjects->At(i));
269 }
270 this->fMerged += pack->fMerged;
271 }
272
273 void Package::PrintInfo(Int_t max_deep) const { PrintInfo(max_deep - 1, 0); }
274
275 void Package::PrintStruct(Int_t I, Int_t Index) const {
276 TString text = "";
277 for (int i = 0; i < I; i++) {
278 text += "-";
279 }
280 TString final_text = Form("%s %s No [%i]", text.Data(), this->ClassName(), Index);
281 if (Index < 0) Index = 0;
282 Cout::Text(final_text, "L");
283 for (Int_t i = 0; i < fNo; i++) {
284 TString index = Hal::Std::RoundToString(i);
285 TString type = (fArrayObjects->At(i))->ClassName();
286 TString name = (fArrayObjects->At(i))->GetName();
287 if (name == this->ClassName()) { ((Package*) fArrayObjects->At(i))->PrintStruct(I + 1, i); }
288 // HalCout::ShiftDatabase(deep,3,index.Data(),type.Data(),name.Data());
289 }
290 }
291
292 void Package::PrintStruct() const {
293 Cout::Stars();
294 PrintStruct(0, -1);
295 Cout::Stars();
296 }
297
298 void Package::SetName(TString name) { fNameClass.SetString(name.Data()); }
299
300 Bool_t Package::Exist(TString name, Int_t index, Option_t* opt) const {
301 TString option = opt;
302 if (option == " ") { // check by name && index
303 Int_t counter = 0;
304 for (Int_t i = 0; i < fNo; i++) {
305 TString temp_name = (TString)((fArrayObjects->At(i))->GetName());
306 if (name.EqualTo(temp_name)) {
307 counter++;
308 if (counter > index) return kTRUE;
309 }
310 }
311 } else if (option == "index") {
312 if (index < fNo && index >= 0) return kTRUE;
313 } else if (option == "classname") {
314 Int_t counter = 0;
315 for (Int_t i = 0; i < fNo; i++) {
316 TString namet = (fArrayObjects->At(i))->ClassName();
317 if (name.EqualTo(namet)) {
318 counter++;
319 if (counter > index) return kTRUE;
320 }
321 }
322 } else if (option == "name") { // only check by name
323 for (Int_t i = 0; i < fNo; i++) {
324 TString temp_name = (TString)((fArrayObjects->At(i))->GetName());
325 if (name.EqualTo(temp_name)) { return kTRUE; }
326 }
327 }
328 return kFALSE;
329 }
330
331 void Package::RemoveObjectByName(TString name, Int_t index, Bool_t remove) {
332 Int_t counter = 0;
333 TObject* obj = NULL;
334 for (Int_t i = 0; i < fNo; i++) {
335 TString temp_name = ((fArrayObjects->At(i))->GetName());
336 if (name.EqualTo(temp_name)) {
337 counter++;
338 if (counter > index) {
339 obj = fArrayObjects->RemoveAt(i);
340 fNo--;
341 fArrayObjects->Compress();
342 }
343 }
344 }
345 if (obj && remove) { delete obj; }
346 }
347
348 Long64_t Package::Merge(TCollection* collection) {
349 if (collection) {
350 Package* pack = NULL;
351 TIter iterator(collection);
352 while ((pack = (Package*) iterator())) {
353 Add(pack);
354 }
355 }
356 return 1;
357 }
358
359} // namespace Hal
static void Text(TString text, TString option="L", Color_t color=-1)
Definition Cout.cxx:92
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
static void Stars(Color_t color=-1)
Definition Cout.cxx:150
static void ShiftDatabase(Int_t shift, Int_t no...)
Definition Cout.cxx:317
static void ShiftInStars(Int_t shift, TString text, Color_t color=-1)
Definition Cout.cxx:355
static void ShiftText(Int_t shift, TString text, TString option, Color_t color=-1)
Definition Cout.cxx:350
Bool_t Exist(TString name, Int_t index=0, Option_t *opt=" ") const
Definition Package.cxx:300
TObject * GetObject(Int_t i) const
Definition Package.cxx:52
virtual Long64_t Merge(TCollection *collection)
Definition Package.cxx:348
void RemoveObjectByName(TString name, Int_t index=0, Bool_t delete_obj=kTRUE)
Definition Package.cxx:331
void SetAdditionalCounter(Int_t counter)
Definition Package.cxx:50
Int_t GetMerged() const
Definition Package.cxx:140
TString GetComment() const
Definition Package.cxx:63
TObject * GetObjectByName(TString name, Int_t index=0, Bool_t quited=kFALSE) const
Definition Package.cxx:67
void Add(const Package *pack)
Definition Package.cxx:254
Package & operator+(const Package &rhs)
Definition Package.cxx:218
TObject * GetObjectByClassName(TString name, Int_t index=0) const
Definition Package.cxx:169
void PrintStruct() const
Definition Package.cxx:292
Package * Report() const
Definition Package.cxx:129
Int_t GetEntries() const
Definition Package.cxx:61
const char * GetName() const
Definition Package.cxx:167
void SetComment(TString name)
Definition Package.cxx:65
void SetName(TString name)
Definition Package.cxx:298
virtual void Browse(TBrowser *b)
Definition Package.cxx:195
void AddObject(TObject *object)
Definition Package.cxx:209