44#include "PStringUtils.h"
134 std::cerr << std::endl <<
">> PRunMuMinus::PRunMuMinus: **SEVERE ERROR**: Couldn't find any packing information!";
135 std::cerr << std::endl <<
">> This is very bad :-(, will quit ...";
136 std::cerr << std::endl;
150 std::cerr << std::endl <<
">> PRunMuMinus::PRunMuMinus: **SEVERE ERROR**: Couldn't prepare data for fitting!";
151 std::cerr << std::endl <<
">> This is very bad :-(, will quit ...";
152 std::cerr << std::endl;
210 Double_t chisq = 0.0;
214 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
215 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
233 #pragma omp parallel for default(shared) private(i,time,diff) schedule(dynamic,chunk) reduction(+:chisq)
236 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
238 chisq += diff*diff / (
fData.GetError()->at(i)*
fData.GetError()->at(i));
278 Double_t chisq = 0.0;
283 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
284 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
302 #pragma omp parallel for default(shared) private(i,time,diff) schedule(dynamic,chunk) reduction(+:chisq)
305 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
307 diff =
fData.GetValue()->at(i) - theo;
308 chisq += diff*diff / theo;
366 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
367 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
387 #pragma omp parallel for default(shared) private(i,time,theo,data) schedule(dynamic,chunk) reduction(+:mllh)
390 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
394 data =
fData.GetValue()->at(i);
397 std::cerr <<
">> PRunMuMinus::CalcMaxLikelihood: **WARNING** NEGATIVE theory!!" << std::endl;
402 mllh += (theo-data) + data*log(data/theo);
456 std::vector<std::string> tok = PStringUtils::Split(fitRange.Data(),
" \t");
458 if (tok.size() == 3) {
462 idx = str.First(
"+");
464 str.Remove(0, idx+1);
473 idx = str.First(
"-");
475 str.Remove(0, idx+1);
480 }
else if ((tok.size() > 3) && (tok.size() % 2 == 1)) {
481 UInt_t pos = 2*(
fRunNo+1)-1;
483 if (pos + 1 >= tok.size()) {
484 std::cerr << std::endl <<
">> PRunMuMinus::SetFitRangeBin(): **ERROR** invalid FIT_RANGE command found: '" << fitRange <<
"'";
485 std::cerr << std::endl <<
">> will ignore it. Sorry ..." << std::endl;
490 idx = str.First(
"+");
492 str.Remove(0, idx+1);
501 idx = str.First(
"-");
503 str.Remove(0, idx+1);
510 std::cerr << std::endl <<
">> PRunMuMinus::SetFitRangeBin(): **ERROR** invalid FIT_RANGE command found: '" << fitRange <<
"'";
511 std::cerr << std::endl <<
">> will ignore it. Sorry ..." << std::endl;
546 std::vector<Double_t> par;
548 for (UInt_t i=0; i<paramList->size(); i++)
549 par.push_back((*paramList)[i].fValue);
552 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
557 UInt_t size =
fData.GetValue()->size();
558 Double_t start =
fData.GetDataTimeStart();
559 Double_t resolution =
fData.GetDataTimeStep();
561 for (UInt_t i=0; i<size; i++) {
562 time = start +
static_cast<Double_t
>(i)*resolution;
588 Bool_t success =
true;
599 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **ERROR** Couldn't get run " <<
fRunInfo->GetRunName()->Data() <<
"!";
600 std::cerr << std::endl;
616 for (UInt_t i=0; i<
fRunInfo->GetForwardHistoNoSize(); i++) {
617 histoNo.push_back(
fRunInfo->GetForwardHistoNo(i));
620 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **PANIC ERROR**:";
621 std::cerr << std::endl <<
">> histoNo found = " << histoNo[i] <<
", which is NOT present in the data file!?!?";
622 std::cerr << std::endl <<
">> Will quit :-(";
623 std::cerr << std::endl;
631 std::cout.precision(10);
632 std::cout << std::endl <<
">> PRunMuMinus::PrepareData(): time resolution=" << std::fixed << runData->
GetTimeResolution() <<
"(ns)" << std::endl;
640 std::vector<PDoubleVector> forward;
641 forward.resize(histoNo.size());
642 for (UInt_t i=0; i<histoNo.size(); i++) {
643 forward[i].resize(runData->
GetDataBin(histoNo[i])->size());
644 forward[i] = *runData->
GetDataBin(histoNo[i]);
648 if (
fRunInfo->GetRunNameSize() > 1) {
650 for (UInt_t i=1; i<
fRunInfo->GetRunNameSize(); i++) {
654 if (addRunData ==
nullptr) {
655 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **ERROR** Couldn't get addrun " <<
fRunInfo->GetRunName(i)->Data() <<
"!";
656 std::cerr << std::endl;
662 for (UInt_t k=0; k<histoNo.size(); k++) {
663 addRunSize = addRunData->
GetDataBin(histoNo[k])->size();
664 for (UInt_t j=0; j<addRunData->
GetDataBin(histoNo[k])->size(); j++) {
666 if ((
static_cast<Int_t
>(j)+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]) >= 0) &&
667 (j+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]) < addRunSize)) {
668 forward[k][j] += addRunData->
GetDataBin(histoNo[k])->at(j+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]));
677 for (UInt_t i=0; i<
fForward.size(); i++) {
682 for (UInt_t i=1; i<histoNo.size(); i++) {
683 for (UInt_t j=0; j<runData->
GetDataBin(histoNo[i])->size(); j++) {
686 fForward[j] += forward[i][j+
static_cast<Int_t
>(
fT0s[i])-
static_cast<Int_t
>(
fT0s[0])];
736 Int_t t0 =
static_cast<Int_t
>(
fT0s[0]);
737 Double_t value = 0.0;
745 fData.AppendValue(value);
747 fData.AppendErrorValue(1.0);
749 fData.AppendErrorValue(TMath::Sqrt(value));
752 fData.AppendValue(value);
754 fData.AppendErrorValue(1.0);
756 fData.AppendErrorValue(TMath::Sqrt(value));
792 if (
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking > 0) {
793 packing =
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking;
797 Double_t theoryNorm = 1.0;
798 if (
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking > 0) {
799 theoryNorm =
static_cast<Double_t
>(
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking)/
static_cast<Double_t
>(
fPacking);
806 Int_t end = start + ((
fForward.size()-start)/packing)*packing;
810 start = (
static_cast<Int_t
>(
fT0s[0])+offset) - ((
static_cast<Int_t
>(
fT0s[0])+offset)/packing)*packing;
811 end = start + ((
fForward.size()-start)/packing)*packing;
812 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **WARNING** data range was not provided, will try data range start = " << start <<
".";
813 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
814 std::cerr << std::endl;
824 if ((start < 0) || (start >
static_cast<Int_t
>(
fForward.size()))) {
825 std::cerr << std::endl <<
">> PRunMuMinus::PrepareRawViewData(): **ERROR** start data bin doesn't make any sense!";
826 std::cerr << std::endl;
830 if ((end < 0) || (end >
static_cast<Int_t
>(
fForward.size()))) {
831 std::cerr << std::endl <<
">> PRunMuMinus::PrepareRawViewData(): **ERROR** end data bin doesn't make any sense!";
832 std::cerr << std::endl;
843 Int_t t0 =
static_cast<Int_t
>(
fT0s[0]);
844 Double_t value = 0.0;
846 fData.SetDataTimeStart(
fTimeResolution*((
static_cast<Double_t
>(start)-0.5) +
static_cast<Double_t
>(packing)/2.0 -
static_cast<Double_t
>(t0)));
849 for (Int_t i=start; i<end; i++) {
850 if (((i-start) % packing == 0) && (i != start)) {
851 fData.AppendValue(value);
853 fData.AppendErrorValue(1.0);
855 fData.AppendErrorValue(TMath::Sqrt(value));
866 std::vector<Double_t> par;
868 for (UInt_t i=0; i<paramList->size(); i++)
869 par.push_back((*paramList)[i].fValue);
872 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
891 fData.SetTheoryTimeStart(
fData.GetDataTimeStart());
893 fData.SetTheoryTimeStep(
fData.GetDataTimeStep());
897 fData.SetTheoryTimeStep(
fData.GetDataTimeStep()/(Double_t)factor);
901 Double_t theoryValue;
902 for (UInt_t i=0; i<size; i++) {
903 time =
fData.GetTheoryTimeStart() + i*
fData.GetTheoryTimeStep();
905 if (fabs(theoryValue) > 1.0e10) {
908 fData.AppendTheoryValue(theoryNorm*theoryValue);
940 fT0s.resize(histoNo.size());
941 for (UInt_t i=0; i<
fT0s.size(); i++) {
946 for (UInt_t i=0; i<
fRunInfo->GetT0BinSize(); i++) {
952 if (
fT0s[i] == -1.0) {
958 for (UInt_t i=0; i<histoNo.size(); i++) {
959 if (
fT0s[i] == -1.0) {
960 if (runData->
GetT0Bin(histoNo[i]) > 0.0) {
968 for (UInt_t i=0; i<histoNo.size(); i++) {
969 if (
fT0s[i] == -1.0) {
973 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **WARRNING** NO t0's found, neither in the run data nor in the msr-file!";
974 std::cerr << std::endl <<
">> run: " <<
fRunInfo->GetRunName()->Data();
975 std::cerr << std::endl <<
">> will try the estimated one: forward t0 = " << runData->
GetT0BinEstimated(histoNo[i]);
976 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS OK!! For instance for LEM this is almost for sure rubbish!";
977 std::cerr << std::endl;
982 for (UInt_t i=0; i<
fRunInfo->GetForwardHistoNoSize(); i++) {
983 if ((
fT0s[i] < 0) || (
fT0s[i] >
static_cast<Int_t
>(runData->
GetDataBin(histoNo[i])->size()))) {
984 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** t0 data bin (" <<
fT0s[i] <<
") doesn't make any sense!";
985 std::cerr << std::endl;
991 if (
fRunInfo->GetRunNameSize() > 1) {
994 for (UInt_t i=1; i<
fRunInfo->GetRunNameSize(); i++) {
998 if (addRunData ==
nullptr) {
999 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** Couldn't get addrun " <<
fRunInfo->GetRunName(i)->Data() <<
"!";
1000 std::cerr << std::endl;
1006 fAddT0s[i-1].resize(histoNo.size());
1007 for (UInt_t j=0; j<
fAddT0s[i-1].size(); j++) {
1012 for (UInt_t j=0; j<
fRunInfo->GetT0BinSize(); j++) {
1017 for (UInt_t j=0; j<histoNo.size(); j++) {
1019 if (addRunData->
GetT0Bin(histoNo[j]) > 0.0) {
1026 for (UInt_t j=0; j<histoNo.size(); j++) {
1027 if (
fAddT0s[i-1][j] == -1.0) {
1031 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **WARRNING** NO t0's found, neither in the run data nor in the msr-file!";
1032 std::cerr << std::endl <<
">> run: " <<
fRunInfo->GetRunName(i)->Data();
1033 std::cerr << std::endl <<
">> will try the estimated one: forward t0 = " << addRunData->
GetT0BinEstimated(histoNo[j]);
1034 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS OK!! For instance for LEM this is almost for sure rubbish!";
1035 std::cerr << std::endl;
1040 for (UInt_t j=0; j<
fRunInfo->GetForwardHistoNoSize(); j++) {
1042 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** addt0 data bin (" <<
fAddT0s[i-1][j] <<
") doesn't make any sense!";
1043 std::cerr << std::endl;
1076 start =
fMsrInfo->GetMsrGlobal()->GetDataRange(0);
1079 end =
fMsrInfo->GetMsrGlobal()->GetDataRange(1);
1085 start =
static_cast<Int_t
>(
fT0s[0])+offset;
1087 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **WARNING** data range was not provided, will try data range start = t0+" << offset <<
"(=10ns) = " << start <<
".";
1088 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
1089 std::cerr << std::endl;
1094 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **WARNING** data range was not provided, will try data range end = " << end <<
".";
1095 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
1096 std::cerr << std::endl;
1107 if ((start < 0) || (start >
static_cast<Int_t
>(
fForward.size()))) {
1108 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **ERROR** start data bin (" << start <<
") doesn't make any sense!";
1109 std::cerr << std::endl;
1113 if ((end < 0) || (end >
static_cast<Int_t
>(
fForward.size()))) {
1114 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **ERROR** end data bin (" << end <<
") doesn't make any sense!";
1115 std::cerr << std::endl;
1169 std::cerr <<
">> PRunMuMinus::GetProperFitRange(): **WARNING** Couldn't get fit start/end time!" << std::endl;
1170 std::cerr <<
">> Will set it to fgb/lgb which given in time is: " <<
fFitStartTime <<
"..." <<
fFitEndTime <<
" (usec)" << std::endl;
std::vector< UInt_t > PUIntVector
@ kEmpty
No operation active.
@ kFit
Fitting mode - perform least-squares fit to data.
@ kView
Viewing mode - display data and theory without fitting.
std::vector< PMsrParamStructure > PMsrParamList
virtual void SetFitRange(Double_t dval, UInt_t idx)
virtual Double_t GetFitRange(UInt_t idx)
virtual UInt_t GetT0BinSize()
virtual Bool_t IsFitRangeInBin()
virtual Int_t GetFitRangeOffset(UInt_t idx)
virtual Double_t GetT0Bin(UInt_t idx=0)
MSR file parser and manager for the musrfit framework.
virtual const PDoubleVector * GetDataBin(const UInt_t histoNo)
virtual const Double_t GetT0Bin(const UInt_t histoNo)
virtual const Double_t GetTimeResolution()
virtual const Bool_t IsPresent(UInt_t histoNo)
virtual const UInt_t GetNoOfTemperatures()
virtual const Double_t GetField()
virtual const Double_t GetEnergy()
virtual const Double_t GetT0BinEstimated(const UInt_t histoNo)
virtual const PDoublePairVector * GetTemperature() const
Double_t fTimeResolution
Time resolution of raw histogram data in microseconds (μs), e.g., 0.01953125 μs for PSI GPS.
Bool_t fValid
Flag indicating if run object initialized successfully; false if any error occurred.
Double_t fFitEndTime
Fit range end time in microseconds (μs) relative to t0.
PDoubleVector fFuncValues
Cached values of user-defined functions from FUNCTIONS block, evaluated at current parameters.
PMsrHandler * fMsrInfo
Pointer to MSR file handler (owned externally, not deleted here)
PMetaData fMetaData
Experimental metadata extracted from data file header (magnetic field, temperature,...
std::unique_ptr< PTheory > fTheory
Theory function evaluator (smart pointer, automatically deleted)
std::vector< PDoubleVector > fAddT0s
Time-zero bin values for additional runs to be added to main run.
EPMusrHandleTag fHandleTag
Operation mode: kFit (fitting), kView (display only), kEmpty (uninitialized)
PRunData fData
Processed data container: background-corrected, packed, with theory values.
PRunDataHandler * fRawData
Pointer to raw data handler (owned externally, not deleted here)
PDoubleVector fT0s
Time-zero bin values for all histograms in this run (forward, backward, etc.)
PRunBase()
Default constructor.
Int_t fRunNo
Run number (0-based index in MSR file RUN blocks)
PMsrRunBlock * fRunInfo
Pointer to this run's RUN block settings within fMsrInfo.
Double_t fFitStartTime
Fit range start time in microseconds (μs) relative to t0.
Raw data file reader and format converter for μSR data.
virtual ~PRunMuMinus()
Virtual destructor cleaning up allocated resources.
Bool_t fTheoAsData
Theory calculation mode flag.
PRunMuMinus()
Default constructor creating an empty, invalid μ⁻ run object.
Int_t fPacking
Bin packing factor (REQUIRED for μ⁻).
Int_t fGoodBins[2]
Good bin markers for bin-based fit range specification.
virtual Bool_t PrepareData()
Main data preparation routine for μ⁻ fitting and viewing.
virtual Double_t CalcChiSquareExpected(const std::vector< Double_t > &par)
Calculates expected χ² based on theory predictions (statistical diagnostic).
virtual Bool_t GetProperDataRange()
Determines data range (region of valid histogram data).
virtual void SetFitRangeBin(const TString fitRange)
Sets fit range using bin-offset specification (COMMANDS block syntax).
virtual void GetProperFitRange(PMsrGlobalBlock *globalBlock)
Determines fit range from MSR file settings.
virtual UInt_t GetNoOfFitBins()
Returns the number of bins included in the fit range.
virtual void CalcTheory()
Evaluates theory function at all data points (or high-resolution grid).
PDoubleVector fForward
Forward detector histogram data (background-corrected, packed).
virtual Bool_t GetProperT0(PRawRunData *runData, PMsrGlobalBlock *globalBlock, PUIntVector &histoNo)
Determines and validates t0 values for μ⁻ histogram.
Int_t fEndTimeBin
Last bin index in fit range (exclusive: loop as i < fEndTimeBin)
Int_t fStartTimeBin
First bin index in fit range (inclusive, 0-based after packing)
virtual Bool_t PrepareFitData(PRawRunData *runData, const UInt_t histoNo)
Prepares μ⁻ histogram data for fitting.
virtual Double_t CalcChiSquare(const std::vector< Double_t > &par)
Calculates χ² between μ⁻ data and theory (least-squares fit metric).
virtual void CalcNoOfFitBins()
Calculates start/end bin indices from fit time range.
virtual Double_t CalcMaxLikelihood(const std::vector< Double_t > &par)
Calculates negative log-likelihood for Poisson statistics (low-count fit metric).
virtual Bool_t PrepareRawViewData(PRawRunData *runData, const UInt_t histoNo)
Prepares μ⁻ histogram data for viewing/plotting (minimal processing).
UInt_t fNoOfFitBins
Number of bins within fit range (between fStartTimeBin and fEndTimeBin)