GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FitComponent.cxx
Go to the documentation of this file.
1 // $Id: TGo4FitComponent.cxx 2769 2020-04-16 14:54:23Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4FitComponent.h"
15 
16 #include <iostream>
17 
18 #include "TCutG.h"
19 
20 #include "TGo4FitParameter.h"
21 
22 const UInt_t kExcludeCut = 0x00100000;
23 
25  fxRangeAxis(), fxRangeValue(), fxCuts(), fiAmplIndex(-1), fbUseBuffers(kFALSE) {
26 }
27 
28 TGo4FitComponent::TGo4FitComponent(const char* iName, const char* iTitle) :
29  TGo4FitParsList(iName,iTitle,kTRUE), TGo4FitSlotList(),
30  fxRangeAxis(), fxRangeValue(), fxCuts(), fiAmplIndex(-1), fbUseBuffers(kFALSE) {
31  fxCuts.SetOwner(kTRUE);
32 }
33 
35 }
36 
37 TGo4FitParameter* TGo4FitComponent::NewParameter(const char* Name, const char* Title, Double_t iValue, Bool_t Fixed, Int_t AtIndx) {
38  TGo4FitParameter* par = new TGo4FitParameter(Name, Title, iValue);
39  if (AtIndx<0) AddPar(par);
40  else InsertPar(par,AtIndx);
41  if(Fixed) par->SetFixed(kTRUE);
42  return par;
43 }
44 
45 TGo4FitParameter* TGo4FitComponent::NewAmplitude(const char* Name, Double_t iValue, Bool_t IsFixed, Int_t AtIndx) {
46  TGo4FitParameter* par = 0;
47  if (Name==0) par = new TGo4FitParameter("Ampl", "Amplitude", iValue);
48  else par = new TGo4FitParameter(Name, "Amplitude", iValue);
49  if (AtIndx<0) { AddPar(par); AtIndx = NumPars()-1; }
50  else InsertPar(par,AtIndx);
51  SetAmplIndex(AtIndx);
52  if(IsFixed) par->SetFixed(kTRUE);
53  return par;
54 }
55 
57 {
58  return fiAmplIndex<0 ? 0 : GetPar(fiAmplIndex);
59 }
60 
62 {
63  return GetAmplPar() ? GetAmplPar()->GetName() : 0;
64 }
65 
67 {
68  return GetAmplPar() ? GetAmplPar()->GetFullName() : 0;
69 }
71 {
72  return GetAmplPar() ? GetAmplPar()->GetValue() : 1.0;
73 }
74 
75 void TGo4FitComponent::SetAmplValue(Double_t iAmpl)
76 {
77  if(GetAmplPar()) Get(fiAmplIndex)->SetValue(iAmpl);
78 }
79 
81 {
82  return GetAmplPar() ? GetAmplPar()->GetError() : 0.0;
83 }
84 
85 void TGo4FitComponent::SetAmplError(Double_t iError)
86 {
87  if(GetAmplPar()) Get(fiAmplIndex)->SetError(iError);
88 }
89 
90 Bool_t TGo4FitComponent::MakeAmpl(Bool_t isFixed) {
91  if (GetAmplPar()!=0) return kFALSE;
92  NewAmplitude();
93  if (isFixed) GetAmplPar()->SetFixed(kTRUE);
94  return kTRUE;
95 }
96 
98  if (GetAmplPar()==0) return kFALSE;
100  SetAmplIndex(-1);
101  return kTRUE;
102 }
103 
104 void TGo4FitComponent::GetRangeCondition(Int_t n, Int_t& typ, Int_t& naxis, Double_t& left, Double_t& right) const {
105  if ((n>=0) && (n<GetNumRangeCondition())) {
106  Int_t naxis1 = fxRangeAxis[n*2];
107  Int_t naxis2 = fxRangeAxis[n*2+1];
108  Double_t value1 = fxRangeValue[n*2];
109  Double_t value2 = fxRangeValue[n*2+1];
110 
111  if ((naxis1<0) && (naxis2>0)) {
112  typ = 0;
113  naxis = -naxis1 - 1;
114  left = value1;
115  right = value2;
116  } else
117  if ((naxis1>0) && (naxis2<0)) {
118  typ = 1;
119  naxis = naxis1 - 1;
120  left = value1;
121  right = value2;
122  } else
123  if (naxis1<0) {
124  typ = 2;
125  naxis = -naxis1 - 1;
126  left = value1;
127  right = 0.;
128  } else
129  if (naxis1>0) {
130  typ = 3;
131  naxis = naxis1 - 1;
132  left = 0.;
133  right = value1;
134  }
135  }
136 }
137 
138 void TGo4FitComponent::SetRangeCondition(Int_t n, Int_t typ, Int_t naxis, Double_t left, Double_t right) {
139  if ((n>=0) && (n<GetNumRangeCondition())) {
140  Int_t naxis1, naxis2;
141  Double_t value1, value2;
142 
143  switch (typ) {
144  case 0: naxis1 = -(naxis+1); value1 = left;
145  naxis2 = +(naxis+1); value2 = right;
146  break;
147  case 1: naxis1 = +(naxis+1); value1 = left;
148  naxis2 = -(naxis+1); value2 = right;
149  break;
150  case 2: naxis1 = -(naxis+1); value1 = left;
151  naxis2 = 0; value2 = 0.;
152  break;
153  case 3: naxis1 = +(naxis+1); value1 = right;
154  naxis2 = 0; value2 = 0.;
155  break;
156  default: return;
157  }
158 
159  fxRangeAxis[n*2] = naxis1;
160  fxRangeAxis[n*2+1] = naxis2;
161  fxRangeValue[n*2] = value1;
162  fxRangeValue[n*2+1] = value2;
163  }
164 }
165 
166 void TGo4FitComponent::AddRangeCondition(Int_t typ, Int_t naxis, Double_t left, Double_t right) {
167  Int_t sz = fxRangeValue.GetSize()+2;
168  fxRangeAxis.Set(sz); fxRangeValue.Set(sz);
169  SetRangeCondition(sz/2-1,typ,naxis,left,right);
170 }
171 
173  if ((n<0) || (n>=GetNumRangeCondition())) return;
174 
175  Int_t indx = n*2;
176  while (indx<fxRangeAxis.GetSize()-2) {
177  fxRangeAxis[indx]=fxRangeAxis[indx+2];
178  fxRangeValue[indx]=fxRangeValue[indx+2];
179  indx+=1;
180  }
181  fxRangeAxis.Set(indx);
182  fxRangeValue.Set(indx);
183 }
184 
185 void TGo4FitComponent::SetRange(Int_t naxis, Double_t min, Double_t max) {
186  AddRangeCondition(0,naxis,min,max);
187 }
188 
189 void TGo4FitComponent::ExcludeRange(Int_t naxis, Double_t min, Double_t max) {
190  AddRangeCondition(1,naxis,min,max);
191 }
192 
193 void TGo4FitComponent::SetRangeMin(Int_t naxis, Double_t value) {
194  AddRangeCondition(2,naxis,value,0.);
195 }
196 
197 void TGo4FitComponent::SetRangeMax(Int_t naxis, Double_t value) {
198  AddRangeCondition(3,naxis,0.,value);
199 }
200 
201 void TGo4FitComponent::ClearRanges(Int_t naxis) {
202  if (naxis<0) { fxRangeAxis.Set(0); fxRangeValue.Set(0); fxCuts.Delete(); } else {
203  Int_t indx = 0, dindx = 0;
204  while (indx<fxRangeAxis.GetSize()) {
205  if ( TMath::Abs(fxRangeAxis[indx]) != (naxis+1) ) {
206  fxRangeAxis[dindx]=fxRangeAxis[indx];
207  fxRangeAxis[dindx+1]=fxRangeAxis[indx+1];
208  fxRangeValue[dindx]=fxRangeValue[indx];
209  fxRangeValue[dindx+1]=fxRangeValue[indx+1];
210  dindx+=2;
211  }
212  indx+=2;
213  }
214  fxRangeAxis.Set(dindx);
215  fxRangeValue.Set(dindx);
216  if ((naxis==0) || (naxis==1)) fxCuts.Delete();
217  }
218 }
219 
221 {
222  return (GetNumRangeCondition()>0) || (GetNumRangeCut()>0);
223 }
224 
225 void TGo4FitComponent::AddRangeCut(TCutG* cut, Bool_t exclude)
226 {
227  cut->SetBit(kExcludeCut, exclude);
228  fxCuts.Add(cut);
229 }
230 
232  return fxCuts.GetLast()+1;
233 }
234 
235 TCutG* TGo4FitComponent::GetRangeCut(Int_t n) const {
236  return (n>=0) && (n<=fxCuts.GetLast()) ? dynamic_cast<TCutG*> (fxCuts.At(n)) : 0;
237 }
238 
240  TCutG* cut = GetRangeCut(n);
241  return cut==0 ? kFALSE : cut->TestBit(kExcludeCut);
242 }
243 
244 void TGo4FitComponent::SetRangeCutExcluding(Int_t n, Bool_t exclude) {
245  TCutG* cut = GetRangeCut(n);
246  if (cut) cut->SetBit(kExcludeCut, exclude);
247 }
248 
250  TCutG* cut = GetRangeCut(n);
251  if (cut) {
252  fxCuts.Remove(cut);
253  delete cut;
254  fxCuts.Compress();
255  }
256 }
257 
258 Bool_t TGo4FitComponent::GetRangeMin(Int_t naxis, Double_t& value) {
259  Bool_t isany = kFALSE;
260  for(Int_t indx=0;indx<fxRangeAxis.GetSize();indx+=2)
261  if ( fxRangeAxis[indx] == -(naxis+1) ) {
262  Double_t zn = fxRangeValue[indx];
263  if( (!isany) || (zn<value) ) value = zn;
264  isany = kTRUE;
265  }
266  if(naxis<2)
267  for(Int_t ncut=0;ncut<GetNumRangeCut();ncut++) {
268  TCutG* cut = GetRangeCut(ncut);
269  if(cut->TestBit(kExcludeCut)) continue;
270  Double_t* arr = naxis == 0 ? cut->GetX() : cut->GetY();
271  Double_t zn = arr[0];
272  for(Int_t i=1;i<cut->GetN();i++)
273  if(arr[i]<zn) zn = arr[i];
274  if( (!isany) || (zn<value) ) value = zn;
275  isany = kTRUE;
276  }
277 
278  return isany;
279 }
280 
281 Bool_t TGo4FitComponent::GetRangeMax(Int_t naxis, Double_t& value) {
282  Bool_t isany = kFALSE;
283  Double_t zn;
284  for(Int_t indx=0;indx<fxRangeAxis.GetSize();indx+=2) {
285  if ( (fxRangeAxis[indx] == -(naxis+1)) && (fxRangeAxis[indx+1] == +(naxis+1))) zn = fxRangeValue[indx+1]; else
286  if ( (fxRangeAxis[indx] == +(naxis+1)) && (fxRangeAxis[indx+1] == 0)) zn = fxRangeValue[indx]; else continue;
287  if( (!isany) || (zn>value) ) value = zn;
288  isany = kTRUE;
289  }
290  if(naxis<2)
291  for(Int_t ncut=0;ncut<GetNumRangeCut();ncut++) {
292  TCutG* cut = GetRangeCut(ncut);
293  if(cut->TestBit(kExcludeCut)) continue;
294  Double_t* arr = naxis == 0 ? cut->GetX() : cut->GetY();
295  zn = arr[0];
296  for(Int_t i=1;i<cut->GetN();i++)
297  if(arr[i]>zn) zn = arr[i];
298  if( (!isany) || (zn>value) ) value = zn;
299  isany = kTRUE;
300  }
301 
302  return isany;
303 }
304 
305 Bool_t TGo4FitComponent::CheckRangeConditions(const Double_t* values, Int_t numaxis) {
306  Int_t condsize = GetNumRangeCondition();
307 
308  if ((condsize==0) && (GetNumRangeCut()==0)) return kTRUE;
309 
310  Bool_t res1 = kTRUE, res2 = kTRUE;
311 
312  Bool_t isanycond = kFALSE;
313 
314  for(Int_t naxis=0;naxis<numaxis;naxis++) {
315  Char_t resaxis1 = kTRUE, resaxis2 = kFALSE;
316  Bool_t isany = kFALSE;
317  for(Int_t ncond=0;ncond<condsize;ncond++) {
318 
319  Int_t typ, axisnumber;
320  Double_t left, right;
321  GetRangeCondition(ncond, typ, axisnumber, left, right);
322 
323  if (axisnumber != naxis) continue;
324 
325  switch(typ) {
326  case 0:
327  isany = kTRUE; // include range
328  if((values[naxis]>=left) && (values[naxis]<=right)) resaxis2 = kTRUE;
329  break;
330  case 1: // exclude range
331  if((values[naxis]>=left) && (values[naxis]<=right)) resaxis1 = kFALSE;
332  break;
333  case 2: if(values[naxis]<left) resaxis1 = kFALSE; break; // set left bound
334  case 3: if(values[naxis]>right) resaxis1 = kFALSE; break; // set right bound
335  }
336  }
337 
338  res1 = res1 && resaxis1;
339  if (!isany) resaxis2 = kTRUE;
340  res2 = res2 && resaxis2;
341  isanycond = isanycond || isany;
342  if (!res1) break;
343  }
344 
345  if ( res1 && !(isanycond && res2) && (numaxis>1) && (GetNumRangeCut()>0)) {
346  res2 = kFALSE;
347  for (Int_t n=0;n<GetNumRangeCut();n++) {
348  TCutG* cut = GetRangeCut(n);
349  if (cut->IsInside(values[0],values[1]))
350  {
351  if (cut->TestBit(kExcludeCut)) { res1 = kFALSE; break; }
352  else { res2 = kTRUE; break; }
353  }
354  }
355  }
356 
357  return res1 && res2;
358 }
359 
362  for(Int_t n=0;n<NumSlots();n++) {
363  TGo4FitSlot* slot = GetSlot(n);
364  if (slot && slot->GetOwned()) {
365  TGo4FitParsList* pars = dynamic_cast<TGo4FitParsList*> (slot->GetObject());
366  if (pars) pars->CollectParsTo(list);
367  }
368  }
369 }
370 
371 void TGo4FitComponent::Print(Option_t* option) const
372 {
373  std::cout << "***************************************************************************" << std::endl;
374  TGo4FitNamed::Print(option);
375  TGo4FitParsList::Print(option);
376  std::cout << " Amplitude index: " << fiAmplIndex << std::endl;
377 
378  for (Int_t ncond=0; ncond<GetNumRangeCondition(); ncond++) {
379  if (ncond==0) std::cout << " Range selection: " << std::endl;
380  Int_t typ, naxis;
381  Double_t left, right;
382  GetRangeCondition(ncond, typ, naxis, left, right);
383 
384  std::cout << " axis " << naxis << " ";
385 
386  switch(typ) {
387  case 0: std::cout << " select range from " << left << " to " << right; break;
388  case 1: std::cout << " exclude range from " << left << " to " << right; break;
389  case 2: std::cout << " set left bound to " << left; break;
390  case 3: std::cout << " set right bound to " << right; break;
391  }
392 
393  std::cout << std::endl;
394  }
395 
396  for (Int_t n=0;n<GetNumRangeCut();n++) {
397  TCutG* cut = GetRangeCut(n);
398  if (cut->TestBit(kExcludeCut)) std::cout << " Exclude"; else std::cout << " Include";
399  std::cout << " axes ranges, using TCutG object " << std::endl;
400  cut->Print(option);
401  }
402 }
virtual void Print(Option_t *option) const
TGo4FitParameter * InsertPar(TGo4FitParameter *par, Int_t indx)
Bool_t CheckRangeConditions(const Double_t *values, Int_t numaxis)
Int_t GetNumRangeCut() const
TGo4FitSlot * GetSlot(Int_t nslot)
void ClearRanges(Int_t naxis=-1)
void SetRangeCondition(Int_t n, Int_t typ, Int_t naxis, Double_t left, Double_t right)
Int_t GetNumRangeCondition() const
const char * GetAmplFullName()
void SetRangeCutExcluding(Int_t n, Bool_t exclude=kTRUE)
void SetRangeMin(Int_t naxis, Double_t value)
TGo4FitParameter * GetPar(Int_t n)
Int_t GetAmplIndex() const
void SetAmplError(Double_t iError)
virtual void Print(Option_t *option) const
TGo4FitParameter * NewParameter(const char *Name, const char *Title, Double_t iValue=0., Bool_t Fixed=kFALSE, Int_t AtIndx=-1)
const char * GetFullName()
void SetAmplValue(Double_t iAmpl)
TGo4FitParameter * NewAmplitude(const char *Name=0, Double_t iValue=0., Bool_t IsFixed=kFALSE, Int_t AtIndx=0)
void SetAmplIndex(Int_t iAmplIndex=-1)
Bool_t GetOwned()
Definition: TGo4FitSlot.h:161
virtual Int_t NumPars()
TCutG * GetRangeCut(Int_t n) const
void SetRange(Int_t naxis, Double_t min, Double_t max)
void Print(Option_t *option) const
Double_t GetError() const
void AddRangeCut(TCutG *cut, Bool_t exclude=kFALSE)
void SetRangeMax(Int_t naxis, Double_t value)
const UInt_t kExcludeCut
Bool_t RemoveParByIndex(Int_t indx)
void GetRangeCondition(Int_t n, Int_t &typ, Int_t &naxis, Double_t &left, Double_t &right) const
virtual void CollectParsTo(TGo4FitParsList &list)
void SetValue(Double_t iValue)
void RemoveRangeCut(Int_t n)
Double_t GetValue() const
Bool_t IsRangeCutExcluding(Int_t n)
void RemoveRangeCondition(Int_t n)
void AddRangeCondition(Int_t typ, Int_t naxis, Double_t left, Double_t right)
Bool_t MakeAmpl(Bool_t isFixed=kFALSE)
Bool_t GetRangeMin(Int_t naxis, Double_t &value)
const char * GetAmplName()
virtual TGo4FitParameter * Get(Int_t n)
TObject * GetObject() const
void ExcludeRange(Int_t naxis, Double_t min, Double_t max)
void SetFixed(Bool_t iFixed)
virtual void CollectParsTo(TGo4FitParsList &list)
TGo4FitParameter * AddPar(TGo4FitParameter *par)
virtual ~TGo4FitComponent()
Bool_t GetRangeMax(Int_t naxis, Double_t &value)
void SetError(Double_t iError)
TGo4FitParameter * GetAmplPar()