42#include <TObjString.h>
133 std::cerr << std::endl <<
">> PRunMuMinus::PRunMuMinus: **SEVERE ERROR**: Couldn't find any packing information!";
134 std::cerr << std::endl <<
">> This is very bad :-(, will quit ...";
135 std::cerr << std::endl;
149 std::cerr << std::endl <<
">> PRunMuMinus::PRunMuMinus: **SEVERE ERROR**: Couldn't prepare data for fitting!";
150 std::cerr << std::endl <<
">> This is very bad :-(, will quit ...";
151 std::cerr << std::endl;
209 Double_t chisq = 0.0;
213 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
214 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
232 #pragma omp parallel for default(shared) private(i,time,diff) schedule(dynamic,chunk) reduction(+:chisq)
235 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
237 chisq += diff*diff / (
fData.GetError()->at(i)*
fData.GetError()->at(i));
277 Double_t chisq = 0.0;
282 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
283 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
301 #pragma omp parallel for default(shared) private(i,time,diff) schedule(dynamic,chunk) reduction(+:chisq)
304 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
306 diff =
fData.GetValue()->at(i) - theo;
307 chisq += diff*diff / theo;
365 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
366 Int_t funcNo =
fMsrInfo->GetFuncNo(i);
386 #pragma omp parallel for default(shared) private(i,time,theo,data) schedule(dynamic,chunk) reduction(-:mllh)
389 time =
fData.GetDataTimeStart() +
static_cast<Double_t
>(i)*
fData.GetDataTimeStep();
393 data =
fData.GetValue()->at(i);
396 std::cerr <<
">> PRunMuMinus::CalcMaxLikelihood: **WARNING** NEGATIVE theory!!" << std::endl;
401 mllh += (theo-data) + data*log(data/theo);
451 TObjArray *tok =
nullptr;
452 TObjString *ostr =
nullptr;
457 tok = fitRange.Tokenize(
" \t");
459 if (tok->GetEntries() == 3) {
461 ostr =
dynamic_cast<TObjString*
>(tok->At(1));
462 str = ostr->GetString();
464 idx = str.First(
"+");
466 str.Remove(0, idx+1);
473 ostr =
dynamic_cast<TObjString*
>(tok->At(2));
474 str = ostr->GetString();
476 idx = str.First(
"-");
478 str.Remove(0, idx+1);
483 }
else if ((tok->GetEntries() > 3) && (tok->GetEntries() % 2 == 1)) {
484 Int_t pos = 2*(
fRunNo+1)-1;
486 if (pos + 1 >= tok->GetEntries()) {
487 std::cerr << std::endl <<
">> PRunMuMinus::SetFitRangeBin(): **ERROR** invalid FIT_RANGE command found: '" << fitRange <<
"'";
488 std::cerr << std::endl <<
">> will ignore it. Sorry ..." << std::endl;
491 ostr =
dynamic_cast<TObjString*
>(tok->At(pos));
492 str = ostr->GetString();
494 idx = str.First(
"+");
496 str.Remove(0, idx+1);
503 ostr =
dynamic_cast<TObjString*
>(tok->At(pos+1));
504 str = ostr->GetString();
506 idx = str.First(
"-");
508 str.Remove(0, idx+1);
515 std::cerr << std::endl <<
">> PRunMuMinus::SetFitRangeBin(): **ERROR** invalid FIT_RANGE command found: '" << fitRange <<
"'";
516 std::cerr << std::endl <<
">> will ignore it. Sorry ..." << std::endl;
556 std::vector<Double_t> par;
558 for (UInt_t i=0; i<paramList->size(); i++)
559 par.push_back((*paramList)[i].fValue);
562 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
567 UInt_t size =
fData.GetValue()->size();
568 Double_t start =
fData.GetDataTimeStart();
569 Double_t resolution =
fData.GetDataTimeStep();
571 for (UInt_t i=0; i<size; i++) {
572 time = start +
static_cast<Double_t
>(i)*resolution;
598 Bool_t success =
true;
609 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **ERROR** Couldn't get run " <<
fRunInfo->GetRunName()->Data() <<
"!";
610 std::cerr << std::endl;
626 for (UInt_t i=0; i<
fRunInfo->GetForwardHistoNoSize(); i++) {
627 histoNo.push_back(
fRunInfo->GetForwardHistoNo(i));
630 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **PANIC ERROR**:";
631 std::cerr << std::endl <<
">> histoNo found = " << histoNo[i] <<
", which is NOT present in the data file!?!?";
632 std::cerr << std::endl <<
">> Will quit :-(";
633 std::cerr << std::endl;
641 std::cout.precision(10);
642 std::cout << std::endl <<
">> PRunMuMinus::PrepareData(): time resolution=" << std::fixed << runData->
GetTimeResolution() <<
"(ns)" << std::endl;
650 std::vector<PDoubleVector> forward;
651 forward.resize(histoNo.size());
652 for (UInt_t i=0; i<histoNo.size(); i++) {
653 forward[i].resize(runData->
GetDataBin(histoNo[i])->size());
654 forward[i] = *runData->
GetDataBin(histoNo[i]);
658 if (
fRunInfo->GetRunNameSize() > 1) {
660 for (UInt_t i=1; i<
fRunInfo->GetRunNameSize(); i++) {
664 if (addRunData ==
nullptr) {
665 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **ERROR** Couldn't get addrun " <<
fRunInfo->GetRunName(i)->Data() <<
"!";
666 std::cerr << std::endl;
672 for (UInt_t k=0; k<histoNo.size(); k++) {
673 addRunSize = addRunData->
GetDataBin(histoNo[k])->size();
674 for (UInt_t j=0; j<addRunData->
GetDataBin(histoNo[k])->size(); j++) {
676 if ((
static_cast<Int_t
>(j)+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]) >= 0) &&
677 (j+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]) < addRunSize)) {
678 forward[k][j] += addRunData->
GetDataBin(histoNo[k])->at(j+
static_cast<Int_t
>(
fAddT0s[i-1][k])-
static_cast<Int_t
>(
fT0s[k]));
687 for (UInt_t i=0; i<
fForward.size(); i++) {
692 for (UInt_t i=1; i<histoNo.size(); i++) {
693 for (UInt_t j=0; j<runData->
GetDataBin(histoNo[i])->size(); j++) {
696 fForward[j] += forward[i][j+
static_cast<Int_t
>(
fT0s[i])-
static_cast<Int_t
>(
fT0s[0])];
746 Int_t t0 =
static_cast<Int_t
>(
fT0s[0]);
747 Double_t value = 0.0;
755 fData.AppendValue(value);
757 fData.AppendErrorValue(1.0);
759 fData.AppendErrorValue(TMath::Sqrt(value));
762 fData.AppendValue(value);
764 fData.AppendErrorValue(1.0);
766 fData.AppendErrorValue(TMath::Sqrt(value));
802 if (
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking > 0) {
803 packing =
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking;
807 Double_t theoryNorm = 1.0;
808 if (
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking > 0) {
809 theoryNorm =
static_cast<Double_t
>(
fMsrInfo->GetMsrPlotList()->at(0).fViewPacking)/
static_cast<Double_t
>(
fPacking);
816 Int_t end = start + ((
fForward.size()-start)/packing)*packing;
820 start = (
static_cast<Int_t
>(
fT0s[0])+offset) - ((
static_cast<Int_t
>(
fT0s[0])+offset)/packing)*packing;
821 end = start + ((
fForward.size()-start)/packing)*packing;
822 std::cerr << std::endl <<
">> PRunMuMinus::PrepareData(): **WARNING** data range was not provided, will try data range start = " << start <<
".";
823 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
824 std::cerr << std::endl;
834 if ((start < 0) || (start >
static_cast<Int_t
>(
fForward.size()))) {
835 std::cerr << std::endl <<
">> PRunMuMinus::PrepareRawViewData(): **ERROR** start data bin doesn't make any sense!";
836 std::cerr << std::endl;
840 if ((end < 0) || (end >
static_cast<Int_t
>(
fForward.size()))) {
841 std::cerr << std::endl <<
">> PRunMuMinus::PrepareRawViewData(): **ERROR** end data bin doesn't make any sense!";
842 std::cerr << std::endl;
853 Int_t t0 =
static_cast<Int_t
>(
fT0s[0]);
854 Double_t value = 0.0;
856 fData.SetDataTimeStart(
fTimeResolution*((
static_cast<Double_t
>(start)-0.5) +
static_cast<Double_t
>(packing)/2.0 -
static_cast<Double_t
>(t0)));
859 for (Int_t i=start; i<end; i++) {
860 if (((i-start) % packing == 0) && (i != start)) {
861 fData.AppendValue(value);
863 fData.AppendErrorValue(1.0);
865 fData.AppendErrorValue(TMath::Sqrt(value));
876 std::vector<Double_t> par;
878 for (UInt_t i=0; i<paramList->size(); i++)
879 par.push_back((*paramList)[i].fValue);
882 for (Int_t i=0; i<
fMsrInfo->GetNoOfFuncs(); i++) {
901 fData.SetTheoryTimeStart(
fData.GetDataTimeStart());
903 fData.SetTheoryTimeStep(
fData.GetDataTimeStep());
907 fData.SetTheoryTimeStep(
fData.GetDataTimeStep()/(Double_t)factor);
911 Double_t theoryValue;
912 for (UInt_t i=0; i<size; i++) {
913 time =
fData.GetTheoryTimeStart() + i*
fData.GetTheoryTimeStep();
915 if (fabs(theoryValue) > 1.0e10) {
918 fData.AppendTheoryValue(theoryNorm*theoryValue);
950 fT0s.resize(histoNo.size());
951 for (UInt_t i=0; i<
fT0s.size(); i++) {
956 for (UInt_t i=0; i<
fRunInfo->GetT0BinSize(); i++) {
962 if (
fT0s[i] == -1.0) {
968 for (UInt_t i=0; i<histoNo.size(); i++) {
969 if (
fT0s[i] == -1.0) {
970 if (runData->
GetT0Bin(histoNo[i]) > 0.0) {
978 for (UInt_t i=0; i<histoNo.size(); i++) {
979 if (
fT0s[i] == -1.0) {
983 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **WARRNING** NO t0's found, neither in the run data nor in the msr-file!";
984 std::cerr << std::endl <<
">> run: " <<
fRunInfo->GetRunName()->Data();
985 std::cerr << std::endl <<
">> will try the estimated one: forward t0 = " << runData->
GetT0BinEstimated(histoNo[i]);
986 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS OK!! For instance for LEM this is almost for sure rubbish!";
987 std::cerr << std::endl;
992 for (UInt_t i=0; i<
fRunInfo->GetForwardHistoNoSize(); i++) {
993 if ((
fT0s[i] < 0) || (
fT0s[i] >
static_cast<Int_t
>(runData->
GetDataBin(histoNo[i])->size()))) {
994 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** t0 data bin (" <<
fT0s[i] <<
") doesn't make any sense!";
995 std::cerr << std::endl;
1001 if (
fRunInfo->GetRunNameSize() > 1) {
1004 for (UInt_t i=1; i<
fRunInfo->GetRunNameSize(); i++) {
1008 if (addRunData ==
nullptr) {
1009 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** Couldn't get addrun " <<
fRunInfo->GetRunName(i)->Data() <<
"!";
1010 std::cerr << std::endl;
1016 fAddT0s[i-1].resize(histoNo.size());
1017 for (UInt_t j=0; j<
fAddT0s[i-1].size(); j++) {
1022 for (UInt_t j=0; j<
fRunInfo->GetT0BinSize(); j++) {
1027 for (UInt_t j=0; j<histoNo.size(); j++) {
1029 if (addRunData->
GetT0Bin(histoNo[j]) > 0.0) {
1036 for (UInt_t j=0; j<histoNo.size(); j++) {
1037 if (
fAddT0s[i-1][j] == -1.0) {
1041 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **WARRNING** NO t0's found, neither in the run data nor in the msr-file!";
1042 std::cerr << std::endl <<
">> run: " <<
fRunInfo->GetRunName(i)->Data();
1043 std::cerr << std::endl <<
">> will try the estimated one: forward t0 = " << addRunData->
GetT0BinEstimated(histoNo[j]);
1044 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS OK!! For instance for LEM this is almost for sure rubbish!";
1045 std::cerr << std::endl;
1050 for (UInt_t j=0; j<
fRunInfo->GetForwardHistoNoSize(); j++) {
1052 std::cerr << std::endl <<
">> PRunMuMinus::GetProperT0(): **ERROR** addt0 data bin (" <<
fAddT0s[i-1][j] <<
") doesn't make any sense!";
1053 std::cerr << std::endl;
1086 start =
fMsrInfo->GetMsrGlobal()->GetDataRange(0);
1089 end =
fMsrInfo->GetMsrGlobal()->GetDataRange(1);
1095 start =
static_cast<Int_t
>(
fT0s[0])+offset;
1097 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **WARNING** data range was not provided, will try data range start = t0+" << offset <<
"(=10ns) = " << start <<
".";
1098 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
1099 std::cerr << std::endl;
1104 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **WARNING** data range was not provided, will try data range end = " << end <<
".";
1105 std::cerr << std::endl <<
">> NO WARRANTY THAT THIS DOES MAKE ANY SENSE.";
1106 std::cerr << std::endl;
1117 if ((start < 0) || (start >
static_cast<Int_t
>(
fForward.size()))) {
1118 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **ERROR** start data bin (" << start <<
") doesn't make any sense!";
1119 std::cerr << std::endl;
1123 if ((end < 0) || (end >
static_cast<Int_t
>(
fForward.size()))) {
1124 std::cerr << std::endl <<
">> PRunMuMinus::GetProperDataRange(): **ERROR** end data bin (" << end <<
") doesn't make any sense!";
1125 std::cerr << std::endl;
1179 std::cerr <<
">> PRunMuMinus::GetProperFitRange(): **WARNING** Couldn't get fit start/end time!" << std::endl;
1180 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)