musrfit 1.10.0
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-2026 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 <TFolder.h>
36
37#include "PRunBase.h"
38
39//--------------------------------------------------------------------------
40// Constructor
41//--------------------------------------------------------------------------
53{
54 fRunNo = -1;
55 fRunInfo = nullptr;
56 fRawData = nullptr;
58 fMetaData.fEnergy = PMUSR_UNDEFINED;
59 fTimeResolution = -1.0;
60
63
64 fValid = true;
66}
67
68//--------------------------------------------------------------------------
69// Constructor
70//--------------------------------------------------------------------------
92PRunBase::PRunBase(PMsrHandler *msrInfo, PRunDataHandler *rawData, UInt_t runNo, EPMusrHandleTag tag) :
93 fHandleTag(tag), fMsrInfo(msrInfo), fRawData(rawData)
94{
95 fValid = true;
96
97 fRunNo = static_cast<Int_t>(runNo);
98 if (runNo > fMsrInfo->GetMsrRunList()->size()) {
99 fRunInfo = nullptr;
100 return;
101 }
102
103 // keep the run header info for this run
104 fRunInfo = &(*fMsrInfo->GetMsrRunList())[runNo];
105
106 // check the parameter and map range of the functions
107 if (!fMsrInfo->CheckMapAndParamRange(static_cast<UInt_t>(fRunInfo->GetMap()->size()), fMsrInfo->GetNoOfParams())) {
108 std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: map and/or parameter out of range in FUNCTIONS." << std::endl;
109 exit(0);
110 }
111
112 // init private variables
113 fMetaData.fField = PMUSR_UNDEFINED;
114 fMetaData.fEnergy = PMUSR_UNDEFINED;
115 fTimeResolution = -1.0;
116 for (Int_t i=0; i<fMsrInfo->GetNoOfFuncs(); i++)
117 fFuncValues.push_back(0.0);
118
119 // generate theory
120 fTheory = std::make_unique<PTheory>(fMsrInfo, runNo);
121 if (fTheory == nullptr) {
122 std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: Couldn't create an instance of PTheory :-(, will quit" << std::endl;
123 exit(0);
124 }
125 if (!fTheory->IsValid()) {
126 std::cerr << std::endl << "**SEVERE ERROR** PRunBase::PRunBase: Theory is not valid :-(, will quit" << std::endl;
127 exit(0);
128 }
129
130 // set fit time ranges
133}
134
135//--------------------------------------------------------------------------
136// Destructor
137//--------------------------------------------------------------------------
151{
152 fT0s.clear();
153 for (UInt_t i=0; i<fAddT0s.size(); i++)
154 fAddT0s[i].clear();
155 fAddT0s.clear();
156
157 fFuncValues.clear();
158}
159
160//--------------------------------------------------------------------------
161// DeadTimeCorrection (protected)
162//--------------------------------------------------------------------------
169void PRunBase::DeadTimeCorrection(std::vector<PDoubleVector> &histos, PUIntVector &histoNo)
170{
171 PRawRunData* runData = fRawData->GetRunData(*fRunInfo->GetRunName());
172
173 if (runData == nullptr) {
174 std::cerr << std::endl << "**WARNING** PRunBase::DeadTimeCorrection: couldn't get data from '" << *fRunInfo->GetRunName() << "'" << std::endl;
175 return;
176 }
177
178 bool checkDeadTime{false};
179 PMsrRunList *rl = fMsrInfo->GetMsrRunList();
180 for (unsigned int i=0; i<rl->size(); i++) {
181 if (!rl->at(i).GetFileFormat()->CompareTo("nexus", TString::kIgnoreCase)) {
182 checkDeadTime = true;
183 break;
184 }
185 }
186
187 if (!runData->DeadTimeCorrectionReady() && checkDeadTime) {
188 std::cerr << std::endl << "**WARNING** PRunBase::DeadTimeCorrection: missing input for dead time correction" << std::endl;
189 return;
190 }
191
192
193 // check if a dead time correction has to be done
194 // first check the global block
195 TString dtcg = fMsrInfo->GetMsrGlobal()->GetDeadTimeCorrection();
196 Int_t dtcg_tag = 0; // 0=no, 1=file, 2=estimate
197 if (dtcg.Contains("file", TString::kIgnoreCase)) {
198 dtcg_tag = 1;
199 } else if (dtcg.Contains("estimate", TString::kIgnoreCase)) {
200 dtcg_tag = 2;
201 }
202 // now check each run
203 TString dtcr{"no"};
204 Bool_t needToCheck{true};
205 for (UInt_t i=0; i<histoNo.size(); i++) {
206 dtcr = fRunInfo->GetDeadTimeCorrection();
207 if (dtcr.Contains("file", TString::kIgnoreCase) || (dtcg_tag != 0)) {
208 if (runData->DeadTimeCorrectionReady()) {
209 needToCheck = false;
210 // Dead time correction: n_true = n_obs / (1 - n_obs * t_dt / (good_frames * dt))
211 Double_t n_true;
212 Int_t gf = runData->GetNumberOfGoodFrames();
213 Double_t t_dt = runData->GetDeadTimeParam(histoNo[i]);
214 for (UInt_t j=0; j<histos[i].size(); j++) {
215 n_true = histos[i][j] / (1.0 - histos[i][j] * t_dt / (gf * fTimeResolution));
216 histos[i][j] = n_true;
217 }
218 }
219 }
220 if ((dtcr.Contains("estimate", TString::kIgnoreCase) || (dtcg_tag != 0)) && needToCheck) {
221 std::cerr << std::endl << "**INFO** deadtime correction estimate not yet implemented." << std::endl;
222 }
223 }
224}
225
226//--------------------------------------------------------------------------
227// SetFitRange (public)
228//--------------------------------------------------------------------------
251{
252 Double_t start=0.0, end=0.0;
253
254 assert(fitRange.size()); // make sure fitRange is not empty
255
256 if (fitRange.size()==1) { // one fit range for all
257 start = fitRange[0].first;
258 end = fitRange[0].second;
259 } else {
260 // check that fRunNo is found within fitRange
261 UInt_t idx=static_cast<UInt_t>(fRunNo);
262 if (idx < fitRange.size()) { // fRunNo present
263 start = fitRange[idx].first;
264 end = fitRange[idx].second;
265 } else { // fRunNo NOT present
266 std::cerr << std::endl << ">> PRunBase::SetFitRange(): **ERROR** msr-file run entry " << fRunNo << " not present in fit range vector.";
267 std::cerr << std::endl << ">> Will not do anything! Please check, this shouldn't happen." << std::endl;
268 return;
269 }
270 }
271
272 // check that start is before end
273 if (start > end) {
274 std::cerr << std::endl << ">> PRunBase::SetFitRange(): **WARNING** start=" << start << " is > as end=" << end;
275 std::cerr << std::endl << ">> Will swap them, since otherwise chisq/logLH == 0.0" << std::endl;
276 fFitStartTime = end;
277 fFitEndTime = start;
278 } else {
279 fFitStartTime = start;
280 fFitEndTime = end;
281 }
282}
283
284//--------------------------------------------------------------------------
285// CleanUp (public)
286//--------------------------------------------------------------------------
297{
298 fTheory.reset();
299}
300
301//--------------------------------------------------------------------------
302// CalculateKaiserFilterCoeff (protected)
303//--------------------------------------------------------------------------
337void PRunBase::CalculateKaiserFilterCoeff(Double_t wc, Double_t A, Double_t dw)
338{
339 Double_t beta;
340 Double_t dt = fData.GetTheoryTimeStep();
341 Int_t m;
342
343 // estimate beta (see reference above, p.574ff)
344 if (A > 50.0) {
345 beta = 0.1102*(A-8.7);
346 } else if ((A >= 21.0) && (A <= 50.0)) {
347 beta = 0.5842*TMath::Power(A-21.0, 0.4) + 0.07886*(A-21.0);
348 } else {
349 beta = 0.0;
350 }
351
352 m = TMath::FloorNint((A-8.0)/(2.285*dw*TMath::Pi()));
353 // make sure m is odd
354 if (m % 2 == 0)
355 m++;
356
357 Double_t alpha = static_cast<Double_t>(m)/2.0;
358 Double_t dval;
359 Double_t dsum = 0.0;
360 for (Int_t i=0; i<=m; i++) {
361 dval = TMath::Sin(wc*(i-alpha)*dt)/(TMath::Pi()*(i-alpha)*dt);
362 dval *= TMath::BesselI0(beta*TMath::Sqrt(1.0-TMath::Power((i-alpha)*dt/alpha, 2.0)))/TMath::BesselI0(beta);
363 dsum += dval;
364 fKaiserFilter.push_back(dval);
365 }
366 for (UInt_t i=0; i<=static_cast<UInt_t>(m); i++) {
367 fKaiserFilter[i] /= dsum;
368 }
369}
370
371//--------------------------------------------------------------------------
372// FilterTheo (protected)
373//--------------------------------------------------------------------------
404{
405 PDoubleVector theoFiltered;
406 Double_t dval = 0.0;
407 const PDoubleVector *theo = fData.GetTheory();
408
409 for (UInt_t i=0; i<theo->size(); i++) {
410 for (UInt_t j=0; j<fKaiserFilter.size(); j++) {
411 if (i<j)
412 dval = 0.0;
413 else
414 dval += fKaiserFilter[j]*theo->at(i-j);
415 }
416 theoFiltered.push_back(dval);
417 dval = 0.0;
418 }
419
420 fData.ReplaceTheory(theoFiltered);
421
422 // shift time start by half the filter length
423 dval = fData.GetTheoryTimeStart() - 0.5*static_cast<Double_t>(fKaiserFilter.size())*fData.GetTheoryTimeStep();
424 fData.SetTheoryTimeStart(dval);
425
426 theoFiltered.clear();
427}
std::vector< UInt_t > PUIntVector
Definition PMusr.h:375
EPMusrHandleTag
Definition PMusr.h:427
@ kEmpty
No operation active.
Definition PMusr.h:428
std::vector< PMsrRunBlock > PMsrRunList
Definition PMusr.h:1263
#define PMUSR_UNDEFINED
Definition PMusr.h:177
std::vector< PDoublePair > PDoublePairVector
Definition PMusr.h:411
std::vector< Double_t > PDoubleVector
Definition PMusr.h:399
if(xmlFile.is_open())
MSR file parser and manager for the musrfit framework.
virtual const Int_t GetNumberOfGoodFrames()
Definition PMusr.h:890
virtual const Bool_t DeadTimeCorrectionReady()
Definition PMusr.cpp:768
virtual const std::vector< float > GetDeadTimeParam()
Definition PMusr.h:891
virtual void CleanUp()
Cleans up internal data structures.
Definition PRunBase.cpp:296
Double_t fTimeResolution
Time resolution of raw histogram data in microseconds (μs), e.g., 0.01953125 μs for PSI GPS.
Definition PRunBase.h:276
virtual ~PRunBase()
Virtual destructor.
Definition PRunBase.cpp:150
Bool_t fValid
Flag indicating if run object initialized successfully; false if any error occurred.
Definition PRunBase.h:266
Double_t fFitEndTime
Fit range end time in microseconds (μs) relative to t0.
Definition PRunBase.h:282
PDoubleVector fFuncValues
Cached values of user-defined functions from FUNCTIONS block, evaluated at current parameters.
Definition PRunBase.h:284
PDoubleVector fKaiserFilter
Kaiser window FIR filter coefficients for smoothing RRF theory curves.
Definition PRunBase.h:287
virtual void SetFitRange(PDoublePairVector fitRange)
Sets the fit time range for this run.
Definition PRunBase.cpp:250
PMsrHandler * fMsrInfo
Pointer to MSR file handler (owned externally, not deleted here)
Definition PRunBase.h:271
virtual void DeadTimeCorrection(std::vector< PDoubleVector > &histos, PUIntVector &histoNo)
carry out dead time correction
Definition PRunBase.cpp:169
PMetaData fMetaData
Experimental metadata extracted from data file header (magnetic field, temperature,...
Definition PRunBase.h:277
std::unique_ptr< PTheory > fTheory
Theory function evaluator (smart pointer, automatically deleted)
Definition PRunBase.h:285
std::vector< PDoubleVector > fAddT0s
Time-zero bin values for additional runs to be added to main run.
Definition PRunBase.h:279
EPMusrHandleTag fHandleTag
Operation mode: kFit (fitting), kView (display only), kEmpty (uninitialized)
Definition PRunBase.h:268
virtual void CalculateKaiserFilterCoeff(Double_t wc, Double_t A, Double_t dw)
Calculates Kaiser window FIR filter coefficients for RRF smoothing.
Definition PRunBase.cpp:337
PRunData fData
Processed data container: background-corrected, packed, with theory values.
Definition PRunBase.h:275
PRunDataHandler * fRawData
Pointer to raw data handler (owned externally, not deleted here)
Definition PRunBase.h:273
PDoubleVector fT0s
Time-zero bin values for all histograms in this run (forward, backward, etc.)
Definition PRunBase.h:278
PRunBase()
Default constructor.
Definition PRunBase.cpp:52
Int_t fRunNo
Run number (0-based index in MSR file RUN blocks)
Definition PRunBase.h:270
PMsrRunBlock * fRunInfo
Pointer to this run's RUN block settings within fMsrInfo.
Definition PRunBase.h:272
Double_t fFitStartTime
Fit range start time in microseconds (μs) relative to t0.
Definition PRunBase.h:281
virtual void FilterTheo()
Applies Kaiser FIR filter to theory values for RRF fits.
Definition PRunBase.cpp:403
Raw data file reader and format converter for μSR data.