00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4FitData.h"
00015
00016 #include "Riostream.h"
00017 #include "TMath.h"
00018 #include "TObjArray.h"
00019 #include "TArrayC.h"
00020 #include "TH1.h"
00021 #include "TH2.h"
00022 #include "TH3.h"
00023 #include "TGraph.h"
00024 #include "TClass.h"
00025
00026 #include "TGo4FitSlot.h"
00027 #include "TGo4FitAxisTrans.h"
00028
00029
00030 TGo4FitData::TGo4FitData() : TGo4FitComponent(),
00031 fiDataType(0), fbUseBinScale(kFALSE), fiTakeSigmasFrom(1), fdSigmaValue(1.), fdExcludeLessThen(0.),
00032 fxAxisTrans() {
00033 ResetAllPoinetrs();
00034 }
00035
00036 TGo4FitData::TGo4FitData(const char* iName, const char* iTitle, Int_t iDataType, Bool_t AddAmpl) :
00037 TGo4FitComponent(iName,iTitle), fiDataType(iDataType),
00038 fbUseBinScale(kFALSE), fiTakeSigmasFrom(1), fdSigmaValue(1.), fdExcludeLessThen(0.),
00039 fxAxisTrans() {
00040
00041 ResetAllPoinetrs();
00042
00043 if(AddAmpl) NewAmplitude("Ampl",1.0,kTRUE);
00044
00045 fxAxisTrans.SetOwner(kTRUE);
00046 }
00047
00048 TGo4FitData::~TGo4FitData() {
00049 ReleaseAllPointers();
00050 }
00051
00052
00053 Bool_t TGo4FitData::SetNumberOfTransSlots(Int_t nslots) {
00054 Int_t oldnum = GetNumberOfTransSlots();
00055 if ( (nslots<0) || (nslots == oldnum) ) return kFALSE;
00056
00057 if (oldnum<nslots)
00058 for(Int_t n=oldnum;n<nslots;n++) {
00059 TString name("Trans");
00060 name+=n;
00061 fxAxisTrans.Add(new TGo4FitSlot(name.Data(),"Axis transformation", this, TGo4FitAxisTrans::Class(), kFALSE));
00062 }
00063 else
00064 for (Int_t n=oldnum;n>nslots;n--) {
00065 TObject* slot = fxAxisTrans.Last();
00066 fxAxisTrans.Remove(slot);
00067 fxAxisTrans.Compress();
00068 delete slot;
00069 }
00070 SetUpdateSlotList();
00071 return kTRUE;
00072 }
00073
00074 TGo4FitSlot* TGo4FitData::GetAxisTransSlot(Int_t nslot)
00075 {
00076 return (nslot>=0) && (nslot<=fxAxisTrans.GetLast()) ? dynamic_cast<TGo4FitSlot*> (fxAxisTrans[nslot]) : 0;
00077 }
00078
00079 TGo4FitAxisTrans* TGo4FitData::GetAxisTrans(Int_t nslot)
00080 {
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 {
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 {
00243 TGo4FitDataIter* iter = MakeIter();
00244 if (iter==0) return 0;
00245
00246 Int_t res = iter->CountPoints(kTRUE);
00247 delete iter;
00248
00249 return res;
00250 }
00251
00252 const Double_t* TGo4FitData::GetScaleValues(const Int_t nbin)
00253 {
00254 if(fxFullScale) return &(fxFullScale[nbin*GetScalesSize()]);
00255 else return 0;
00256 }
00257
00258 const Double_t* TGo4FitData::GetWidthValues(const Int_t nbin)
00259 {
00260 if(fxFullWidth) return &(fxFullWidth[nbin*GetScalesSize()]);
00261 else return 0;
00262 }
00263
00264 const Int_t* TGo4FitData::GetFullIndex(Int_t nbin)
00265 {
00266 if (fxFullIndex) return &(fxFullIndex[nbin*GetIndexesSize()]);
00267 else return 0;
00268 }
00269
00270 Bool_t TGo4FitData::IsCompatibleData(TGo4FitData* data)
00271 {
00272 if (data==0) return kFALSE;
00273 TGo4FitDataIter* iter = data->MakeIter();
00274 if (iter==0) return kFALSE;
00275
00276 Bool_t res = kFALSE;
00277 if (iter->Reset(kFALSE)) res = (iter->IndexesSize()==GetIndexesSize()) && (GetIndexesSize()>0);
00278 delete iter;
00279
00280 return res;
00281 }
00282
00283 void TGo4FitData::ApplyRangesForModelMask(TGo4FitComponent* model, Char_t* ModelMask)
00284 {
00285
00286 if (ModelMask==0) return;
00287
00288 if (BuffersAllocated())
00289 for(Int_t nbin=0;nbin<GetBinsSize();nbin++) {
00290 const Double_t* values = GetScaleValues(nbin);
00291
00292 Bool_t res = model->CheckRangeConditions(values, GetScalesSize());
00293
00294 ModelMask[nbin] = res ? 1 : 0;
00295 }
00296 else {
00297 TGo4FitDataIter* iter = MakeIter();
00298 Int_t nbin = 0;
00299 if (iter->Reset()) do {
00300 Bool_t res = model->CheckRangeConditions(iter->Scales(), iter->ScalesSize());
00301 ModelMask[nbin] = res ? 1 : 0;
00302 nbin++;
00303 } while (iter->Next());
00304 }
00305 }
00306
00307 void TGo4FitData::FillSlotList(TSeqCollection* list)
00308 {
00309 TGo4FitComponent::FillSlotList(list);
00310 for(Int_t n=0;n<=fxAxisTrans.GetLast();n++)
00311 list->Add(fxAxisTrans[n]);
00312 }
00313
00314 void TGo4FitData::Print(Option_t* option) const
00315 {
00316 TGo4FitComponent::Print(option);
00317 std::cout << " Data type: ";
00318 switch(fiDataType) {
00319 case 1: std::cout << "histogram" << std::endl; break;
00320 case 2: std::cout << "graph" << std::endl; break;
00321 default: std::cout << fiDataType << std::endl;
00322 }
00323 std::cout << " Use bin scale: " << fbUseBinScale << std::endl;
00324 std::cout << " Take sigmas from: " ;
00325 switch(GetSigmaSource()) {
00326 case 0: std::cout << "none" << std::endl; break;
00327 case 1: std::cout << "data" << std::endl; break;
00328 case 2: std::cout << "const value " << GetSigmaValue() << std::endl; break;
00329 }
00330 std::cout << " Exclude bins less then: " << GetExcludeLessThen() << std::endl;
00331 std::cout << " Axis transformation data: " << std::endl;
00332 fxAxisTrans.Print(option);
00333 }
00334
00335 void TGo4FitData::Streamer(TBuffer& b)
00336 {
00337 if (b.IsReading()) {
00338
00339 TGo4FitData::Class()->ReadBuffer(b, this);
00340
00341 for(Int_t n=0;n<=fxAxisTrans.GetLast();n++) {
00342 TGo4FitSlot* dc = (TGo4FitSlot*) fxAxisTrans[n];
00343 dc->SetDefaults(this, TGo4FitAxisTrans::Class());
00344 }
00345
00346 } else {
00347 TGo4FitData::Class()->WriteBuffer(b, this);
00348 }
00349 }
00350
00351
00352
00353
00354 TGo4FitDataIter::TGo4FitDataIter() : TObject(),
00355 fxIndexes(), fxScales(), fxWidths(), fdValue(0.), fdStandardDeviation(1.), fiNumPoint(0), fbReachEnd(kTRUE) {
00356 }
00357
00358 TGo4FitDataIter::~TGo4FitDataIter() {
00359 }
00360
00361 Bool_t TGo4FitDataIter::ReserveArrays(Int_t NumDimen, Int_t NumOwnAxis, Bool_t HasWidth) {
00362 TGo4FitData* data = GetData();
00363 if (data==0) return kFALSE;
00364
00365 fxIndexes.Set(NumDimen); fxIndexes.Reset(0);
00366
00367 Int_t size = 0;
00368 if (data->GetUseBinScale()) size = NumDimen;
00369 else size = NumOwnAxis;
00370
00371 if (size<=0) return kFALSE;
00372
00373 fxScales.Set(size); fxScales.Reset(0.);
00374 if (HasWidth) { fxWidths.Set(size); fxWidths.Reset(1.); }
00375 else fxWidths.Set(0);
00376
00377 return kTRUE;
00378 }
00379
00380 void TGo4FitDataIter::TransformScales(Double_t* scales)
00381 {
00382 TGo4FitData* data = GetData();
00383 for(Int_t nslot=0;nslot<data->GetNumberOfTransSlots();nslot++) {
00384 TGo4FitAxisTrans* trans = data->GetAxisTrans(nslot);
00385 if (trans) trans->Transformation(scales, ScalesSize());
00386 }
00387 }
00388
00389 Bool_t TGo4FitDataIter::ProduceScales(const Int_t* index, const Double_t* ownscales, const Double_t* ownwidths)
00390 {
00391 TGo4FitData* data = GetData();
00392 if (data==0) return kFALSE;
00393
00394 if ( (data->GetUseBinScale()) || (ownscales==0) ) {
00395 if (index==0) return kFALSE;
00396 Double_t add = (data->GetDataType() == TGo4FitData::dtHistogram) ? .5 : 0.;
00397 for(Int_t n=0;n<fxScales.GetSize();n++)
00398 fxScales[n] = index[n] + add;
00399 fxWidths.Reset(1.);
00400 } else {
00401 for(Int_t n=0; n<fxScales.GetSize();n++)
00402 fxScales[n] = ownscales[n];
00403 if (ownwidths!=0)
00404 for(Int_t n=0; n<fxWidths.GetSize();n++)
00405 fxWidths[n] = ownwidths[n];
00406 }
00407
00408 if (data->GetNumberOfTransSlots()>0) {
00409 if (fxWidths.GetSize()==ScalesSize()) {
00410 TArrayD arr1(ScalesSize()), arr2(ScalesSize());
00411 for(Int_t n=0;n<ScalesSize();n++) {
00412 arr1[n] = fxScales[n]-fxWidths[n]/2.;
00413 arr2[n] = fxScales[n]+fxWidths[n]/2.;
00414 }
00415 TransformScales(arr1.GetArray());
00416 TransformScales(arr2.GetArray());
00417 for(Int_t n=0;n<ScalesSize();n++)
00418 fxWidths[n] = TMath::Abs(arr2[n]-arr1[n]);
00419 }
00420
00421 TransformScales(fxScales.GetArray());
00422 }
00423
00424 return kTRUE;
00425 }
00426
00427 Bool_t TGo4FitDataIter::NextIndex(TArrayI& Index, TArrayI& Limits) {
00428 Int_t n=0;
00429 while (n<Index.GetSize()) {
00430 Index[n]++;
00431 if (Index[n]<Limits[n]) return kTRUE;
00432 Index[n] = 0; n++;
00433 }
00434 return kFALSE;
00435 }
00436
00437 Bool_t TGo4FitDataIter::GetDeviation() {
00438 TGo4FitData* data = GetData();
00439 if (data==0) return kFALSE;
00440 if (data->GetSigmaSource()==2) {
00441 fdStandardDeviation = data->GetSigmaValue()*data->GetSigmaValue();
00442 return kTRUE;
00443 } else return kFALSE;
00444 }
00445
00446 Bool_t TGo4FitDataIter::CheckPointForRange() {
00447 TGo4FitData* data = GetData();
00448 if (data==0) return kFALSE;
00449 if (Value()<data->GetExcludeLessThen()) return kFALSE;
00450 return data->CheckRangeConditions(Scales(),ScalesSize());
00451 }
00452
00453 Bool_t TGo4FitDataIter::Reset(Bool_t UseRanges) {
00454 fbReachEnd = kTRUE;
00455
00456 if (!StartReset()) return kFALSE;
00457
00458 fiNumPoint = 0;
00459
00460 if (!ReadCurrentPoint()) return kFALSE;
00461 if (!UseRanges) { fbReachEnd = kFALSE; return kTRUE; }
00462
00463 while (!CheckPointForRange()) {
00464 if (!ShiftToNextPoint()) return kFALSE;
00465 if (!ReadCurrentPoint()) return kFALSE;
00466 }
00467
00468 fbReachEnd = kFALSE;
00469 return kTRUE;
00470 }
00471
00472 Bool_t TGo4FitDataIter::Next(Bool_t UseRanges) {
00473 fiNumPoint++;
00474
00475 if (fbReachEnd || (GetData()==0)) { fbReachEnd = kTRUE; return kFALSE; }
00476
00477 do {
00478 if (!ShiftToNextPoint()) { fbReachEnd = kTRUE; return kFALSE; }
00479
00480 if (!ReadCurrentPoint()) { fbReachEnd = kTRUE; return kFALSE; }
00481
00482 if (!UseRanges) return kTRUE;
00483
00484 } while (!CheckPointForRange());
00485
00486 return kTRUE;
00487 }
00488
00489 Double_t TGo4FitDataIter::xWidths() const
00490 {
00491 double res = 1.;
00492 if(HasWidths())
00493 for(int n=0;n<fxWidths.GetSize();n++)
00494 res=res*fxWidths[n];
00495 return res;
00496 }
00497
00498 Int_t TGo4FitDataIter::CountPoints(Bool_t UseRanges) {
00499 if (!Reset(UseRanges)) return 0;
00500 Int_t cnt=0;
00501 do {
00502 cnt+=1;
00503 } while (Next(UseRanges)) ;
00504 return cnt;
00505 }
00506
00507 Bool_t TGo4FitDataIter::DefineIndexesLimits(TArrayI& Limits) {
00508 if (!Reset(kFALSE)) return kFALSE;
00509 if (IndexesSize()<=0) return kFALSE;
00510 Limits.Set(IndexesSize()); Limits.Reset(0);
00511 do {
00512 for(Int_t n=0;n<IndexesSize();n++)
00513 if (Indexes()[n]>Limits[n]) Limits[n] = Indexes()[n];
00514 } while(Next(kFALSE));
00515 return kTRUE;
00516 }
00517
00518 TH1* TGo4FitDataIter::CreateHistogram(const char* HistoName, Bool_t UseRanges, Bool_t SetBins) {
00519 TArrayI Limits;
00520 if (!DefineIndexesLimits(Limits)) return 0;
00521 if (!HasIndexes() || (IndexesSize()!=ScalesSize()) || !HasWidths()) return 0;
00522
00523 Int_t NumDim = IndexesSize();
00524 if (NumDim>3) NumDim=3;
00525
00526 Double_t* dummy = 0;
00527 TH1* histo = 0;
00528 switch(NumDim) {
00529 case 1: histo = new TH1D(HistoName, "result", Limits[0]+1, dummy); break;
00530 case 2: histo = new TH2D(HistoName, "result", Limits[0]+1, dummy, Limits[1]+1, dummy); break;
00531 case 3: histo = new TH3D(HistoName, "result", Limits[0]+1, dummy, Limits[1]+1, dummy, Limits[2]+1, dummy); break;
00532 default: return 0;
00533 }
00534
00535 histo->SetDirectory(0);
00536
00537 Double_t* Axises[3];
00538 for (Int_t n=0;n<NumDim;n++)
00539 Axises[n] = new Double_t[Limits[n]+2];
00540
00541 Double_t ampl = GetData()->GetAmplValue();
00542
00543 if (Reset(UseRanges)) do {
00544 if (SetBins)
00545 switch (NumDim) {
00546 case 1: histo->SetBinContent(Indexes()[0]+1, ampl*Value()); break;
00547 case 2: histo->SetBinContent(Indexes()[0]+1, Indexes()[1]+1, ampl*Value()); break;
00548 case 3: histo->SetBinContent(Indexes()[0]+1, Indexes()[1]+1, Indexes()[2]+1, ampl*Value()); break;
00549 }
00550 for(Int_t n=0;n<NumDim;n++) {
00551 Int_t indx = Indexes()[n];
00552 Axises[n][indx] = Scales()[n]-Widths()[n]/2.;
00553 Axises[n][indx+1] = Scales()[n]+Widths()[n]/2.;
00554 }
00555 } while(Next(UseRanges));
00556
00557 histo->GetXaxis()->Set(Limits[0]+1,Axises[0]);
00558 if (NumDim>1) histo->GetYaxis()->Set(Limits[1]+1,Axises[1]);
00559 if (NumDim>2) histo->GetZaxis()->Set(Limits[2]+1,Axises[2]);
00560
00561 for (Int_t n=0;n<NumDim;n++)
00562 delete[] Axises[n];
00563
00564 return histo;
00565 }
00566
00567 TGraph* TGo4FitDataIter::CreateGraph(const char* GraphName, Bool_t UseRanges, Bool_t SetBins) {
00568 Int_t NumPoints = CountPoints(UseRanges);
00569 if ((NumPoints<=0) || (ScalesSize()<1)) return 0;
00570
00571 TGraph* gr = new TGraph(NumPoints);
00572 gr->SetName(GraphName);
00573 if (Reset(UseRanges)) do {
00574 (gr->GetX())[Point()] = x();
00575 if (SetBins)
00576 (gr->GetY())[Point()] = GetData()->GetAmplValue() * Value();
00577 } while(Next(UseRanges));
00578
00579 return gr;
00580 }
00581
00582 TObject* TGo4FitDataIter::CreateDrawObject(const char* ObjName) {
00583 if (!Reset(kFALSE)) return 0;
00584 if (HasIndexes() && (IndexesSize()==ScalesSize()) && HasWidths()) return CreateHistogram(ObjName, kFALSE, kTRUE);
00585 else return CreateGraph(ObjName, kFALSE, kTRUE);
00586 }