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