AliPhysics  96866e8 (96866e8)
AliEMCALConfiguration.cxx
Go to the documentation of this file.
1 /*
2  * AliEMCALConfiguration.cxx
3  *
4  * Created on: 06.11.2014
5  * Author: markusfasel
6  */
7 #include <cstring>
8 #include <sstream>
9 #include <iostream>
10 #include <TList.h>
11 
12 #include "AliJSONReader.h"
13 #include "AliEMCALConfiguration.h"
14 #include "AliJSONData.h"
15 
16 
18  TNamed(name, ""),
19  fParams(NULL)
20 {
21  fParams = new TList;
22  fParams->SetOwner();
23 }
24 
26  if(!fParams) delete fParams;
27 }
28 
29 void AliEMCALConfiguration::AddParam(const char* name,
30  AliJSONValue* value) {
31  AliJSONData *entry = dynamic_cast<AliJSONData *>(fParams->FindObject(name));
32  if(entry) entry->SetValue(value);
33  else fParams->Add(new AliJSONData(name, value));
34 }
35 
36 void AliEMCALConfiguration::Build(const char *jsonstring) {
37  AliJSONReader parser;
38  Build(parser.Decode(jsonstring));
39 }
40 
42  TIter objects(entries);
43  for(TIter entry = objects.Begin(); entry != objects.End(); ++entry){
44  AliJSONData *val = dynamic_cast<AliJSONData *>(*entry);
45  if(val)
46  fParams->Add(val);
47  else{
48  TList *conf = dynamic_cast<TList *>(*entry);
49  if(conf){
50  AliEMCALConfiguration *daughter = new AliEMCALConfiguration(conf->GetName());
51  daughter->Build(conf);
52  fParams->Add(daughter);
53  }
54  }
55  }
56 }
57 
58 void AliEMCALConfiguration::Print(Option_t * /*value*/) const {
59  std::cout << "Configuration " << GetName() << ":" << std::endl;
60  std::cout << "=================================================" << std::endl;
61  TIter parIter(fParams);
62  AliJSONData *conf(NULL);
63  while((conf = dynamic_cast<AliJSONData *>(parIter()))){
64  std::cout << "Key " << conf->GetName() << ", value " << conf->GetValue()->ToString() << std::endl;
65  }
66  std::cout << "=================================================" << std::endl;
67 }
69  fParams->Add(conf);
70 }
71 
73  AliJSONData *val = dynamic_cast<AliJSONData *>(fParams->FindObject(key));
74  if(!val) return NULL;
75  return val->GetValue();
76 }
77 
79  std::stringstream jsonbuilder;
80  jsonbuilder << "{";
81  TIter confentries(fParams);
82  bool isFirst = true;
83  for(TIter it = confentries.Begin(); it != confentries.End(); ++it){
84  AliEMCALConfiguration *conf = dynamic_cast<AliEMCALConfiguration *>(*it);
85  if(conf){
86  if(!isFirst) jsonbuilder << ",";
87  jsonbuilder << "\"" << conf->GetName() << "\":" << conf->CreateJSONString();
88  } else {
89  AliJSONData *obj = dynamic_cast<AliJSONData *>(*it);
90  if(obj){
91  if(!isFirst) jsonbuilder << ",";
92  jsonbuilder << obj->ToString();
93  }
94  }
95  if(isFirst) isFirst = false;
96  }
97  jsonbuilder << "}";
98 
99  char * result = new char[jsonbuilder.str().length()];
100  strcpy(result, jsonbuilder.str().c_str());
101  return result;
102 }
103 
104 std::ostream &operator<<(std::ostream & os, const AliEMCALConfiguration &conf){
105  os << conf.CreateJSONString();
106  return os;
107 }
std::string CreateJSONString() const
void Print(Option_t *) const
void AddConfiguration(AliEMCALConfiguration *conf)
void SetValue(AliJSONValue *val)
Definition: AliJSONData.h:140
AliJSONValue * GetValue() const
Definition: AliJSONData.h:145
AliEMCALConfiguration(const char *name)
AliJSONValue * GetValue(const char *key) const
std::string ToString() const
Definition: AliJSONData.cxx:56
void Build(const char *jsonstring)
std::ostream & operator<<(std::ostream &os, const AliEMCALConfiguration &conf)
virtual std::string ToString() const =0
const char Option_t
Definition: External.C:48
TList * Decode(const char *jsosnstring) const
void AddParam(const char *name, AliJSONValue *value)