musrfit  1.9.2
PRunNonMusr.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  PRunNonMusr.cpp
4 
5  Author: Andreas Suter
6  e-mail: andreas.suter@psi.ch
7 
8 ***************************************************************************/
9 
10 /***************************************************************************
11  * Copyright (C) 2007-2023 by Andreas Suter *
12  * andreas.suter@psi.ch *
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the *
26  * Free Software Foundation, Inc., *
27  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28  ***************************************************************************/
29 
30 #include <iostream>
31 
32 #include "PRunNonMusr.h"
33 
34 //--------------------------------------------------------------------------
35 // Constructor
36 //--------------------------------------------------------------------------
41 {
42  fNoOfFitBins = 0;
43  fPacking = 1;
44  fStartTimeBin = 0;
45  fEndTimeBin = 0;
46 
48 
49  fRawRunData = nullptr;
50 }
51 
52 //--------------------------------------------------------------------------
53 // Constructor
54 //--------------------------------------------------------------------------
63 PRunNonMusr::PRunNonMusr(PMsrHandler *msrInfo, PRunDataHandler *rawData, UInt_t runNo, EPMusrHandleTag tag, Bool_t theoAsData) :
64  PRunBase(msrInfo, rawData, runNo, tag), fTheoAsData(theoAsData)
65 {
66  // get the proper run
68  if (!fRawRunData) { // couldn't get run
69  std::cerr << std::endl << "PRunNonMusr::PRunNonMusr(): **ERROR** Couldn't get raw run data!";
70  std::cerr << std::endl;
71  fValid = false;
72  }
73 
74  // calculate fData
75  if (!PrepareData())
76  fValid = false;
77 }
78 
79 //--------------------------------------------------------------------------
80 // Destructor
81 //--------------------------------------------------------------------------
86 {
87 }
88 
89 //--------------------------------------------------------------------------
90 // CalcChiSquare
91 //--------------------------------------------------------------------------
100 Double_t PRunNonMusr::CalcChiSquare(const std::vector<Double_t>& par)
101 {
102  Double_t chisq = 0.0;
103  Double_t diff = 0.0;
104 
105  // calculate functions
106  for (Int_t i=0; i<fMsrInfo->GetNoOfFuncs(); i++) {
108  }
109 
110  // calculate chi square
111  Double_t x(1.0);
112  for (UInt_t i=fStartTimeBin; i<=fEndTimeBin; i++) {
113  x = fData.GetX()->at(i);
114  diff = fData.GetValue()->at(i) - fTheory->Func(x, par, fFuncValues);
115  chisq += diff*diff / (fData.GetError()->at(i)*fData.GetError()->at(i));
116  }
117 
118  return chisq;
119 }
120 
121 //--------------------------------------------------------------------------
122 // CalcChiSquareExpected (public)
123 //--------------------------------------------------------------------------
132 Double_t PRunNonMusr::CalcChiSquareExpected(const std::vector<Double_t>& par)
133 {
134  std::cout << std::endl << "PRunNonMusr::CalcChiSquareExpected(): not implemented yet ..." << std::endl;
135 
136  return 0.0;
137 }
138 
139 //--------------------------------------------------------------------------
140 // CalcMaxLikelihood
141 //--------------------------------------------------------------------------
150 Double_t PRunNonMusr::CalcMaxLikelihood(const std::vector<Double_t>& par)
151 {
152  std::cout << std::endl << "PRunNonMusr::CalcMaxLikelihood(): not implemented yet ..." << std::endl;
153 
154  return 1.0;
155 }
156 
157 //--------------------------------------------------------------------------
158 // CalcTheory
159 //--------------------------------------------------------------------------
164 {
165 }
166 
167 //--------------------------------------------------------------------------
168 // GetNoOfFitBins (public)
169 //--------------------------------------------------------------------------
176 {
177  fNoOfFitBins=0;
178  Double_t x;
179  for (UInt_t i=0; i<fData.GetValue()->size(); i++) {
180  x = fData.GetX()->at(i);
181  if ((x >= fFitStartTime) && (x <= fFitEndTime))
182  fNoOfFitBins++;
183  }
184 
185  return fNoOfFitBins;
186 }
187 
188 //--------------------------------------------------------------------------
189 // PrepareData
190 //--------------------------------------------------------------------------
199 {
200  Bool_t success = true;
201 
202  if (!fValid)
203  return false;
204 
205  if (fRunInfo->GetRunNameSize() > 1) { // ADDRUN present which is not supported for NonMusr
206  std::cerr << std::endl << ">> PRunNonMusr::PrepareData(): **WARNING** ADDRUN NOT SUPPORTED FOR THIS FIT TYPE, WILL IGNORE IT." << std::endl;
207  }
208 
209  // get packing info
211  if (fPacking == -1) { // packing not present in the RUN block, will try the GLOBAL block
213  }
214  if (fPacking == -1) { // packing NOT present, in neither the RUN block, nor in the GLOBAL block
215  std::cerr << std::endl << ">> PRunNonMusr::PrepareData(): **ERROR** couldn't find any packing information." << std::endl;
216  return false;
217  }
218 
219  // get fit start/end time
222  if (fFitStartTime == PMUSR_UNDEFINED) { // not present in the RUN block, will try GLOBAL block
225  }
226 
227  if (fHandleTag == kFit)
228  success = PrepareFitData();
229  else if (fHandleTag == kView)
230  success = PrepareViewData();
231  else
232  success = false;
233 
234  return success;
235 }
236 
237 //--------------------------------------------------------------------------
238 // PrepareFitData
239 //--------------------------------------------------------------------------
248 {
249  Bool_t success = true;
250 
251  // get x-, y-index
252  UInt_t xIndex = GetXIndex();
253  UInt_t yIndex = GetYIndex();
254 
255  // pack the raw data
256  Double_t value = 0.0;
257  Double_t err = 0.0;
258  for (UInt_t i=0; i<fRawRunData->fDataNonMusr.GetData()->at(xIndex).size(); i++) {
259  if (fPacking == 1) {
260  fData.AppendXValue(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i));
261  fData.AppendValue(fRawRunData->fDataNonMusr.GetData()->at(yIndex).at(i));
263  } else { // packed data, i.e. fPacking > 1
264  if ((i % fPacking == 0) && (i != 0)) { // fill data
265  fData.AppendXValue(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i)-(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i)-fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i-fPacking))/2.0);
266  fData.AppendValue(value);
267  fData.AppendErrorValue(TMath::Sqrt(err));
268  value = 0.0;
269  err = 0.0;
270  }
271  // sum raw data values
272  value += fRawRunData->fDataNonMusr.GetData()->at(yIndex).at(i);
273  err += fRawRunData->fDataNonMusr.GetErrData()->at(yIndex).at(i)*fRawRunData->fDataNonMusr.GetErrData()->at(yIndex).at(i);
274  }
275  }
276 
277  // count the number of bins to be fitted
278  fNoOfFitBins=0;
279  Double_t x;
280  for (UInt_t i=0; i<fData.GetValue()->size(); i++) {
281  x = fData.GetX()->at(i);
282  if ((x >= fFitStartTime) && (x <= fFitEndTime))
283  fNoOfFitBins++;
284  }
285 
286  // get start/end bin
287  const PDoubleVector *xx = fData.GetX();
288  fStartTimeBin = 0;
289  fEndTimeBin = xx->size()-1;
290  for (UInt_t i=0; i<xx->size(); i++) {
291  if (xx->at(i) < fFitStartTime)
292  fStartTimeBin = i;
293  if (xx->at(i) < fFitEndTime)
294  fEndTimeBin = i;
295  }
296 
297  return success;
298 }
299 
300 //--------------------------------------------------------------------------
301 // PrepareViewData
302 //--------------------------------------------------------------------------
311 {
312  Bool_t success = true;
313 
314  // get x-, y-index
315  UInt_t xIndex = GetXIndex();
316  UInt_t yIndex = GetYIndex();
317 
318  // fill data histo
319  // pack the raw data
320  Double_t value = 0.0;
321  Double_t err = 0.0;
322  for (UInt_t i=0; i<fRawRunData->fDataNonMusr.GetData()->at(xIndex).size(); i++) {
323  if (fPacking == 1) {
324  fData.AppendXValue(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i));
325  fData.AppendValue(fRawRunData->fDataNonMusr.GetData()->at(yIndex).at(i));
327  } else { // packed data, i.e. fPacking > 1
328  if ((i % fPacking == 0) && (i != 0)) { // fill data
329  fData.AppendXValue(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i)-(fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i)-fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i-fPacking))/2.0);
330  fData.AppendValue(value);
331  fData.AppendErrorValue(TMath::Sqrt(err));
332  value = 0.0;
333  err = 0.0;
334  }
335  // sum raw data values
336  value += fRawRunData->fDataNonMusr.GetData()->at(yIndex).at(i);
337  err += fRawRunData->fDataNonMusr.GetErrData()->at(yIndex).at(i)*fRawRunData->fDataNonMusr.GetErrData()->at(yIndex).at(i);
338  }
339  }
340 
341  // count the number of bins to be fitted
342  fNoOfFitBins = fData.GetValue()->size();
343 
344  // fill theory histo
345  // feed the parameter vector
346  std::vector<Double_t> par;
347  PMsrParamList *paramList = fMsrInfo->GetMsrParamList();
348  for (UInt_t i=0; i<paramList->size(); i++)
349  par.push_back((*paramList)[i].fValue);
350  // calculate functions
351  for (Int_t i=0; i<fMsrInfo->GetNoOfFuncs(); i++) {
353  }
354 
355  // get plot range
356  PMsrPlotList *plotList;
357  PMsrPlotStructure plotBlock;
358  plotList = fMsrInfo->GetMsrPlotList();
359  // find the proper plot block
360  // Here a small complication to be handled: there are potentially multiple
361  // run blocks and the run might be present in various of these run blocks. In
362  // order to get a nice resolution on the theory the following procedure will be
363  // followed: the smallest x-interval found will be used to for the fXTheory resolution
364  // which is 1000 function points. The function will be calculated from the smallest
365  // xmin found up to the largest xmax found.
366  Double_t xMin = 0.0, xMax = 0.0;
367 
368  // init xMin/xMax, xAbsMin/xAbsMax
369  plotBlock = plotList->at(0);
370  if (plotBlock.fTmin.size() == 0) { // check if no range information is present
371  PMsrRunList *runList = fMsrInfo->GetMsrRunList();
372  xMin = runList->at(0).GetFitRange(0);
373  xMax = runList->at(0).GetFitRange(1);
374  for (UInt_t i=1; i<runList->size(); i++) {
375  if (runList->at(i).GetFitRange(0) < xMin)
376  xMin = runList->at(i).GetFitRange(0);
377  if (runList->at(i).GetFitRange(1) > xMax)
378  xMax = runList->at(i).GetFitRange(1);
379  }
380  } else if (plotBlock.fTmin.size() == 1) { // check if 'range' information is present
381  xMin = plotBlock.fTmin[0];
382  xMax = plotBlock.fTmax[0];
383  } else if (plotBlock.fTmin.size() > 1) { // check if 'sub_ranges' information is present
384  xMin = plotBlock.fTmin[0];
385  xMax = plotBlock.fTmax[0];
386  for (UInt_t i=1; i<plotBlock.fTmin.size(); i++) {
387  if (plotBlock.fTmin[i] < xMin)
388  xMin = plotBlock.fTmin[i];
389  if (plotBlock.fTmax[i] > xMax)
390  xMax = plotBlock.fTmax[i];
391  }
392  }
393 
394  if (plotBlock.fUseFitRanges) { // check if 'use_fit_ranges' information is present
395  PMsrRunList *runList = fMsrInfo->GetMsrRunList();
396  xMin = runList->at(0).GetFitRange(0);
397  xMax = runList->at(0).GetFitRange(1);
398  for (UInt_t i=1; i<runList->size(); i++) {
399  if (runList->at(i).GetFitRange(0) < xMin)
400  xMin = runList->at(i).GetFitRange(0);
401  if (runList->at(i).GetFitRange(1) > xMax)
402  xMax = runList->at(i).GetFitRange(1);
403  }
404  }
405 
406  for (UInt_t i=1; i<plotList->size(); i++) { // go through all the plot blocks
407  plotBlock = plotList->at(i);
408 
409  if (plotBlock.fTmin.size() == 0) { // check if no range information is present
410  PMsrRunList *runList = fMsrInfo->GetMsrRunList();
411  for (UInt_t i=0; i<runList->size(); i++) {
412  if (runList->at(i).GetFitRange(0) < xMin)
413  xMin = runList->at(i).GetFitRange(0);
414  if (runList->at(i).GetFitRange(1) > xMax)
415  xMax = runList->at(i).GetFitRange(1);
416  }
417  } else if (plotBlock.fTmin.size() == 1) { // check if 'range' information is present
418  if (plotBlock.fTmin[0] < xMin)
419  xMin = plotBlock.fTmin[0];
420  if (plotBlock.fTmax[0] > xMax)
421  xMax = plotBlock.fTmax[0];
422  } else if (plotBlock.fTmin.size() > 1) { // check if 'sub_ranges' information is present
423  for (UInt_t i=0; i<plotBlock.fTmin.size(); i++) {
424  if (plotBlock.fTmin[i] < xMin)
425  xMin = plotBlock.fTmin[i];
426  if (plotBlock.fTmax[i] > xMax)
427  xMax = plotBlock.fTmax[i];
428  }
429  }
430 
431  if (plotBlock.fUseFitRanges) { // check if 'use_fit_ranges' information is present
432  PMsrRunList *runList = fMsrInfo->GetMsrRunList();
433  for (UInt_t i=0; i<runList->size(); i++) {
434  if (runList->at(i).GetFitRange(0) < xMin)
435  xMin = runList->at(i).GetFitRange(0);
436  if (runList->at(i).GetFitRange(1) > xMax)
437  xMax = runList->at(i).GetFitRange(1);
438  }
439  }
440  }
441 
442  // typically take 1000 points to calculate the theory, except if there are more data points, than take that number
443  Double_t xStep;
444  if (fData.GetX()->size() > 1000.0)
445  xStep = (xMax-xMin)/fData.GetX()->size();
446  else
447  xStep = (xMax-xMin)/1000.0;
448 
449  if (fTheoAsData) {
450  Double_t xx;
451  for (UInt_t i=0; i<fRawRunData->fDataNonMusr.GetData()->at(xIndex).size(); i++) {
452  // fill x-vector
453  xx = fRawRunData->fDataNonMusr.GetData()->at(xIndex).at(i);
455  // fill y-vector
456  fData.AppendTheoryValue(fTheory->Func(xx, par, fFuncValues));
457  }
458  } else {
459  Double_t xx = xMin;
460  do {
461  // fill x-vector
463  // fill y-vector
464  fData.AppendTheoryValue(fTheory->Func(xx, par, fFuncValues));
465  // calculate next xx
466  xx += xStep;
467  } while (xx < xMax);
468  }
469 
470  // clean up
471  par.clear();
472 
473  return success;
474 }
475 
476 //--------------------------------------------------------------------------
477 // GetXIndex
478 //--------------------------------------------------------------------------
486 {
487  UInt_t index = 0;
488  Bool_t found = false;
489 
490  if (fRawRunData->fDataNonMusr.FromAscii()) { // ascii-file format
491  index = 0;
492  found = true;
493  } else { // db-file format
494  if (fRunInfo->GetXDataIndex() > 0) { // xy-data already indices
495  index = fRunInfo->GetXDataIndex()-1; // since xy-data start with 1 ...
496  found = true;
497  } else { // xy-data data tags which needs to be converted to an index
498  for (UInt_t i=0; i<fRawRunData->fDataNonMusr.GetDataTags()->size(); i++) {
499  if (fRawRunData->fDataNonMusr.GetDataTags()->at(i).CompareTo(*fRunInfo->GetXDataLabel()) == 0) {
500  index = i;
501  found = true;
502  break;
503  }
504  }
505  }
506  }
507 
508  if (!found) {
509  std::cerr << std::endl << "PRunNonMusr::GetXIndex(): **ERROR** Couldn't obtain x-data index!";
510  std::cerr << std::endl;
511  assert(0);
512  }
513 
514  return index;
515 }
516 
517 //--------------------------------------------------------------------------
518 // GetYIndex
519 //--------------------------------------------------------------------------
527 {
528  UInt_t index = 0;
529  Bool_t found = false;
530 
531  if (fRawRunData->fDataNonMusr.FromAscii()) { // ascii-file format
532  index = 1;
533  found = true;
534  } else { // db-file format
535  if (fRunInfo->GetYDataIndex() > 0) { // xy-data already indices
536  index = fRunInfo->GetYDataIndex()-1; // since xy-data start with 1 ...
537  found = true;
538  } else { // xy-data data tags which needs to be converted to an index
539  for (UInt_t i=0; i<fRawRunData->fDataNonMusr.GetDataTags()->size(); i++) {
540  if (fRawRunData->fDataNonMusr.GetDataTags()->at(i).CompareTo(*fRunInfo->GetYDataLabel()) == 0) {
541  index = i;
542  found = true;
543  break;
544  }
545  }
546  }
547  }
548 
549  if (!found) {
550  std::cerr << std::endl << "PRunNonMusr::GetYIndex(): **ERROR** Couldn't obtain y-data index!";
551  std::cerr << std::endl;
552  assert(0);
553  }
554 
555  return index;
556 }
virtual Bool_t FromAscii()
Definition: PMusr.h:290
Bool_t fValid
flag showing if the state of the class is valid
Definition: PRunBase.h:66
Definition: PMusr.h:220
virtual Double_t CalcMaxLikelihood(const std::vector< Double_t > &par)
virtual const PDoubleVector * GetError()
Definition: PMusr.h:249
Definition: PMusr.h:220
virtual void AppendXValue(Double_t dval)
Definition: PMusr.h:258
virtual Int_t GetNoOfFuncs()
Definition: PMsrHandler.h:93
virtual Bool_t PrepareViewData()
std::vector< PMsrRunBlock > PMsrRunList
Definition: PMusr.h:754
virtual Int_t GetPacking()
Definition: PMusr.h:668
PRunDataHandler * fRawData
holds the raw run data
Definition: PRunBase.h:73
PMsrHandler * fMsrInfo
msr-file handler
Definition: PRunBase.h:71
virtual const std::vector< PDoubleVector > * GetData()
Definition: PMusr.h:293
PRawRunData * fRawRunData
raw run data handler
Definition: PRunNonMusr.h:64
PMetaData fMetaData
keeps the meta data from the data file like field, temperature, energy, ...
Definition: PRunBase.h:77
virtual Int_t GetXDataIndex()
Definition: PMusr.h:670
virtual PRawRunData * GetRunData(const TString &runName)
Bool_t fTheoAsData
true=only calculate the theory points at the data points, false=calculate more points for the theory ...
Definition: PRunNonMusr.h:68
Int_t fEndTimeBin
bin at which the fit ends
Definition: PRunNonMusr.h:71
virtual UInt_t GetNoOfFitBins()
std::vector< PMsrPlotStructure > PMsrPlotList
Definition: PMusr.h:802
virtual void AppendErrorValue(Double_t dval)
Definition: PMusr.h:260
virtual TString * GetYDataLabel()
Definition: PMusr.h:673
virtual Double_t CalcChiSquare(const std::vector< Double_t > &par)
PRunData fData
data to be fitted, viewed, i.e. binned data
Definition: PRunBase.h:75
virtual Double_t CalcChiSquareExpected(const std::vector< Double_t > &par)
UInt_t fNoOfFitBins
number of bins to be be fitted
Definition: PRunNonMusr.h:66
std::vector< PMsrParamStructure > PMsrParamList
Definition: PMusr.h:564
std::vector< Double_t > PDoubleVector
Definition: PMusr.h:196
virtual Bool_t PrepareData()
virtual PMsrRunList * GetMsrRunList()
Definition: PMsrHandler.h:63
virtual const PStringVector * GetDataTags()
Definition: PMusr.h:292
Int_t fStartTimeBin
bin at which the fit starts
Definition: PRunNonMusr.h:70
virtual Int_t GetYDataIndex()
Definition: PMusr.h:671
virtual const PDoubleVector * GetX()
Definition: PMusr.h:247
virtual void AppendTheoryValue(Double_t dval)
Definition: PMusr.h:262
virtual PMsrGlobalBlock * GetMsrGlobal()
Definition: PMsrHandler.h:62
Int_t fPacking
packing for this particular run. Either given in the RUN- or GLOBAL-block.
Definition: PRunNonMusr.h:67
virtual void AppendValue(Double_t dval)
Definition: PMusr.h:259
virtual const PDoubleVector * GetValue()
Definition: PMusr.h:248
virtual const std::vector< PDoubleVector > * GetErrData()
Definition: PMusr.h:294
PDoubleVector fTmin
time minimum
Definition: PMusr.h:787
PDoubleVector fTmax
time maximum
Definition: PMusr.h:788
virtual UInt_t GetFuncNo(Int_t idx)
Definition: PMsrHandler.h:94
virtual TString * GetXDataLabel()
Definition: PMusr.h:672
#define PMUSR_UNDEFINED
Definition: PMusr.h:89
virtual PMsrPlotList * GetMsrPlotList()
Definition: PMsrHandler.h:66
EPMusrHandleTag fHandleTag
tag telling whether this is used for fit, view, ...
Definition: PRunBase.h:68
EPMusrHandleTag
Definition: PMusr.h:220
PMsrRunBlock * fRunInfo
run info used to filter out needed infos of a run
Definition: PRunBase.h:72
Bool_t fUseFitRanges
yes -> use the fit ranges to plot the data, no (default) -> use range information if present ...
Definition: PMusr.h:782
Double_t fFitEndTime
fit end time
Definition: PRunBase.h:82
Double_t fFitStartTime
fit start time
Definition: PRunBase.h:81
virtual PMsrParamList * GetMsrParamList()
Definition: PMsrHandler.h:59
virtual void CalcTheory()
virtual PIntVector * GetMap()
Definition: PMusr.h:650
virtual Double_t EvalFunc(UInt_t i, std::vector< Int_t > map, std::vector< Double_t > param, PMetaData metaData)
Definition: PMsrHandler.h:98
virtual UInt_t GetYIndex()
virtual UInt_t GetRunNameSize()
Definition: PMusr.h:635
virtual void AppendXTheoryValue(Double_t dval)
Definition: PMusr.h:261
PNonMusrRawRunData fDataNonMusr
keeps all ascii- or db-file info in case of nonMusr fit
Definition: PMusr.h:479
std::unique_ptr< PTheory > fTheory
theory needed to calculate chi-square
Definition: PRunBase.h:85
virtual Int_t GetPacking()
Definition: PMusr.h:591
virtual Double_t GetFitRange(UInt_t idx)
Definition: PMusr.cpp:1051
virtual ~PRunNonMusr()
Definition: PRunNonMusr.cpp:85
virtual UInt_t GetXIndex()
virtual Bool_t PrepareFitData()
virtual TString * GetRunName(UInt_t idx=0)
Definition: PMusr.cpp:1232
virtual Double_t GetFitRange(UInt_t idx)
Definition: PMusr.cpp:1811
Definition: PMusr.h:220
PDoubleVector fFuncValues
is keeping the values of the functions from the FUNCTIONS block
Definition: PRunBase.h:84