00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4Parameter.h"
00015
00016 #include <string.h>
00017 #include <stdlib.h>
00018
00019 #include "Riostream.h"
00020
00021 #include "TList.h"
00022 #include "TObjArray.h"
00023 #include "TClass.h"
00024 #include "TROOT.h"
00025 #include "TDataMember.h"
00026 #include "TDataType.h"
00027 #include "TBaseClass.h"
00028 #include "TDatime.h"
00029
00030 #include "TGo4Log.h"
00031 #include "TGo4ParameterMember.h"
00032
00033 TGo4Parameter::TGo4Parameter() :
00034 TNamed()
00035 {
00036 }
00037
00038 TGo4Parameter::TGo4Parameter(const char* name, const char* title) :
00039 TNamed(name ? name : "Parameter", title)
00040 {
00041 TRACE((12,"TGo4Parameter ::TGo4Parameter (const char*, const char*)",__LINE__, __FILE__));
00042
00043 }
00044
00045 void TGo4Parameter::Print(Option_t* dummy) const
00046 {
00047
00048 TGo4Parameter* const localthis= const_cast<TGo4Parameter* const>(this);
00049 localthis->PrintParameter();
00050 }
00051
00052 Int_t TGo4Parameter::PrintParameter(Text_t* buffer, Int_t buflen)
00053 {
00054 TRACE((12,"TGo4Parameter ::PrintParameter()",__LINE__, __FILE__));
00055
00056 if(buflen<=0 && buffer!=0) return 0;
00057
00058 TObjArray items;
00059 GetMemberValues(&items);
00060
00061 Int_t size = 0;
00062 TString localbuf = TString::Format("Parameter name: %s class %s\n", GetName(), ClassName());
00063
00064 if(buffer==0) {
00065 cout << localbuf;
00066 } else {
00067 size = localbuf.Length();
00068 if(size>buflen) size = buflen;
00069 strncpy(buffer, localbuf.Data(), size);
00070 buflen -= size;
00071 buffer += size;
00072 }
00073
00074 TIter iter(&items);
00075 TGo4ParameterMember* info = 0;
00076
00077 TROOT::IncreaseDirLevel();
00078
00079 while ((info = (TGo4ParameterMember*) iter()) !=0 ) {
00080 Int_t size1 = info->PrintMember(buffer, buflen);
00081
00082 size += size1;
00083 buflen -= size1;
00084 buffer += size1;
00085 }
00086
00087 TROOT::DecreaseDirLevel();
00088
00089 return size;
00090 }
00091
00092 TGo4Parameter::~TGo4Parameter()
00093 {
00094 TRACE((12,"TGo4Parameter ::~TGo4Parameter ()",__LINE__, __FILE__));
00095 }
00096
00097 Bool_t TGo4Parameter::UpdateFrom(TGo4Parameter* rhs)
00098 {
00099 if (rhs==0) return kFALSE;
00100
00101 if (rhs->IsA() != IsA()) {
00102 cout << "GO4> !!! ERROR: Wrong parameter class is used in TGo4Parameter::UpdateFrom() method!!!" << endl;
00103 cout << "GO4> !!! ERROR: One cannot update " << IsA()->GetName() << " class from " << rhs->IsA()->GetName() << endl;
00104 cout << "GO4> !!! ERROR: Implement your custom UpdateFrom() method" << endl;
00105 return kFALSE;
00106 }
00107
00108 TObjArray items;
00109
00110 rhs->GetMemberValues(&items);
00111
00112 return SetMemberValues(&items);
00113 }
00114
00115 void TGo4Parameter::Clear(Option_t* opt)
00116 {
00117
00118 cout << "GO4> !!! Default TGo4Parameter::Clear() method is used." << endl;
00119 cout << "GO4> !!! You probably need to overwrite Clear() method for your class" << IsA()->GetName() << endl;
00120
00121 TObjArray items;
00122
00123 GetMemberValues(&items);
00124
00125 TIter iter(&items);
00126 TGo4ParameterMember* info = 0;
00127
00128 while ((info = (TGo4ParameterMember*) iter()) !=0 ) info->SetToZero();
00129
00130 SetMemberValues(&items);
00131 }
00132
00133 void TGo4Parameter::GetMemberValues(TObjArray* fItems)
00134 {
00135 GetMemberValues(fItems, IsA(), (char*) this, 0);
00136 }
00137
00138 Bool_t TGo4Parameter::SetMemberValues(TObjArray* fItems)
00139 {
00140 if (fItems==0) return kFALSE;
00141
00142 TIterator* iter = fItems->MakeIterator();
00143
00144 Bool_t res = SetMemberValues(iter, IsA(), (char*) this, 0);
00145
00146 delete iter;
00147
00148 return res;
00149 }
00150
00151
00152 void TGo4Parameter::GetMemberValues(TObjArray* fItems, TClass* cl, char* ptr, unsigned long int cloffset)
00153 {
00154 if ((fItems==0) || (cl==0) || (ptr==0)) return;
00155
00156 TIter iter(cl->GetListOfDataMembers());
00157 Int_t lastmemberid = -1;
00158 if (fItems->GetLast()>=0)
00159 lastmemberid = ((TGo4ParameterMember*) fItems->Last())->GetMemberId();
00160
00161 TObject* obj = 0;
00162 while ((obj=iter()) != 0) {
00163 TDataMember* member = dynamic_cast<TDataMember*>(obj);
00164 if (member==0) continue;
00165 const char* memtypename = member->GetFullTypeName();
00166 Int_t memtypeid = 0;
00167
00168
00169 if(strcmp(memtypename,"TClass*")==0) continue;
00170
00171
00172 if (strcmp(memtypename,"TString")==0)
00173 memtypeid = TGo4ParameterMember::kTString_t;
00174 else
00175 if (strcmp(memtypename,"TGo4Fitter*")==0)
00176 memtypeid = TGo4ParameterMember::kTGo4Fitter_t;
00177 else {
00178 if (!member->IsBasic()) continue;
00179 memtypeid = member->GetDataType()->GetType();
00180 }
00181
00182 Int_t unitsize = member->GetUnitSize();
00183 Int_t arraydim = member->GetArrayDim();
00184
00185 Int_t maxindex1 = 1;
00186 Int_t maxindex2 = 1;
00187
00188 switch(arraydim) {
00189 case 1:
00190 maxindex1 = member->GetMaxIndex(0);
00191 break;
00192 case 2:
00193 maxindex1 = member->GetMaxIndex(0);
00194 maxindex2 = member->GetMaxIndex(1);
00195 break;
00196 }
00197
00198 lastmemberid++;
00199
00200 for(Int_t ix1=0;ix1<maxindex1;ix1++)
00201 for(Int_t ix2=0;ix2<maxindex2;ix2++) {
00202 TGo4ParameterMember* info = new TGo4ParameterMember(member->GetName(), member->GetTitle());
00203 fItems->Add(info);
00204
00205 info->SetMemberId(lastmemberid);
00206
00207 info->SetType(memtypename, memtypeid);
00208
00209 info->SetVisible((ix1==0) && (ix2==0));
00210
00211 info->SetArrayIndexes(arraydim, ix1, ix2);
00212
00213 char* addr = ptr + cloffset + member->GetOffset() + (ix1*maxindex2 + ix2) * unitsize;
00214 info->SetValue(addr);
00215 }
00216 }
00217
00218
00219 TIter cliter(cl->GetListOfBases());
00220 while((obj=cliter()) !=0) {
00221 TBaseClass* baseclass = dynamic_cast<TBaseClass*>(obj);
00222 if (baseclass==0) continue;
00223 TClass* bclass = baseclass->GetClassPointer();
00224 if(bclass==0) continue;
00225 if(strcmp(bclass->GetName(), "TGo4Parameter")==0) continue;
00226 if(strcmp(bclass->GetName(), "TNamed")==0) continue;
00227
00228 GetMemberValues(fItems, bclass, ptr, cloffset + baseclass->GetDelta());
00229 }
00230 }
00231
00232 Bool_t TGo4Parameter::SetMemberValues(TIterator* fItems, TClass* cl, char* ptr, unsigned long int cloffset)
00233 {
00234 if ((fItems==0) || (cl==0) || (ptr==0)) return kFALSE;
00235
00236 TIter iter(cl->GetListOfDataMembers());
00237
00238 TObject* obj = 0;
00239 while ((obj=iter()) != 0) {
00240 TDataMember* member = dynamic_cast<TDataMember*>(obj);
00241 if (member==0) continue;
00242 const char* memtypename = member->GetFullTypeName();
00243 Int_t memtypeid = 0;
00244
00245
00246 if(strcmp(memtypename,"TClass*")==0) continue;
00247
00248
00249 if (strcmp(memtypename,"TString")==0)
00250 memtypeid = TGo4ParameterMember::kTString_t;
00251 else
00252 if (strcmp(memtypename,"TGo4Fitter*")==0)
00253 memtypeid = TGo4ParameterMember::kTGo4Fitter_t;
00254 else {
00255 if (!member->IsBasic()) continue;
00256 memtypeid = member->GetDataType()->GetType();
00257 }
00258
00259 Int_t unitsize = member->GetUnitSize();
00260 Int_t arraydim = member->GetArrayDim();
00261
00262 Int_t maxindex1 = 1;
00263 Int_t maxindex2 = 1;
00264
00265 switch(arraydim) {
00266 case 1:
00267 maxindex1 = member->GetMaxIndex(0);
00268 break;
00269 case 2:
00270 maxindex1 = member->GetMaxIndex(0);
00271 maxindex2 = member->GetMaxIndex(1);
00272 break;
00273 }
00274
00275 for(Int_t ix1=0;ix1<maxindex1;ix1++)
00276 for(Int_t ix2=0;ix2<maxindex2;ix2++) {
00277 TGo4ParameterMember* info =
00278 dynamic_cast<TGo4ParameterMember*> (fItems->Next());
00279 if (info==0) return kFALSE;
00280
00281 if (strcmp(info->GetName(), member->GetName())!=0) return kFALSE;
00282 if (strcmp(info->GetTitle(), member->GetTitle())!=0) return kFALSE;
00283
00284 if (strcmp(info->GetTypeName(), memtypename)!=0) return kFALSE;
00285 if (info->GetTypeId() != memtypeid) return kFALSE;
00286
00287 if (!info->CheckArrayIndexes(arraydim, ix1, ix2)) return kFALSE;
00288
00289 char* addr = ptr + cloffset + member->GetOffset() + (ix1*maxindex2 + ix2) * unitsize;
00290 info->GetValue(addr);
00291 }
00292 }
00293
00294
00295 TIter cliter(cl->GetListOfBases());
00296 while((obj=cliter()) !=0) {
00297 TBaseClass* baseclass = dynamic_cast<TBaseClass*>(obj);
00298 if (baseclass==0) continue;
00299 TClass* bclass = baseclass->GetClassPointer();
00300 if(bclass==0) continue;
00301 if(strcmp(bclass->GetName(), "TGo4Parameter")==0) continue;
00302 if(strcmp(bclass->GetName(), "TNamed")==0) continue;
00303
00304 if (!SetMemberValues(fItems, bclass, ptr, cloffset + baseclass->GetDelta())) return kFALSE;
00305 }
00306
00307 return kTRUE;
00308 }
00309
00310 void TGo4Parameter::SavePrimitive(ostream& out, Option_t* opt)
00311 {
00312 static int cnt = 0;
00313 TString varname = TString::Format("param%d", cnt++);
00314 Bool_t savemacro = (opt!=0) && (strstr(opt,"savemacro")!=0);
00315
00316 if (savemacro) {
00317 out << Form(" %s* %s = (%s*) go4->GetObject(\"%s\",\"Go4\");",
00318 ClassName(), varname.Data(), ClassName(), GetName()) << endl << endl;
00319 out << Form(" if (%s==0) {", varname.Data()) << endl;
00320 out << Form(" cout << \"Could not find parameter %s\" << endl;", GetName()) << endl;
00321 out << Form(" return kFALSE;") << endl;
00322 out << Form(" }") << endl << endl;
00323 out << Form(" if (strcmp(%s->ClassName(), \"%s\") != 0) {", varname.Data(), ClassName()) << endl;
00324 out << Form(" cout << \"Parameter %s has wrong class \" << %s->ClassName() << endl;", GetName(), varname.Data()) << endl;
00325 out << Form(" return kFALSE;") << endl;
00326 out << Form(" }") << endl << endl;
00327 out << Form(" cout << \"Set parameter %s as saved at %s\" << endl;", GetName(), TDatime().AsString()) << endl << endl;
00328 } else {
00329 out << Form(" %s* %s = new %s;", ClassName(), varname.Data(), ClassName()) << endl;
00330 out << Form(" %s->SetName(\"%s\");", varname.Data(), GetName()) << endl;
00331 out << Form(" %s->SetTitle(\"%s\");", varname.Data(), GetTitle()) << endl;
00332 }
00333
00334 TObjArray *fitems = new TObjArray();
00335 fitems->SetOwner(kTRUE);
00336 GetMemberValues(fitems);
00337
00338 TIter iter(fitems);
00339 TGo4ParameterMember* info = 0;
00340
00341 while ((info = (TGo4ParameterMember*) iter()) !=0 ) {
00342 if (info->GetTypeId()==TGo4ParameterMember::kTGo4Fitter_t) continue;
00343
00344 TString membername;
00345 info->GetFullName(membername);
00346
00347 if (info->GetTypeId()==TGo4ParameterMember::kTString_t)
00348 out << Form(" %s->%s = \"%s\";", varname.Data(), membername.Data(), info->GetStrValue()) << endl;
00349 else
00350 if (info->GetTypeId()==kBool_t)
00351 out << Form(" %s->%s = %s;", varname.Data(), membername.Data(), (atoi(info->GetStrValue()) ? "kTRUE" : "kFALSE")) << endl;
00352 else
00353 out << Form(" %s->%s = %s;", varname.Data(), membername.Data(), info->GetStrValue()) << endl;
00354 }
00355
00356 delete fitems;
00357 }
00358