00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4FitData.h"
00017
00018 #include "Riostream.h"
00019
00020 #include "TMath.h"
00021 #include "TObjArray.h"
00022 #include "TArrayC.h"
00023 #include "TH1.h"
00024 #include "TH2.h"
00025 #include "TH3.h"
00026 #include "TGraph.h"
00027 #include "TClass.h"
00028
00029 #include "TGo4FitSlot.h"
00030 #include "TGo4FitAxisTrans.h"
00031
00032
00033 TGo4FitData::TGo4FitData() : TGo4FitComponent(),
00034 fiDataType(0), fbUseBinScale(kFALSE), fiTakeSigmasFrom(1), fdSigmaValue(1.), fdExcludeLessThen(0.),
00035 fxAxisTrans() {
00036 ResetAllPoinetrs();
00037 }
00038
00039 TGo4FitData::TGo4FitData(const char* iName, const char* iTitle, Int_t iDataType, Bool_t AddAmpl) :
00040 TGo4FitComponent(iName,iTitle), fiDataType(iDataType),
00041 fbUseBinScale(kFALSE), fiTakeSigmasFrom(1), fdSigmaValue(1.), fdExcludeLessThen(0.),
00042 fxAxisTrans() {
00043
00044 ResetAllPoinetrs();
00045
00046 if(AddAmpl) NewAmplitude("Ampl",1.0,kTRUE);
00047
00048 fxAxisTrans.SetOwner(kTRUE);
00049 }
00050
00051 TGo4FitData::~TGo4FitData() {
00052 ReleaseAllPointers();
00053 }
00054
00055
00056 Bool_t TGo4FitData::SetNumberOfTransSlots(Int_t nslots) {
00057 Int_t oldnum = GetNumberOfTransSlots();
00058 if ( (nslots<0) || (nslots == oldnum) ) return kFALSE;
00059
00060 if (oldnum<nslots)
00061 for(Int_t n=oldnum;n<nslots;n++) {
00062 TString name("Trans");
00063 name+=n;
00064 fxAxisTrans.Add(new TGo4FitSlot(name.Data(),"Axis transformation", this, TGo4FitAxisTrans::Class(), kFALSE));
00065 }
00066 else
00067 for (Int_t n=oldnum;n>nslots;n--) {
00068 TObject* slot = fxAxisTrans.Last();
00069 fxAxisTrans.Remove(slot);
00070 fxAxisTrans.Compress();
00071 delete slot;
00072 }
00073 SetUpdateSlotList();
00074 return kTRUE;
00075 }
00076
00077 TGo4FitSlot* TGo4FitData::GetAxisTransSlot(Int_t nslot) {
00078 return (nslot>=0) && (nslot<=fxAxisTrans.GetLast()) ? dynamic_cast<TGo4FitSlot*> (fxAxisTrans[nslot]) : 0;
00079 }
00080
00081 TGo4FitAxisTrans* TGo4FitData::GetAxisTrans(Int_t nslot) {
00082 TGo4FitSlot* slot = GetAxisTransSlot(nslot);
00083 return (slot==0) ? 0 : dynamic_cast<TGo4FitAxisTrans*> (slot->GetObject());
00084 }
00085
00086 void TGo4FitData::SetAxisTrans(Int_t nslot, TGo4FitAxisTrans *Trans, Bool_t TransOwned) {
00087 if (nslot<0) return;
00088 if(nslot>=GetNumberOfTransSlots())
00089 SetNumberOfTransSlots(nslot+1);
00090 ((TGo4FitSlot*) (fxAxisTrans[nslot]))->SetObject(Trans,TransOwned);
00091 }
00092
00093 void TGo4FitData::AddAxisTrans(TGo4FitAxisTrans* Trans, Bool_t TransOwned) {
00094 Int_t nslot = GetNumberOfTransSlots();
00095 SetNumberOfTransSlots(nslot+1);
00096 ((TGo4FitSlot*) (fxAxisTrans[nslot]))->SetObject(Trans,TransOwned);
00097 }
00098
00099 void TGo4FitData::SetAxisTransNeeded(Int_t nslot, Bool_t iNeeded) {
00100 if( iNeeded && (nslot>=GetNumberOfTransSlots()))
00101 SetNumberOfTransSlots(nslot+1);
00102 if ((nslot>=0) && (nslot<GetNumberOfTransSlots()))
00103 ((TGo4FitSlot*) (fxAxisTrans[nslot]))->SetNeeded(iNeeded);
00104 }
00105
00106 Bool_t TGo4FitData::IsAnyDataTransform() {
00107 if (GetUseBinScale() || (GetExcludeLessThen()>0)) return kTRUE;
00108 for (Int_t n=0;n<GetNumberOfTransSlots();n++)
00109 if (GetAxisTrans(n)) return kTRUE;
00110 return kFALSE;
00111 }
00112
00113 TObject* TGo4FitData::CreateDrawObject(const char* ObjName) {
00114 TGo4FitDataIter* iter = MakeIter();
00115 if (iter==0) return 0;
00116 TObject* obj = iter->CreateDrawObject(ObjName);
00117 delete iter;
00118 return obj;
00119 }
00120
00121 Bool_t TGo4FitData::Initialize(Int_t UseBuffers) {
00122
00123 TGo4FitDataIter* iter = MakeIter();
00124 if (iter==0) return kFALSE;
00125
00126 fiBinsSize = iter->CountPoints(kTRUE);
00127
00128 fiIndexesSize = iter->IndexesSize();
00129 fiScalesSize = iter->ScalesSize();
00130
00131 Bool_t use = ((UseBuffers<0) && GetUseBuffers()) || (UseBuffers>0);
00132
00133 if (use)
00134 for(Int_t n=0; n<GetNumberOfTransSlots();n++) {
00135 TGo4FitAxisTrans* trans = GetAxisTrans(n);
00136 if (trans && !trans->IsAllParsFixed()) {
00137 use = kFALSE;
00138 break;
00139 }
00140 }
00141
00142 if (use) {
00143
00144 fxValues = new Double_t[fiBinsSize];
00145 fxStandDev = new Double_t[fiBinsSize];
00146 fxBinsResult = new Double_t[fiBinsSize];
00147
00148 if (iter->HasIndexes()) fxFullIndex = new Int_t[fiBinsSize*fiIndexesSize];
00149 fxFullScale = new Double_t[fiBinsSize*fiScalesSize];
00150 if (iter->HasWidths()) fxFullWidth = new Double_t [fiBinsSize*fiScalesSize];
00151
00152 Int_t nbin = 0;
00153 if (iter->Reset()) do {
00154
00155 fxValues[nbin] = iter->Value();
00156 fxStandDev[nbin] = iter->StandardDeviation();
00157
00158 if(fxFullIndex)
00159 for(Int_t n=0;n<fiIndexesSize;n++)
00160 fxFullIndex[nbin*fiIndexesSize+n] = iter->Indexes()[n];
00161
00162 if(fxFullScale)
00163 for(Int_t naxis = 0;naxis<fiScalesSize;naxis++)
00164 fxFullScale[nbin*fiScalesSize+naxis] = iter->Scales()[naxis];
00165
00166 if(fxFullWidth && iter->HasWidths())
00167 for(Int_t naxis = 0;naxis<fiScalesSize;naxis++)
00168 fxFullWidth[nbin*fiScalesSize+naxis] = iter->Widths()[naxis];
00169
00170 nbin++;
00171 } while (iter->Next());
00172 }
00173
00174 delete iter;
00175
00176 return kTRUE;
00177 }
00178
00179 void TGo4FitData::Finalize() {
00180 ReleaseAllPointers();
00181 }
00182
00183 void TGo4FitData::ResetAllPoinetrs() {
00184 fiBinsSize = 0;
00185 fiIndexesSize = 0;
00186 fiScalesSize = 0;
00187
00188 fxValues = 0;
00189 fxStandDev = 0;
00190 fxBinsResult = 0;
00191
00192 fxFullScale = 0;
00193 fxFullWidth = 0;
00194
00195 fxFullIndex = 0;
00196 }
00197
00198 void TGo4FitData::ReleaseAllPointers() {
00199 if (fxValues) delete[] fxValues;
00200
00201 if (fxStandDev) delete[] fxStandDev;
00202
00203 if (fxBinsResult) delete[] fxBinsResult;
00204
00205 if (fxFullIndex) delete[] fxFullIndex;
00206
00207 if (fxFullScale) delete[] fxFullScale;
00208
00209 if (fxFullWidth) delete[] fxFullWidth;
00210
00211 ResetAllPoinetrs();
00212 }
00213
00214 Bool_t TGo4FitData::DefineScaleMinMax(Int_t naxis, Double_t& min, Double_t& max) {
00215 TGo4FitDataIter* iter = MakeIter();
00216 if (iter==0) return kFALSE;
00217 Bool_t res = kFALSE;
00218 if (iter->Reset(kFALSE) && (iter->ScalesSize()<=naxis)) {
00219 min = iter->Scales()[naxis]; max = min;
00220 do {
00221 Double_t value = iter->Scales()[naxis];
00222 if (value<min) min = value; else
00223 if (value>max) max = value;
00224 } while (iter->Next(kFALSE));
00225 res = kTRUE;
00226 }
00227
00228 delete iter;
00229 return res;
00230 }
00231
00232 Int_t TGo4FitData::DefineDimensions() {
00233 TGo4FitDataIter* iter = MakeIter();
00234 if (iter==0) return 0;
00235 Int_t res = 0;
00236 if (iter->Reset(kFALSE)) res = iter->IndexesSize();
00237 delete iter;
00238 return res;
00239 }
00240
00241 Int_t TGo4FitData::DefineBinsSize() {
00242 TGo4FitDataIter* iter = MakeIter();
00243 if (iter==0) return 0;
00244
00245 Int_t res = iter->CountPoints(kTRUE);
00246 delete iter;
00247
00248 return res;
00249 }
00250
00251 const Double_t* TGo4FitData::GetScaleValues(const Int_t nbin) {
00252 if(fxFullScale) return &(fxFullScale[nbin*GetScalesSize()]);
00253 else return 0;
00254 }
00255
00256 const Double_t* TGo4FitData::GetWidthValues(const Int_t nbin) {
00257 if(fxFullWidth) return &(fxFullWidth[nbin*GetScalesSize()]);
00258 else return 0;
00259 }
00260
00261 const Int_t* TGo4FitData::GetFullIndex(Int_t nbin) {
00262 if (fxFullIndex) return &(fxFullIndex[nbin*GetIndexesSize()]);
00263 else return 0;
00264 }
00265
00266 Bool_t TGo4FitData::IsCompatibleData(TGo4FitData* data) {
00267 if (data==0) return kFALSE;
00268 TGo4FitDataIter* iter = data->MakeIter();
00269 if (iter==0) return kFALSE;
00270
00271 Bool_t res = kFALSE;
00272 if (iter->Reset(kFALSE)) res = (iter->IndexesSize()==GetIndexesSize()) && (GetIndexesSize()>0);
00273 delete iter;
00274
00275 return res;
00276 }
00277
00278 void TGo4FitData::ApplyRangesForModelMask(TGo4FitComponent* model, Char_t* ModelMask) {
00279
00280 if (ModelMask==0) return;
00281
00282 if (BuffersAllocated())
00283 for(Int_t nbin=0;nbin<GetBinsSize();nbin++) {
00284 const Double_t* values = GetScaleValues(nbin);
00285
00286 Bool_t res = model->CheckRangeConditions(values, GetScalesSize());
00287
00288 ModelMask[nbin] = res ? 1 : 0;
00289 }
00290 else {
00291 TGo4FitDataIter* iter = MakeIter();
00292 Int_t nbin = 0;
00293 if (iter->Reset()) do {
00294 Bool_t res = model->CheckRangeConditions(iter->Scales(), iter->ScalesSize());
00295 ModelMask[nbin] = res ? 1 : 0;
00296 nbin++;
00297 } while (iter->Next());
00298 }
00299 }
00300
00301 void TGo4FitData::FillSlotList(TSeqCollection* list) {
00302 TGo4FitComponent::FillSlotList(list);
00303 for(Int_t n=0;n<=fxAxisTrans.GetLast();n++)
00304 list->Add(fxAxisTrans[n]);
00305 }
00306
00307 void TGo4FitData::Print(Option_t* option) const {
00308 TGo4FitComponent::Print(option);
00309 cout << " Data type: ";
00310 switch(fiDataType) {
00311 case 1: cout << "histogram" << endl; break;
00312 case 2: cout << "graph" << endl; break;
00313 default: cout << fiDataType << endl;
00314 }
00315 cout << " Use bin scale: " << fbUseBinScale << endl;
00316 cout << " Take sigmas from: " ;
00317 switch(GetSigmaSource()) {
00318 case 0: cout << "none" << endl; break;
00319 case 1: cout << "data" << endl; break;
00320 case 2: cout << "const value " << GetSigmaValue() << endl; break;
00321 }
00322 cout << " Exclude bins less then: " << GetExcludeLessThen() << endl;
00323 cout << " Axis transfromation data: " << endl;
00324 fxAxisTrans.Print(option);
00325 }
00326
00327 void TGo4FitData::Streamer(TBuffer& b) {
00328 if (b.IsReading()) {
00329
00330 TGo4FitData::Class()->ReadBuffer(b, this);
00331
00332 for(Int_t n=0;n<=fxAxisTrans.GetLast();n++) {
00333 TGo4FitSlot* dc = (TGo4FitSlot*) fxAxisTrans[n];
00334 dc->SetDefaults(this, TGo4FitAxisTrans::Class());
00335 }
00336
00337 } else {
00338 TGo4FitData::Class()->WriteBuffer(b, this);
00339 }
00340 }
00341
00342
00343
00344
00345 TGo4FitDataIter::TGo4FitDataIter() : TObject(),
00346 fxIndexes(), fxScales(), fxWidths(), fdValue(0.), fdStandardDeviation(1.), fiNumPoint(0), fbReachEnd(kTRUE) {
00347 }
00348
00349 TGo4FitDataIter::~TGo4FitDataIter() {
00350 }
00351
00352 Bool_t TGo4FitDataIter::ReserveArrays(Int_t NumDimen, Int_t NumOwnAxis, Bool_t HasWidth) {
00353 TGo4FitData* data = GetData();
00354 if (data==0) return kFALSE;
00355
00356 fxIndexes.Set(NumDimen); fxIndexes.Reset(0);
00357
00358 Int_t size = 0;
00359 if (data->GetUseBinScale()) size = NumDimen;
00360 else size = NumOwnAxis;
00361
00362 if (size<=0) return kFALSE;
00363
00364 fxScales.Set(size); fxScales.Reset(0.);
00365 if (HasWidth) { fxWidths.Set(size); fxWidths.Reset(1.); }
00366 else fxWidths.Set(0);
00367
00368 return kTRUE;
00369 }
00370
00371 void TGo4FitDataIter::TransformScales(Double_t* scales) {
00372 TGo4FitData* data = GetData();
00373 for(Int_t nslot=0;nslot<data->GetNumberOfTransSlots();nslot++) {
00374 TGo4FitAxisTrans* trans = data->GetAxisTrans(nslot);
00375 if (trans) trans->Transformation(scales, ScalesSize());
00376 }
00377 }
00378
00379 Bool_t TGo4FitDataIter::ProduceScales(const Int_t* index, const Double_t* ownscales, const Double_t* ownwidths) {
00380 TGo4FitData* data = GetData();
00381 if (data==0) return kFALSE;
00382
00383 if ( (data->GetUseBinScale()) || (ownscales==0) ) {
00384 if (index==0) return kFALSE;
00385 Double_t add = (data->GetDataType() == TGo4FitData::dtHistogram) ? .5 : 0.;
00386 for(Int_t n=0;n<fxScales.GetSize();n++)
00387 fxScales[n] = index[n] + add;
00388 fxWidths.Reset(1.);
00389 } else {
00390 for(Int_t n=0; n<fxScales.GetSize();n++)
00391 fxScales[n] = ownscales[n];
00392 if (ownwidths!=0)
00393 for(Int_t n=0; n<fxWidths.GetSize();n++)
00394 fxWidths[n] = ownwidths[n];
00395 }
00396
00397 if (data->GetNumberOfTransSlots()>0) {
00398 if (fxWidths.GetSize()==ScalesSize()) {
00399 TArrayD arr1(ScalesSize()), arr2(ScalesSize());
00400 for(Int_t n=0;n<ScalesSize();n++) {
00401 arr1[n] = fxScales[n]-fxWidths[n]/2.;
00402 arr2[n] = fxScales[n]+fxWidths[n]/2.;
00403 }
00404 TransformScales(arr1.GetArray());
00405 TransformScales(arr2.GetArray());
00406 for(Int_t n=0;n<ScalesSize();n++)
00407 fxWidths[n] = TMath::Abs(arr2[n]-arr1[n]);
00408 }
00409
00410 TransformScales(fxScales.GetArray());
00411 }
00412
00413 return kTRUE;
00414 }
00415
00416 Bool_t TGo4FitDataIter::NextIndex(TArrayI& Index, TArrayI& Limits) {
00417 Int_t n=0;
00418 while (n<Index.GetSize()) {
00419 Index[n]++;
00420 if (Index[n]<Limits[n]) return kTRUE;
00421 Index[n] = 0; n++;
00422 }
00423 return kFALSE;
00424 }
00425
00426 Bool_t TGo4FitDataIter::GetDeviation() {
00427 TGo4FitData* data = GetData();
00428 if (data==0) return kFALSE;
00429 if (data->GetSigmaSource()==2) {
00430 fdStandardDeviation = data->GetSigmaValue()*data->GetSigmaValue();
00431 return kTRUE;
00432 } else return kFALSE;
00433 }
00434
00435 Bool_t TGo4FitDataIter::CheckPointForRange() {
00436 TGo4FitData* data = GetData();
00437 if (data==0) return kFALSE;
00438 if (Value()<data->GetExcludeLessThen()) return kFALSE;
00439 return data->CheckRangeConditions(Scales(),ScalesSize());
00440 }
00441
00442 Bool_t TGo4FitDataIter::Reset(Bool_t UseRanges) {
00443 fbReachEnd = kTRUE;
00444
00445 if (!StartReset()) return kFALSE;
00446
00447 fiNumPoint = 0;
00448
00449 if (!ReadCurrentPoint()) return kFALSE;
00450 if (!UseRanges) { fbReachEnd = kFALSE; return kTRUE; }
00451
00452 while (!CheckPointForRange()) {
00453 if (!ShiftToNextPoint()) return kFALSE;
00454 if (!ReadCurrentPoint()) return kFALSE;
00455 }
00456
00457 fbReachEnd = kFALSE;
00458 return kTRUE;
00459 }
00460
00461 Bool_t TGo4FitDataIter::Next(Bool_t UseRanges) {
00462 fiNumPoint++;
00463
00464 if (fbReachEnd || (GetData()==0)) { fbReachEnd = kTRUE; return kFALSE; }
00465
00466 do {
00467 if (!ShiftToNextPoint()) { fbReachEnd = kTRUE; return kFALSE; }
00468
00469 if (!ReadCurrentPoint()) { fbReachEnd = kTRUE; return kFALSE; }
00470
00471 if (!UseRanges) return kTRUE;
00472
00473 } while (!CheckPointForRange());
00474
00475 return kTRUE;
00476 }
00477
00478 Double_t TGo4FitDataIter::xWidths() const
00479 {
00480 double res = 1.;
00481 if(HasWidths())
00482 for(int n=0;n<fxWidths.GetSize();n++)
00483 res=res*fxWidths[n];
00484 return res;
00485 }
00486
00487 Int_t TGo4FitDataIter::CountPoints(Bool_t UseRanges) {
00488 if (!Reset(UseRanges)) return 0;
00489 Int_t cnt=0;
00490 do {
00491 cnt+=1;
00492 } while (Next(UseRanges)) ;
00493 return cnt;
00494 }
00495
00496 Bool_t TGo4FitDataIter::DefineIndexesLimits(TArrayI& Limits) {
00497 if (!Reset(kFALSE)) return kFALSE;
00498 if (IndexesSize()<=0) return kFALSE;
00499 Limits.Set(IndexesSize()); Limits.Reset(0);
00500 do {
00501 for(Int_t n=0;n<IndexesSize();n++)
00502 if (Indexes()[n]>Limits[n]) Limits[n] = Indexes()[n];
00503 } while(Next(kFALSE));
00504 return kTRUE;
00505 }
00506
00507 TH1* TGo4FitDataIter::CreateHistogram(const char* HistoName, Bool_t UseRanges, Bool_t SetBins) {
00508 TArrayI Limits;
00509 if (!DefineIndexesLimits(Limits)) return 0;
00510 if (!HasIndexes() || (IndexesSize()!=ScalesSize()) || !HasWidths()) return 0;
00511
00512 Int_t NumDim = IndexesSize();
00513 if (NumDim>3) NumDim=3;
00514
00515 Double_t* dummy = 0;
00516 TH1* histo = 0;
00517 switch(NumDim) {
00518 case 1: histo = new TH1D(HistoName, "result", Limits[0]+1, dummy); break;
00519 case 2: histo = new TH2D(HistoName, "result", Limits[0]+1, dummy, Limits[1]+1, dummy); break;
00520 case 3: histo = new TH3D(HistoName, "result", Limits[0]+1, dummy, Limits[1]+1, dummy, Limits[2]+1, dummy); break;
00521 default: return 0;
00522 }
00523
00524 histo->SetDirectory(0);
00525
00526 Double_t* Axises[3];
00527 for (Int_t n=0;n<NumDim;n++)
00528 Axises[n] = new Double_t[Limits[n]+2];
00529
00530 Double_t ampl = GetData()->GetAmplValue();
00531
00532 if (Reset(UseRanges)) do {
00533 if (SetBins)
00534 switch (NumDim) {
00535 case 1: histo->SetBinContent(Indexes()[0]+1, ampl*Value()); break;
00536 case 2: histo->SetBinContent(Indexes()[0]+1, Indexes()[1]+1, ampl*Value()); break;
00537 case 3: histo->SetBinContent(Indexes()[0]+1, Indexes()[1]+1, Indexes()[2]+1, ampl*Value()); break;
00538 }
00539 for(Int_t n=0;n<NumDim;n++) {
00540 Int_t indx = Indexes()[n];
00541 Axises[n][indx] = Scales()[n]-Widths()[n]/2.;
00542 Axises[n][indx+1] = Scales()[n]+Widths()[n]/2.;
00543 }
00544 } while(Next(UseRanges));
00545
00546 histo->GetXaxis()->Set(Limits[0]+1,Axises[0]);
00547 if (NumDim>1) histo->GetYaxis()->Set(Limits[1]+1,Axises[1]);
00548 if (NumDim>2) histo->GetZaxis()->Set(Limits[2]+1,Axises[2]);
00549
00550 for (Int_t n=0;n<NumDim;n++)
00551 delete[] Axises[n];
00552
00553 return histo;
00554 }
00555
00556 TGraph* TGo4FitDataIter::CreateGraph(const char* GraphName, Bool_t UseRanges, Bool_t SetBins) {
00557 Int_t NumPoints = CountPoints(UseRanges);
00558 if ((NumPoints<=0) || (ScalesSize()<1)) return 0;
00559
00560 TGraph* gr = new TGraph(NumPoints);
00561 gr->SetName(GraphName);
00562 if (Reset(UseRanges)) do {
00563 (gr->GetX())[Point()] = x();
00564 if (SetBins)
00565 (gr->GetY())[Point()] = GetData()->GetAmplValue() * Value();
00566 } while(Next(UseRanges));
00567
00568 return gr;
00569 }
00570
00571 TObject* TGo4FitDataIter::CreateDrawObject(const char* ObjName) {
00572 if (!Reset(kFALSE)) return 0;
00573 if (HasIndexes() && (IndexesSize()==ScalesSize()) && HasWidths()) return CreateHistogram(ObjName, kFALSE, kTRUE);
00574 else return CreateGraph(ObjName, kFALSE, kTRUE);
00575 }
00576
00577