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