musrfit  1.9.2
PRunBase.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  PRunBase.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 <TROOT.h>
33 #include <TSystem.h>
34 #include <TString.h>
35 #include <TObjArray.h>
36 #include <TObjString.h>
37 #include <TFolder.h>
38 
39 #include "PRunBase.h"
40 
41 //--------------------------------------------------------------------------
42 // Constructor
43 //--------------------------------------------------------------------------
48 {
49  fRunNo = -1;
50  fRunInfo = nullptr;
51  fRawData = nullptr;
54  fTimeResolution = -1.0;
55 
58 
59  fValid = true;
61 }
62 
63 //--------------------------------------------------------------------------
64 // Constructor
65 //--------------------------------------------------------------------------
74 PRunBase::PRunBase(PMsrHandler *msrInfo, PRunDataHandler *rawData, UInt_t runNo, EPMusrHandleTag tag) :
75  fHandleTag(tag), fMsrInfo(msrInfo), fRawData(rawData)
76 {
77  fValid = true;
78 
79  fRunNo = static_cast<Int_t>(runNo);
80  if (runNo > fMsrInfo->GetMsrRunList()->size()) {
81  fRunInfo = nullptr;
82  return;
83  }
84 
85  // keep the run header info for this run
86  fRunInfo = &(*fMsrInfo->GetMsrRunList())[runNo];
87 
88  // check the parameter and map range of the functions
89  if (!fMsrInfo->CheckMapAndParamRange(static_cast<UInt_t>(fRunInfo->GetMap()->size()), fMsrInfo->GetNoOfParams())) {
90  std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: map and/or parameter out of range in FUNCTIONS." << std::endl;
91  exit(0);
92  }
93 
94  // init private variables
97  fTimeResolution = -1.0;
98  for (Int_t i=0; i<fMsrInfo->GetNoOfFuncs(); i++)
99  fFuncValues.push_back(0.0);
100 
101  // generate theory
102  fTheory = std::make_unique<PTheory>(fMsrInfo, runNo);
103  if (fTheory == nullptr) {
104  std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: Couldn't create an instance of PTheory :-(, will quit" << std::endl;
105  exit(0);
106  }
107  if (!fTheory->IsValid()) {
108  std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: Theory is not valid :-(, will quit" << std::endl;
109  exit(0);
110  }
111 
112  // set fit time ranges
115 }
116 
117 //--------------------------------------------------------------------------
118 // Destructor
119 //--------------------------------------------------------------------------
124 {
125  fT0s.clear();
126  for (UInt_t i=0; i<fAddT0s.size(); i++)
127  fAddT0s[i].clear();
128  fAddT0s.clear();
129 
130  fFuncValues.clear();
131 }
132 
133 
134 //--------------------------------------------------------------------------
135 // SetFitRange (public)
136 //--------------------------------------------------------------------------
143 {
144  Double_t start=0.0, end=0.0;
145 
146  assert(fitRange.size()); // make sure fitRange is not empty
147 
148  if (fitRange.size()==1) { // one fit range for all
149  start = fitRange[0].first;
150  end = fitRange[0].second;
151  } else {
152  // check that fRunNo is found within fitRange
153  UInt_t idx=static_cast<UInt_t>(fRunNo);
154  if (idx < fitRange.size()) { // fRunNo present
155  start = fitRange[idx].first;
156  end = fitRange[idx].second;
157  } else { // fRunNo NOT present
158  std::cerr << std::endl << ">> PRunBase::SetFitRange(): **ERROR** msr-file run entry " << fRunNo << " not present in fit range vector.";
159  std::cerr << std::endl << ">> Will not do anything! Please check, this shouldn't happen." << std::endl;
160  return;
161  }
162  }
163 
164  // check that start is before end
165  if (start > end) {
166  std::cerr << std::endl << ">> PRunBase::SetFitRange(): **WARNING** start=" << start << " is > as end=" << end;
167  std::cerr << std::endl << ">> Will swap them, since otherwise chisq/logLH == 0.0" << std::endl;
168  fFitStartTime = end;
169  fFitEndTime = start;
170  } else {
171  fFitStartTime = start;
172  fFitEndTime = end;
173  }
174 }
175 
176 //--------------------------------------------------------------------------
177 // CleanUp (public)
178 //--------------------------------------------------------------------------
183 {
184  fTheory.reset();
185 }
186 
187 //--------------------------------------------------------------------------
188 // CalculateKaiserFilterCoeff (protected)
189 //--------------------------------------------------------------------------
200 void PRunBase::CalculateKaiserFilterCoeff(Double_t wc, Double_t A, Double_t dw)
201 {
202  Double_t beta;
203  Double_t dt = fData.GetTheoryTimeStep();
204  Int_t m;
205 
206  // estimate beta (see reference above, p.574ff)
207  if (A > 50.0) {
208  beta = 0.1102*(A-8.7);
209  } else if ((A >= 21.0) && (A <= 50.0)) {
210  beta = 0.5842*TMath::Power(A-21.0, 0.4) + 0.07886*(A-21.0);
211  } else {
212  beta = 0.0;
213  }
214 
215  m = TMath::FloorNint((A-8.0)/(2.285*dw*TMath::Pi()));
216  // make sure m is odd
217  if (m % 2 == 0)
218  m++;
219 
220  Double_t alpha = static_cast<Double_t>(m)/2.0;
221  Double_t dval;
222  Double_t dsum = 0.0;
223  for (Int_t i=0; i<=m; i++) {
224  dval = TMath::Sin(wc*(i-alpha)*dt)/(TMath::Pi()*(i-alpha)*dt);
225  dval *= TMath::BesselI0(beta*TMath::Sqrt(1.0-TMath::Power((i-alpha)*dt/alpha, 2.0)))/TMath::BesselI0(beta);
226  dsum += dval;
227  fKaiserFilter.push_back(dval);
228  }
229  for (UInt_t i=0; i<=static_cast<UInt_t>(m); i++) {
230  fKaiserFilter[i] /= dsum;
231  }
232 }
233 
234 //--------------------------------------------------------------------------
235 // FilterTheo (protected)
236 //--------------------------------------------------------------------------
241 {
242  PDoubleVector theoFiltered;
243  Double_t dval = 0.0;
244  const PDoubleVector *theo = fData.GetTheory();
245 
246  for (UInt_t i=0; i<theo->size(); i++) {
247  for (UInt_t j=0; j<fKaiserFilter.size(); j++) {
248  if (i<j)
249  dval = 0.0;
250  else
251  dval += fKaiserFilter[j]*theo->at(i-j);
252  }
253  theoFiltered.push_back(dval);
254  dval = 0.0;
255  }
256 
257  fData.ReplaceTheory(theoFiltered);
258 
259  // shift time start by half the filter length
260  dval = fData.GetTheoryTimeStart() - 0.5*static_cast<Double_t>(fKaiserFilter.size())*fData.GetTheoryTimeStep();
262 
263  theoFiltered.clear();
264 }
Bool_t fValid
flag showing if the state of the class is valid
Definition: PRunBase.h:66
Definition: PMusr.h:220
virtual void CleanUp()
Definition: PRunBase.cpp:182
virtual void SetFitRange(PDoublePairVector fitRange)
Definition: PRunBase.cpp:142
virtual Int_t GetNoOfFuncs()
Definition: PMsrHandler.h:93
PRunDataHandler * fRawData
holds the raw run data
Definition: PRunBase.h:73
std::vector< PDoubleVector > fAddT0s
all t0 bins of all addrun&#39;s of a run! The derived classes will handle it.
Definition: PRunBase.h:79
virtual void SetTheoryTimeStart(Double_t dval)
Definition: PMusr.h:255
PMsrHandler * fMsrInfo
msr-file handler
Definition: PRunBase.h:71
Double_t fTimeResolution
time resolution in (us)
Definition: PRunBase.h:76
virtual const PDoubleVector * GetTheory()
Definition: PMusr.h:251
PMetaData fMetaData
keeps the meta data from the data file like field, temperature, energy, ...
Definition: PRunBase.h:77
PDoubleVector fKaiserFilter
stores the Kaiser filter vector (needed for the RRF).
Definition: PRunBase.h:87
virtual ~PRunBase()
Definition: PRunBase.cpp:123
virtual Double_t GetTheoryTimeStep()
Definition: PMusr.h:245
PRunData fData
data to be fitted, viewed, i.e. binned data
Definition: PRunBase.h:75
virtual Double_t GetTheoryTimeStart()
Definition: PMusr.h:244
std::vector< Double_t > PDoubleVector
Definition: PMusr.h:196
virtual PMsrRunList * GetMsrRunList()
Definition: PMsrHandler.h:63
virtual UInt_t GetNoOfParams()
Definition: PMsrHandler.h:73
virtual void CalculateKaiserFilterCoeff(Double_t wc, Double_t A, Double_t dw)
Definition: PRunBase.cpp:200
#define PMUSR_UNDEFINED
Definition: PMusr.h:89
PDoubleVector fT0s
all t0 bins of a run! The derived classes will handle it.
Definition: PRunBase.h:78
Double_t fEnergy
energy in (keV)
Definition: PMusr.h:228
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
PRunBase()
Definition: PRunBase.cpp:47
Double_t fFitEndTime
fit end time
Definition: PRunBase.h:82
std::vector< PDoublePair > PDoublePairVector
Definition: PMusr.h:208
Double_t fFitStartTime
fit start time
Definition: PRunBase.h:81
virtual PIntVector * GetMap()
Definition: PMusr.h:650
virtual void FilterTheo()
Definition: PRunBase.cpp:240
std::unique_ptr< PTheory > fTheory
theory needed to calculate chi-square
Definition: PRunBase.h:85
virtual void ReplaceTheory(const PDoubleVector &theo)
Definition: PMusr.cpp:103
Int_t fRunNo
number of the run within the msr-file
Definition: PRunBase.h:70
virtual Bool_t CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize)
Definition: PMsrHandler.h:96
PDoubleVector fFuncValues
is keeping the values of the functions from the FUNCTIONS block
Definition: PRunBase.h:84
Double_t fField
field in (G)
Definition: PMusr.h:227