Page MenuHomeHEPForge

No OneTemporary

This file is larger than 256 KB, so syntax highlighting was skipped.
diff --git a/examples/SimFitCoordinator.cc b/examples/SimFitCoordinator.cc
index c930c12..ca60c52 100644
--- a/examples/SimFitCoordinator.cc
+++ b/examples/SimFitCoordinator.cc
@@ -1,91 +1,93 @@
/*
Copyright 2013 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include "TFile.h"
#include "TRandom.h"
#include "TString.h"
#include "TSystem.h"
#include "LauSimFitCoordinator.hh"
void usage( std::ostream& out, const TString& progName )
{
out<<"Usage:\n";
- out<<progName<<" <iFit> <nExpt> [firstExpt = 0] [numTasks = 2] [port = 0] [refFitDir] \n";
+ out<<progName<<" <iFit> <isData> <nExpt> [firstExpt = 0] [numTasks = 2] [port = 0] [refFitDir] \n";
}
int main(const int argc, const char ** argv)
{
- if ( argc < 3 ) {
+ if ( argc < 4 ) {
usage( std::cerr, argv[0] );
return EXIT_FAILURE;
}
UInt_t iFit = atoi( argv[1] );
- UInt_t nExpt = atoi( argv[2] );
+ Bool_t isToy = ! atoi( argv[2] );
+ UInt_t nExpt = atoi( argv[3] );
UInt_t firstExpt = 0;
UInt_t nTasks = 2;
UInt_t port = 0;
TString refDirName = "";
Bool_t useAsymmErrors = kFALSE;
Bool_t twoStageFit = kFALSE;
TString ntupleName = "";
- if ( argc > 3 ) {
- firstExpt = atoi( argv[3] );
+ if ( argc > 4 ) {
+ firstExpt = atoi( argv[4] );
- if ( argc > 4 ) {
- nTasks = atoi( argv[4] );
+ if ( argc > 5 ) {
+ nTasks = atoi( argv[5] );
- if ( argc > 5 ) {
- port = atoi( argv[5] );
+ if ( argc > 6 ) {
+ port = atoi( argv[6] );
- if ( argc > 6 ) {
- refDirName = argv[6];
+ if ( argc > 7 ) {
+ refDirName = argv[7];
ntupleName += refDirName; ntupleName += "/";
}
}
}
}
UInt_t lastExpt = firstExpt + nExpt - 1;
ntupleName += "coordinator-ntuple_expts";
ntupleName += firstExpt;
ntupleName += "-";
ntupleName += lastExpt;
ntupleName += "_fit";
ntupleName += iFit;
ntupleName += ".root";
LauSimFitCoordinator coordinator( nTasks, port );
- coordinator.runSimFit( ntupleName, nExpt, firstExpt, useAsymmErrors, twoStageFit );
+ coordinator.setNExpts( nExpt, firstExpt, isToy );
+ coordinator.runSimFit( ntupleName, useAsymmErrors, twoStageFit );
return EXIT_SUCCESS;
}
diff --git a/examples/SimFitCoordinator_Bd2Dpipi.cc b/examples/SimFitCoordinator_Bd2Dpipi.cc
index cc7c8d9..3452d4f 100644
--- a/examples/SimFitCoordinator_Bd2Dpipi.cc
+++ b/examples/SimFitCoordinator_Bd2Dpipi.cc
@@ -1,169 +1,176 @@
/*
Copyright 2013 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
#include <cstdlib>
#include <iostream>
#include <vector>
#include "TFile.h"
#include "TMatrixD.h"
#include "TRandom.h"
#include "TString.h"
#include "TSystem.h"
#include "TVectorD.h"
#include "LauJsonTools.hh"
#include "LauSimFitCoordinator.hh"
void usage( std::ostream& out, const TString& progName )
{
out<<"Usage:\n";
- out<<progName<<" <iFit> <nExpt> [firstExpt = 0] [numTasks = 2] [port = 0] [refFitDir] \n";
+ out<<progName<<" <iFit> <isData> <nExpt> [firstExpt = 0] [numTasks = 2] [port = 0] [refFitDir] \n";
}
int main(const int argc, const char ** argv)
{
- if ( argc < 3 ) {
+ if ( argc < 4 ) {
usage( std::cerr, argv[0] );
return EXIT_FAILURE;
}
UInt_t iFit = atoi( argv[1] );
- UInt_t nExpt = atoi( argv[2] );
+ Bool_t isToy = ! atoi( argv[2] );
+ UInt_t nExpt = atoi( argv[3] );
UInt_t firstExpt = 0;
UInt_t nTasks = 2;
UInt_t port = 0;
TString refDirName = "";
Bool_t useAsymmErrors = kFALSE;
Bool_t twoStageFit = kFALSE;
TString ntupleName = "";
- if ( argc > 3 ) {
- firstExpt = atoi( argv[3] );
+ if ( argc > 4 ) {
+ firstExpt = atoi( argv[4] );
- if ( argc > 4 ) {
- nTasks = atoi( argv[4] );
+ if ( argc > 5 ) {
+ nTasks = atoi( argv[5] );
- if ( argc > 5 ) {
- port = atoi( argv[5] );
+ if ( argc > 6 ) {
+ port = atoi( argv[6] );
- if ( argc > 6 ) {
- refDirName = argv[6];
+ if ( argc > 7 ) {
+ refDirName = argv[7];
ntupleName += refDirName; ntupleName += "/";
}
}
}
}
UInt_t lastExpt = firstExpt + nExpt - 1;
ntupleName += "coordinator-ntuple_expts";
ntupleName += firstExpt;
ntupleName += "-";
ntupleName += lastExpt;
ntupleName += "_fit";
ntupleName += iFit;
ntupleName += ".root";
LauSimFitCoordinator coordinator( nTasks, port );
const std::string signalYieldsJsonFile { "Bd2D0pipi_SignalYields.json" };
const std::string bkgndsJsonFile { "Bd2D0pipi_Bkgnds.json" };
const nlohmann::json signalYieldsJson = LauJsonTools::readJsonFile( signalYieldsJsonFile, "", LauJsonTools::JsonType::Object );
if ( signalYieldsJson.is_null() ) {
std::cerr << "ERROR : unable to retrieve root JSON object from file \"" << signalYieldsJsonFile << "\"" << std::endl;
return EXIT_FAILURE;
}
const nlohmann::json bkgndsJson = LauJsonTools::readJsonFile( bkgndsJsonFile, "", LauJsonTools::JsonType::Object );
if ( bkgndsJson.is_null() ) {
std::cerr << "ERROR : unable to retrieve root JSON object from file \"" << bkgndsJsonFile << "\"" << std::endl;
return EXIT_FAILURE;
}
const std::array<std::string,2> runs {"Run1", "Run2"};
const std::array<std::string,3> modes {"Kpi", "KK", "pipi"};
const std::map<std::string,std::size_t> bkgndIndex {
{ "comb" , 1 },
{ "Bd2DKpi" , 2 },
{ "Bs2DKpi" , 3 },
{ "Lb2Dppi" , 4 },
{ "Bu2Dstpi_Dpi0" , 5 },
{ "Bu2Dstpi" , 6 },
{ "Bd2Dstpi" , 7 }
};
const std::size_t nComponents{bkgndIndex.size()+1};
for ( auto& run : runs ) {
+
+ if ( nTasks == 3 && run == "Run2" ) {
+ continue;
+ }
+
for ( auto& mode : modes ) {
if ( nTasks < 3 && mode != "Kpi" ) {
continue;
}
const std::string dir { "B2Dhh_" + mode };
std::vector<TString> parNames;
parNames.resize( nComponents );
TVectorD means( nComponents );
parNames[0] = Form( "signalEvents_%s_%s", dir.c_str(), run.c_str() );
const auto& nSigJson { signalYieldsJson.at(run).at(dir) };
if ( nSigJson.is_number() ) {
means[0] = nSigJson.get<Double_t>();
} else {
means[0] = LauJsonTools::getValue<Double_t>( nSigJson, "value" );
}
for ( auto& [ bkgndName, bkgndJson ] : bkgndsJson.items() ) {
std::size_t iBkgnd{ bkgndIndex.at(bkgndName) };
std::cout << "Processing background " << iBkgnd << " called " << bkgndName << std::endl;
parNames[iBkgnd] = Form( "%sEvents_%s_%s", bkgndName.c_str(), dir.c_str(), run.c_str() );
const auto& yieldJson { bkgndJson.at("yields").at(run).at(dir) };
if ( yieldJson.is_number() ) {
means[iBkgnd] = yieldJson.get<Double_t>();
} else {
means[iBkgnd] = LauJsonTools::getValue<Double_t>( yieldJson, "value" );
}
}
std::unique_ptr<TFile> file { TFile::Open( Form( "root://eoslhcb.cern.ch//eos/lhcb/wg/b2oc/B2D0pipi/timedep_fit_histos/Overall_fitResult_%s_%s.root", run.c_str(), mode.c_str() ) ) };
TMatrixD * covMat { static_cast<TMatrixD*>( file->Get("covariance_sigRegion") ) };
coordinator.addMultiDimConstraint( parNames, means, *covMat );
file->Close();
}
}
- coordinator.runSimFit( ntupleName, nExpt, firstExpt, useAsymmErrors, twoStageFit );
+ coordinator.setNExpts( nExpt, firstExpt, isToy );
+ coordinator.runSimFit( ntupleName, useAsymmErrors, twoStageFit );
return EXIT_SUCCESS;
}
diff --git a/examples/runCoordinatorRooFitTask.sh b/examples/runCoordinatorRooFitTask.sh
index ad6b6f0..1a5c227 100755
--- a/examples/runCoordinatorRooFitTask.sh
+++ b/examples/runCoordinatorRooFitTask.sh
@@ -1,64 +1,64 @@
#!/bin/bash
# Copyright 2017 University of Warwick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Laura++ package authors:
# John Back
# Paul Harrison
# Thomas Latham
if [ $# -lt 1 ]
then
echo "Usage: $0 <nExpt> [firstExpt = 0] [numTasks = 2]"
exit 1
fi
nexpt=$1
firstexpt=0
numtasks=2
if [ $# -gt 1 ]
then
firstexpt=$2
if [ $# -gt 2 ]
then
numtasks=$3
fi
fi
# Do whatever you need to do to setup your ROOT environment
# Generate the toy MC
#echo "Generating MC"
#./SimFitTaskRooFit gen DD $nexpt $firstexpt > gen-log-DD.out 2>&1
#./SimFitTaskRooFit gen LL $nexpt $firstexpt > gen-log-LL.out 2>&1
# Do the simultaneous fit
-./SimFitCoordinator 0 $nexpt $firstexpt $numtasks > coordinator-log.out 2>&1 &
+./SimFitCoordinator 0 0 $nexpt $firstexpt $numtasks > coordinator-log.out 2>&1 &
sleep 5
port=`tail -1 coordinator-log.out | awk '{print $NF}'`
echo $port
./SimFitTaskRooFit fit DD $port > task-dd-log.out 2>&1 &
sleep 1
./SimFitTaskRooFit fit LL $port > task-ll-log.out 2>&1
diff --git a/examples/runCoordinatorTask.sh b/examples/runCoordinatorTask.sh
index 5d5de00..6dbcdd6 100755
--- a/examples/runCoordinatorTask.sh
+++ b/examples/runCoordinatorTask.sh
@@ -1,88 +1,88 @@
#!/bin/bash
# Copyright 2013 University of Warwick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Laura++ package authors:
# John Back
# Paul Harrison
# Thomas Latham
if [ $# -lt 1 ]
then
echo "Usage: $0 <nExpt> [firstExpt = 0]"
exit 1
fi
nexpt=$1
firstexpt=0
numtasks=2
if [ $# -gt 1 ]
then
firstexpt=$2
fi
# Do whatever you need to do to setup your ROOT environment
# Generate the toy MC
if [ ! -e gen-DD-Task.root ]
then
echo "Generating MC for DD category"
./SimFitTask gen DD $nexpt $firstexpt > gen-log-DD.out 2>&1
fi
if [ ! -e gen-LL-Task.root ]
then
echo "Generating MC for LL category"
./SimFitTask gen LL $nexpt $firstexpt > gen-log-LL.out 2>&1
fi
# Do the simultaneous fit
for ifit in `seq 0 19`
do
echo "Running fit $ifit"
- ./SimFitCoordinator $ifit $nexpt $firstexpt $numtasks > coordinator-log-$ifit.out 2>&1 &
+ ./SimFitCoordinator $ifit 0 $nexpt $firstexpt $numtasks > coordinator-log-$ifit.out 2>&1 &
sleep 5
NUMOFLINES=$(wc -l < "coordinator-log-$ifit.out")
while [ $NUMOFLINES -lt 1 ]
do
sleep 5
NUMOFLINES=$(wc -l < "coordinator-log-$ifit.out")
done
port=`tail -1 coordinator-log-$ifit.out | awk '{print $NF}'`
./SimFitTask fit DD $ifit $port localhost > task-dd-log-$ifit.out 2>&1 &
sleep 1
./SimFitTask fit LL $ifit $port localhost > task-ll-log-$ifit.out 2>&1
done
# Extract the best fit
echo "Extracting the best fit results"
ls fitDD*.root > input-list-DD.txt
ls fitLL*.root > input-list-LL.txt
ls coordinator-ntuple-*.root > input-list-coordinator.txt
./ResultsExtractorMain $nexpt input-list-DD.txt best-fits-DD.root > resultsextractor-DD.out 2>&1
./ResultsExtractorMain $nexpt input-list-LL.txt best-fits-LL.root > resultsextractor-LL.out 2>&1
./ResultsExtractorMain $nexpt input-list-coordinator.txt best-fits-coordinator.root > resultsextractor-coordinator.out 2>&1
diff --git a/examples/runGenFitIncoherent_ToyMC.sh b/examples/runGenFitIncoherent_ToyMC.sh
index d6843f0..e33b738 100755
--- a/examples/runGenFitIncoherent_ToyMC.sh
+++ b/examples/runGenFitIncoherent_ToyMC.sh
@@ -1,125 +1,125 @@
#!/bin/bash
# Copyright 2015 University of Warwick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Laura++ package authors:
# John Back
# Paul Harrison
# Thomas Latham
if [ $# -lt 2 ]
then
echo "Usage: $0 <gen> <nExpts>"
echo "or"
echo "Usage: $0 <fit> <nExpts> <ifit>"
exit 1
fi
config=$1
nexpt=$2
firstexpt=0
numtasks=12
ifit=$3
# Models condiguration
model="Model1"
#res="K*+(892),RelBW K*0(892),RelBW"
res="K*+(892),RelBW K*0(892),RelBW K*+_0(1430),LASS K*0_0(1430),LASS"
mkdir -p Results/
mkdir -p Results/$model/logs/
mkdir -p Results/$model/Fit/
mkdir -p Results/$model/GenToyMC/
mkdir -p Results/$model/ToyMC/
# Perform the simultaneous fit
if [[ $config = "gen" ]]
then
# Printing information about the model for the user
echo
echo "runGenFitIncoherent_ToyMC::INFO : configuration of the signal model using $model"
rm -f Results/$model/input-Sig-Gen-Model.txt
echo $model : $res | tee -a Results/$model/input-Sig-Gen-Model.txt
echo
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSKpi 2011 $model $res > Results/$model/logs/log-DD-2011-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSKpi 2011 $model $res > Results/$model/logs/log-LL-2011-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSKpi 2012a $model $res > Results/$model/logs/log-DD-2012a-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSKpi 2012a $model $res > Results/$model/logs/log-LL-2012a-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSKpi 2012b $model $res > Results/$model/logs/log-DD-2012b-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSKpi 2012b $model $res > Results/$model/logs/log-LL-2012b-KSKpi-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSpiK 2011 $model $res > Results/$model/logs/log-DD-2011-KSpiK-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSpiK 2011 $model $res > Results/$model/logs/log-LL-2011-KSpiK-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSpiK 2012a $model $res > Results/$model/logs/log-DD-2012a-KSpiK-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSpiK 2012a $model $res > Results/$model/logs/log-LL-2012a-KSpiK-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt DD KSpiK 2012b $model $res > Results/$model/logs/log-DD-2012b-KSpiK-log-gen.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi gen $nexpt $firstexpt LL KSpiK 2012b $model $res > Results/$model/logs/log-LL-2012b-KSpiK-log-gen.out 2>&1
elif [[ $config = "fit" ]]
then
# Printing information about the model for the user
echo
echo "runGenFitIncoherent_ToyMC::INFO : configuration of the signal model using $model"
rm -f Results/$model/input-Sig-Fit-Model.txt
echo $model : $res | tee -a Results/$model/input-Sig-Fit-Model.txt
echo
echo "Running fit ... $ifit"
- ./SimFitCoordinator $ifit $nexpt $firstexpt $numtasks Results/$model > Results/$model/logs/coordinator-log-$ifit.out 2>&1 &
+ ./SimFitCoordinator $ifit 0 $nexpt $firstexpt $numtasks Results/$model > Results/$model/logs/coordinator-log-$ifit.out 2>&1 &
sleep 5
port=`tail -1 Results/$model/logs/coordinator-log-$ifit.out | awk '{print $NF}'`
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSKpi 2011 $port $model $res > Results/$model/logs/task-DD-2011-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSKpi 2011 $port $model $res > Results/$model/logs/task-LL-2011-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSKpi 2012a $port $model $res > Results/$model/logs/task-DD-2012a-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSKpi 2012a $port $model $res > Results/$model/logs/task-LL-2012a-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSKpi 2012b $port $model $res > Results/$model/logs/task-DD-2012b-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSKpi 2012b $port $model $res > Results/$model/logs/task-LL-2012b-KSKpi-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSpiK 2011 $port $model $res > Results/$model/logs/task-DD-2011-KSpiK-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSpiK 2011 $port $model $res > Results/$model/logs/task-LL-2011-KSpiK-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSpiK 2012a $port $model $res > Results/$model/logs/task-DD-2012a-KSpiK-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSpiK 2012a $port $model $res > Results/$model/logs/task-LL-2012a-KSpiK-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt DD KSpiK 2012b $port $model $res > Results/$model/logs/task-DD-2012b-KSpiK-log-$ifit.out 2>&1 &
sleep 1
./GenFitIncoherent_Bs2KSKpi fit $ifit $nexpt $firstexpt LL KSpiK 2012b $port $model $res > Results/$model/logs/task-LL-2012b-KSpiK-log-$ifit.out 2>&1
else
echo "runGenFitIncoherent_ToyMC::ERROR : wrong configuration -- either 'gen' or 'fit'"
exit 1
fi
diff --git a/examples/runTimeDepTest.sh b/examples/runTimeDepTest.sh
index d18a767..61780d0 100755
--- a/examples/runTimeDepTest.sh
+++ b/examples/runTimeDepTest.sh
@@ -1,239 +1,239 @@
#!/bin/bash
#SBATCH --partition=epp
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=6
#SBATCH --mem-per-cpu=3997
#SBATCH --time=02:00:00
#Run with sbatch runTimeDepTest.sh
# Copyright 2020 University of Warwick
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Laura++ package authors:
# John Back
# Paul Harrison
# Thomas Latham
processes=()
# trap ^C and call ctrl_c()
trap ctrl_c INT
function ctrl_c()
{
echo " ** Caught ^C event!"
for process in ${processes[@]}
do
if ps -u $USER | grep -q "$process"
then
echo "Killing : $process"
kill $process
fi
done
exit 1
}
# Check that we have a kerberos token, since nothing will work without one!
klist -s > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "You don't have a valid Kerberos ticket!"
exit 1
fi
iFit=0
dta_model="spline"
dtr=1
dtr_perevent=0
fixProdAsym=0
fixTau=1
fixDeltaM=1
floatYields=1
seed=140279 # $(pwd | xargs basename)
dataFit=1
runNo=12
# Modify these if you want to run more experiments
nExpt=1
firstExpt=0
logName_genQFS_run1=Logs/gen-QFS-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_genQFS_run2=Logs/gen-QFS-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_genCPE_KK_run1=Logs/gen-CPEvenKK-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_genCPE_KK_run2=Logs/gen-CPEvenKK-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_genCPE_pipi_run1=Logs/gen-CPEvenpipi-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_genCPE_pipi_run2=Logs/gen-CPEvenpipi-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent.log
logName_coord=Logs/coordinator-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskQFS_run1=Logs/task-QFS-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskQFS_run2=Logs/task-QFS-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskCPE_KK_run1=Logs/task-CPEvenKK-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskCPE_KK_run2=Logs/task-CPEvenKK-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskCPE_pipi_run1=Logs/task-CPEvenpipi-run1-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
logName_taskCPE_pipi_run2=Logs/task-CPEvenpipi-run2-dtamodel_$dta_model-dtr_$dtr-dtrperevent_$dtr_perevent-$iFit.log
mkdir -p Logs
# make sure we have the most up-to-date json files
eosRoot="root://eoslhcb.cern.ch//eos/lhcb/wg/b2oc/B2D0pipi"
eosConfigDir="timedep_fit_histos"
eosArea="$eosRoot/$eosConfigDir"
for jsonfile in Bd2D0pipi_Bkgnds.json Bd2D0pipi_DP_Model_Coeffs_Vetoes.json Bd2D0pipi_DecayTime.json Bd2D0pipi_DecayTime_Acceptance.json Bd2D0pipi_SignalYields.json Bd2D0pipi_TaggingCalib.json
do
rm -f $jsonfile
xrdcp $eosArea/$jsonfile .
if [ $? -ne 0 ]
then
echo "Problem downloading json files from EOS, exiting"
exit 1
fi
done
if [ $iFit == 0 -a $dataFit == 0 ]
then
#run generation of the modes in parallel (put the tasks in the bg)
echo "Generating samples..."
if [ $runNo -eq 1 -o $runNo -eq 12 ]
then
GenFitTimeDep_Bd2Dpipi gen --dtype QFS --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_Kpi > $logName_genQFS_run1 2>&1 &
task=$!
processes+=( $task )
GenFitTimeDep_Bd2Dpipi gen --dtype CPEven --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_KK > $logName_genCPE_KK_run1 2>&1 &
task=$!
processes+=( $task )
GenFitTimeDep_Bd2Dpipi gen --dtype CPEven --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_pipi > $logName_genCPE_pipi_run1 2>&1 &
task=$!
processes+=( $task )
fi
if [ $runNo -eq 2 -o $runNo -eq 12 ]
then
GenFitTimeDep_Bd2Dpipi gen --dtype QFS --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_Kpi > $logName_genQFS_run2 2>&1 &
task=$!
processes+=( $task )
GenFitTimeDep_Bd2Dpipi gen --dtype CPEven --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_KK > $logName_genCPE_KK_run2 2>&1 &
task=$!
processes+=( $task )
GenFitTimeDep_Bd2Dpipi gen --dtype CPEven --eosConfigDir $eosConfigDir --fixProdAsym $fixProdAsym --fixTau $fixTau --seed $seed --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_pipi > $logName_genCPE_pipi_run2 2>&1 &
task=$!
processes+=( $task )
fi
wait #wait for the generation to complete
fi
echo "Running fit $iFit"
if [ $runNo -eq 12 ]
then
nTasks=6
else
nTasks=3
fi
-SimFitCoordinator_Bd2Dpipi $iFit $nExpt $firstExpt $nTasks > $logName_coord 2>&1 &
+SimFitCoordinator_Bd2Dpipi $iFit $dataFit $nExpt $firstExpt $nTasks > $logName_coord 2>&1 &
task=$!
echo "SimFitCoordinator process : $task"
processes+=( $task )
echo "Initialised coordinator process for fit $iFit"
sleep 5
port=""
while [ "x$port" == "x" ]
do
sleep 2
port=`tail $logName_coord | grep "Waiting for connection" | awk '{print $NF}'`
done
echo "Coordinator is listening on port $port"
echo "Initialising tasks..."
if [ $runNo -eq 1 -o $runNo -eq 12 ]
then
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype QFS --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_Kpi --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskQFS_run1 2>&1 &
task=$!
echo "QFS Run 1 process : $task"
processes+=( $task )
sleep 5
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype CPEven --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_KK --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskCPE_KK_run1 2>&1 &
task=$!
echo "CP KK Run 1 process : $task"
processes+=( $task )
sleep 5
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype CPEven --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 1 --dir B2Dhh_pipi --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskCPE_pipi_run1 2>&1 &
task=$!
echo "CP pipi Run 1 process : $task"
processes+=( $task )
sleep 5
fi
if [ $runNo -eq 2 -o $runNo -eq 12 ]
then
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype QFS --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_Kpi --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskQFS_run2 2>&1 &
task=$!
echo "QFS Run 2 process : $task"
processes+=( $task )
sleep 5
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype CPEven --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_KK --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskCPE_KK_run2 2>&1 &
task=$!
echo "CP KK Run 2 process : $task"
processes+=( $task )
sleep 5
GenFitTimeDep_Bd2Dpipi simfit --blindFit $dataFit --dataFit $dataFit --eosConfigDir $eosConfigDir --dtype CPEven --seed $seed --port $port --iFit $iFit --fixProdAsym $fixProdAsym --fixTau $fixTau --dta-model $dta_model --dtr $dtr --dtr-perevent $dtr_perevent --firstExpt $firstExpt --nExpt $nExpt --firstExptGen $firstExpt --nExptGen $nExpt --run 2 --dir B2Dhh_pipi --fixDm $fixDeltaM --floatYields $floatYields > $logName_taskCPE_pipi_run2 2>&1 &
task=$!
echo "CP pipi Run 2 process : $task"
processes+=( $task )
sleep 5
fi
wait #wait for the fits and coordinator to terminate
echo "Fit completed"
echo "Warnings/Errors from coordinator:"
grep -e ERROR -e WARNING -e Error -e Warning $logName_coord
echo "Warnings/Errors from CPeven KK tasks:"
if [ $runNo -eq 1 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskCPE_KK_run1
fi
if [ $runNo -eq 2 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskCPE_KK_run2
fi
echo "Warnings/Errors from CPeven pipi tasks:"
if [ $runNo -eq 1 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskCPE_pipi_run1
fi
if [ $runNo -eq 2 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskCPE_pipi_run2
fi
echo "Warnings/Errors from QFS tasks:"
if [ $runNo -eq 1 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskQFS_run1
fi
if [ $runNo -eq 2 -o $runNo -eq 12 ]
then
grep -e ERROR -e WARNING -e Error -e Warning $logName_taskQFS_run2
fi
diff --git a/inc/LauFitNtuple.hh b/inc/LauFitNtuple.hh
index 8eb69ed..482d368 100644
--- a/inc/LauFitNtuple.hh
+++ b/inc/LauFitNtuple.hh
@@ -1,134 +1,138 @@
/*
Copyright 2004 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauFitNtuple.hh
\brief File containing declaration of LauFitNtuple class.
*/
/*! \class LauFitNtuple
\brief Class to store the results from the fit into an ntuple
Class to store the results from the fit into an ntuple.
The fitted values and their errors are stored. In the case of toy MC, the true values are also stored, along with the pulls.
Derived quantities can also be stored, such as the fit fractions. What precisely is stored depends on the fit model.
*/
//****************************************************************************
// Class to store the results from the fit/toy MC into an ntuple
// -- CLASS DESCRIPTION [MISC] --
/// Class to store the results from the fit/toy MC into an ntuple
#ifndef LAU_FIT_NTUPLE
#define LAU_FIT_NTUPLE
+#include <set>
#include <vector>
#include "TMatrixDfwd.h"
#include "TString.h"
#include "LauAbsFitter.hh"
class TFile;
class TTree;
class LauParameter;
class LauFitNtuple {
public:
//! Constructor
/*!
\param [in] fileName the name for the ntuple
\param [in] storeAsymErrors whether or not to store the asymmetric error variables
*/
LauFitNtuple(const TString& fileName, Bool_t storeAsymErrors);
//! Destructor
virtual ~LauFitNtuple();
//! Store the correlation matrix and other fit information
/*!
\param [in] iExpt the experiment number
\param [in] fitStatus the status of the fit
\param [in] covMatrix the fit covariance matrix
*/
void storeCorrMatrix(const UInt_t iExpt, const LauAbsFitter::FitStatus& fitStatus, const TMatrixD& covMatrix);
//! Store parameters and their errors
/*!
\param [in] fitVars the fit parameters
+ \param [in] constrainedVars the names of fit parameters that are included in multi-dimensional constraints
\param [in] extraVars variables in addition to the fit parameters, e.g. derived quantities such as fit fractions
*/
- void storeParsAndErrors(const std::vector<LauParameter*>& fitVars, const std::vector<LauParameter>& extraVars);
+ void storeParsAndErrors(const std::vector<LauParameter*>& fitVars, const std::set<TString>& constrainedVars, const std::vector<LauParameter>& extraVars);
//! Update the fit ntuple
void updateFitNtuple();
//! Write out fit results
void writeOutFitResults();
private:
//! Copy constructor (not implemented)
LauFitNtuple(const LauFitNtuple& rhs);
//! Copy assignment operator (not implemented)
LauFitNtuple& operator=(const LauFitNtuple& rhs);
//! Name of root file
TString rootFileName_;
//! Root file
TFile* rootFile_;
//! Fit results
TTree* fitResults_;
//! Fit variables
std::vector<LauParameter*> fitVars_;
+ //! Fit variables that are included in a multi-dimensional constraint
+ std::set<TString> constrainedVars_;
//! Extra variables
std::vector<LauParameter> extraVars_;
//! Correlation matrix
std::vector< std::vector<Double_t> > corrMatrix_;
//! Flags whether the fit tree has been defined
Bool_t definedFitTree_;
//! Flags whether or not to store the asymmetric error information
Bool_t storeAsymErrors_;
//! Status of fit
LauAbsFitter::FitStatus fitStatus_;
//! Number of fit parameters
UInt_t nFitPars_;
//! Number of free parameters
UInt_t nFreePars_;
//! Number of extra parameters
UInt_t nExtraPars_;
//! Experiment number
Int_t iExpt_;
ClassDef(LauFitNtuple,0) // Fit/toyMC results ntuple
};
#endif
diff --git a/inc/LauFitObject.hh b/inc/LauFitObject.hh
index d036fc1..6155c40 100644
--- a/inc/LauFitObject.hh
+++ b/inc/LauFitObject.hh
@@ -1,391 +1,422 @@
/*
Copyright 2013 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauFitObject.hh
\brief File containing declaration of LauFitObject class.
*/
#ifndef LAU_FIT_OBJECT
#define LAU_FIT_OBJECT
+#include <set>
#include <vector>
#include "TMatrixD.h"
#include "TObject.h"
#include "TString.h"
#include "TVectorD.h"
#include "LauAbsFitter.hh"
#include "LauFormulaPar.hh"
/*! \class LauFitObject
\brief The abstract interface for the objects that control the calculation of the likelihood.
*/
class LauFitObject : public TObject {
public:
//! Constructor (default)
LauFitObject() = default;
//! Move constructor (default)
LauFitObject(LauFitObject&& rhs) = default;
//! Copy constructor (deleted)
LauFitObject(const LauFitObject& rhs) = delete;
//! Move assignment operator (default)
LauFitObject& operator=(LauFitObject&& rhs) = default;
//! Copy assignment operator (deleted)
LauFitObject& operator=(const LauFitObject& rhs) = delete;
//! Destructor
virtual ~LauFitObject() = default;
//! Turn on or off the computation of asymmetric errors (e.g. MINOS routine in Minuit)
/*!
\param [in] useAsymmErrors boolean specifying whether or not the computation of asymmetric errors is enabled
*/
void useAsymmFitErrors(Bool_t useAsymmErrors) {useAsymmFitErrors_ = useAsymmErrors;}
//! Report whether or not calculation of asymmetric errors is enabled
Bool_t useAsymmFitErrors() const {return useAsymmFitErrors_;}
//! Turn on or off the two stage fit
/*!
The two-stage fit allows certain parameters to be fixed
in one stage and floated in another stage of the fit.
Can be used, for example, in a CP fit where the
CP-parameters are fixed to zero in the first stage
(while the CP-average parameters are determined), then
floated in the second.
\param [in] doTwoStageFit boolean specifying whether or not the two-stage fit should be enabled
*/
void twoStageFit(Bool_t doTwoStageFit) {twoStageFit_ = doTwoStageFit;}
//! Report whether the two-stage fit is enabled
Bool_t twoStageFit() const {return twoStageFit_;}
//! Mark that the fit is calculating asymmetric errors
/*!
This is called by the fitter interface to mark when
entering and exiting the asymmetric error calculation.
\param [in] inAsymErrCalc boolean marking that the fit is calculating the asymmetric errors
*/
virtual void withinAsymErrorCalc(const Bool_t inAsymErrCalc) {withinAsymErrorCalc_ = inAsymErrCalc;}
//! Query whether the fit is calculating the asymmetric errors
/*!
\return kTRUE if the fit is calculating the asymmetric errors, kFALSE otherwise
*/
Bool_t withinAsymErrorCalc() const {return withinAsymErrorCalc_;}
//! Should the event loop terminate on encountering an event with a bad likelihood?
Bool_t breakOnBadLikelihood() const {return breakOnBadLikelihood_;}
//! Set whether the event loop terminate on encountering an event with a bad likelihood
/*!
\param [in] newBreakOnBadLikelihood the new value of the setting
*/
void breakOnBadLikelihood( const Bool_t newBreakOnBadLikelihood ) {breakOnBadLikelihood_ = newBreakOnBadLikelihood;}
//! Set the number of experiments, the first experiment, and whether this is toy
/*!
The default settings are nExperiments = 1, firstExperiment = 0, toyExpts = kFALSE,
i.e. the settings for fitting a single data sample.
As such, this function only needs to be called if generating/fitting toy samples.
\param [in] nExperiments the number of experiments
\param [in] firstExperiment the number of the first experiment
\param [in] toyExpts whether this is toy - determines whether to generate per-experiment means for each Gaussian constraint, as per arXiv:1210.7141
*/
void setNExpts(UInt_t nExperiments, UInt_t firstExperiment, Bool_t toyExpts);
//! Obtain the total number of events in the current experiment
UInt_t eventsPerExpt() const {return evtsPerExpt_;}
//! Obtain the number of experiments
UInt_t nExpt() const {return nExpt_;}
//! Obtain the number of the first experiment
UInt_t firstExpt() const {return firstExpt_;}
//! Obtain the number of the current experiment
UInt_t iExpt() const {return iExpt_;}
//! Obtain whether this is toy
Bool_t toyExpts() const {return toyExpts_;}
//! This function sets the parameter values from Minuit
/*!
This function has to be public since it is called from the global FCN.
It should not be called otherwise!
\param [in] par an array storing the various parameter values
\param [in] npar the number of free parameters
*/
virtual void setParsFromMinuit(Double_t* par, Int_t npar) = 0;
//! Calculate the new value of the negative log likelihood
/*!
This function has to be public since it is called from the global FCN.
It should not be called otherwise!
*/
virtual Double_t getTotNegLogLikelihood() = 0;
//! Store constraint information for fit parameters
/*!
\deprecated Renamed to addFormulaConstraint, please switch to use this. Will be dropped in next major release.
\param [in] formula the formula to be used in the LauFormulaPar
\param [in] pars a vector of LauParameter names to be used in the Formula, in the order specified by the formula
\param [in] mean the value of the mean of the Gaussian constraint
\param [in] width the value of the width of the Gaussian constraint
*/
- virtual void addConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width);
+ void addConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width);
//! Store constraint information for fit parameters
/*!
\param [in] formula the formula to be used in the LauFormulaPar
\param [in] pars a vector of LauParameter names to be used in the Formula, in the order specified by the formula
\param [in] mean the value of the mean of the Gaussian constraint
\param [in] width the value of the width of the Gaussian constraint
*/
- virtual void addFormulaConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width);
+ void addFormulaConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width);
//! Store n-dimensional constraint information for fit parameters
/*!
\param [in] pars a vector of LauParameter names to be used in the constraint
\param [in] means the values of the means of the Gaussian constraint
\param [in] covMat the covariance matrix of the parameters of the Gaussian constraint
*/
- virtual void addMultiDimConstraint(const std::vector<TString>& pars, const TVectorD& means, const TMatrixD& covMat);
-
- //! Check if parameters names for constraints have already been used elsewhere
- /*!
- \param [in] names a vector of parameter names
- \return kTRUE if no repetitions found, kFALSE if one or more repetitions found
- */
- virtual Bool_t checkRepetition(const std::vector<TString>& names);
+ void addMultiDimConstraint(const std::vector<TString>& pars, const TVectorD& means, const TMatrixD& covMat);
protected:
+ /*!
+ \enum ConstraintType
+ \brief Enumeration of the different types of constraint
+ */
+ enum class ConstraintType {
+ Formula, //!< Formula-based constraint on a combination of parameters
+ MultDim //!< Multi-dimensional constraint on several parameters
+ };
+
/*!
\struct FormulaConstraint
\brief Struct to store formula-based constraint information
*/
struct FormulaConstraint {
//! The formula to be used in the LauFormulaPar
TString formula_;
//! The list of LauParameter names to be used in the LauFormulaPar
std::vector<TString> conPars_;
//! The mean value of the Gaussian constraint to be applied
Double_t mean_;
//! The width of the Gaussian constraint to be applied
Double_t width_;
//! The LauFormulaPar pointer
std::unique_ptr<LauFormulaPar> formulaPar_;
};
// Setup a class to store information on n-dimensional constrained fit parameters
/*!
\class MultiDimConstraint
\brief Class to store n-dimensional constraint information
*/
class MultiDimConstraint {
public:
//! Default constructor
MultiDimConstraint() = default;
//! Constructor
MultiDimConstraint( const std::vector<TString>& parNames, const TVectorD& means, const TMatrixD& covMat );
//! Get the penalty term
Double_t constraintPenalty() const;
//! Generate per-experiment constraint means
void generateConstraintMeans();
//! The list of LauParameter names to be used in the constraint
std::vector<TString> conPars_;
//! The true mean values of the constraint
TVectorD trueMeans_;
//! The per-experiment mean values of the constraint
TVectorD means_;
//! The inverse covariance matrix of the parameters
TMatrixD invCovMat_;
//! The Cholesky Decomposition of the covariance matrix of the parameters
TMatrixD sqrtCovMat_;
//! The LauParameters used in the constraints
std::vector<LauParameter*> conLauPars_;
};
+ //! Check if parameters names for constraints have already been used elsewhere
+ /*!
+ Also adds the names to the appropriate set
+
+ \param [in] names a vector of parameter names
+ \param [in] conType the type of constraint being added - determines which set to which to add the parmaeters
+ \return kTRUE if no repetitions found, kFALSE if one or more repetitions found
+ */
+ Bool_t checkRepetition(const std::vector<TString>& names, const ConstraintType conType);
+
//! Generate per-experiment mean for each Gaussian constraint
/*!
Generates a new mean for all Gaussian constraints.
The constraints on a single fit parameter and the
formula-based constraints are provided as the argument,
while the multi-dimensional constraints are already a
member variable.
\param [in,out] conVars the fit parameter and formula-based constraints
*/
void generateConstraintMeans( std::vector<LauAbsRValue*>& conVars );
- //! Const access to the constraints store
+ //! Const access to the formula constraints store
const std::vector<FormulaConstraint>& formulaConstraints() const {return formulaConstraints_;}
- //! Access to the constraints store
+ //! Access to the formula constraints store
std::vector<FormulaConstraint>& formulaConstraints() {return formulaConstraints_;}
//! Const access to the ND constraints store
const std::vector<MultiDimConstraint>& multiDimConstraints() const {return multiDimConstraints_;}
//! Access to the ND constraints store
std::vector<MultiDimConstraint>& multiDimConstraints() {return multiDimConstraints_;}
+ //! Const access to the parameter names used in formula constraints
+ const std::set<TString>& formulaConstrainedPars() const {return formulaConstrainedPars_;}
+
+ //! Access to the parameter names used in formula constraints
+ std::set<TString>& formulaConstrainedPars() {return formulaConstrainedPars_;}
+
+ //! Const access to the parameter names used in ND constraints
+ const std::set<TString>& multiDimConstrainedPars() const {return multiDimConstrainedPars_;}
+
+ //! Access to the parameter names used in ND constraints
+ std::set<TString>& multiDimConstrainedPars() {return multiDimConstrainedPars_;}
+
//! Reset the good/bad fit counters
void resetFitCounters();
//! Set the ID of the current experiment
/*!
\param [in] curExpt the experiment number
*/
void setCurrentExperiment( const UInt_t curExpt ) { iExpt_ = curExpt; }
//! Indicate the start of a new fit
/*!
\param [in] nPars the total number of fit parameters
\param [in] nFreePars the number of free fit parameters
*/
void startNewFit( const UInt_t nPars, const UInt_t nFreePars );
//! Set the number of events in the current experiment
void eventsPerExpt(UInt_t nEvents) {evtsPerExpt_ = nEvents;}
//! Access the worst log likelihood found so far
Double_t worstLogLike() const {return worstLogLike_;}
//! Set a new value for the worst log likelihood
/*!
\param [in] newWorstLogLike the new value of the worst log likelihood
*/
void worstLogLike( const Double_t newWorstLogLike ) {worstLogLike_ = newWorstLogLike;}
//! Store fit status information
/*!
\param [in] status the status information of the fit
\param [in] covMatrix the fit covariance matrix
*/
void storeFitStatus( const LauAbsFitter::FitStatus& status, const TMatrixD& covMatrix );
//! Access the total number of fit parameters
UInt_t nTotParams() const {return nParams_;}
//! Access the total number of fit parameters
UInt_t nFreeParams() const {return nFreeParams_;}
//! Access the fit status information
const LauAbsFitter::FitStatus& fitStatus() const {return fitStatus_;}
//! Access the current NLL value
Double_t nll() const {return fitStatus_.NLL;}
//! Access the current EDM value
Double_t edm() const {return fitStatus_.EDM;}
//! Access the fit status code
Int_t statusCode() const {return fitStatus_.status;}
//! Access the fit covariance matrix
const TMatrixD& covarianceMatrix() const {return covMatrix_;}
//! Access the number of successful fits
UInt_t numberOKFits() const {return numberOKFits_;}
//! Access the number of failed fits
UInt_t numberBadFits() const {return numberBadFits_;}
private:
//! Store the constraints for fit parameters until initialisation is complete
std::vector<FormulaConstraint> formulaConstraints_;
//! Store the ND constraints for fit parameters until initialisation is complete
std::vector<MultiDimConstraint> multiDimConstraints_;
+ //! Store the names of all parameters used in all formula constraints
+ std::set<TString> formulaConstrainedPars_;
+
+ //! Store the names of all parameters used in all multi-dimensional constraints
+ std::set<TString> multiDimConstrainedPars_;
+
//! Option to perform a two stage fit
Bool_t twoStageFit_{kFALSE};
//! Option to use asymmetric errors
Bool_t useAsymmFitErrors_{kFALSE};
//! The number of fit parameters
UInt_t nParams_{0};
//! The number of free fit parameters
UInt_t nFreeParams_{0};
//! Flag to indicate if the asymmetric error calculation (e.g. MINOS) is currently running
Bool_t withinAsymErrorCalc_{kFALSE};
//! Flag to indicate whether this is toy
/*! Determines whether to generate per-experiment means for each Gaussian constraint, as per arXiv:1210.7141 */
Bool_t toyExpts_{kFALSE};
//! The number of the first experiment to consider
UInt_t firstExpt_{0};
//! The number of experiments to consider
UInt_t nExpt_{1};
//! The current experiment number
UInt_t iExpt_{0};
//! The number of events in the current experiment
UInt_t evtsPerExpt_{0};
//! The status of the current fit
LauAbsFitter::FitStatus fitStatus_{-1, 0.0, 0.0};
//! Whether to terminate the event loop when finding an event with a bad likelihood
Bool_t breakOnBadLikelihood_{kTRUE};
//! The worst log likelihood value found so far
Double_t worstLogLike_{std::numeric_limits<Double_t>::max()};
//! The fit covariance matrix
TMatrixD covMatrix_;
//! The number of successful fits
UInt_t numberOKFits_{0};
//! The number of fit failures
UInt_t numberBadFits_{0};
ClassDef(LauFitObject,0)
};
#endif
diff --git a/inc/LauParameter.hh b/inc/LauParameter.hh
index debd293..885bc14 100644
--- a/inc/LauParameter.hh
+++ b/inc/LauParameter.hh
@@ -1,588 +1,591 @@
/*
Copyright 2006 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauParameter.hh
\brief File containing declaration of LauParameter class.
*/
#ifndef LAU_PARAMETER
#define LAU_PARAMETER
#include "LauAbsRValue.hh"
#include "LauBlind.hh"
#include "TObject.h"
#include "TString.h"
#include <nlohmann/json_fwd.hpp>
#include <iosfwd>
#include <map>
#include <vector>
/*! \class LauParameter
\brief Class for defining the fit parameter objects.
Holds all relevant information for the parameters for both generation and fitting step:
current, initial and generated value, maximum and minimum range, error, asymmetric error, fix and float and etc.
*/
class LauParameter final : public TObject, public LauAbsRValue {
public:
//! Default constructor
LauParameter() = default;
//! Constructor for named parameter
/*!
\param [in] parName the parameter name
*/
explicit LauParameter(const TString& parName);
//! Constructor for named parameter with (fixed or floated) value
/*!
\param [in] parName the parameter name
\param [in] parValue the parameter value
\param [in] parFixed boolean flag to fix (kTRUE) or float (kFALSE) the parameter
*/
LauParameter(const TString& parName, const Double_t parValue, const Bool_t parFixed = kTRUE);
//! Constructor for named parameter with (fixed or floated) value and error
/*!
\param [in] parName the parameter name
\param [in] parValue the parameter value
\param [in] parError the parameter error
\param [in] parFixed boolean flag to fix (kTRUE) or float (kFALSE) the parameter
*/
LauParameter(const TString& parName, const Double_t parValue, const Double_t parError, const Bool_t parFixed = kTRUE);
//! Constructor for named parameter with (fixed or floated) value and limits
/*!
\param [in] parName the parameter name
\param [in] parValue the parameter value
\param [in] min the minimum value of the parameter
\param [in] max the maximum value of the parameter
\param [in] parFixed boolean flag to fix (kTRUE) or float (kFALSE) the parameter
*/
LauParameter(const TString& parName, const Double_t parValue, const Double_t min, const Double_t max, const Bool_t parFixed = kTRUE);
//! Constructor for named parameter with (fixed or floated) value, error and limits
/*!
\param [in] parName the parameter name
\param [in] parValue the parameter value
\param [in] parError the parameter error
\param [in] min the minimum value of the parameter
\param [in] max the maximum value of the parameter
\param [in] parFixed boolean flag to fix (kTRUE) or float (kFALSE) the parameter
*/
LauParameter(const TString& parName, const Double_t parValue, const Double_t parError, const Double_t min, const Double_t max, const Bool_t parFixed = kTRUE);
// Destructor
virtual ~LauParameter() noexcept;
//! Copy constructor
/*!
\param [in] rhs the parameter to be copied
*/
LauParameter(const LauParameter& rhs) = default;
//! Copy assignment operator
/*!
\param [in] rhs the parameter to be copied
*/
LauParameter& operator=(const LauParameter& rhs) = default;
//! Move constructor
/*!
\param [in] rhs the parameter to be moved from
*/
LauParameter(LauParameter&& rhs) = default;
//! Move assignment operator
/*!
\param [in] rhs the parameter to be moved from
*/
LauParameter& operator=(LauParameter&& rhs) = default;
// the simple accessor functions
//! The parameter name
/*!
\return the name of the parameter
*/
const TString& name() const override {return name_;}
//! The blinding state
/*!
\return the blinding state: kTRUE means that it is blinded, kFALSE that it is not blinded
*/
Bool_t blind() const override {return blinder_.active();}
//! Access the blinder object
/*!
\return the blinder
*/
const LauBlind* blinder() const {return blinder_.active() ? &blinder_ : nullptr;}
//! The value of the parameter
/*!
\return the value of the parameter
*/
Double_t value() const override {return value_;}
//! The unblinded value of the parameter
/*!
\return the unblinded value of the parameter
*/
Double_t unblindValue() const override {return blinder_.active() ? blinder_.unblind(value_) : value_;}
//! The error on the parameter
/*!
\return the error on the parameter
*/
Double_t error() const {return error_;}
//! The lower error on the parameter
/*!
\return the lower error on the parameter
*/
Double_t negError() const {return negError_;}
//! The upper error on the parameter
/*!
\return the upper error on the parameter
*/
Double_t posError() const {return posError_;}
//! The value generated for the parameter
/*!
\return the value generated for the parameter
*/
Double_t genValue() const override {return genValue_;}
//! The initial value of the parameter
/*!
\return the initial value of the parameter given to the fitter
*/
Double_t initValue() const override {return initValue_;}
//! The minimum value allowed for the parameter
/*!
\return the minimum value allowed for the parameter
*/
Double_t minValue() const {return minValue_;}
//! The maximum value allowed for the parameter
/*!
\return the maximum value allowed for the parameter
*/
Double_t maxValue() const {return maxValue_;}
//! The limits for the parameter
/*!
\return the minimum and maximum value allowed for the parameter
*/
std::pair<Double_t,Double_t> limits() const {return std::make_pair(minValue_,maxValue_);}
//! The range allowed for the parameter
/*!
\return the range allowed for the parameters, defined as the difference between the max and min value
*/
Double_t range() const {return this->maxValue() - this->minValue();}
//! Check whether the parameter has limits
/*!
\return the boolean flag true/false whether the parameter is limited
*/
Bool_t limited() const {return minValue_ != 0.0 || maxValue_ != 0.0;}
//! Check whether the parameter is fixed or floated
/*!
\return the boolean flag true/false whether the parameter is fixed
*/
Bool_t fixed() const override {return fixed_;}
//! Check whether the parameter should be floated only in the second stage of a two stage fit
/*!
\return the boolean flag true/false whether it floats only in the second stage
*/
Bool_t secondStage() const {return secondStage_;}
//! Check whether a Gaussian constraints is applied
/*!
\return the boolean flag true/false whether a Gaussian constraint is applied
*/
Bool_t gaussConstraint() const override {return gaussConstraint_;}
//! The penalty term from the Gaussian constraint
/*!
\return the penalty term from the Gaussian constraint
*/
Double_t constraintPenalty() const override;
//! The parameter global correlation coefficient
/*!
\return the global correlation coefficient
*/
Double_t globalCorrelationCoeff() const {return gcc_;}
//! The bias in the parameter
/*!
\return the bias in the parameter, defined as the difference between the value and the generated value
*/
Double_t bias() const {return bias_;}
//! The pull value for the parameter
/*!
\return the pull value for the parameter, defined as the bias divided by the error
*/
Double_t pull() const {return pull_;}
//! Boolean to say it is an L value
/*!
\return kTRUE, LauParameters are L values
*/
Bool_t isLValue() const override {return kTRUE;}
//! Get the LauParameter itself
/*!
\return a vector of the LauParameter
*/
std::vector<LauParameter*> getPars() override;
// the simple "setter" functions
//! Set the parameter name
/*!
\param [in] newName the name of the parameter
*/
void name(const TString& newName) override;
//! Set the value of the parameter
/*!
\param [in] newValue the value of the parameter
*/
void value(const Double_t newValue);
//! Set the error on the parameter
/*!
\param [in] newError the error on the parameter
*/
void error(const Double_t newError);
//! Set the lower error on the parameter
/*!
\param [in] newNegError the lower error on the parameter
*/
void negError(const Double_t newNegError);
//! Set the upper error on the parameter
/*!
\param [in] newPosError the upper error on the parameter
*/
void posError(const Double_t newPosError);
//! Set the error values on the parameter
/*!
\param [in] newError the error on the parameter
\param [in] newNegError the lower error on the parameter
\param [in] newPosError the upper error on the parameter
*/
void errors(const Double_t newError, const Double_t newNegError, const Double_t newPosError);
//! Set the value and errors on the parameter
/*!
\param [in] newValue the value of the parameter
\param [in] newError the error on the parameter
\param [in] newNegError the lower error on the parameter (default set to zero)
\param [in] newPosError the upper error on the parameter (default set to zero)
*/
void valueAndErrors(const Double_t newValue, const Double_t newError, const Double_t newNegError = 0.0, const Double_t newPosError = 0.0);
//! Set the global correlation coefficient
/*!
\param [in] newGCCValue the value of the coefficient
*/
void globalCorrelationCoeff(const Double_t newGCCValue);
//! Set the generated value for the parameter
/*!
\param [in] newGenValue the generated value for the parameter
*/
void genValue(const Double_t newGenValue);
//! Set the inital value for the parameter
/*!
\param [in] newInitValue the initial value for the parameter
*/
void initValue(const Double_t newInitValue);
//! Set the minimum value for the parameter
/*!
\param [in] newMinValue the minimum value for the parameter
*/
void minValue(const Double_t newMinValue);
//! Set the maximum value for the parameter
/*!
\param [in] newMaxValue the maximum value for the parameter
*/
void maxValue(const Double_t newMaxValue);
//! Set the range for the parameter
/*!
\param [in] newMinValue the minimum value for the parameter
\param [in] newMaxValue the maximum value for the parameter
*/
void range(const Double_t newMinValue, const Double_t newMaxValue);
//! Set the value and range for the parameter
/*!
\param [in] newValue the value of the parameter
\param [in] newMinValue the minimum value for the parameter
\param [in] newMaxValue the maximum value for the parameter
*/
void valueAndRange(const Double_t newValue, const Double_t newMinValue, const Double_t newMaxValue);
//! Remove limits from the parameter
void removeLimits() { this->range(0.0,0.0); }
//! Fix or float the given parameter
/*!
\param [in] parFixed boolean flag to fix or float the parameter
*/
void fixed(const Bool_t parFixed);
//! Set parameter as second-stage or not of the fit
/*!
\param [in] secondStagePar boolean flag to check whether is a second-stage parameter
*/
void secondStage(const Bool_t secondStagePar);
//! Add a Gaussian constraint (or modify an existing one)
/*!
\param [in] newGaussMean the new value of the Gaussian constraint mean
\param [in] newGaussWidth the new value of the Gaussian constraint width
*/
void addGaussianConstraint(const Double_t newGaussMean, const Double_t newGaussWidth);
//! Remove the Gaussian constraint
void removeGaussianConstraint();
//! Generate per-experiment constraint mean
void generateConstraintMean() override;
//! Blind the parameter
/*!
See LauBlind documentation for details of blinding procedure
\param [in] blindingString the unique blinding string used to seed the random number generator
\param [in] width the width of the Gaussian from which the offset should be sampled
\param [in] flipSign activate possible random sign flip (off by default)
*/
void blindParameter(const TString& blindingString, const Double_t width, const Bool_t flipSign = kFALSE);
// functions for the cloning mechanism
//! Check whether is a clone or not
/*!
\return true/false whether is a clone
*/
Bool_t clone() const {return (parent_ != nullptr);}
//! Method to create a clone from the parent parameter using the copy constructor
/*!
\param [in] constFactor the optional constant factor by which the clone shold be multiplied
\return the cloned parameter
*/
LauParameter* createClone(const Double_t constFactor = 1.0);
//! Method to create a clone from the parent parameter using the copy constructor and setting a new name
/*!
\param [in] newName the new name of the cloned parameter
\param [in] constFactor the optional constant factor by which the clone shold be multiplied
\return the cloned parameter
*/
LauParameter* createClone(const TString& newName, const Double_t constFactor = 1.0);
//! The parent parameter
/*!
\return the parent parameter
*/
LauParameter* parent() const {return parent_;}
//! Call to update the bias and pull values
void updatePull();
//! Randomise the value of the parameter (if it is floating).
/*!
The pre-defined parameter range is used as the randomisation range.
*/
void randomiseValue();
//! Randomise the value of the parameter (if it is floating).
/*!
Use the given range unless either of the given values are
outside the range of the parameter, in which case that value
will be altered to the current max or min.
\param [in] minVal the minimum value for the parameter
\param [in] maxVal the maximum value for the parameter
*/
void randomiseValue(const Double_t minVal, const Double_t maxVal);
//! Write state to a JSON record
/*!
\param [in,out] j the JSON record to write to
*/
void serialiseToJson( nlohmann::json& j ) const override;
protected:
//! Method to check whether value provided is within the range and that the minimum and maximum limits make sense
/*!
\param [in] val the value of the parameter
\param [in] minVal the minimum value allowed
\param [in] maxVal the maximum value allowed
*/
void checkRange(const Double_t val, const Double_t minVal, const Double_t maxVal);
//! Method to check whether value provided is whithin the range and that the minimum and maximum limits make sense
void checkRange()
{
this->checkRange(this->value(),this->minValue(),this->maxValue());
}
//! Mark this as a clone of the given parent
/*!
\param theparent the parent parameter
*/
void clone(LauParameter* theparent)
{
parent_ = theparent;
}
//! Method to remove a clone from the list of clones
/*!
This is used in the destructor to allow a clone to inform its parent it is no longer around
\param [in] clone the clone to be removed from the list
*/
void removeFromCloneList(LauParameter* clone)
{
auto iter = clones_.find( clone );
if ( iter != clones_.end() ) {
clones_.erase( iter );
}
}
//! Method to clear the clone parameters
void wipeClones() {clones_.clear();}
//! Method to update clone values
/*!
\param [in] justValue boolean flag to determine whether it is necessary to update all the parameter settings or only its value.
*/
void updateClones(const Bool_t justValue);
private:
//! LauFitNtuple is a friend class
friend class LauFitNtuple;
+ //! LauFitObject is a friend class
+ friend class LauFitObject;
+
//! The output streaming operator function is also a friend
friend std::ostream& operator << (std::ostream& stream, const LauParameter& par);
//! The parameter name
TString name_;
//! The parameter value
Double_t value_{0.0};
//! The error on the parameter
Double_t error_{0.0};
//! The lower error on the parameter
Double_t negError_{0.0};
//! The upper error on the parameter
Double_t posError_{0.0};
//! Toy generation value
Double_t genValue_{0.0};
//! Initial fit value
Double_t initValue_{0.0};
//! Minimum value for the parameter
Double_t minValue_{0.0};
//! Maximum value for the parameter
Double_t maxValue_{0.0};
//! Fix/float option for parameter
Bool_t fixed_{kTRUE};
//! Flag whether it is floated only in the second stage of the fit
Bool_t secondStage_{kFALSE};
//! Choice to use Gaussian constraint
Bool_t gaussConstraint_{kFALSE};
//! True mean of the Gaussian constraint
Double_t constraintTrueMean_{0.0};
//! Mean value of the Gaussian constraint
Double_t constraintMean_{0.0};
//! Width of the Gaussian constraint
Double_t constraintWidth_{0.0};
//! Global correlation coefficient
Double_t gcc_{0.0};
//! Parameter bias
Double_t bias_{0.0};
//! Parameter pull
Double_t pull_{0.0};
//! The parent parameter
LauParameter* parent_{nullptr};
//! The clones of this parameter
std::map<LauParameter*, Double_t> clones_;
//! The blinding engine
LauBlind blinder_;
ClassDefOverride(LauParameter, 5)
};
//! Output stream operator
std::ostream& operator << (std::ostream& stream, const LauParameter& par);
//! Type to define an array of parameters
typedef std::vector< std::vector<LauParameter> > LauParArray;
//! \cond DOXYGEN_IGNORE
namespace nlohmann {
template <>
struct adl_serializer<LauParameter> {
static LauParameter from_json(const json& j);
};
}
//! \endcond
#endif
diff --git a/inc/LauSimFitCoordinator.hh b/inc/LauSimFitCoordinator.hh
index b49bddf..7f4f1a3 100644
--- a/inc/LauSimFitCoordinator.hh
+++ b/inc/LauSimFitCoordinator.hh
@@ -1,232 +1,230 @@
/*
Copyright 2013 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauSimFitCoordinator.hh
\brief File containing declaration of LauSimFitCoordinator class.
*/
/*! \class LauSimFitCoordinator
\brief The coordinator process for simultaneous/combined fits
Implementation of the JFit method described in arXiv:1409.5080 [physics.data-an].
This class acts as the interface between the task processes and the minimiser.
*/
#ifndef LAU_SIM_FIT_COORDINATOR
#define LAU_SIM_FIT_COORDINATOR
#include <map>
#include <vector>
#include "TMatrixD.h"
#include "TStopwatch.h"
#include "TString.h"
#include "LauFitObject.hh"
class TMessage;
class TMonitor;
class TSocket;
class LauAbsRValue;
class LauParameter;
class LauFitNtuple;
class LauSimFitCoordinator : public LauFitObject {
public:
//! Constructor
/*!
\param [in] numTasks the number of tasks processes to expect connections from
\param [in] port the port on which to listen for connections from the tasks
*/
LauSimFitCoordinator( UInt_t numTasks, UInt_t port = 9090 );
//! Destructor
virtual ~LauSimFitCoordinator();
//! Run the fit
/*!
\param [in] fitNtupleFileName the file to which the ntuple containing the fit results should be written
- \param [in] nExp the number of experiments to be fitted
- \param [in] firstExp the ID of the first experiment to be fitted
\param [in] useAsymmErrors should asymmetric errors be calculated or not
\param [in] doTwoStageFit should the fit be performed in two stages or not
*/
- void runSimFit( const TString& fitNtupleFileName, const UInt_t nExp, const UInt_t firstExp = 0, const Bool_t useAsymmErrors = kFALSE, const Bool_t doTwoStageFit = kFALSE );
+ void runSimFit( const TString& fitNtupleFileName, const Bool_t useAsymmErrors = kFALSE, const Bool_t doTwoStageFit = kFALSE );
//! Mark that the fit is calculating asymmetric errors
/*!
This function has to be public since it is called by
the fitter interface to mark when entering and exiting
the asymmetric error calculation.
It should not be called otherwise!
\param [in] inAsymErrCalc boolean marking that the fit is calculating the asymmetric errors
*/
virtual void withinAsymErrorCalc(const Bool_t inAsymErrCalc);
// Need to unshadow the query method defined in the base class
using LauFitObject::withinAsymErrorCalc;
//! This function sets the parameter values from Minuit
/*!
This function has to be public since it is called from the global FCN.
It should not be called otherwise!
\param [in] par an array storing the various parameter values
\param [in] npar the number of free parameters
*/
virtual void setParsFromMinuit(Double_t* par, Int_t npar);
//! Calculate the new value of the negative log likelihood
/*!
This function has to be public since it is called from the global FCN.
It should not be called otherwise!
*/
virtual Double_t getTotNegLogLikelihood();
protected:
//! Print information on the parameters
void printParInfo() const;
//! Initialise
void initialise();
//! Initialise socket connections for the tasks
void initSockets();
//! Determine/update the parameter initial values from all tasks
void getParametersFromTasks();
//! Determine the parameter names and initial values from all tasks
void getParametersFromTasksFirstTime();
//! Update and verify the parameter initial values from all tasks
void updateParametersFromTasks();
//! Check for compatibility between two same-named parameters, which should therefore be identical
void checkParameter( const LauParameter* param, UInt_t index ) const;
//! Add parameters to the list of Gaussian constrained parameters
void addConParameters();
//! Calculate the penalty terms to the log likelihood from Gaussian constraints
Double_t getLogLikelihoodPenalty();
//! Instruct the tasks to read the input data for the given experiment
/*!
\return success/failure of the reading operations
*/
Bool_t readData();
//! Instruct the tasks to perform the caching
/*!
\return success/failure of the caching operations
*/
Bool_t cacheInputData();
//! Perform the fit for the current experiment
void fitExpt();
//! Instruct the tasks to update the initial fit parameter values, if required
void checkInitFitParams();
//! Return the final parameters to the tasks and instruct them to perform their finalisation
Bool_t finalise();
//! Instruct the tasks to write out the fit results
Bool_t writeOutResults();
private:
//! Copy constructor (not implemented)
LauSimFitCoordinator(const LauSimFitCoordinator& rhs);
//! Copy assignment operator (not implemented)
LauSimFitCoordinator& operator=(const LauSimFitCoordinator& rhs);
//! The number of tasks
const UInt_t nTasks_;
//! The requested port
const UInt_t reqPort_;
//! The covariance sub-matrices for each task
std::vector<TMatrixD> covMatrices_;
//! Parallel setup monitor
TMonitor* socketMonitor_;
//! Sockets for each of the tasks
std::vector<TSocket*> socketTasks_;
//! Messages to tasks
std::vector<TMessage*> messagesToTasks_;
//! Message from tasks to the coordinator
TMessage* messageFromTask_;
//! Map of parameter names to index in the values vector
std::map< TString, UInt_t > parIndices_;
//! Reverse map of index in the values vector to parameter names
std::map< UInt_t, TString > parNames_;
//! Parameters
std::vector<LauParameter*> params_;
//! Gaussian constraints
std::vector<LauAbsRValue*> conVars_;
//! Parameter values
std::vector<Double_t> parValues_;
//! Lists of indices for each task
std::vector< std::vector<UInt_t> > taskIndices_;
//! Lists of indices of free parameters for each task
std::vector< std::vector<UInt_t> > taskFreeIndices_;
//! Parameter values to send to the tasks
std::vector<Double_t*> vectorPar_;
//! Likelihood values returned from the tasks
std::vector<Double_t> vectorRes_;
//! The fit timer
TStopwatch timer_;
//! The total fit timer
TStopwatch cumulTimer_;
//! The fit results ntuple
LauFitNtuple* fitNtuple_;
ClassDef(LauSimFitCoordinator,0)
};
#endif
diff --git a/src/LauBsCPFitModel.cc b/src/LauBsCPFitModel.cc
index 4aee87a..cf0c8fe 100644
--- a/src/LauBsCPFitModel.cc
+++ b/src/LauBsCPFitModel.cc
@@ -1,2680 +1,2678 @@
/*
Copyright 2015 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauBsCPFitModel.cc
\brief File containing implementation of LauBsCPFitModel class.
*/
#include "LauBsCPFitModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauAsymmCalc.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDPPartialIntegralInfo.hh"
#include "LauDaughters.hh"
#include "LauEffModel.hh"
#include "LauFitNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TFile.h"
#include "TH2.h"
#include "TMath.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
ClassImp(LauBsCPFitModel)
LauBsCPFitModel::LauBsCPFitModel(LauIsobarDynamics* negModel, LauIsobarDynamics* posModel, Double_t D) : LauAbsFitModel(),
negSigModel_(negModel), posSigModel_(posModel),
negKinematics_(negModel ? negModel->getKinematics() : 0),
posKinematics_(posModel ? posModel->getKinematics() : 0),
D_(D),
usingBkgnd_(kFALSE),
nSigComp_(0),
nSigDPPar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
interTermReNorm_(0.0),
normDP_(0.0),
negMeanEff_("negMeanEff",0.0,0.0,1.0), posMeanEff_("posMeanEff",0.0,0.0,1.0),
negDPRate_("negDPRate",0.0,0.0,100.0), posDPRate_("posDPRate",0.0,0.0,100.0),
signalEvents_(0),
useSCF_(kFALSE),
useSCFHist_(kFALSE),
scfFrac_("scfFrac",0.0,0.0,1.0),
scfFracHist_(0),
scfMap_(0),
compareFitData_(kFALSE),
negParent_("B_s0_bar"), posParent_("B_s0"),
iterationsMax_(100000),
nGenLoop_(0),
ASq_(0.0),
aSqMaxVar_(0.0),
aSqMaxSet_(1.25),
sigDPLike_(0.0),
scfDPLike_(0.0),
sigExtraLike_(0.0),
scfExtraLike_(0.0),
sigTotalLike_(0.0),
scfTotalLike_(0.0)
{
const LauDaughters* negDaug = negSigModel_->getDaughters();
if (negDaug != 0) {negParent_ = negDaug->getNameParent();}
const LauDaughters* posDaug = posSigModel_->getDaughters();
if (posDaug != 0) {posParent_ = posDaug->getNameParent();}
}
LauBsCPFitModel::~LauBsCPFitModel()
{
delete scfFracHist_;
}
void LauBsCPFitModel::setupBkgndVectors()
{
UInt_t nBkgnds = this->nBkgndClasses();
bkgndDPModels_.resize( nBkgnds );
bkgndPdfs_.resize( nBkgnds );
bkgndEvents_.resize( nBkgnds );
bkgndDPLike_.resize( nBkgnds );
bkgndExtraLike_.resize( nBkgnds );
bkgndTotalLike_.resize( nBkgnds );
}
void LauBsCPFitModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauBsCPFitModel::setNSigEvents : The signal yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauBsCPFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
TString name = nSigEvents->name();
if ( ! name.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauBsCPFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
}
void LauBsCPFitModel::setNBkgndEvents( LauAbsRValue* nBkgndEvents )
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauBsCPFitModel::setNBgkndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauBsCPFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauBsCPFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
}
void LauBsCPFitModel::splitSignalComponent( const TH2* dpHisto, const Bool_t upperHalf, const Bool_t fluctuateBins, LauScfMap* scfMap )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauBsCPFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
if ( dpHisto == 0 ) {
std::cerr << "ERROR in LauBsCPFitModel::splitSignalComponent : The histogram pointer is null." << std::endl;
return;
}
const LauDaughters* daughters = negSigModel_->getDaughters();
scfFracHist_ = new LauEffModel( daughters, 0 );
scfFracHist_->setEffHisto( dpHisto, kTRUE, fluctuateBins, 0.0, 0.0, upperHalf, daughters->squareDP() );
scfMap_ = scfMap;
useSCF_ = kTRUE;
useSCFHist_ = kTRUE;
}
void LauBsCPFitModel::splitSignalComponent( const Double_t scfFrac, const Bool_t fixed )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauBsCPFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
scfFrac_.range( 0.0, 1.0 );
scfFrac_.value( scfFrac ); scfFrac_.initValue( scfFrac ); scfFrac_.genValue( scfFrac );
scfFrac_.fixed( fixed );
useSCF_ = kTRUE;
useSCFHist_ = kFALSE;
}
void LauBsCPFitModel::setBkgndDPModel(const TString& bkgndClass, LauAbsBkgndDPModel* model)
{
if (model==0){
std::cerr << "ERROR in LauBsCPFitModel::setBkgndDPModels : the model pointer is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass) ) {
std::cerr << "ERROR in LauBsCPFitModel::setBkgndDPModel : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
bkgndDPModels_[bkgndID] = model;
usingBkgnd_ = kTRUE;
}
void LauBsCPFitModel::setSignalPdf(LauAbsPdf* pdf)
{
// if we're doing an untagged analysis we will only use the negative PDFs
if ( pdf==0 ) {
std::cerr << "ERROR in LauBsCPFitModel::setSignalPdfs : The PDF pointer is null." << std::endl;
return;
}
signalPdfs_.push_back(pdf);
}
void LauBsCPFitModel::setSCFPdf(LauAbsPdf* pdf)
{
// if we're doing an untagged analysis we will only use the negative PDFs
if ( pdf==0 ) {
std::cerr << "ERROR in LauBsCPFitModel::setSCFPdfs : The PDF pointer is null." << std::endl;
return;
}
scfPdfs_.push_back(pdf);
}
void LauBsCPFitModel::setBkgndPdf(const TString& bkgndClass, LauAbsPdf* pdf)
{
// if we're doing an untagged analysis we will only use the negative PDFs
if ( pdf==0 ) {
std::cerr << "ERROR in LauBsCPFitModel::setBkgndPdfs : The PDF pointer is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauBsCPFitModel::setBkgndPdfs : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
bkgndPdfs_[bkgndID].push_back(pdf);
usingBkgnd_ = kTRUE;
}
void LauBsCPFitModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
// Is there a component called compName in the signal model?
const TString compName{ coeffSet->name() };
const Bool_t negOK { negSigModel_->hasResonance(compName) };
const Bool_t posOK { posSigModel_->hasResonance(compName) };
if (!negOK) {
std::cerr << "ERROR in LauBsCPFitModel::setMagPhase : " << negParent_ << " signal DP model doesn't contain component \"" << compName << "\"." << std::endl;
return;
}
if (!posOK) {
std::cerr << "ERROR in LauBsCPFitModel::setMagPhase : " << posParent_ << " signal DP model doesn't contain component \"" << compName << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
for ( const auto& coeff : coeffPars_ ) {
if ( coeff->name() == compName ) {
std::cerr << "ERROR in LauBsCPFitModel::setAmpCoeffSet : Have already set coefficients for \"" << compName << "\"." << std::endl;
return;
}
}
coeffSet->index(nSigComp_);
const TString parName { coeffSet->baseName() + "FitFracAsym" };
fitFracAsymm_.emplace_back(parName, 0.0, -1.0, 1.0);
acp_.push_back(coeffSet->acp());
++nSigComp_;
std::cout << "INFO in LauBsCPFitModel::setAmpCoeffSet : Added coefficients for component \"" << compName << "\" to the fit model." << std::endl;
coeffSet->printParValues();
coeffPars_.push_back( std::move(coeffSet) );
}
std::vector<const LauAbsCoeffSet*> LauBsCPFitModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_.size(), nullptr );
std::transform( coeffPars_.begin(), coeffPars_.end(), coeffs.begin(), [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); } );
return coeffs;
}
void LauBsCPFitModel::initialise()
{
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// Initialisation
if (this->useDP() == kTRUE) {
this->initialiseDPModels();
}
if (!this->useDP() && signalPdfs_.empty()) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : Signal model doesn't exist for any variable." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( this->useDP() ) {
// Check that we have all the Dalitz-plot models
if ((negSigModel_ == 0) || (posSigModel_ == 0)) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : the pointer to one (neg or pos) of the signal DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
}
if ( usingBkgnd_ ) {
if ( bkgndDPModels_.empty() ) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : No background DP models found.\n";
std::cerr << " : Removing the Dalitz plot from the model." << std::endl;
this->useDP(kFALSE);
}
for (LauBkgndDPModelList::const_iterator dpmodel_iter = bkgndDPModels_.begin(); dpmodel_iter != bkgndDPModels_.end(); ++dpmodel_iter ) {
if ( (*dpmodel_iter) == 0 ) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : The pointer to one of the background DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
break;
}
}
}
}
// Next check that, if a given component is being used we've got the
// right number of PDFs for all the variables involved
// TODO - should probably check variable names and so on as well
UInt_t nsigpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nsigpdfvars;
}
}
}
if (useSCF_) {
UInt_t nscfpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = scfPdfs_.begin(); pdf_iter != scfPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nscfpdfvars;
}
}
}
if (nscfpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : There are " << nsigpdfvars << " TM signal PDF variables but " << nscfpdfvars << " SCF signal PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
if (usingBkgnd_) {
for (LauBkgndPdfsList::const_iterator bgclass_iter = bkgndPdfs_.begin(); bgclass_iter != bkgndPdfs_.end(); ++bgclass_iter) {
UInt_t nbkgndpdfvars(0);
const LauPdfPList& pdfList = (*bgclass_iter);
for ( LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nbkgndpdfvars;
}
}
}
if (nbkgndpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : There are " << nsigpdfvars << " signal PDF variables but " << nbkgndpdfvars << " bkgnd PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
}
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the various extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nExtraPdfPar_ + nNormPar_)) {
std::cerr << "ERROR in LauBsCPFitModel::initialise : Number of fit parameters not of expected size. Exiting" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauBsCPFitModel::recalculateNormalisation()
{
//std::cout << "INFO in LauBsCPFitModel::recalculateNormalizationInDPModels : Recalc Norm in DP model" << std::endl;
negSigModel_->recalculateNormalisation();
posSigModel_->recalculateNormalisation();
negSigModel_->modifyDataTree();
posSigModel_->modifyDataTree();
this->calcInterferenceTermIntegrals();
}
void LauBsCPFitModel::initialiseDPModels()
{
// Need to check that the number of components we have and that the dynamics has matches up
UInt_t nNegAmp = negSigModel_->getnTotAmp();
UInt_t nPosAmp = posSigModel_->getnTotAmp();
if ( nNegAmp != nPosAmp ) {
std::cerr << "ERROR in LauBsCPFitModel::initialiseDPModels : Unequal number of signal DP components in the negative and positive models: " << nNegAmp << " != " << nPosAmp << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( nNegAmp != nSigComp_ ) {
std::cerr << "ERROR in LauBsCPFitModel::initialiseDPModels : Number of signal DP components in the model (" << nNegAmp << ") not equal to number of coefficients supplied (" << nSigComp_ << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
std::cout << "INFO in LauBsCPFitModel::initialiseDPModels : Initialising signal DP model" << std::endl;
negSigModel_->initialise(negCoeffs_);
posSigModel_->initialise(posCoeffs_);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = bkgndDPModels_.begin(); iter != bkgndDPModels_.end(); ++iter) {
(*iter)->initialise();
}
}
fifjEffSum_.clear();
fifjEffSum_.resize(nSigComp_);
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
fifjEffSum_[iAmp].resize(nSigComp_);
}
// calculate the integrals of the A*Abar terms
this->calcInterferenceTermIntegrals();
this->calcInterTermNorm();
this->calculateAmplitudeNorm();
}
void LauBsCPFitModel::calcInterferenceTermIntegrals()
{
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0bar = negSigModel_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0 = posSigModel_->getIntegralInfos();
LauComplex A, Abar, fifjEffSumTerm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
fifjEffSum_[iAmp][jAmp].zero();
}
}
const UInt_t nIntegralRegions = integralInfoListB0bar.size();
for ( UInt_t iRegion(0); iRegion < nIntegralRegions; ++iRegion ) {
const LauDPPartialIntegralInfo* integralInfoB0bar = integralInfoListB0bar[iRegion];
const LauDPPartialIntegralInfo* integralInfoB0 = integralInfoListB0[iRegion];
const UInt_t nm13Points = integralInfoB0bar->getnm13Points();
const UInt_t nm23Points = integralInfoB0bar->getnm23Points();
for (UInt_t m13 = 0; m13 < nm13Points; ++m13) {
for (UInt_t m23 = 0; m23 < nm23Points; ++m23) {
const Double_t weight = integralInfoB0bar->getWeight(m13,m23);
const Double_t eff = integralInfoB0bar->getEfficiency(m13,m23);
const Double_t effWeight = eff*weight;
// TODO - do we need to check that this point is within the DP or will the values stored for such points be appropriate? Or are they random junk?
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
A = integralInfoB0->getAmplitude(m13, m23, iAmp);
// TODO - this loop over j perhaps needs only to be from jAmp = iAmp (we only need the upper half of the matrix)
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
Abar = integralInfoB0bar->getAmplitude(m13, m23, jAmp);
fifjEffSumTerm = Abar*A.conj();
fifjEffSumTerm.rescale(effWeight);
fifjEffSum_[iAmp][jAmp] += fifjEffSumTerm;
}
}
}
}
}
}
void LauBsCPFitModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauBsCPFitModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; i++) {
LauParameterPList pars = coeffPars_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit
// variables and in a separate container of resonance parameters
LauParameterPList& negSigDPPars = negSigModel_->getFloatingParameters();
LauParameterPList& posSigDPPars = posSigModel_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( negSigDPPars );
nSigDPPar_ += this->addResonanceParameters( posSigDPPars );
}
void LauBsCPFitModel::setExtraPdfParameters()
{
// Include all the parameters of the PDF in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
nExtraPdfPar_ += this->addFitParameters(signalPdfs_);
if (useSCF_ == kTRUE) {
nExtraPdfPar_ += this->addFitParameters(scfPdfs_);
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
nExtraPdfPar_ += this->addFitParameters(*iter);
}
}
}
void LauBsCPFitModel::setFitNEvents()
{
if ( signalEvents_ == 0 ) {
std::cerr << "ERROR in LauBsCPFitModel::setFitNEvents : Signal yield not defined." << std::endl;
return;
}
nNormPar_ = 0;
// initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
nTotEvts += (*iter)->value();
if ( (*iter) == 0 ) {
std::cerr << "ERROR in LauBsCPFitModel::setFitNEvents : Background yield not defined." << std::endl;
return;
}
}
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the number of signal events into the fit parameters
if (this->doEMLFit()) {
std::cout << "INFO in LauBsCPFitModel::setFitNEvents : Initialising number of events for signal and background components..." << std::endl;
// add the signal fraction to the list of fit parameters
nNormPar_ += this->addFitParameters( signalEvents_ );
} else {
std::cout << "INFO in LauBsCPFitModel::setFitNEvents : Initialising number of events for background components (and hence signal)..." << std::endl;
}
if (useSCF_ && !useSCFHist_) {
nNormPar_ += this->addFitParameters( &scfFrac_ );
}
if (usingBkgnd_ == kTRUE) {
nNormPar_ += this->addFitParameters( bkgndEvents_ );
}
}
void LauBsCPFitModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the positive and negative fit fractions for each signal component
negFitFrac_ = negSigModel_->getFitFractions();
if (negFitFrac_.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFrac_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFrac_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
posFitFrac_ = posSigModel_->getFitFractions();
if (posFitFrac_.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFrac_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFrac_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Add the positive and negative fit fractions that have not been corrected for the efficiency for each signal component
negFitFracEffUnCorr_ = negSigModel_->getFitFractionsEfficiencyUncorrected();
if (negFitFracEffUnCorr_.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFracEffUnCorr_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
posFitFracEffUnCorr_ = posSigModel_->getFitFractionsEfficiencyUncorrected();
if (posFitFracEffUnCorr_.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFracEffUnCorr_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
TString name = negFitFrac_[i][j].name();
name.Insert( name.Index("FitFrac"), "Neg" );
negFitFrac_[i][j].name(name);
extraVars.push_back(negFitFrac_[i][j]);
name = posFitFrac_[i][j].name();
name.Insert( name.Index("FitFrac"), "Pos" );
posFitFrac_[i][j].name(name);
extraVars.push_back(posFitFrac_[i][j]);
name = negFitFracEffUnCorr_[i][j].name();
name.Insert( name.Index("FitFrac"), "Neg" );
negFitFracEffUnCorr_[i][j].name(name);
extraVars.push_back(negFitFracEffUnCorr_[i][j]);
name = posFitFracEffUnCorr_[i][j].name();
name.Insert( name.Index("FitFrac"), "Pos" );
posFitFracEffUnCorr_[i][j].name(name);
extraVars.push_back(posFitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> negExtraPars = negSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator negExtraIter;
for (negExtraIter = negExtraPars.begin(); negExtraIter != negExtraPars.end(); ++negExtraIter) {
LauParameter negExtraParameter = (*negExtraIter);
extraVars.push_back(negExtraParameter);
}
std::vector<LauParameter> posExtraPars = posSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator posExtraIter;
for (posExtraIter = posExtraPars.begin(); posExtraIter != posExtraPars.end(); ++posExtraIter) {
LauParameter posExtraParameter = (*posExtraIter);
extraVars.push_back(posExtraParameter);
}
// Now add in the DP efficiency value
Double_t initMeanEff = negSigModel_->getMeanEff().initValue();
negMeanEff_.value(initMeanEff);
negMeanEff_.genValue(initMeanEff);
negMeanEff_.initValue(initMeanEff);
extraVars.push_back(negMeanEff_);
initMeanEff = posSigModel_->getMeanEff().initValue();
posMeanEff_.value(initMeanEff);
posMeanEff_.genValue(initMeanEff);
posMeanEff_.initValue(initMeanEff);
extraVars.push_back(posMeanEff_);
// Also add in the DP rates
Double_t initDPRate = negSigModel_->getDPRate().initValue();
negDPRate_.value(initDPRate);
negDPRate_.genValue(initDPRate);
negDPRate_.initValue(initDPRate);
extraVars.push_back(negDPRate_);
initDPRate = posSigModel_->getDPRate().initValue();
posDPRate_.value(initDPRate);
posDPRate_.genValue(initDPRate);
posDPRate_.initValue(initDPRate);
extraVars.push_back(posDPRate_);
// Calculate the CPC and CPV Fit Fractions, ACPs and FitFrac asymmetries
this->calcExtraFractions(kTRUE);
this->calcAsymmetries(kTRUE);
// Add the CP violating and CP conserving fit fractions for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j = i; j < nSigComp_; j++) {
extraVars.push_back(CPVFitFrac_[i][j]);
}
}
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j = i; j < nSigComp_; j++) {
extraVars.push_back(CPCFitFrac_[i][j]);
}
}
// Add the Fit Fraction asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(fitFracAsymm_[i]);
}
// Add the calculated CP asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(acp_[i]);
}
}
void LauBsCPFitModel::calcExtraFractions(Bool_t initValues)
{
// Calculate the CP-conserving and CP-violating fit fractions
if (initValues) {
// create the structure
CPCFitFrac_.clear();
CPVFitFrac_.clear();
CPCFitFrac_.resize(nSigComp_);
CPVFitFrac_.resize(nSigComp_);
for (UInt_t i(0); i<nSigComp_; ++i) {
CPCFitFrac_[i].resize(nSigComp_);
CPVFitFrac_[i].resize(nSigComp_);
for (UInt_t j(i); j<nSigComp_; ++j) {
TString name = negFitFrac_[i][j].name();
name.Replace( name.Index("Neg"), 3, "CPC" );
CPCFitFrac_[i][j].name( name );
CPCFitFrac_[i][j].valueAndRange( 0.0, -100.0, 100.0 );
name = negFitFrac_[i][j].name();
name.Replace( name.Index("Neg"), 3, "CPV" );
CPVFitFrac_[i][j].name( name );
CPVFitFrac_[i][j].valueAndRange( 0.0, -100.0, 100.0 );
}
}
}
Double_t denom = negDPRate_.value() + posDPRate_.value();
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
Double_t negTerm = negFitFrac_[i][j].value()*negDPRate_.value();
Double_t posTerm = posFitFrac_[i][j].value()*posDPRate_.value();
Double_t cpcFitFrac = (negTerm + posTerm)/denom;
Double_t cpvFitFrac = (negTerm - posTerm)/denom;
CPCFitFrac_[i][j].value(cpcFitFrac);
CPVFitFrac_[i][j].value(cpvFitFrac);
if (initValues) {
CPCFitFrac_[i][j].genValue(cpcFitFrac);
CPCFitFrac_[i][j].initValue(cpcFitFrac);
CPVFitFrac_[i][j].genValue(cpvFitFrac);
CPVFitFrac_[i][j].initValue(cpvFitFrac);
}
}
}
}
void LauBsCPFitModel::calcAsymmetries(Bool_t initValues)
{
// Calculate the CP asymmetries
// Also calculate the fit fraction asymmetries
for (UInt_t i = 0; i < nSigComp_; i++) {
acp_[i] = coeffPars_[i]->acp();
LauAsymmCalc asymmCalc(negFitFrac_[i][i].value(), posFitFrac_[i][i].value());
Double_t asym = asymmCalc.getAsymmetry();
fitFracAsymm_[i].value(asym);
if (initValues) {
fitFracAsymm_[i].genValue(asym);
fitFracAsymm_[i].initValue(asym);
}
}
}
void LauBsCPFitModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// i.e. to make mag > 0.0, phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; ++i) {
// Check whether we have "a/b > 0.0", and phases in the right range
coeffPars_[i]->finaliseValues();
}
}
// update the pulls on the event fractions and asymmetries
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (useSCF_ && !useSCFHist_) {
scfFrac_.updatePull();
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndYieldList::iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
std::vector<LauParameter*> parameters = (*iter)->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
}
// Update the pulls on all the extra PDFs' parameters
this->updateFitParameters(signalPdfs_);
if (useSCF_ == kTRUE) {
this->updateFitParameters(scfPdfs_);
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->updateFitParameters(*iter);
}
}
// Fill the fit results to the ntuple
// update the coefficients and then calculate the fit fractions and ACP's
if (this->useDP() == kTRUE) {
this->updateCoeffs();
negSigModel_->updateCoeffs(negCoeffs_); negSigModel_->calcExtraInfo();
posSigModel_->updateCoeffs(posCoeffs_); posSigModel_->calcExtraInfo();
LauParArray negFitFrac = negSigModel_->getFitFractions();
if (negFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFrac = posSigModel_->getFitFractions();
if (posFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray negFitFracEffUnCorr = negSigModel_->getFitFractionsEfficiencyUncorrected();
if (negFitFracEffUnCorr.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFracEffUnCorr[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFracEffUnCorr = posSigModel_->getFitFractionsEfficiencyUncorrected();
if (posFitFracEffUnCorr.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFracEffUnCorr[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
negFitFrac_[i][j].value(negFitFrac[i][j].value());
posFitFrac_[i][j].value(posFitFrac[i][j].value());
negFitFracEffUnCorr_[i][j].value(negFitFracEffUnCorr[i][j].value());
posFitFracEffUnCorr_[i][j].value(posFitFracEffUnCorr[i][j].value());
}
}
negMeanEff_.value(negSigModel_->getMeanEff().value());
posMeanEff_.value(posSigModel_->getMeanEff().value());
negDPRate_.value(negSigModel_->getDPRate().value());
posDPRate_.value(posSigModel_->getDPRate().value());
this->calcExtraFractions();
this->calcAsymmetries();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions, FF asymmetries, ACPs, mean efficiency and DP rate)
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the positive and negative fit fractions for each signal component
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(negFitFrac_[i][j]);
extraVars.push_back(posFitFrac_[i][j]);
extraVars.push_back(negFitFracEffUnCorr_[i][j]);
extraVars.push_back(posFitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> negExtraPars = negSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator negExtraIter;
for (negExtraIter = negExtraPars.begin(); negExtraIter != negExtraPars.end(); ++negExtraIter) {
LauParameter negExtraParameter = (*negExtraIter);
extraVars.push_back(negExtraParameter);
}
std::vector<LauParameter> posExtraPars = posSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator posExtraIter;
for (posExtraIter = posExtraPars.begin(); posExtraIter != posExtraPars.end(); ++posExtraIter) {
LauParameter posExtraParameter = (*posExtraIter);
extraVars.push_back(posExtraParameter);
}
extraVars.push_back(negMeanEff_);
extraVars.push_back(posMeanEff_);
extraVars.push_back(negDPRate_);
extraVars.push_back(posDPRate_);
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(CPVFitFrac_[i][j]);
}
}
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(CPCFitFrac_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(fitFracAsymm_[i]);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(acp_[i]);
}
this->printFitFractions(std::cout);
this->printAsymmetries(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauBsCPFitModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
// First for the B- events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << negParent_ << " FitFraction for component " << i << " (" << compName << ") = " << negFitFrac_[i][i].value() << std::endl;
}
output << negParent_ << " overall DP rate (integral of matrix element squared) = " << negDPRate_.value() << std::endl;
output << negParent_ << " average efficiency weighted by whole DP dynamics = " << negMeanEff_.value() << std::endl;
// Then for the positive sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
//const TString conjName(negSigModel_->getConjResName(compName));
output << posParent_ << " FitFraction for component " << i << " (" << compName << ") = " << posFitFrac_[i][i].value() << std::endl;
}
output << posParent_ << " overall DP rate (integral of matrix element squared) = " << posDPRate_.value() << std::endl;
output << posParent_ << " average efficiency weighted by whole DP dynamics = " << posMeanEff_.value() << std::endl;
}
void LauBsCPFitModel::printAsymmetries(std::ostream& output)
{
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << "Fit Fraction asymmetry for component " << i << " (" << compName << ") = " << fitFracAsymm_[i].value() << std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << "ACP for component " << i << " (" << compName << ") = " << acp_[i].value() << std::endl;
}
}
void LauBsCPFitModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
// TODO - need to include the yields in this table
std::ofstream fout(outputFile);
LauPrint print;
std::cout << "INFO in LauBsCPFitModel::writeOutTable : Writing out results of the fit to the tex file " << outputFile << std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->printTableRow(fout);
}
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
// print the fit fractions and asymmetries in another
fout << "\\begin{tabular}{|l|c|c|c|c|}" << std::endl;
fout << "\\hline" << std::endl;
fout << "Component & " << negParent_ << " Fit Fraction & " << posParent_ << " Fit Fraction & Fit Fraction Asymmetry & ACP \\\\" << std::endl;
fout << "\\hline" << std::endl;
Double_t negFitFracSum(0.0);
Double_t posFitFracSum(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
Double_t negFitFrac = negFitFrac_[i][i].value();
Double_t posFitFrac = posFitFrac_[i][i].value();
negFitFracSum += negFitFrac;
posFitFracSum += posFitFrac;
Double_t fitFracAsymm = fitFracAsymm_[i].value();
Double_t acp = acp_[i].value();
Double_t acpErr = acp_[i].error();
fout << resName << " & $";
print.printFormat(fout, negFitFrac);
fout << "$ & $";
print.printFormat(fout, posFitFrac);
fout << "$ & $";
print.printFormat(fout, fitFracAsymm);
fout << "$ & $";
print.printFormat(fout, acp);
fout << " \\pm ";
print.printFormat(fout, acpErr);
fout << "$ \\\\" << std::endl;
}
fout << "\\hline" << std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum & $";
print.printFormat(fout, negFitFracSum);
fout << "$ & $";
print.printFormat(fout, posFitFracSum);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "DP rate & $";
print.printFormat(fout, negDPRate_.value());
fout << "$ & $";
print.printFormat(fout, posDPRate_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > $ & $";
print.printFormat(fout, negMeanEff_.value());
fout << "$ & $";
print.printFormat(fout, posMeanEff_.value());
fout << "$ & & \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
}
if (!signalPdfs_.empty()) {
fout << "\\begin{tabular}{|l|c|}" << std::endl;
fout << "\\hline" << std::endl;
if (useSCF_ == kTRUE) {
fout << "\\Extra TM Signal PDFs' Parameters: & \\\\" << std::endl;
} else {
fout << "\\Extra Signal PDFs' Parameters: & \\\\" << std::endl;
}
this->printFitParameters(signalPdfs_, fout);
if (useSCF_ == kTRUE && !scfPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra SCF Signal PDFs' Parameters: & \\\\" << std::endl;
this->printFitParameters(scfPdfs_, fout);
}
if (usingBkgnd_ == kTRUE && !bkgndPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra Background PDFs' Parameters: & \\\\" << std::endl;
for (LauBkgndPdfsList::const_iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->printFitParameters(*iter, fout);
}
}
fout << "\\hline \n\\end{tabular}" << std::endl << std::endl;
}
}
void LauBsCPFitModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
std::cout << "INFO in LauBsCPFitModel::checkInitFitParams : Setting random parameters for the signal model" << std::endl;
this->randomiseInitFitPars();
}
}
void LauBsCPFitModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout << "INFO in LauBsCPFitModel::randomiseInitFitPars : Randomising the initial fit magnitudes and phases of the components..." << std::endl;
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
}
LauBsCPFitModel::LauGenInfo LauBsCPFitModel::eventsToGenerate()
{
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
LauGenInfo nEvtsGen;
// Keep track of whether any yield or asymmetry parameters are blinded
Bool_t blind = kFALSE;
// Signal
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0.0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
if ( signalEvents_->blind() ) {
blind = kTRUE;
}
Double_t asym(0.0);
Double_t sigAsym(0.0);
Double_t negRate = negSigModel_->getDPNorm();
Double_t posRate = posSigModel_->getDPNorm();
if (negRate+posRate>1e-30) {
sigAsym = (negRate-posRate)/(negRate+posRate);
}
asym = sigAsym;
Int_t nPosEvts = static_cast<Int_t>((nEvts/2.0 * (1.0 - asym)) + 0.5);
Int_t nNegEvts = static_cast<Int_t>((nEvts/2.0 * (1.0 + asym)) + 0.5);
if (this->doPoissonSmearing()) {
nNegEvts = LauRandom::randomFun()->Poisson(nNegEvts);
nPosEvts = LauRandom::randomFun()->Poisson(nPosEvts);
}
nEvtsGen[std::make_pair("signal",-1)] = std::make_pair(nNegEvts,evtWeight);
nEvtsGen[std::make_pair("signal",+1)] = std::make_pair(nPosEvts,evtWeight);
// backgrounds
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
const LauAbsRValue* evtsPar = bkgndEvents_[bkgndID];
if ( evtsPar->blind() ) {
blind = kTRUE;
}
evtWeight = 1.0;
nEvts = TMath::FloorNint( evtsPar->genValue() );
if ( nEvts < 0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
Int_t nBkgEvts = nEvts;
if (this->doPoissonSmearing()) {
nBkgEvts = LauRandom::randomFun()->Poisson(nBkgEvts);
}
nEvtsGen[std::make_pair(bkgndClass,+1)] = std::make_pair(nBkgEvts,evtWeight);
}
// Print out the information on what we're generating, but only if none of the parameters are blind (otherwise we risk unblinding them!)
if ( !blind ) {
std::cout << "INFO in LauBsCPFitModel::eventsToGenerate : Generating toy MC with:" << std::endl;
std::cout << " : Signal asymmetry = " << sigAsym << " and number of signal events = " << signalEvents_->genValue() << std::endl;
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
const LauAbsRValue* evtsPar = bkgndEvents_[bkgndID];
std::cout << " : Number of " << bkgndClass << " events = " << evtsPar->genValue() << std::endl;
}
}
return nEvtsGen;
}
Bool_t LauBsCPFitModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
LauGenInfo nEvts = this->eventsToGenerate();
Bool_t genOK(kTRUE);
Int_t evtNum(0);
const UInt_t nBkgnds = this->nBkgndClasses();
std::vector<TString> bkgndClassNames(nBkgnds);
std::vector<TString> bkgndClassNamesGen(nBkgnds);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
bkgndClassNames[iBkgnd] = name;
bkgndClassNamesGen[iBkgnd] = "gen"+name;
}
const Bool_t storeSCFTruthInfo = useSCF_;
// Loop over the hypotheses and generate the requested number of events for each one
for (LauGenInfo::const_iterator iter = nEvts.begin(); iter != nEvts.end(); ++iter) {
const TString& type(iter->first.first);
Double_t evtWeight( iter->second.second );
Int_t nEvtsGen( iter->second.first );
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
this->setGenNtupleDoubleBranchValue( "efficiency", 1.0 );
if (type == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], 0 );
}
genOK = this->generateSignalEvent();
this->setGenNtupleDoubleBranchValue( "efficiency", negSigModel_->getEvtEff() );
} else {
this->setGenNtupleIntegerBranchValue("genSig",0);
if ( storeSCFTruthInfo ) {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
}
UInt_t bkgndID(0);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
Int_t gen(0);
if ( bkgndClassNames[iBkgnd] == type ) {
gen = 1;
bkgndID = iBkgnd;
}
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], gen );
}
genOK = this->generateBkgndEvent(bkgndID);
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPBranchValues();
}
// Store the event number (within this experiment)
// and then increment it
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
++evtNum;
this->fillGenNtupleBranches();
if (iEvt%500 == 0) {std::cout << "INFO in LauBsCPFitModel::genExpt : Generated event number " << iEvt << " out of " << nEvtsGen << " " << type << " events." << std::endl;}
}
if (!genOK) {
break;
}
}
if (this->useDP() && genOK) {
negSigModel_->checkToyMC(kTRUE,kTRUE);
posSigModel_->checkToyMC(kTRUE,kTRUE);
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray negFitFrac = negSigModel_->getFitFractions();
if (negFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << negFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << negFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFrac = posSigModel_->getFitFractions();
if (posFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << posFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauBsCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << posFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
negFitFrac_[i][j].value(negFitFrac[i][j].value());
posFitFrac_[i][j].value(posFitFrac[i][j].value());
}
}
negMeanEff_.value(negSigModel_->getMeanEff().value());
posMeanEff_.value(posSigModel_->getMeanEff().value());
negDPRate_.value(negSigModel_->getDPRate().value());
posDPRate_.value(posSigModel_->getDPRate().value());
}
}
return genOK;
}
Bool_t LauBsCPFitModel::generateSignalEvent()
{
// Generate signal event
Bool_t genOK(kTRUE);
Bool_t genSCF(kFALSE);
Bool_t generatedEvent(kFALSE);
//LauPdfPList* sigPdfs(0);
//LauPdfPList* scfPdfs(0);
if (this->useDP()) {
nGenLoop_ = 0;
while (generatedEvent == kFALSE && nGenLoop_ < iterationsMax_) {
// DP variables
Double_t m13Sq(0.0), m23Sq(0.0);
// Generate DP position
negKinematics_->genFlatPhaseSpace(m13Sq, m23Sq);
posKinematics_->updateKinematics(m13Sq, m23Sq);
// Calculate the total A and Abar for the given DP position
negSigModel_->calcLikelihoodInfo(m13Sq, m23Sq);
posSigModel_->calcLikelihoodInfo(m13Sq, m23Sq);
// Calculate DP terms
this->calculateDPterms();
//Finally we throw the dice to see whether this event should be generated
Double_t randNo = LauRandom::randomFun()->Rndm();
if (randNo <= ASq_/aSqMaxSet_ ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ASq_ > aSqMaxVar_) {aSqMaxVar_ = ASq_;}
} else {
nGenLoop_++;
}
}
// Check whether we have generated the toy MC OK.
if (nGenLoop_ >= iterationsMax_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepNonFlavModel::generateSignalEvent : Hit max iterations: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
} else if (aSqMaxVar_ > aSqMaxSet_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepNonFlavModel::generateSignalEvent : Found a larger ASq value: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
}
if ( genOK && useSCF_ ) {
Double_t frac(0.0);
if ( useSCFHist_ ) {
frac = scfFracHist_->calcEfficiency( negKinematics_ );
} else {
frac = scfFrac_.genValue();
}
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
// Optionally smear the DP position
// of the SCF event
if ( scfMap_ != 0 ) {
Double_t xCoord(0.0), yCoord(0.0);
if ( negKinematics_->squareDP() ) {
xCoord = negKinematics_->getmPrime();
yCoord = negKinematics_->getThetaPrime();
} else {
xCoord = negKinematics_->getm13Sq();
yCoord = negKinematics_->getm23Sq();
}
// Find the bin number where this event is generated
Int_t binNo = scfMap_->binNumber( xCoord, yCoord );
// Retrieve the migration histogram
TH2* histo = scfMap_->trueHist( binNo );
const LauAbsEffModel * effModel = negSigModel_->getEffModel();
do {
// Get a random point from the histogram
histo->GetRandom2( xCoord, yCoord );
// Update the kinematics
if ( negKinematics_->squareDP() ) {
negKinematics_->updateSqDPKinematics( xCoord, yCoord );
} else {
negKinematics_->updateKinematics( xCoord, yCoord );
}
} while ( ! effModel->passVeto( negKinematics_ ) );
}
}
}
} else {
if ( useSCF_ ) {
Double_t frac = scfFrac_.genValue();
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
}
}
if (genOK) {
// TODO! Check if the removal is right
//sigPdfs = &signalPdfs_;
//scfPdfs = &scfPdfs_;
if ( useSCF_ ) {
if ( genSCF ) {
this->generateExtraPdfValues(&scfPdfs_);
} else {
this->generateExtraPdfValues(&signalPdfs_);
}
} else {
this->generateExtraPdfValues(&signalPdfs_);
}
}
return genOK;
}
void LauBsCPFitModel::calculateDPterms()
{
// Retrieve the amplitudes and efficiency from the dynamics
const LauComplex& Abar = negSigModel_->getEvtDPAmp();
const LauComplex& A = posSigModel_->getEvtDPAmp();
Double_t eff = negSigModel_->getEvtEff();
// Calculate the DP terms
Double_t aSqSum = A.abs2() + Abar.abs2();
LauComplex inter = Abar * A.conj();
Double_t interTermRe = inter.re();
// Total amplitude and multiply by the efficiency
ASq_ = aSqSum - 2.0 * D_ * interTermRe;
ASq_ *= eff;
}
Bool_t LauBsCPFitModel::generateBkgndEvent(UInt_t bkgndID)
{
// Generate Bkgnd event
Bool_t genOK(kTRUE);
LauAbsBkgndDPModel* model(0);
LauPdfPList* extraPdfs(0);
//LauKinematics* kinematics(0);
// TODO: Check charge again
/*
if (curEvtCharge_<0) {
model = negBkgndDPModels_[bkgndID];
if (this->enableEmbedding()) {
embeddedData = negBkgndTree_[bkgndID];
}
extraPdfs = &negBkgndPdfs_[bkgndID];
kinematics = negKinematics_;
} else {
model = posBkgndDPModels_[bkgndID];
if (this->enableEmbedding()) {
embeddedData = posBkgndTree_[bkgndID];
}
if ( tagged_ ) {
extraPdfs = &posBkgndPdfs_[bkgndID];
} else {
extraPdfs = &negBkgndPdfs_[bkgndID];
}
kinematics = posKinematics_;
}
*/
model = bkgndDPModels_[bkgndID];
//if (this->enableEmbedding()) {
// embeddedData = bkgndTree_[bkgndID];
//}
extraPdfs = &bkgndPdfs_[bkgndID];
//kinematics = negKinematics_;
// Finishing here
if (this->useDP()) {
// TODO! Check!
/*if (embeddedData) {
embeddedData->getEmbeddedEvent(kinematics);
} else {*/
if (model == 0) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
std::cerr << "ERROR in LauBsCPFitModel::generateBkgndEvent : Can't find the DP model for background class \"" << bkgndClass << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
genOK = model->generate();
//}
//} else {
// if (embeddedData) {
// embeddedData->getEmbeddedEvent(0);
// }
}
if (genOK) {
this->generateExtraPdfValues(extraPdfs);
}
return genOK;
}
void LauBsCPFitModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleDoubleBranch("efficiency");
if ( useSCF_ ) {
this->addGenNtupleIntegerBranch("genTMSig");
this->addGenNtupleIntegerBranch("genSCFSig");
}
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name.Prepend("gen");
this->addGenNtupleIntegerBranch(name);
}
if (this->useDP() == kTRUE) {
this->addGenNtupleDoubleBranch("m12");
this->addGenNtupleDoubleBranch("m23");
this->addGenNtupleDoubleBranch("m13");
this->addGenNtupleDoubleBranch("m12Sq");
this->addGenNtupleDoubleBranch("m23Sq");
this->addGenNtupleDoubleBranch("m13Sq");
this->addGenNtupleDoubleBranch("cosHel12");
this->addGenNtupleDoubleBranch("cosHel23");
this->addGenNtupleDoubleBranch("cosHel13");
if (negKinematics_->squareDP() && posKinematics_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime");
this->addGenNtupleDoubleBranch("thPrime");
}
}
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
this->addGenNtupleDoubleBranch( (*var_iter) );
}
}
}
}
void LauBsCPFitModel::setDPBranchValues()
{
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12", negKinematics_->getm12());
this->setGenNtupleDoubleBranchValue("m23", negKinematics_->getm23());
this->setGenNtupleDoubleBranchValue("m13", negKinematics_->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq", negKinematics_->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq", negKinematics_->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq", negKinematics_->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12", negKinematics_->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23", negKinematics_->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13", negKinematics_->getc13());
if (negKinematics_->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime", negKinematics_->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime", negKinematics_->getThetaPrime());
}
}
void LauBsCPFitModel::generateExtraPdfValues(LauPdfPList* extraPdfs)
{
if (!extraPdfs) {
std::cerr << "ERROR in LauBsCPFitModel::generateExtraPdfValues : Null pointer to PDF list." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (extraPdfs->empty()) {
//std::cerr << "WARNING in LauBsCPFitModel::generateExtraPdfValues : PDF list is empty." << std::endl;
return;
}
// Generate from the extra PDFs
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
LauFitData genValues;
genValues = (*pdf_iter)->generate(negKinematics_);
for ( LauFitData::const_iterator var_iter = genValues.begin(); var_iter != genValues.end(); ++var_iter ) {
TString varName = var_iter->first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var_iter->second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
void LauBsCPFitModel::propagateParUpdates()
{
// Update the signal parameters and then the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
negSigModel_->updateCoeffs(negCoeffs_);
posSigModel_->updateCoeffs(posCoeffs_);
this->calcInterTermNorm();
this->calculateAmplitudeNorm();
}
// Update the signal fraction from the background fractions if not doing an extended fit
this->updateSigEvents();
}
void LauBsCPFitModel::updateSigEvents()
{
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
// Subtract background events (if any) from signal.
if ( usingBkgnd_ == kTRUE ) {
for ( LauAbsRValue* bkgndYield : bkgndEvents_ ) {
signalEvents -= bkgndYield->value();
}
}
signalEvents_->value(signalEvents);
}
void LauBsCPFitModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and background models.
// Note that we store the events of both charges in both the
// negative and the positive models. It's only later, at the stage
// when the likelihood is being calculated, that we separate them.
LauFitDataTree* inputFitData = this->fitData();
// First the Dalitz plot variables (m_ij^2)
if (this->useDP() == kTRUE) {
// need to append SCF smearing bins before caching DP amplitudes
if ( scfMap_ != 0 ) {
this->appendBinCentres( inputFitData );
}
negSigModel_->fillDataTree(*inputFitData);
posSigModel_->fillDataTree(*inputFitData);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = bkgndDPModels_.begin(); iter != bkgndDPModels_.end(); ++iter) {
(*iter)->fillDataTree(*inputFitData);
}
}
}
// ...and then the extra PDFs
this->cacheInfo(signalPdfs_, *inputFitData);
this->cacheInfo(scfPdfs_, *inputFitData);
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->cacheInfo((*iter), *inputFitData);
}
// the SCF fractions and jacobians
if ( useSCF_ && useSCFHist_ ) {
if ( !inputFitData->haveBranch( "m13Sq" ) || !inputFitData->haveBranch( "m23Sq" ) ) {
std::cerr << "ERROR in LauBsCPFitModel::cacheInputFitVars : Input data does not contain DP branches and so can't cache the SCF fraction." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t nEvents = inputFitData->nEvents();
recoSCFFracs_.clear();
recoSCFFracs_.reserve( nEvents );
if ( negKinematics_->squareDP() ) {
recoJacobians_.clear();
recoJacobians_.reserve( nEvents );
}
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator m13_iter = dataValues.find("m13Sq");
LauFitData::const_iterator m23_iter = dataValues.find("m23Sq");
negKinematics_->updateKinematics( m13_iter->second, m23_iter->second );
Double_t scfFrac = scfFracHist_->calcEfficiency( negKinematics_ );
recoSCFFracs_.push_back( scfFrac );
if ( negKinematics_->squareDP() ) {
recoJacobians_.push_back( negKinematics_->calcSqDPJacobian() );
}
}
}
}
void LauBsCPFitModel::appendBinCentres( LauFitDataTree* inputData )
{
// We'll be caching the DP amplitudes and efficiencies of the centres of the true bins.
// To do so, we attach some fake points at the end of inputData, the number of the entry
// minus the total number of events corresponding to the number of the histogram for that
// given true bin in the LauScfMap object. (What this means is that when Laura is provided with
// the LauScfMap object by the user, it's the latter who has to make sure that it contains the
// right number of histograms and in exactly the right order!)
// Get the x and y co-ordinates of the bin centres
std::vector<Double_t> binCentresXCoords;
std::vector<Double_t> binCentresYCoords;
scfMap_->listBinCentres(binCentresXCoords, binCentresYCoords);
// The SCF histograms could be in square Dalitz plot histograms.
// The dynamics takes normal Dalitz plot coords, so we might have to convert them back.
Bool_t sqDP = negKinematics_->squareDP();
UInt_t nBins = binCentresXCoords.size();
fakeSCFFracs_.clear();
fakeSCFFracs_.reserve( nBins );
if ( sqDP ) {
fakeJacobians_.clear();
fakeJacobians_.reserve( nBins );
}
for (UInt_t iBin = 0; iBin < nBins; ++iBin) {
if ( sqDP ) {
negKinematics_->updateSqDPKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
binCentresXCoords[iBin] = negKinematics_->getm13Sq();
binCentresYCoords[iBin] = negKinematics_->getm23Sq();
fakeJacobians_.push_back( negKinematics_->calcSqDPJacobian() );
} else {
negKinematics_->updateKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
}
fakeSCFFracs_.push_back( scfFracHist_->calcEfficiency( negKinematics_ ) );
}
// Set up inputFitVars_ object to hold the fake events
inputData->appendFakePoints(binCentresXCoords,binCentresYCoords);
}
Double_t LauBsCPFitModel::getTotEvtLikelihood(UInt_t iEvt)
{
// Find out whether we have B- or B+
// Get the DP likelihood for signal and backgrounds
this->getEvtDPLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal and backgrounds
this->getEvtExtraLikelihoods(iEvt);
// If appropriate, combine the TM and SCF likelihoods
Double_t sigLike = sigDPLike_ * sigExtraLike_;
if ( useSCF_ ) {
Double_t scfFrac(0.0);
if (useSCFHist_) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
sigLike *= (1.0 - scfFrac);
if ( (scfMap_ != 0) && (this->useDP() == kTRUE) ) {
// if we're smearing the SCF DP PDF then the SCF frac
// is already included in the SCF DP likelihood
sigLike += (scfDPLike_ * scfExtraLike_);
} else {
sigLike += (scfFrac * scfDPLike_ * scfExtraLike_);
}
}
// Signal asymmetry is built into the DP model...
Double_t signalEvents = signalEvents_->unblindValue();
// Construct the total event likelihood
Double_t likelihood(0.0);
if (usingBkgnd_) {
likelihood = sigLike*signalEvents;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
Double_t bkgndEvents = bkgndEvents_[bkgndID]->unblindValue();
likelihood += bkgndEvents*bkgndDPLike_[bkgndID]*bkgndExtraLike_[bkgndID];
}
} else {
// TODO: receives this 1/2 term?
//likelihood = sigLike*0.5;
likelihood = sigLike;
}
return likelihood;
}
Double_t LauBsCPFitModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
if (usingBkgnd_) {
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
eventSum += (*iter)->unblindValue();
}
}
return eventSum;
}
void LauBsCPFitModel::getEvtDPLikelihood(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
if ( ! this->useDP() ) {
// There's always going to be a term in the likelihood for the
// signal, so we'd better not zero it.
sigDPLike_ = 1.0;
scfDPLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = 1.0;
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
return;
}
const UInt_t nBkgnds = this->nBkgndClasses();
// Completely revised
posSigModel_->calcLikelihoodInfo(iEvt);
negSigModel_->calcLikelihoodInfo(iEvt);
this->calculateDPterms();
sigDPLike_ = ASq_;
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = bkgndDPModels_[bkgndID]->getLikelihood(iEvt);
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
if ( useSCF_ == kTRUE ) {
if ( scfMap_ == 0 ) {
// we're not smearing the SCF DP position
// so the likelihood is the same as the TM
scfDPLike_ = sigDPLike_;
} else {
// calculate the smeared SCF DP likelihood
scfDPLike_ = this->getEvtSCFDPLikelihood(iEvt);
}
}
// Calculate the signal normalisation
// NB the 2.0 is there so that the 0.5 factor is applied to
// signal and background in the same place otherwise you get
// normalisation problems when you switch off the DP in the fit
// TODO: need to fix this
sigDPLike_ *= 1.0/normDP_;
scfDPLike_ *= 1.0/normDP_;
}
void LauBsCPFitModel::calculateAmplitudeNorm()
{
// Integrals of the sum of the ampltudes to the f(fbar) integral( |A|^2 + |Abar|^2 ) dP
Double_t normASq = posSigModel_->getDPNorm();
Double_t normAbarSq = negSigModel_->getDPNorm();
// Integrals of cross terms Abar*Aconj
Double_t normInterTerm = interTermReNorm_;
// Complete DP normalisation terms
normDP_ = normASq + normAbarSq - 2.0 * D_ * normInterTerm;
}
void LauBsCPFitModel::calcInterTermNorm()
{
const std::vector<Double_t> fNormB0 = posSigModel_->getFNorm();
const std::vector<Double_t> fNormB0bar = negSigModel_->getFNorm();
// TODO - compare this implementation with that of LauIsobarDynamics::calcSigDPNorm
LauComplex norm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
LauComplex coeffTerm = negCoeffs_[jAmp]*posCoeffs_[iAmp].conj();
coeffTerm *= fifjEffSum_[iAmp][jAmp];
coeffTerm.rescale(fNormB0bar[jAmp] * fNormB0[iAmp]);
norm += coeffTerm;
}
}
interTermReNorm_ = norm.re();
//interTermImNorm_f_ = norm_f.im();
}
Double_t LauBsCPFitModel::getEvtSCFDPLikelihood(UInt_t iEvt)
{
Double_t scfDPLike(0.0);
Double_t recoJacobian(1.0);
Double_t xCoord(0.0);
Double_t yCoord(0.0);
Bool_t squareDP = negKinematics_->squareDP();
if ( squareDP ) {
xCoord = negSigModel_->getEvtmPrime();
yCoord = negSigModel_->getEvtthPrime();
recoJacobian = recoJacobians_[iEvt];
} else {
xCoord = negSigModel_->getEvtm13Sq();
yCoord = negSigModel_->getEvtm23Sq();
}
// Find the bin that our reco event falls in
Int_t recoBin = scfMap_->binNumber( xCoord, yCoord );
// Find out which true Bins contribute to the given reco bin
const std::vector<Int_t>* trueBins = scfMap_->trueBins(recoBin);
const Int_t nDataEvents = this->eventsPerExpt();
// Loop over the true bins
for (std::vector<Int_t>::const_iterator iter = trueBins->begin(); iter != trueBins->end(); ++iter)
{
Int_t trueBin = (*iter);
// prob of a true event in the given true bin migrating to the reco bin
Double_t pRecoGivenTrue = scfMap_->prob( recoBin, trueBin );
Double_t pTrue(0.0);
// We've cached the DP amplitudes and the efficiency for the
// true bin centres, just after the data points
// TODO: Check this!!
/*if ( tagged_ ) {
LauIsobarDynamics* sigModel(0);
if (curEvtCharge_<0) {
sigModel = negSigModel_;
} else {
sigModel = posSigModel_;
}
sigModel->calcLikelihoodInfo( nDataEvents + trueBin );
pTrue = sigModel->getEvtDPAmp().abs2() * sigModel->getEvtEff();
} else {*/
posSigModel_->calcLikelihoodInfo( nDataEvents + trueBin );
negSigModel_->calcLikelihoodInfo( nDataEvents + trueBin );
//pTrue = 0.5 * ( posSigModel_->getEvtDPAmp().abs2() * posSigModel_->getEvtEff() +
// negSigModel_->getEvtDPAmp().abs2() * negSigModel_->getEvtEff() );
const LauComplex& A = posSigModel_->getEvtDPAmp();
const LauComplex& Abar = negSigModel_->getEvtDPAmp();
const LauComplex AstAbar = A.conj() * Abar;
pTrue = A.abs2() * posSigModel_->getEvtEff() +
Abar.abs2() * negSigModel_->getEvtEff() -
2.0 * D_ * AstAbar.re();
//}
// Get the cached SCF fraction (and jacobian if we're using the square DP)
Double_t scfFraction = fakeSCFFracs_[ trueBin ];
Double_t jacobian(1.0);
if ( squareDP ) {
jacobian = fakeJacobians_[ trueBin ];
}
scfDPLike += pTrue * jacobian * scfFraction * pRecoGivenTrue;
}
// Divide by the reco jacobian
scfDPLike /= recoJacobian;
return scfDPLike;
}
void LauBsCPFitModel::getEvtExtraLikelihoods(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
// TODO: Not sure here about this!
// I will comment out the minimum I can
/*if ( ! tagged_ || curEvtCharge_ < 0 ) {
sigExtraLike_ = this->prodPdfValue( negSignalPdfs_, iEvt );
if (useSCF_) {
scfExtraLike_ = this->prodPdfValue( negScfPdfs_, iEvt );
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( negBkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
} else {
sigExtraLike_ = this->prodPdfValue( posSignalPdfs_, iEvt );
if (useSCF_) {
scfExtraLike_ = this->prodPdfValue( posScfPdfs_, iEvt );
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( posBkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
}
*/
sigExtraLike_ = this->prodPdfValue( signalPdfs_, iEvt );
if (useSCF_) {
scfExtraLike_ = this->prodPdfValue( scfPdfs_, iEvt );
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( bkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
}
void LauBsCPFitModel::updateCoeffs()
{
negCoeffs_.clear(); posCoeffs_.clear();
negCoeffs_.reserve(nSigComp_); posCoeffs_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; i++) {
negCoeffs_.push_back(coeffPars_[i]->antiparticleCoeff());
posCoeffs_.push_back(coeffPars_[i]->particleCoeff());
}
}
void LauBsCPFitModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->addSPlotNtupleDoubleBranch("scffraction");
}
}
// Store the total event likelihood for each species.
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFFrac");
} else {
this->addSPlotNtupleDoubleBranch("sigTotalLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "TotalLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
// Store the DP likelihoods
if (this->useDP()) {
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMDPLike");
this->addSPlotNtupleDoubleBranch("sigSCFDPLike");
} else {
this->addSPlotNtupleDoubleBranch("sigDPLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "DPLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
}
// Store the likelihoods for each extra PDF
if (useSCF_) {
this->addSPlotNtupleBranches(&signalPdfs_, "sigTM");
this->addSPlotNtupleBranches(&scfPdfs_, "sigSCF");
} else {
this->addSPlotNtupleBranches(&signalPdfs_, "sig");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList* pdfList = &(bkgndPdfs_[iBkgnd]);
this->addSPlotNtupleBranches(pdfList, bkgndClass);
}
}
}
void LauBsCPFitModel::addSPlotNtupleBranches(const LauPdfPList* extraPdfs, const TString& prefix)
{
if (extraPdfs) {
// Loop through each of the PDFs
for (LauPdfPList::const_iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
TString name(prefix);
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr << "WARNING in LauBsCPFitModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
}
Double_t LauBsCPFitModel::setSPlotNtupleBranchValues(LauPdfPList* extraPdfs, const TString& prefix, UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// calculate the likelihood for this event
(*pdf_iter)->calcLikelihoodInfo(iEvt);
extraLike = (*pdf_iter)->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
Double_t indivLike = (*pdf_iter)->getLikelihood( (*var_iter) );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
TString name(prefix);
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr << "WARNING in LauBsCPFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
return totalLike;
}
LauSPlot::NameSet LauBsCPFitModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
// Loop through all the signal PDFs
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
// Loop over the variables involved in each PDF
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
// If they are not DP coordinates then add them
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
nameSet.insert( (*var_iter) );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauBsCPFitModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (!par->fixed()) {
numbMap[bkgndClass] = par->genValue();
}
}
}
return numbMap;
}
LauSPlot::NumbMap LauBsCPFitModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (par->fixed()) {
numbMap[bkgndClass] = par->genValue();
}
}
}
return numbMap;
}
LauSPlot::TwoDMap LauBsCPFitModel::twodimPDFs() const
{
// This makes the assumption that the form of the positive and
// negative PDFs are the same, which seems reasonable to me
LauSPlot::TwoDMap twodimMap;
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
if (useSCF_) {
twodimMap.insert( std::make_pair( "sigTM", std::make_pair( varNames[0], varNames[1] ) ) );
} else {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if ( useSCF_ ) {
for (LauPdfPList::const_iterator pdf_iter = scfPdfs_.begin(); pdf_iter != scfPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sigSCF", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList& pdfList = bkgndPdfs_[iBkgnd];
for (LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( bkgndClass, std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
}
return twodimMap;
}
void LauBsCPFitModel::storePerEvtLlhds()
{
std::cout << "INFO in LauBsCPFitModel::storePerEvtLlhds : Storing per-event likelihood values..." << std::endl;
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
LauFitDataTree* inputFitData = this->fitData();
if (!this->useDP() && this->storeDPEff()) {
negSigModel_->initialise(negCoeffs_);
posSigModel_->initialise(posCoeffs_);
negSigModel_->fillDataTree(*inputFitData);
posSigModel_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
//LauIsobarDynamics* sigModel(0);
LauPdfPList* sigPdfs(0);
LauPdfPList* scfPdfs(0);
LauBkgndPdfsList* bkgndPdfs(0);
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
this->setSPlotNtupleIntegerBranchValue("iExpt",this->iExpt());
this->setSPlotNtupleIntegerBranchValue("iEvtWithinExpt",iEvt);
// Find out whether we have B- or B+
// TODO: Check this (Commented out for the time being)
/*if ( tagged_ ) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator iter = dataValues.find("charge");
curEvtCharge_ = static_cast<Int_t>(iter->second);
if (curEvtCharge_==+1) {
sigModel = posSigModel_;
sigPdfs = &posSignalPdfs_;
scfPdfs = &posScfPdfs_;
bkgndPdfs = &posBkgndPdfs_;
} else {
sigModel = negSigModel_;
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
bkgndPdfs = &negBkgndPdfs_;
}
} else {
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
bkgndPdfs = &negBkgndPdfs_;
} */
//sigModel = negSigModel_;
sigPdfs = &signalPdfs_;
scfPdfs = &scfPdfs_;
bkgndPdfs = &bkgndPdfs_;
// the DP information
this->getEvtDPLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
posSigModel_->calcLikelihoodInfo(iEvt);
negSigModel_->calcLikelihoodInfo(iEvt);
}
/*if ( tagged_ ) {
this->setSPlotNtupleDoubleBranchValue("efficiency",sigModel->getEvtEff());
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->setSPlotNtupleDoubleBranchValue("scffraction",sigModel->getEvtScfFraction());
}
} else {*/
// TODO: Does it matter?
this->setSPlotNtupleDoubleBranchValue("efficiency",0.5*(posSigModel_->getEvtEff() + negSigModel_->getEvtEff()) );
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->setSPlotNtupleDoubleBranchValue("scffraction",0.5*(posSigModel_->getEvtScfFraction() + negSigModel_->getEvtScfFraction()));
}
//}
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
if (useSCF_) {
scfTotalLike_ = scfDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigTMDPLike",sigDPLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFDPLike",scfDPLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "DPLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndDPLike_[iBkgnd]);
}
}
} else {
sigTotalLike_ = 1.0;
if (useSCF_) {
scfTotalLike_ = 1.0;
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
bkgndTotalLike_[iBkgnd] = 1.0;
}
}
}
// the signal PDF values
if ( useSCF_ ) {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sigTM", iEvt);
scfTotalLike_ *= this->setSPlotNtupleBranchValues(scfPdfs, "sigSCF", iEvt);
} else {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sig", iEvt);
}
// the background PDF values
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
LauPdfPList& pdfs = (*bkgndPdfs)[iBkgnd];
bkgndTotalLike_[iBkgnd] *= this->setSPlotNtupleBranchValues(&(pdfs), bkgndClass, iEvt);
}
}
// the total likelihoods
if (useSCF_) {
Double_t scfFrac(0.0);
if ( useSCFHist_ ) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
this->setSPlotNtupleDoubleBranchValue("sigSCFFrac",scfFrac);
sigTotalLike_ *= ( 1.0 - scfFrac );
if ( scfMap_ == 0 ) {
scfTotalLike_ *= scfFrac;
}
this->setSPlotNtupleDoubleBranchValue("sigTMTotalLike",sigTotalLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFTotalLike",scfTotalLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "TotalLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndTotalLike_[iBkgnd]);
}
}
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout << "INFO in LauBsCPFitModel::storePerEvtLlhds : Finished storing per-event likelihood values." << std::endl;
}
/*
void LauBsCPFitModel::embedNegSignal(const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment,
Bool_t useReweighting)
{
if (negSignalTree_) {
std::cerr << "ERROR in LauBsCPFitModel::embedNegSignal : Already embedding signal from a file." << std::endl;
return;
}
if (!reuseEventsWithinEnsemble && reuseEventsWithinExperiment) {
std::cerr << "WARNING in LauBsCPFitModel::embedNegSignal : Conflicting options provided, will not reuse events at all." << std::endl;
reuseEventsWithinExperiment = kFALSE;
}
negSignalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = negSignalTree_->findBranches();
if (!dataOK) {
delete negSignalTree_; negSignalTree_ = 0;
std::cerr << "ERROR in LauBsCPFitModel::embedNegSignal : Problem creating data tree for embedding." << std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
useNegReweighting_ = useReweighting;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauBsCPFitModel::embedBkgnd(const TString& bkgndClass, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauBsCPFitModel::embedBkgnd : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
if (bkgndTree_[bkgndID]) {
std::cerr << "ERROR in LauBsCPFitModel::embedBkgnd : Already embedding background from a file." << std::endl;
return;
}
if (!reuseEventsWithinEnsemble && reuseEventsWithinExperiment) {
std::cerr << "WARNING in LauBsCPFitModel::embedBkgnd : Conflicting options provided, will not reuse events at all." << std::endl;
reuseEventsWithinExperiment = kFALSE;
}
bkgndTree_[bkgndID] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = bkgndTree_[bkgndID]->findBranches();
if (!dataOK) {
delete bkgndTree_[bkgndID]; bkgndTree_[bkgndID] = 0;
std::cerr << "ERROR in LauBsCPFitModel::embedBkgnd : Problem creating data tree for embedding." << std::endl;
return;
}
reuseBkgnd_[bkgndID] = reuseEventsWithinEnsemble;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauBsCPFitModel::embedPosSignal(const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment,
Bool_t useReweighting)
{
if (posSignalTree_) {
std::cerr << "ERROR in LauBsCPFitModel::embedPosSignal : Already embedding signal from a file." << std::endl;
return;
}
if (!reuseEventsWithinEnsemble && reuseEventsWithinExperiment) {
std::cerr << "WARNING in LauBsCPFitModel::embedPosSignal : Conflicting options provided, will not reuse events at all." << std::endl;
reuseEventsWithinExperiment = kFALSE;
}
posSignalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = posSignalTree_->findBranches();
if (!dataOK) {
delete posSignalTree_; posSignalTree_ = 0;
std::cerr << "ERROR in LauBsCPFitModel::embedPosSignal : Problem creating data tree for embedding." << std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
usePosReweighting_ = useReweighting;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
*/
void LauBsCPFitModel::weightEvents( const TString& /*dataFileName*/, const TString& /*dataTreeName*/ )
{
std::cerr << "ERROR in LauBsCPFitModel::weightEvents : Method not available for this fit model." << std::endl;
return;
}
void LauBsCPFitModel::savePDFPlots(const TString& /*label*/)
{
}
void LauBsCPFitModel::savePDFPlotsWave(const TString& /*label*/, const Int_t& /*spin*/)
{
}
diff --git a/src/LauCPFitModel.cc b/src/LauCPFitModel.cc
index 5aadb7c..2986790 100644
--- a/src/LauCPFitModel.cc
+++ b/src/LauCPFitModel.cc
@@ -1,3426 +1,3424 @@
/*
Copyright 2004 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauCPFitModel.cc
\brief File containing implementation of LauCPFitModel class.
*/
#include "LauCPFitModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauAsymmCalc.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDaughters.hh"
#include "LauEffModel.hh"
#include "LauEmbeddedData.hh"
#include "LauFitNtuple.hh"
#include "LauGenNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TBranch.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TGraph.h"
#include "TGraph2D.h"
#include "TH2.h"
#include "TLeaf.h"
#include "TMath.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TStyle.h"
#include "TSystem.h"
#include "TTree.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <vector>
ClassImp(LauCPFitModel)
LauCPFitModel::LauCPFitModel(LauIsobarDynamics* negModel, LauIsobarDynamics* posModel, Bool_t tagged, const TString& tagVarName) : LauAbsFitModel(),
negSigModel_(negModel), posSigModel_(posModel),
negKinematics_(negModel ? negModel->getKinematics() : 0),
posKinematics_(posModel ? posModel->getKinematics() : 0),
usingBkgnd_(kFALSE),
nSigComp_(0),
nSigDPPar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
negMeanEff_("negMeanEff",0.0,0.0,1.0), posMeanEff_("posMeanEff",0.0,0.0,1.0),
negDPRate_("negDPRate",0.0,0.0,100.0), posDPRate_("posDPRate",0.0,0.0,100.0),
signalEvents_(0),
signalAsym_(0),
forceAsym_(kFALSE),
tagged_(tagged),
tagVarName_(tagVarName),
curEvtCharge_(0),
useSCF_(kFALSE),
useSCFHist_(kFALSE),
scfFrac_("scfFrac",0.0,0.0,1.0),
scfFracHist_(0),
scfMap_(0),
compareFitData_(kFALSE),
negParent_("B-"), posParent_("B+"),
negSignalTree_(0), posSignalTree_(0),
reuseSignal_(kFALSE),
useNegReweighting_(kFALSE), usePosReweighting_(kFALSE),
sigDPLike_(0.0),
scfDPLike_(0.0),
sigExtraLike_(0.0),
scfExtraLike_(0.0),
sigTotalLike_(0.0),
scfTotalLike_(0.0)
{
const LauDaughters* negDaug = negSigModel_->getDaughters();
if (negDaug != 0) {negParent_ = negDaug->getNameParent();}
const LauDaughters* posDaug = posSigModel_->getDaughters();
if (posDaug != 0) {posParent_ = posDaug->getNameParent();}
}
LauCPFitModel::~LauCPFitModel()
{
delete negSignalTree_;
delete posSignalTree_;
for (LauBkgndEmbDataList::iterator iter = negBkgndTree_.begin(); iter != negBkgndTree_.end(); ++iter) {
delete (*iter);
}
for (LauBkgndEmbDataList::iterator iter = posBkgndTree_.begin(); iter != posBkgndTree_.end(); ++iter) {
delete (*iter);
}
delete scfFracHist_;
}
void LauCPFitModel::setupBkgndVectors()
{
UInt_t nBkgnds = this->nBkgndClasses();
negBkgndDPModels_.resize( nBkgnds );
posBkgndDPModels_.resize( nBkgnds );
negBkgndPdfs_.resize( nBkgnds );
posBkgndPdfs_.resize( nBkgnds );
bkgndEvents_.resize( nBkgnds );
bkgndAsym_.resize( nBkgnds );
negBkgndTree_.resize( nBkgnds );
posBkgndTree_.resize( nBkgnds );
reuseBkgnd_.resize( nBkgnds );
bkgndDPLike_.resize( nBkgnds );
bkgndExtraLike_.resize( nBkgnds );
bkgndTotalLike_.resize( nBkgnds );
}
void LauCPFitModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : The LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_ = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauCPFitModel::setNSigEvents( LauParameter* nSigEvents, LauParameter* sigAsym, Bool_t forceAsym )
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : The event LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( sigAsym == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : The asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauCPFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
signalAsym_ = sigAsym;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_->name(asymName);
forceAsym_ = forceAsym;
}
void LauCPFitModel::setNBkgndEvents( LauAbsRValue* nBkgndEvents )
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBgkndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
if ( bkgndAsym_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : You are trying to overwrite the background asymmetry." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
bkgndAsym_[bkgndID] = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauCPFitModel::setNBkgndEvents(LauAbsRValue* nBkgndEvents, LauAbsRValue* bkgndAsym)
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( bkgndAsym == nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : The background asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
bkgndAsym->name( asymName );
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
if ( bkgndAsym_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauCPFitModel::setNBkgndEvents : You are trying to overwrite the background asymmetry." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
bkgndAsym_[bkgndID] = bkgndAsym;
}
void LauCPFitModel::splitSignalComponent( const TH2* dpHisto, const Bool_t upperHalf, const Bool_t fluctuateBins, LauScfMap* scfMap )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauCPFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
if ( dpHisto == 0 ) {
std::cerr << "ERROR in LauCPFitModel::splitSignalComponent : The histogram pointer is null." << std::endl;
return;
}
const LauDaughters* daughters = negSigModel_->getDaughters();
scfFracHist_ = new LauEffModel( daughters, 0 );
scfFracHist_->setEffHisto( dpHisto, kTRUE, fluctuateBins, 0.0, 0.0, upperHalf, daughters->squareDP() );
scfMap_ = scfMap;
useSCF_ = kTRUE;
useSCFHist_ = kTRUE;
}
void LauCPFitModel::splitSignalComponent( const Double_t scfFrac, const Bool_t fixed )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauCPFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
scfFrac_.range( 0.0, 1.0 );
scfFrac_.value( scfFrac ); scfFrac_.initValue( scfFrac ); scfFrac_.genValue( scfFrac );
scfFrac_.fixed( fixed );
useSCF_ = kTRUE;
useSCFHist_ = kFALSE;
}
void LauCPFitModel::setBkgndDPModels(const TString& bkgndClass, LauAbsBkgndDPModel* negModel, LauAbsBkgndDPModel* posModel)
{
if ((negModel==0) || (posModel==0)) {
std::cerr << "ERROR in LauCPFitModel::setBkgndDPModels : One or both of the model pointers is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass) ) {
std::cerr << "ERROR in LauCPFitModel::setBkgndDPModel : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
negBkgndDPModels_[bkgndID] = negModel;
posBkgndDPModels_[bkgndID] = posModel;
usingBkgnd_ = kTRUE;
}
void LauCPFitModel::setSignalPdfs(LauAbsPdf* negPdf, LauAbsPdf* posPdf)
{
if ( tagged_ ) {
if (negPdf==0 || posPdf==0) {
std::cerr << "ERROR in LauCPFitModel::setSignalPdfs : One or both of the PDF pointers is null." << std::endl;
return;
}
} else {
// if we're doing an untagged analysis we will only use the negative PDFs
if ( negPdf==0 ) {
std::cerr << "ERROR in LauCPFitModel::setSignalPdfs : The negative PDF pointer is null." << std::endl;
return;
}
if ( posPdf!=0 ) {
std::cerr << "WARNING in LauCPFitModel::setSignalPdfs : Doing an untagged fit so will not use the positive PDF." << std::endl;
}
}
negSignalPdfs_.push_back(negPdf);
posSignalPdfs_.push_back(posPdf);
}
void LauCPFitModel::setSCFPdfs(LauAbsPdf* negPdf, LauAbsPdf* posPdf)
{
if ( tagged_ ) {
if (negPdf==0 || posPdf==0) {
std::cerr << "ERROR in LauCPFitModel::setSCFPdfs : One or both of the PDF pointers is null." << std::endl;
return;
}
} else {
// if we're doing an untagged analysis we will only use the negative PDFs
if ( negPdf==0 ) {
std::cerr << "ERROR in LauCPFitModel::setSCFPdfs : The negative PDF pointer is null." << std::endl;
return;
}
if ( posPdf!=0 ) {
std::cerr << "WARNING in LauCPFitModel::setSCFPdfs : Doing an untagged fit so will not use the positive PDF." << std::endl;
}
}
negScfPdfs_.push_back(negPdf);
posScfPdfs_.push_back(posPdf);
}
void LauCPFitModel::setBkgndPdfs(const TString& bkgndClass, LauAbsPdf* negPdf, LauAbsPdf* posPdf)
{
if ( tagged_ ) {
if (negPdf==0 || posPdf==0) {
std::cerr << "ERROR in LauCPFitModel::setBkgndPdfs : One or both of the PDF pointers is null." << std::endl;
return;
}
} else {
// if we're doing an untagged analysis we will only use the negative PDFs
if ( negPdf==0 ) {
std::cerr << "ERROR in LauCPFitModel::setBkgndPdfs : The negative PDF pointer is null." << std::endl;
return;
}
if ( posPdf!=0 ) {
std::cerr << "WARNING in LauCPFitModel::setBkgndPdfs : Doing an untagged fit so will not use the positive PDF." << std::endl;
}
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauCPFitModel::setBkgndPdfs : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
negBkgndPdfs_[bkgndID].push_back(negPdf);
posBkgndPdfs_[bkgndID].push_back(posPdf);
usingBkgnd_ = kTRUE;
}
void LauCPFitModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
// Resize the coeffPars vector if not already done
if ( coeffPars_.empty() ) {
const UInt_t nNegAmp { negSigModel_->getnTotAmp() };
const UInt_t nPosAmp { posSigModel_->getnTotAmp() };
if ( nNegAmp != nPosAmp ) {
std::cerr << "ERROR in LauCPFitModel::setAmpCoeffSet : Unequal number of signal DP components in the negative and positive models: " << nNegAmp << " != " << nPosAmp << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
coeffPars_.resize( nNegAmp );
fitFracAsymm_.resize( nNegAmp );
acp_.resize( nNegAmp );
}
// Is there a component called compName in the signal model?
const TString compName { coeffSet->name()};
const TString conjName { negSigModel_->getConjResName(compName) };
const Int_t negIndex { negSigModel_->resonanceIndex(compName) };
const Int_t posIndex { posSigModel_->resonanceIndex(conjName) };
if ( negIndex < 0 ) {
std::cerr << "ERROR in LauCPFitModel::setAmpCoeffSet : " << negParent_ << " signal DP model doesn't contain component \"" << compName << "\"." << std::endl;
return;
}
if ( posIndex < 0 ) {
std::cerr << "ERROR in LauCPFitModel::setAmpCoeffSet : " << posParent_ << " signal DP model doesn't contain component \"" << conjName << "\"." << std::endl;
return;
}
if ( posIndex != negIndex ) {
std::cerr << "ERROR in LauCPFitModel::setAmpCoeffSet : " << negParent_ << " signal DP model and " << posParent_ << " signal DP model have different indices for components \"" << compName << "\" and \"" << conjName << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
if ( coeffPars_[negIndex] != nullptr && coeffPars_[negIndex]->name() == compName) {
std::cerr << "ERROR in LauCPFitModel::setAmpCoeffSet : Have already set coefficients for \"" << compName << "\"." << std::endl;
return;
}
coeffSet->index(negIndex);
std::cout << "INFO in LauCPFitModel::setAmpCoeffSet : Added coefficients for component \"" << compName << "\" to the fit model." << std::endl;
coeffSet->printParValues();
const TString parName { coeffSet->baseName() + "FitFracAsym" };
fitFracAsymm_[negIndex] = LauParameter{parName, 0.0, -1.0, 1.0};
acp_[negIndex] = coeffSet->acp();
coeffPars_[negIndex] = std::move(coeffSet);
++nSigComp_;
}
std::vector<const LauAbsCoeffSet*> LauCPFitModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_.size(), nullptr );
std::transform( coeffPars_.begin(), coeffPars_.end(), coeffs.begin(), [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); } );
return coeffs;
}
void LauCPFitModel::initialise()
{
// Initialisation
if (!this->useDP() && negSignalPdfs_.empty()) {
std::cerr << "ERROR in LauCPFitModel::initialise : Signal model doesn't exist for any variable." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( this->useDP() ) {
// Check that we have all the Dalitz-plot models
if ((negSigModel_ == 0) || (posSigModel_ == 0)) {
std::cerr << "ERROR in LauCPFitModel::initialise : the pointer to one (neg or pos) of the signal DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
}
if ( usingBkgnd_ ) {
if ( negBkgndDPModels_.empty() || posBkgndDPModels_.empty() ) {
std::cerr << "ERROR in LauCPFitModel::initialise : No background DP models found.\n";
std::cerr << " : Removing the Dalitz plot from the model." << std::endl;
this->useDP(kFALSE);
}
for (LauBkgndDPModelList::const_iterator dpmodel_iter = negBkgndDPModels_.begin(); dpmodel_iter != negBkgndDPModels_.end(); ++dpmodel_iter ) {
if ( (*dpmodel_iter) == 0 ) {
std::cerr << "ERROR in LauCPFitModel::initialise : The pointer to one of the background DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
break;
}
}
for (LauBkgndDPModelList::const_iterator dpmodel_iter = posBkgndDPModels_.begin(); dpmodel_iter != posBkgndDPModels_.end(); ++dpmodel_iter ) {
if ( (*dpmodel_iter) == 0 ) {
std::cerr << "ERROR in LauCPFitModel::initialise : The pointer to one of the background DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
break;
}
}
}
// Need to check that the number of components we have and that the dynamics has matches up
const UInt_t nNegAmp = negSigModel_->getnTotAmp();
const UInt_t nPosAmp = posSigModel_->getnTotAmp();
if ( nNegAmp != nPosAmp ) {
std::cerr << "ERROR in LauCPFitModel::initialise : Unequal number of signal DP components in the negative and positive models: " << nNegAmp << " != " << nPosAmp << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( nNegAmp != nSigComp_ ) {
std::cerr << "ERROR in LauCPFitModel::initialise : Number of signal DP components in the model (" << nNegAmp << ") not equal to number of coefficients supplied (" << nSigComp_ << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( !fixParamFileName_.IsNull() || !fixParamMap_.empty() ) {
// Set coefficients
std::vector<LauParameter*> params;
for ( auto itr = coeffPars_.begin(); itr != coeffPars_.end(); ++itr ) {
std::vector<LauParameter*> p = (*itr)->getParameters();
params.insert(params.end(), p.begin(), p.end());
}
this->fixParams(params);
// Set resonance parameters (if they exist)
negSigModel_->collateResonanceParameters();
posSigModel_->collateResonanceParameters();
this->fixParams(negSigModel_->getFloatingParameters());
this->fixParams(posSigModel_->getFloatingParameters());
}
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// If all is well, go ahead and initialise them
this->initialiseDPModels();
}
// Next check that, if a given component is being used we've got the
// right number of PDFs for all the variables involved
// TODO - should probably check variable names and so on as well
UInt_t nsigpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = negSignalPdfs_.begin(); pdf_iter != negSignalPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nsigpdfvars;
}
}
}
if (useSCF_) {
UInt_t nscfpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = negScfPdfs_.begin(); pdf_iter != negScfPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nscfpdfvars;
}
}
}
if (nscfpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauCPFitModel::initialise : There are " << nsigpdfvars << " TM signal PDF variables but " << nscfpdfvars << " SCF signal PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
if (usingBkgnd_) {
for (LauBkgndPdfsList::const_iterator bgclass_iter = negBkgndPdfs_.begin(); bgclass_iter != negBkgndPdfs_.end(); ++bgclass_iter) {
UInt_t nbkgndpdfvars(0);
const LauPdfPList& pdfList = (*bgclass_iter);
for ( LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nbkgndpdfvars;
}
}
}
if (nbkgndpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauCPFitModel::initialise : There are " << nsigpdfvars << " signal PDF variables but " << nbkgndpdfvars << " bkgnd PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
}
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the various extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nExtraPdfPar_ + nNormPar_)) {
std::cerr << "ERROR in LauCPFitModel::initialise : Number of fit parameters not of expected size. Exiting" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauCPFitModel::recalculateNormalisation()
{
//std::cout << "INFO in LauCPFitModel::recalculateNormalizationInDPModels : Recalc Norm in DP model" << std::endl;
negSigModel_->recalculateNormalisation();
posSigModel_->recalculateNormalisation();
negSigModel_->modifyDataTree();
posSigModel_->modifyDataTree();
}
void LauCPFitModel::initialiseDPModels()
{
std::cout << "INFO in LauCPFitModel::initialiseDPModels : Initialising signal DP model" << std::endl;
negSigModel_->initialise(negCoeffs_);
posSigModel_->initialise(posCoeffs_);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = negBkgndDPModels_.begin(); iter != negBkgndDPModels_.end(); ++iter) {
(*iter)->initialise();
}
for (LauBkgndDPModelList::iterator iter = posBkgndDPModels_.begin(); iter != posBkgndDPModels_.end(); ++iter) {
(*iter)->initialise();
}
}
}
void LauCPFitModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauCPFitModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; i++) {
LauParameterPList pars = coeffPars_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit variables and in a separate vector
// Need to make sure that they are unique because some might appear in both DP models
LauParameterPList& negSigDPPars = negSigModel_->getFloatingParameters();
LauParameterPList& posSigDPPars = posSigModel_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( negSigDPPars );
nSigDPPar_ += this->addResonanceParameters( posSigDPPars );
}
void LauCPFitModel::setExtraPdfParameters()
{
// Include all the parameters of the PDF in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
nExtraPdfPar_ += this->addFitParameters(negSignalPdfs_);
if ( tagged_ ) {
nExtraPdfPar_ += this->addFitParameters(posSignalPdfs_);
}
if (useSCF_ == kTRUE) {
nExtraPdfPar_ += this->addFitParameters(negScfPdfs_);
if ( tagged_ ) {
nExtraPdfPar_ += this->addFitParameters(posScfPdfs_);
}
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = negBkgndPdfs_.begin(); iter != negBkgndPdfs_.end(); ++iter) {
nExtraPdfPar_ += this->addFitParameters(*iter);
}
if ( tagged_ ) {
for (LauBkgndPdfsList::iterator iter = posBkgndPdfs_.begin(); iter != posBkgndPdfs_.end(); ++iter) {
nExtraPdfPar_ += this->addFitParameters(*iter);
}
}
}
}
void LauCPFitModel::setFitNEvents()
{
if ( signalEvents_ == 0 ) {
std::cerr << "ERROR in LauCPFitModel::setFitNEvents : Signal yield not defined." << std::endl;
return;
}
nNormPar_ = 0;
// initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
nTotEvts += (*iter)->value();
if ( (*iter) == 0 ) {
std::cerr << "ERROR in LauCPFitModel::setFitNEvents : Background yield not defined." << std::endl;
return;
}
}
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the number of signal events into the fit parameters
if (this->doEMLFit()) {
std::cout << "INFO in LauCPFitModel::setFitNEvents : Initialising number of events for signal and background components..." << std::endl;
// add the signal fraction to the list of fit parameters
nNormPar_ += this->addFitParameters( signalEvents_ );
} else {
std::cout << "INFO in LauCPFitModel::setFitNEvents : Initialising number of events for background components (and hence signal)..." << std::endl;
}
// if not using the DP in the model we need an explicit signal asymmetry parameter
if (this->useDP() == kFALSE) {
nNormPar_ += this->addFitParameters( signalAsym_ );
}
if (useSCF_ && !useSCFHist_) {
nNormPar_ += this->addFitParameters( &scfFrac_ );
}
if (usingBkgnd_ == kTRUE) {
nNormPar_ += this->addFitParameters( bkgndEvents_ );
nNormPar_ += this->addFitParameters( bkgndAsym_ );
}
}
void LauCPFitModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the positive and negative fit fractions for each signal component
negFitFrac_ = negSigModel_->getFitFractions();
if (negFitFrac_.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFrac_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFrac_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
posFitFrac_ = posSigModel_->getFitFractions();
if (posFitFrac_.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFrac_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFrac_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Add the positive and negative fit fractions that have not been corrected for the efficiency for each signal component
negFitFracEffUnCorr_ = negSigModel_->getFitFractionsEfficiencyUncorrected();
if (negFitFracEffUnCorr_.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFracEffUnCorr_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
posFitFracEffUnCorr_ = posSigModel_->getFitFractionsEfficiencyUncorrected();
if (posFitFracEffUnCorr_.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFracEffUnCorr_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
TString name = negFitFrac_[i][j].name();
name.Insert( name.Index("FitFrac"), "Neg" );
negFitFrac_[i][j].name(name);
extraVars.push_back(negFitFrac_[i][j]);
name = posFitFrac_[i][j].name();
name.Insert( name.Index("FitFrac"), "Pos" );
posFitFrac_[i][j].name(name);
extraVars.push_back(posFitFrac_[i][j]);
name = negFitFracEffUnCorr_[i][j].name();
name.Insert( name.Index("FitFrac"), "Neg" );
negFitFracEffUnCorr_[i][j].name(name);
extraVars.push_back(negFitFracEffUnCorr_[i][j]);
name = posFitFracEffUnCorr_[i][j].name();
name.Insert( name.Index("FitFrac"), "Pos" );
posFitFracEffUnCorr_[i][j].name(name);
extraVars.push_back(posFitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> negExtraPars = negSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator negExtraIter;
for (negExtraIter = negExtraPars.begin(); negExtraIter != negExtraPars.end(); ++negExtraIter) {
LauParameter negExtraParameter = (*negExtraIter);
extraVars.push_back(negExtraParameter);
}
std::vector<LauParameter> posExtraPars = posSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator posExtraIter;
for (posExtraIter = posExtraPars.begin(); posExtraIter != posExtraPars.end(); ++posExtraIter) {
LauParameter posExtraParameter = (*posExtraIter);
extraVars.push_back(posExtraParameter);
}
// Now add in the DP efficiency value
Double_t initMeanEff = negSigModel_->getMeanEff().initValue();
negMeanEff_.value(initMeanEff);
negMeanEff_.genValue(initMeanEff);
negMeanEff_.initValue(initMeanEff);
extraVars.push_back(negMeanEff_);
initMeanEff = posSigModel_->getMeanEff().initValue();
posMeanEff_.value(initMeanEff);
posMeanEff_.genValue(initMeanEff);
posMeanEff_.initValue(initMeanEff);
extraVars.push_back(posMeanEff_);
// Also add in the DP rates
Double_t initDPRate = negSigModel_->getDPRate().initValue();
negDPRate_.value(initDPRate);
negDPRate_.genValue(initDPRate);
negDPRate_.initValue(initDPRate);
extraVars.push_back(negDPRate_);
initDPRate = posSigModel_->getDPRate().initValue();
posDPRate_.value(initDPRate);
posDPRate_.genValue(initDPRate);
posDPRate_.initValue(initDPRate);
extraVars.push_back(posDPRate_);
// Calculate the CPC and CPV Fit Fractions, ACPs and FitFrac asymmetries
this->calcExtraFractions(kTRUE);
this->calcAsymmetries(kTRUE);
// Add the CP violating and CP conserving fit fractions for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j = i; j < nSigComp_; j++) {
extraVars.push_back(CPVFitFrac_[i][j]);
}
}
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j = i; j < nSigComp_; j++) {
extraVars.push_back(CPCFitFrac_[i][j]);
}
}
// Add the Fit Fraction asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(fitFracAsymm_[i]);
}
// Add the calculated CP asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(acp_[i]);
}
}
void LauCPFitModel::calcExtraFractions(Bool_t initValues)
{
// Calculate the CP-conserving and CP-violating fit fractions
if (initValues) {
// create the structure
CPCFitFrac_.clear();
CPVFitFrac_.clear();
CPCFitFrac_.resize(nSigComp_);
CPVFitFrac_.resize(nSigComp_);
for (UInt_t i(0); i<nSigComp_; ++i) {
CPCFitFrac_[i].resize(nSigComp_);
CPVFitFrac_[i].resize(nSigComp_);
for (UInt_t j(i); j<nSigComp_; ++j) {
TString name = negFitFrac_[i][j].name();
name.Replace( name.Index("Neg"), 3, "CPC" );
CPCFitFrac_[i][j].name( name );
CPCFitFrac_[i][j].valueAndRange( 0.0, -100.0, 100.0 );
name = negFitFrac_[i][j].name();
name.Replace( name.Index("Neg"), 3, "CPV" );
CPVFitFrac_[i][j].name( name );
CPVFitFrac_[i][j].valueAndRange( 0.0, -100.0, 100.0 );
}
}
}
Double_t denom = negDPRate_.value() + posDPRate_.value();
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
Double_t negTerm = negFitFrac_[i][j].value()*negDPRate_.value();
Double_t posTerm = posFitFrac_[i][j].value()*posDPRate_.value();
Double_t cpcFitFrac = (negTerm + posTerm)/denom;
Double_t cpvFitFrac = (negTerm - posTerm)/denom;
CPCFitFrac_[i][j].value(cpcFitFrac);
CPVFitFrac_[i][j].value(cpvFitFrac);
if (initValues) {
CPCFitFrac_[i][j].genValue(cpcFitFrac);
CPCFitFrac_[i][j].initValue(cpcFitFrac);
CPVFitFrac_[i][j].genValue(cpvFitFrac);
CPVFitFrac_[i][j].initValue(cpvFitFrac);
}
}
}
}
void LauCPFitModel::calcAsymmetries(Bool_t initValues)
{
// Calculate the CP asymmetries
// Also calculate the fit fraction asymmetries
for (UInt_t i = 0; i < nSigComp_; i++) {
acp_[i] = coeffPars_[i]->acp();
LauAsymmCalc asymmCalc(negFitFrac_[i][i].value(), posFitFrac_[i][i].value());
Double_t asym = asymmCalc.getAsymmetry();
fitFracAsymm_[i].value(asym);
if (initValues) {
fitFracAsymm_[i].genValue(asym);
fitFracAsymm_[i].initValue(asym);
}
}
}
void LauCPFitModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// i.e. to make mag > 0.0, phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; ++i) {
// Check whether we have "a/b > 0.0", and phases in the right range
coeffPars_[i]->finaliseValues();
}
}
// update the pulls on the event fractions and asymmetries
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (this->useDP() == kFALSE) {
signalAsym_->updatePull();
}
if (useSCF_ && !useSCFHist_) {
scfFrac_.updatePull();
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndYieldList::iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
std::vector<LauParameter*> parameters = (*iter)->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
for (LauBkgndYieldList::iterator iter = bkgndAsym_.begin(); iter != bkgndAsym_.end(); ++iter) {
std::vector<LauParameter*> parameters = (*iter)->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
}
// Update the pulls on all the extra PDFs' parameters
this->updateFitParameters(negSignalPdfs_);
this->updateFitParameters(posSignalPdfs_);
if (useSCF_ == kTRUE) {
this->updateFitParameters(negScfPdfs_);
this->updateFitParameters(posScfPdfs_);
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = negBkgndPdfs_.begin(); iter != negBkgndPdfs_.end(); ++iter) {
this->updateFitParameters(*iter);
}
for (LauBkgndPdfsList::iterator iter = posBkgndPdfs_.begin(); iter != posBkgndPdfs_.end(); ++iter) {
this->updateFitParameters(*iter);
}
}
// Fill the fit results to the ntuple
// update the coefficients and then calculate the fit fractions and ACP's
if (this->useDP() == kTRUE) {
this->updateCoeffs();
negSigModel_->updateCoeffs(negCoeffs_); negSigModel_->calcExtraInfo();
posSigModel_->updateCoeffs(posCoeffs_); posSigModel_->calcExtraInfo();
LauParArray negFitFrac = negSigModel_->getFitFractions();
if (negFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFrac = posSigModel_->getFitFractions();
if (posFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray negFitFracEffUnCorr = negSigModel_->getFitFractionsEfficiencyUncorrected();
if (negFitFracEffUnCorr.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFracEffUnCorr[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << negFitFracEffUnCorr[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFracEffUnCorr = posSigModel_->getFitFractionsEfficiencyUncorrected();
if (posFitFracEffUnCorr.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFracEffUnCorr[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << posFitFracEffUnCorr[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
negFitFrac_[i][j].value(negFitFrac[i][j].value());
posFitFrac_[i][j].value(posFitFrac[i][j].value());
negFitFracEffUnCorr_[i][j].value(negFitFracEffUnCorr[i][j].value());
posFitFracEffUnCorr_[i][j].value(posFitFracEffUnCorr[i][j].value());
}
}
negMeanEff_.value(negSigModel_->getMeanEff().value());
posMeanEff_.value(posSigModel_->getMeanEff().value());
negDPRate_.value(negSigModel_->getDPRate().value());
posDPRate_.value(posSigModel_->getDPRate().value());
this->calcExtraFractions();
this->calcAsymmetries();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions, FF asymmetries, ACPs, mean efficiency and DP rate)
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the positive and negative fit fractions for each signal component
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(negFitFrac_[i][j]);
extraVars.push_back(posFitFrac_[i][j]);
extraVars.push_back(negFitFracEffUnCorr_[i][j]);
extraVars.push_back(posFitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> negExtraPars = negSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator negExtraIter;
for (negExtraIter = negExtraPars.begin(); negExtraIter != negExtraPars.end(); ++negExtraIter) {
LauParameter negExtraParameter = (*negExtraIter);
extraVars.push_back(negExtraParameter);
}
std::vector<LauParameter> posExtraPars = posSigModel_->getExtraParameters();
std::vector<LauParameter>::iterator posExtraIter;
for (posExtraIter = posExtraPars.begin(); posExtraIter != posExtraPars.end(); ++posExtraIter) {
LauParameter posExtraParameter = (*posExtraIter);
extraVars.push_back(posExtraParameter);
}
extraVars.push_back(negMeanEff_);
extraVars.push_back(posMeanEff_);
extraVars.push_back(negDPRate_);
extraVars.push_back(posDPRate_);
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(CPVFitFrac_[i][j]);
}
}
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(CPCFitFrac_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(fitFracAsymm_[i]);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(acp_[i]);
}
this->printFitFractions(std::cout);
this->printAsymmetries(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauCPFitModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
// First for the B- events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << negParent_ << " FitFraction for component " << i << " (" << compName << ") = " << negFitFrac_[i][i].value() << std::endl;
}
output << negParent_ << " overall DP rate (integral of matrix element squared) = " << negDPRate_.value() << std::endl;
output << negParent_ << " average efficiency weighted by whole DP dynamics = " << negMeanEff_.value() << std::endl;
// Then for the positive sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
const TString conjName(negSigModel_->getConjResName(compName));
output << posParent_ << " FitFraction for component " << i << " (" << conjName << ") = " << posFitFrac_[i][i].value() << std::endl;
}
output << posParent_ << " overall DP rate (integral of matrix element squared) = " << posDPRate_.value() << std::endl;
output << posParent_ << " average efficiency weighted by whole DP dynamics = " << posMeanEff_.value() << std::endl;
}
void LauCPFitModel::printAsymmetries(std::ostream& output)
{
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << "Fit Fraction asymmetry for component " << i << " (" << compName << ") = " << fitFracAsymm_[i].value() << std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output << "ACP for component " << i << " (" << compName << ") = " << acp_[i].value() << std::endl;
}
}
void LauCPFitModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
// TODO - need to include the yields in this table
std::ofstream fout(outputFile);
LauPrint print;
std::cout << "INFO in LauCPFitModel::writeOutTable : Writing out results of the fit to the tex file " << outputFile << std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->printTableRow(fout);
}
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
// print the fit fractions and asymmetries in another
fout << "\\begin{tabular}{|l|c|c|c|c|}" << std::endl;
fout << "\\hline" << std::endl;
fout << "Component & " << negParent_ << " Fit Fraction & " << posParent_ << " Fit Fraction & Fit Fraction Asymmetry & ACP \\\\" << std::endl;
fout << "\\hline" << std::endl;
Double_t negFitFracSum(0.0);
Double_t posFitFracSum(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
Double_t negFitFrac = negFitFrac_[i][i].value();
Double_t posFitFrac = posFitFrac_[i][i].value();
negFitFracSum += negFitFrac;
posFitFracSum += posFitFrac;
Double_t fitFracAsymm = fitFracAsymm_[i].value();
Double_t acp = acp_[i].value();
Double_t acpErr = acp_[i].error();
fout << resName << " & $";
print.printFormat(fout, negFitFrac);
fout << "$ & $";
print.printFormat(fout, posFitFrac);
fout << "$ & $";
print.printFormat(fout, fitFracAsymm);
fout << "$ & $";
print.printFormat(fout, acp);
fout << " \\pm ";
print.printFormat(fout, acpErr);
fout << "$ \\\\" << std::endl;
}
fout << "\\hline" << std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum & $";
print.printFormat(fout, negFitFracSum);
fout << "$ & $";
print.printFormat(fout, posFitFracSum);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "DP rate & $";
print.printFormat(fout, negDPRate_.value());
fout << "$ & $";
print.printFormat(fout, posDPRate_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > $ & $";
print.printFormat(fout, negMeanEff_.value());
fout << "$ & $";
print.printFormat(fout, posMeanEff_.value());
fout << "$ & & \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
}
if (!negSignalPdfs_.empty()) {
fout << "\\begin{tabular}{|l|c|}" << std::endl;
fout << "\\hline" << std::endl;
if (useSCF_ == kTRUE) {
fout << "\\Extra TM Signal PDFs' Parameters: & \\\\" << std::endl;
} else {
fout << "\\Extra Signal PDFs' Parameters: & \\\\" << std::endl;
}
this->printFitParameters(negSignalPdfs_, fout);
if ( tagged_ ) {
this->printFitParameters(posSignalPdfs_, fout);
}
if (useSCF_ == kTRUE && !negScfPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra SCF Signal PDFs' Parameters: & \\\\" << std::endl;
this->printFitParameters(negScfPdfs_, fout);
if ( tagged_ ) {
this->printFitParameters(posScfPdfs_, fout);
}
}
if (usingBkgnd_ == kTRUE && !negBkgndPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra Background PDFs' Parameters: & \\\\" << std::endl;
for (LauBkgndPdfsList::const_iterator iter = negBkgndPdfs_.begin(); iter != negBkgndPdfs_.end(); ++iter) {
this->printFitParameters(*iter, fout);
}
if ( tagged_ ) {
for (LauBkgndPdfsList::const_iterator iter = posBkgndPdfs_.begin(); iter != posBkgndPdfs_.end(); ++iter) {
this->printFitParameters(*iter, fout);
}
}
}
fout << "\\hline \n\\end{tabular}" << std::endl << std::endl;
}
}
void LauCPFitModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
std::cout << "INFO in LauCPFitModel::checkInitFitParams : Setting random parameters for the signal model" << std::endl;
this->randomiseInitFitPars();
}
}
void LauCPFitModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout << "INFO in LauCPFitModel::randomiseInitFitPars : Randomising the initial fit magnitudes and phases of the components..." << std::endl;
if ( fixParamFileName_.IsNull() && fixParamMap_.empty() ) {
// No params are imported - randomise as normal
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
} else {
// Only randomise those that are not imported (i.e., not found in allImportedFreeParams_)
// by temporarily fixing all imported parameters, and then freeing those not set to be fixed when imported,
// except those that are previously set to be fixed anyhow.
// Convoluted, but beats changing the behaviour of functions that call checkInitFitParams or the coeffSet
// itself.
for (auto p : allImportedFreeParams_) { p->fixed(kTRUE); }
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
for (auto p : allImportedFreeParams_) { p->fixed(kFALSE); }
}
}
std::pair<LauCPFitModel::LauGenInfo,Bool_t> LauCPFitModel::eventsToGenerate()
{
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
LauGenInfo nEvtsGen;
// Keep track of whether any yield or asymmetry parameters are blinded
Bool_t blind = kFALSE;
// Signal
if ( signalEvents_->blind() ) {
blind = kTRUE;
}
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0.0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
Double_t sigAsym(0.0);
// need to include this as an alternative in case the DP isn't in the model
if ( !this->useDP() || forceAsym_ ) {
sigAsym = signalAsym_->genValue();
if ( signalAsym_->blind() ) {
blind = kTRUE;
}
} else {
Double_t negRate = negSigModel_->getDPNorm();
Double_t posRate = posSigModel_->getDPNorm();
if (negRate+posRate>1e-30) {
sigAsym = (negRate-posRate)/(negRate+posRate);
}
}
Double_t nPosEvts = (nEvts/2.0 * (1.0 - sigAsym));
Double_t nNegEvts = (nEvts/2.0 * (1.0 + sigAsym));
Int_t nPosEvtsToGen { static_cast<Int_t>(nPosEvts) };
Int_t nNegEvtsToGen { static_cast<Int_t>(nNegEvts) };
if (this->doPoissonSmearing()) {
nPosEvtsToGen = LauRandom::randomFun()->Poisson(nPosEvts);
nNegEvtsToGen = LauRandom::randomFun()->Poisson(nNegEvts);
}
nEvtsGen[std::make_pair("signal",+1)] = std::make_pair(nPosEvtsToGen,evtWeight);
nEvtsGen[std::make_pair("signal",-1)] = std::make_pair(nNegEvtsToGen,evtWeight);
// backgrounds
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const LauAbsRValue* evtsPar = bkgndEvents_[bkgndID];
const LauAbsRValue* asymPar = bkgndAsym_[bkgndID];
if ( evtsPar->blind() || asymPar->blind() ) {
blind = kTRUE;
}
evtWeight = 1.0;
nEvts = evtsPar->genValue();
if ( nEvts < 0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
const Double_t asym = asymPar->genValue();
nPosEvts = (nEvts/2.0 * (1.0 - asym));
nNegEvts = (nEvts/2.0 * (1.0 + asym));
nPosEvtsToGen = static_cast<Int_t>(nPosEvts);
nNegEvtsToGen = static_cast<Int_t>(nNegEvts);
if (this->doPoissonSmearing()) {
nPosEvtsToGen = LauRandom::randomFun()->Poisson(nPosEvts);
nNegEvtsToGen = LauRandom::randomFun()->Poisson(nNegEvts);
}
const TString& bkgndClass = this->bkgndClassName(bkgndID);
nEvtsGen[std::make_pair(bkgndClass,+1)] = std::make_pair(nPosEvtsToGen,evtWeight);
nEvtsGen[std::make_pair(bkgndClass,-1)] = std::make_pair(nNegEvtsToGen,evtWeight);
}
// Print out the information on what we're generating, but only if none of the parameters are blind (otherwise we risk unblinding them!)
if ( !blind ) {
std::cout << "INFO in LauCPFitModel::eventsToGenerate : Generating toy MC with:" << std::endl;
std::cout << " : Signal asymmetry = " << sigAsym << " and number of signal events = " << signalEvents_->genValue() << std::endl;
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
const LauAbsRValue* evtsPar = bkgndEvents_[bkgndID];
const LauAbsRValue* asymPar = bkgndAsym_[bkgndID];
std::cout << " : " << bkgndClass << " asymmetry = " << asymPar->genValue() << " and number of " << bkgndClass << " events = " << evtsPar->genValue() << std::endl;
}
}
return std::make_pair( nEvtsGen, blind );
}
Bool_t LauCPFitModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
std::pair<LauGenInfo,Bool_t> info = this->eventsToGenerate();
LauGenInfo nEvts = info.first;
const Bool_t blind = info.second;
Bool_t genOK(kTRUE);
Int_t evtNum(0);
const UInt_t nBkgnds = this->nBkgndClasses();
std::vector<TString> bkgndClassNames(nBkgnds);
std::vector<TString> bkgndClassNamesGen(nBkgnds);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
bkgndClassNames[iBkgnd] = name;
bkgndClassNamesGen[iBkgnd] = "gen"+name;
}
const Bool_t storeSCFTruthInfo = ( useSCF_ || ( this->enableEmbedding() &&
negSignalTree_ != 0 && negSignalTree_->haveBranch("mcMatch") &&
posSignalTree_ != 0 && posSignalTree_->haveBranch("mcMatch") ) );
// Loop over the hypotheses and generate the requested number of events for each one
for (LauGenInfo::const_iterator iter = nEvts.begin(); iter != nEvts.end(); ++iter) {
const TString& type(iter->first.first);
curEvtCharge_ = iter->first.second;
Double_t evtWeight( iter->second.second );
Int_t nEvtsGen( iter->second.first );
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
this->setGenNtupleDoubleBranchValue( "efficiency", 1.0 );
if (type == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], 0 );
}
genOK = this->generateSignalEvent();
if ( curEvtCharge_ > 0 ){
this->setGenNtupleDoubleBranchValue( "efficiency", posSigModel_->getEvtEff() );
} else {
this->setGenNtupleDoubleBranchValue( "efficiency", negSigModel_->getEvtEff() );
}
} else {
this->setGenNtupleIntegerBranchValue("genSig",0);
if ( storeSCFTruthInfo ) {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
}
UInt_t bkgndID(0);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
Int_t gen(0);
if ( bkgndClassNames[iBkgnd] == type ) {
gen = 1;
bkgndID = iBkgnd;
}
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], gen );
}
genOK = this->generateBkgndEvent(bkgndID);
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPBranchValues();
}
// Store the event charge
this->setGenNtupleIntegerBranchValue(tagVarName_,curEvtCharge_);
// Store the event number (within this experiment)
// and then increment it
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
++evtNum;
this->fillGenNtupleBranches();
if ( !blind && (iEvt%500 == 0) ) {
std::cout << "INFO in LauCPFitModel::genExpt : Generated event number " << iEvt << " out of " << nEvtsGen << " " << type << " events." << std::endl;
}
}
if (!genOK) {
break;
}
}
if (this->useDP() && genOK) {
negSigModel_->checkToyMC(kTRUE,kTRUE);
posSigModel_->checkToyMC(kTRUE,kTRUE);
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray negFitFrac = negSigModel_->getFitFractions();
if (negFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << negFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (negFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << negFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray posFitFrac = posSigModel_->getFitFractions();
if (posFitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << posFitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (posFitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauCPFitModel::genExpt : Fit Fraction array of unexpected dimension: " << posFitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
negFitFrac_[i][j].value(negFitFrac[i][j].value());
posFitFrac_[i][j].value(posFitFrac[i][j].value());
}
}
negMeanEff_.value(negSigModel_->getMeanEff().value());
posMeanEff_.value(posSigModel_->getMeanEff().value());
negDPRate_.value(negSigModel_->getDPRate().value());
posDPRate_.value(posSigModel_->getDPRate().value());
}
}
// If we're reusing embedded events or if the generation is being
// reset then clear the lists of used events
if (reuseSignal_ || !genOK) {
if (negSignalTree_) {
negSignalTree_->clearUsedList();
}
if (posSignalTree_) {
posSignalTree_->clearUsedList();
}
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
LauEmbeddedData* data = negBkgndTree_[bkgndID];
if (reuseBkgnd_[bkgndID] || !genOK) {
if (data) {
data->clearUsedList();
}
}
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
LauEmbeddedData* data = posBkgndTree_[bkgndID];
if (reuseBkgnd_[bkgndID] || !genOK) {
if (data) {
data->clearUsedList();
}
}
}
return genOK;
}
Bool_t LauCPFitModel::generateSignalEvent()
{
// Generate signal event
Bool_t genOK(kTRUE);
Bool_t genSCF(kFALSE);
LauIsobarDynamics* model(0);
LauKinematics* kinematics(0);
LauEmbeddedData* embeddedData(0);
LauPdfPList* sigPdfs(0);
LauPdfPList* scfPdfs(0);
Bool_t doReweighting(kFALSE);
if (curEvtCharge_<0) {
model = negSigModel_;
kinematics = negKinematics_;
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
if (this->enableEmbedding()) {
embeddedData = negSignalTree_;
doReweighting = useNegReweighting_;
}
} else {
model = posSigModel_;
kinematics = posKinematics_;
if ( tagged_ ) {
sigPdfs = &posSignalPdfs_;
scfPdfs = &posScfPdfs_;
} else {
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
}
if (this->enableEmbedding()) {
embeddedData = posSignalTree_;
doReweighting = usePosReweighting_;
}
}
if (this->useDP()) {
if (embeddedData) {
if (doReweighting) {
// Select a (random) event from the generated data. Then store the
// reconstructed DP co-ords, together with other pdf information,
// as the embedded data.
genOK = embeddedData->getReweightedEvent(model);
} else {
// Just get the information of a (randomly) selected event in the
// embedded data
embeddedData->getEmbeddedEvent(kinematics);
}
genSCF = this->storeSignalMCMatch( embeddedData );
} else {
genOK = model->generate();
if ( genOK && useSCF_ ) {
Double_t frac(0.0);
if ( useSCFHist_ ) {
frac = scfFracHist_->calcEfficiency( kinematics );
} else {
frac = scfFrac_.genValue();
}
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
// Optionally smear the DP position
// of the SCF event
if ( scfMap_ != 0 ) {
Double_t xCoord(0.0), yCoord(0.0);
if ( kinematics->squareDP() ) {
xCoord = kinematics->getmPrime();
yCoord = kinematics->getThetaPrime();
} else {
xCoord = kinematics->getm13Sq();
yCoord = kinematics->getm23Sq();
}
// Find the bin number where this event is generated
Int_t binNo = scfMap_->binNumber( xCoord, yCoord );
// Retrieve the migration histogram
TH2* histo = scfMap_->trueHist( binNo );
const LauAbsEffModel * effModel = model->getEffModel();
do {
// Get a random point from the histogram
histo->GetRandom2( xCoord, yCoord );
// Update the kinematics
if ( kinematics->squareDP() ) {
kinematics->updateSqDPKinematics( xCoord, yCoord );
} else {
kinematics->updateKinematics( xCoord, yCoord );
}
} while ( ! effModel->passVeto( kinematics ) );
}
}
}
}
} else {
if (embeddedData) {
embeddedData->getEmbeddedEvent(0);
genSCF = this->storeSignalMCMatch( embeddedData );
} else if ( useSCF_ ) {
Double_t frac = scfFrac_.genValue();
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
}
}
if (genOK) {
if ( useSCF_ ) {
if ( genSCF ) {
this->generateExtraPdfValues(scfPdfs, embeddedData);
} else {
this->generateExtraPdfValues(sigPdfs, embeddedData);
}
} else {
this->generateExtraPdfValues(sigPdfs, embeddedData);
}
}
// Check for problems with the embedding
if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
std::cerr << "WARNING in LauCPFitModel::generateSignalEvent : Source of embedded signal events used up, clearing the list of used events." << std::endl;
embeddedData->clearUsedList();
}
return genOK;
}
Bool_t LauCPFitModel::generateBkgndEvent(UInt_t bkgndID)
{
// Generate Bkgnd event
Bool_t genOK(kTRUE);
LauAbsBkgndDPModel* model(0);
LauEmbeddedData* embeddedData(0);
LauPdfPList* extraPdfs(0);
LauKinematics* kinematics(0);
if (curEvtCharge_<0) {
model = negBkgndDPModels_[bkgndID];
if (this->enableEmbedding()) {
embeddedData = negBkgndTree_[bkgndID];
}
extraPdfs = &negBkgndPdfs_[bkgndID];
kinematics = negKinematics_;
} else {
model = posBkgndDPModels_[bkgndID];
if (this->enableEmbedding()) {
embeddedData = posBkgndTree_[bkgndID];
}
if ( tagged_ ) {
extraPdfs = &posBkgndPdfs_[bkgndID];
} else {
extraPdfs = &negBkgndPdfs_[bkgndID];
}
kinematics = posKinematics_;
}
if (this->useDP()) {
if (embeddedData) {
embeddedData->getEmbeddedEvent(kinematics);
} else {
if (model == 0) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
std::cerr << "ERROR in LauCPFitModel::generateBkgndEvent : Can't find the DP model for background class \"" << bkgndClass << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
genOK = model->generate();
}
} else {
if (embeddedData) {
embeddedData->getEmbeddedEvent(0);
}
}
if (genOK) {
this->generateExtraPdfValues(extraPdfs, embeddedData);
}
// Check for problems with the embedding
if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
const TString& bkgndClass = this->bkgndClassName(bkgndID);
std::cerr << "WARNING in LauCPFitModel::generateBkgndEvent : Source of embedded " << bkgndClass << " events used up, clearing the list of used events." << std::endl;
embeddedData->clearUsedList();
}
return genOK;
}
void LauCPFitModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleDoubleBranch("efficiency");
if ( useSCF_ || ( this->enableEmbedding() &&
negSignalTree_ != 0 && negSignalTree_->haveBranch("mcMatch") &&
posSignalTree_ != 0 && posSignalTree_->haveBranch("mcMatch") ) ) {
this->addGenNtupleIntegerBranch("genTMSig");
this->addGenNtupleIntegerBranch("genSCFSig");
}
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name.Prepend("gen");
this->addGenNtupleIntegerBranch(name);
}
this->addGenNtupleIntegerBranch("charge");
if (this->useDP() == kTRUE) {
this->addGenNtupleDoubleBranch("m12");
this->addGenNtupleDoubleBranch("m23");
this->addGenNtupleDoubleBranch("m13");
this->addGenNtupleDoubleBranch("m12Sq");
this->addGenNtupleDoubleBranch("m23Sq");
this->addGenNtupleDoubleBranch("m13Sq");
this->addGenNtupleDoubleBranch("cosHel12");
this->addGenNtupleDoubleBranch("cosHel23");
this->addGenNtupleDoubleBranch("cosHel13");
if (negKinematics_->squareDP() && posKinematics_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime");
this->addGenNtupleDoubleBranch("thPrime");
}
}
for (LauPdfPList::const_iterator pdf_iter = negSignalPdfs_.begin(); pdf_iter != negSignalPdfs_.end(); ++pdf_iter) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
this->addGenNtupleDoubleBranch( (*var_iter) );
}
}
}
}
void LauCPFitModel::setDPBranchValues()
{
LauKinematics* kinematics(0);
if (curEvtCharge_<0) {
kinematics = negKinematics_;
} else {
kinematics = posKinematics_;
}
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12", kinematics->getm12());
this->setGenNtupleDoubleBranchValue("m23", kinematics->getm23());
this->setGenNtupleDoubleBranchValue("m13", kinematics->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq", kinematics->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq", kinematics->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq", kinematics->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12", kinematics->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23", kinematics->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13", kinematics->getc13());
if (kinematics->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime", kinematics->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime", kinematics->getThetaPrime());
}
}
void LauCPFitModel::generateExtraPdfValues(LauPdfPList* extraPdfs, LauEmbeddedData* embeddedData)
{
LauKinematics* kinematics(0);
if (curEvtCharge_<0) {
kinematics = negKinematics_;
} else {
kinematics = posKinematics_;
}
if (!extraPdfs) {
std::cerr << "ERROR in LauCPFitModel::generateExtraPdfValues : Null pointer to PDF list." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (extraPdfs->empty()) {
//std::cerr << "WARNING in LauCPFitModel::generateExtraPdfValues : PDF list is empty." << std::endl;
return;
}
// Generate from the extra PDFs
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
LauFitData genValues;
if (embeddedData) {
genValues = embeddedData->getValues( (*pdf_iter)->varNames() );
} else {
genValues = (*pdf_iter)->generate(kinematics);
}
for ( LauFitData::const_iterator var_iter = genValues.begin(); var_iter != genValues.end(); ++var_iter ) {
TString varName = var_iter->first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var_iter->second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
Bool_t LauCPFitModel::storeSignalMCMatch(LauEmbeddedData* embeddedData)
{
// Default to TM
Bool_t genSCF(kFALSE);
Int_t match(1);
// Check that we have a valid pointer and that embedded data has
// the mcMatch branch. If so then get the match value.
if ( embeddedData && embeddedData->haveBranch("mcMatch") ) {
match = TMath::Nint( embeddedData->getValue("mcMatch") );
}
// Set the variables accordingly.
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
return genSCF;
}
void LauCPFitModel::propagateParUpdates()
{
// Update the signal parameters and then the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
negSigModel_->updateCoeffs(negCoeffs_);
posSigModel_->updateCoeffs(posCoeffs_);
}
// Update the signal fraction from the background fractions if not doing an extended fit
this->updateSigEvents();
}
void LauCPFitModel::updateSigEvents()
{
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
// Subtract background events (if any) from signal.
if ( usingBkgnd_ == kTRUE ) {
for ( LauAbsRValue* bkgndYield : bkgndEvents_ ) {
signalEvents -= bkgndYield->value();
}
}
signalEvents_->value(signalEvents);
}
void LauCPFitModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and background models.
// Note that we store the events of both charges in both the
// negative and the positive models. It's only later, at the stage
// when the likelihood is being calculated, that we separate them.
LauFitDataTree* inputFitData = this->fitData();
// First the Dalitz plot variables (m_ij^2)
if (this->useDP() == kTRUE) {
// need to append SCF smearing bins before caching DP amplitudes
if ( scfMap_ != 0 ) {
this->appendBinCentres( inputFitData );
}
negSigModel_->fillDataTree(*inputFitData);
posSigModel_->fillDataTree(*inputFitData);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = negBkgndDPModels_.begin(); iter != negBkgndDPModels_.end(); ++iter) {
(*iter)->fillDataTree(*inputFitData);
}
for (LauBkgndDPModelList::iterator iter = posBkgndDPModels_.begin(); iter != posBkgndDPModels_.end(); ++iter) {
(*iter)->fillDataTree(*inputFitData);
}
}
}
// ...and then the extra PDFs
this->cacheInfo(negSignalPdfs_, *inputFitData);
this->cacheInfo(negScfPdfs_, *inputFitData);
for (LauBkgndPdfsList::iterator iter = negBkgndPdfs_.begin(); iter != negBkgndPdfs_.end(); ++iter) {
this->cacheInfo((*iter), *inputFitData);
}
if ( tagged_ ) {
this->cacheInfo(posSignalPdfs_, *inputFitData);
this->cacheInfo(posScfPdfs_, *inputFitData);
for (LauBkgndPdfsList::iterator iter = posBkgndPdfs_.begin(); iter != posBkgndPdfs_.end(); ++iter) {
this->cacheInfo((*iter), *inputFitData);
}
}
// the SCF fractions and jacobians
if ( useSCF_ && useSCFHist_ ) {
if ( !inputFitData->haveBranch( "m13Sq" ) || !inputFitData->haveBranch( "m23Sq" ) ) {
std::cerr << "ERROR in LauCPFitModel::cacheInputFitVars : Input data does not contain DP branches and so can't cache the SCF fraction." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t nEvents = inputFitData->nEvents();
recoSCFFracs_.clear();
recoSCFFracs_.reserve( nEvents );
if ( negKinematics_->squareDP() ) {
recoJacobians_.clear();
recoJacobians_.reserve( nEvents );
}
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator m13_iter = dataValues.find("m13Sq");
LauFitData::const_iterator m23_iter = dataValues.find("m23Sq");
negKinematics_->updateKinematics( m13_iter->second, m23_iter->second );
Double_t scfFrac = scfFracHist_->calcEfficiency( negKinematics_ );
recoSCFFracs_.push_back( scfFrac );
if ( negKinematics_->squareDP() ) {
recoJacobians_.push_back( negKinematics_->calcSqDPJacobian() );
}
}
}
// finally cache the event charge
evtCharges_.clear();
if ( tagged_ ) {
if ( !inputFitData->haveBranch( tagVarName_ ) ) {
std::cerr << "ERROR in LauCPFitModel::cacheInputFitVars : Input data does not contain branch \"" << tagVarName_ << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t nEvents = inputFitData->nEvents();
evtCharges_.reserve( nEvents );
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator iter = dataValues.find( tagVarName_ );
curEvtCharge_ = static_cast<Int_t>( iter->second );
evtCharges_.push_back( curEvtCharge_ );
}
}
}
void LauCPFitModel::appendBinCentres( LauFitDataTree* inputData )
{
// We'll be caching the DP amplitudes and efficiencies of the centres of the true bins.
// To do so, we attach some fake points at the end of inputData, the number of the entry
// minus the total number of events corresponding to the number of the histogram for that
// given true bin in the LauScfMap object. (What this means is that when Laura is provided with
// the LauScfMap object by the user, it's the latter who has to make sure that it contains the
// right number of histograms and in exactly the right order!)
// Get the x and y co-ordinates of the bin centres
std::vector<Double_t> binCentresXCoords;
std::vector<Double_t> binCentresYCoords;
scfMap_->listBinCentres(binCentresXCoords, binCentresYCoords);
// The SCF histograms could be in square Dalitz plot histograms.
// The dynamics takes normal Dalitz plot coords, so we might have to convert them back.
Bool_t sqDP = negKinematics_->squareDP();
UInt_t nBins = binCentresXCoords.size();
fakeSCFFracs_.clear();
fakeSCFFracs_.reserve( nBins );
if ( sqDP ) {
fakeJacobians_.clear();
fakeJacobians_.reserve( nBins );
}
for (UInt_t iBin = 0; iBin < nBins; ++iBin) {
if ( sqDP ) {
negKinematics_->updateSqDPKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
binCentresXCoords[iBin] = negKinematics_->getm13Sq();
binCentresYCoords[iBin] = negKinematics_->getm23Sq();
fakeJacobians_.push_back( negKinematics_->calcSqDPJacobian() );
} else {
negKinematics_->updateKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
}
fakeSCFFracs_.push_back( scfFracHist_->calcEfficiency( negKinematics_ ) );
}
// Set up inputFitVars_ object to hold the fake events
inputData->appendFakePoints(binCentresXCoords,binCentresYCoords);
}
Double_t LauCPFitModel::getTotEvtLikelihood(UInt_t iEvt)
{
// Find out whether we have B- or B+
if ( tagged_ ) {
curEvtCharge_ = evtCharges_[iEvt];
// check that the charge is either +1 or -1
if (TMath::Abs(curEvtCharge_)!=1) {
std::cerr << "ERROR in LauCPFitModel::getTotEvtLikelihood : Charge/tag not accepted value: " << curEvtCharge_ << std::endl;
if (curEvtCharge_>0) {
curEvtCharge_ = +1;
} else {
curEvtCharge_ = -1;
}
std::cerr << " : Making it: " << curEvtCharge_ << "." << std::endl;
}
}
// Get the DP likelihood for signal and backgrounds
this->getEvtDPLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal and backgrounds
this->getEvtExtraLikelihoods(iEvt);
// If appropriate, combine the TM and SCF likelihoods
Double_t sigLike = sigDPLike_ * sigExtraLike_;
if ( useSCF_ ) {
Double_t scfFrac(0.0);
if (useSCFHist_) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
sigLike *= (1.0 - scfFrac);
if ( (scfMap_ != 0) && (this->useDP() == kTRUE) ) {
// if we're smearing the SCF DP PDF then the SCF frac
// is already included in the SCF DP likelihood
sigLike += (scfDPLike_ * scfExtraLike_);
} else {
sigLike += (scfFrac * scfDPLike_ * scfExtraLike_);
}
}
// Get the correct event fractions depending on the charge
// Signal asymmetry is built into the DP model... but when the DP
// isn't in the fit we need an explicit parameter
Double_t signalEvents = signalEvents_->unblindValue() * 0.5;
if (this->useDP() == kFALSE) {
signalEvents *= (1.0 - curEvtCharge_ * signalAsym_->unblindValue());
}
// Construct the total event likelihood
Double_t likelihood(0.0);
if (usingBkgnd_) {
likelihood = sigLike*signalEvents;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
Double_t bkgndEvents = bkgndEvents_[bkgndID]->unblindValue() * 0.5 * (1.0 - curEvtCharge_ * bkgndAsym_[bkgndID]->unblindValue());
likelihood += bkgndEvents*bkgndDPLike_[bkgndID]*bkgndExtraLike_[bkgndID];
}
} else {
likelihood = sigLike*0.5;
}
return likelihood;
}
Double_t LauCPFitModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
if (usingBkgnd_) {
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
eventSum += (*iter)->unblindValue();
}
}
return eventSum;
}
void LauCPFitModel::getEvtDPLikelihood(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
if ( ! this->useDP() ) {
// There's always going to be a term in the likelihood for the
// signal, so we'd better not zero it.
sigDPLike_ = 1.0;
scfDPLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = 1.0;
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
return;
}
const UInt_t nBkgnds = this->nBkgndClasses();
if ( tagged_ ) {
if (curEvtCharge_==+1) {
posSigModel_->calcLikelihoodInfo(iEvt);
sigDPLike_ = posSigModel_->getEvtIntensity();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = posBkgndDPModels_[bkgndID]->getLikelihood(iEvt);
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
} else {
negSigModel_->calcLikelihoodInfo(iEvt);
sigDPLike_ = negSigModel_->getEvtIntensity();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = negBkgndDPModels_[bkgndID]->getLikelihood(iEvt);
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
}
} else {
posSigModel_->calcLikelihoodInfo(iEvt);
negSigModel_->calcLikelihoodInfo(iEvt);
sigDPLike_ = 0.5 * ( posSigModel_->getEvtIntensity() + negSigModel_->getEvtIntensity() );
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = 0.5 * ( posBkgndDPModels_[bkgndID]->getLikelihood(iEvt) +
negBkgndDPModels_[bkgndID]->getLikelihood(iEvt) );
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
}
if ( useSCF_ == kTRUE ) {
if ( scfMap_ == 0 ) {
// we're not smearing the SCF DP position
// so the likelihood is the same as the TM
scfDPLike_ = sigDPLike_;
} else {
// calculate the smeared SCF DP likelihood
scfDPLike_ = this->getEvtSCFDPLikelihood(iEvt);
}
}
// Calculate the signal normalisation
// NB the 2.0 is there so that the 0.5 factor is applied to
// signal and background in the same place otherwise you get
// normalisation problems when you switch off the DP in the fit
Double_t norm = negSigModel_->getDPNorm() + posSigModel_->getDPNorm();
sigDPLike_ *= 2.0/norm;
scfDPLike_ *= 2.0/norm;
}
Double_t LauCPFitModel::getEvtSCFDPLikelihood(UInt_t iEvt)
{
Double_t scfDPLike(0.0);
Double_t recoJacobian(1.0);
Double_t xCoord(0.0);
Double_t yCoord(0.0);
Bool_t squareDP = negKinematics_->squareDP();
if ( squareDP ) {
xCoord = negSigModel_->getEvtmPrime();
yCoord = negSigModel_->getEvtthPrime();
recoJacobian = recoJacobians_[iEvt];
} else {
xCoord = negSigModel_->getEvtm13Sq();
yCoord = negSigModel_->getEvtm23Sq();
}
// Find the bin that our reco event falls in
Int_t recoBin = scfMap_->binNumber( xCoord, yCoord );
// Find out which true Bins contribute to the given reco bin
const std::vector<Int_t>* trueBins = scfMap_->trueBins(recoBin);
const Int_t nDataEvents = this->eventsPerExpt();
// Loop over the true bins
for (std::vector<Int_t>::const_iterator iter = trueBins->begin(); iter != trueBins->end(); ++iter)
{
Int_t trueBin = (*iter);
// prob of a true event in the given true bin migrating to the reco bin
Double_t pRecoGivenTrue = scfMap_->prob( recoBin, trueBin );
Double_t pTrue(0.0);
// We've cached the DP amplitudes and the efficiency for the
// true bin centres, just after the data points
if ( tagged_ ) {
LauIsobarDynamics* sigModel(0);
if (curEvtCharge_<0) {
sigModel = negSigModel_;
} else {
sigModel = posSigModel_;
}
sigModel->calcLikelihoodInfo( nDataEvents + trueBin );
pTrue = sigModel->getEvtIntensity();
} else {
posSigModel_->calcLikelihoodInfo( nDataEvents + trueBin );
negSigModel_->calcLikelihoodInfo( nDataEvents + trueBin );
pTrue = 0.5 * ( posSigModel_->getEvtIntensity() + negSigModel_->getEvtIntensity() );
}
// Get the cached SCF fraction (and jacobian if we're using the square DP)
Double_t scfFraction = fakeSCFFracs_[ trueBin ];
Double_t jacobian(1.0);
if ( squareDP ) {
jacobian = fakeJacobians_[ trueBin ];
}
scfDPLike += pTrue * jacobian * scfFraction * pRecoGivenTrue;
}
// Divide by the reco jacobian
scfDPLike /= recoJacobian;
return scfDPLike;
}
void LauCPFitModel::getEvtExtraLikelihoods(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
if ( ! tagged_ || curEvtCharge_ < 0 ) {
sigExtraLike_ = this->prodPdfValue( negSignalPdfs_, iEvt );
if (useSCF_) {
scfExtraLike_ = this->prodPdfValue( negScfPdfs_, iEvt );
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( negBkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
} else {
sigExtraLike_ = this->prodPdfValue( posSignalPdfs_, iEvt );
if (useSCF_) {
scfExtraLike_ = this->prodPdfValue( posScfPdfs_, iEvt );
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( posBkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
}
}
void LauCPFitModel::updateCoeffs()
{
negCoeffs_.clear(); posCoeffs_.clear();
negCoeffs_.reserve(nSigComp_); posCoeffs_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; i++) {
negCoeffs_.push_back(coeffPars_[i]->antiparticleCoeff());
posCoeffs_.push_back(coeffPars_[i]->particleCoeff());
}
}
void LauCPFitModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->addSPlotNtupleDoubleBranch("scffraction");
}
}
// Store the total event likelihood for each species.
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFFrac");
} else {
this->addSPlotNtupleDoubleBranch("sigTotalLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "TotalLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
// Store the DP likelihoods
if (this->useDP()) {
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMDPLike");
this->addSPlotNtupleDoubleBranch("sigSCFDPLike");
} else {
this->addSPlotNtupleDoubleBranch("sigDPLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "DPLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
}
// Store the likelihoods for each extra PDF
if (useSCF_) {
this->addSPlotNtupleBranches(&negSignalPdfs_, "sigTM");
this->addSPlotNtupleBranches(&negScfPdfs_, "sigSCF");
} else {
this->addSPlotNtupleBranches(&negSignalPdfs_, "sig");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList* pdfList = &(negBkgndPdfs_[iBkgnd]);
this->addSPlotNtupleBranches(pdfList, bkgndClass);
}
}
}
void LauCPFitModel::addSPlotNtupleBranches(const LauPdfPList* extraPdfs, const TString& prefix)
{
if (extraPdfs) {
// Loop through each of the PDFs
for (LauPdfPList::const_iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
TString name(prefix);
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr << "WARNING in LauCPFitModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
}
Double_t LauCPFitModel::setSPlotNtupleBranchValues(LauPdfPList* extraPdfs, const TString& prefix, UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// calculate the likelihood for this event
(*pdf_iter)->calcLikelihoodInfo(iEvt);
extraLike = (*pdf_iter)->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
Double_t indivLike = (*pdf_iter)->getLikelihood( (*var_iter) );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
TString name(prefix);
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr << "WARNING in LauCPFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
return totalLike;
}
LauSPlot::NameSet LauCPFitModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
// Loop through all the signal PDFs
for (LauPdfPList::const_iterator pdf_iter = negSignalPdfs_.begin(); pdf_iter != negSignalPdfs_.end(); ++pdf_iter) {
// Loop over the variables involved in each PDF
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
// If they are not DP coordinates then add them
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
nameSet.insert( (*var_iter) );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauCPFitModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (!par->fixed()) {
numbMap[bkgndClass] = par->genValue();
if ( ! par->isLValue() ) {
std::cerr << "WARNING in LauCPFitModel::freeSpeciesNames : \"" << par->name() << "\" is a LauFormulaPar, which implies it is perhaps not entirely free to float in the fit, so the sWeight calculation may not be reliable" << std::endl;
}
}
}
}
return numbMap;
}
LauSPlot::NumbMap LauCPFitModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (par->fixed()) {
numbMap[bkgndClass] = par->genValue();
}
}
}
return numbMap;
}
LauSPlot::TwoDMap LauCPFitModel::twodimPDFs() const
{
// This makes the assumption that the form of the positive and
// negative PDFs are the same, which seems reasonable to me
LauSPlot::TwoDMap twodimMap;
for (LauPdfPList::const_iterator pdf_iter = negSignalPdfs_.begin(); pdf_iter != negSignalPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
if (useSCF_) {
twodimMap.insert( std::make_pair( "sigTM", std::make_pair( varNames[0], varNames[1] ) ) );
} else {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if ( useSCF_ ) {
for (LauPdfPList::const_iterator pdf_iter = negScfPdfs_.begin(); pdf_iter != negScfPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sigSCF", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList& pdfList = negBkgndPdfs_[iBkgnd];
for (LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( bkgndClass, std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
}
return twodimMap;
}
void LauCPFitModel::storePerEvtLlhds()
{
std::cout << "INFO in LauCPFitModel::storePerEvtLlhds : Storing per-event likelihood values..." << std::endl;
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
LauFitDataTree* inputFitData = this->fitData();
if (!this->useDP() && this->storeDPEff()) {
negSigModel_->initialise(negCoeffs_);
posSigModel_->initialise(posCoeffs_);
negSigModel_->fillDataTree(*inputFitData);
posSigModel_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
LauIsobarDynamics* sigModel(0);
LauPdfPList* sigPdfs(0);
LauPdfPList* scfPdfs(0);
LauBkgndPdfsList* bkgndPdfs(0);
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
this->setSPlotNtupleIntegerBranchValue("iExpt",this->iExpt());
this->setSPlotNtupleIntegerBranchValue("iEvtWithinExpt",iEvt);
// Find out whether we have B- or B+
if ( tagged_ ) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator iter = dataValues.find("charge");
curEvtCharge_ = static_cast<Int_t>(iter->second);
if (curEvtCharge_==+1) {
sigModel = posSigModel_;
sigPdfs = &posSignalPdfs_;
scfPdfs = &posScfPdfs_;
bkgndPdfs = &posBkgndPdfs_;
} else {
sigModel = negSigModel_;
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
bkgndPdfs = &negBkgndPdfs_;
}
} else {
sigPdfs = &negSignalPdfs_;
scfPdfs = &negScfPdfs_;
bkgndPdfs = &negBkgndPdfs_;
}
// the DP information
this->getEvtDPLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
posSigModel_->calcLikelihoodInfo(iEvt);
negSigModel_->calcLikelihoodInfo(iEvt);
}
if ( tagged_ ) {
this->setSPlotNtupleDoubleBranchValue("efficiency",sigModel->getEvtEff());
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->setSPlotNtupleDoubleBranchValue("scffraction",sigModel->getEvtScfFraction());
}
} else {
this->setSPlotNtupleDoubleBranchValue("efficiency",0.5*(posSigModel_->getEvtEff() + negSigModel_->getEvtEff()) );
if ( negSigModel_->usingScfModel() && posSigModel_->usingScfModel() ) {
this->setSPlotNtupleDoubleBranchValue("scffraction",0.5*(posSigModel_->getEvtScfFraction() + negSigModel_->getEvtScfFraction()));
}
}
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
if (useSCF_) {
scfTotalLike_ = scfDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigTMDPLike",sigDPLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFDPLike",scfDPLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "DPLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndDPLike_[iBkgnd]);
}
}
} else {
sigTotalLike_ = 1.0;
if (useSCF_) {
scfTotalLike_ = 1.0;
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
bkgndTotalLike_[iBkgnd] = 1.0;
}
}
}
// the signal PDF values
if ( useSCF_ ) {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sigTM", iEvt);
scfTotalLike_ *= this->setSPlotNtupleBranchValues(scfPdfs, "sigSCF", iEvt);
} else {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sig", iEvt);
}
// the background PDF values
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
LauPdfPList& pdfs = (*bkgndPdfs)[iBkgnd];
bkgndTotalLike_[iBkgnd] *= this->setSPlotNtupleBranchValues(&(pdfs), bkgndClass, iEvt);
}
}
// the total likelihoods
if (useSCF_) {
Double_t scfFrac(0.0);
if ( useSCFHist_ ) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
this->setSPlotNtupleDoubleBranchValue("sigSCFFrac",scfFrac);
sigTotalLike_ *= ( 1.0 - scfFrac );
if ( scfMap_ == 0 ) {
scfTotalLike_ *= scfFrac;
}
this->setSPlotNtupleDoubleBranchValue("sigTMTotalLike",sigTotalLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFTotalLike",scfTotalLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "TotalLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndTotalLike_[iBkgnd]);
}
}
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout << "INFO in LauCPFitModel::storePerEvtLlhds : Finished storing per-event likelihood values." << std::endl;
}
void LauCPFitModel::embedNegSignal(const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment,
Bool_t useReweighting)
{
if (negSignalTree_) {
std::cerr << "ERROR in LauCPFitModel::embedNegSignal : Already embedding signal from a file." << std::endl;
return;
}
negSignalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = negSignalTree_->findBranches();
if (!dataOK) {
delete negSignalTree_; negSignalTree_ = 0;
std::cerr << "ERROR in LauCPFitModel::embedNegSignal : Problem creating data tree for embedding." << std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
useNegReweighting_ = useReweighting;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauCPFitModel::embedNegBkgnd(const TString& bkgndClass, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauCPFitModel::embedBkgnd : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
if (negBkgndTree_[bkgndID]) {
std::cerr << "ERROR in LauCPFitModel::embedNegBkgnd : Already embedding background from a file." << std::endl;
return;
}
negBkgndTree_[bkgndID] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = negBkgndTree_[bkgndID]->findBranches();
if (!dataOK) {
delete negBkgndTree_[bkgndID]; negBkgndTree_[bkgndID] = 0;
std::cerr << "ERROR in LauCPFitModel::embedNegBkgnd : Problem creating data tree for embedding." << std::endl;
return;
}
reuseBkgnd_[bkgndID] = reuseEventsWithinEnsemble;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauCPFitModel::embedPosSignal(const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment,
Bool_t useReweighting)
{
if (posSignalTree_) {
std::cerr << "ERROR in LauCPFitModel::embedPosSignal : Already embedding signal from a file." << std::endl;
return;
}
posSignalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = posSignalTree_->findBranches();
if (!dataOK) {
delete posSignalTree_; posSignalTree_ = 0;
std::cerr << "ERROR in LauCPFitModel::embedPosSignal : Problem creating data tree for embedding." << std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
usePosReweighting_ = useReweighting;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauCPFitModel::embedPosBkgnd(const TString& bkgndClass, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauCPFitModel::embedBkgnd : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
if (posBkgndTree_[bkgndID]) {
std::cerr << "ERROR in LauCPFitModel::embedPosBkgnd : Already embedding background from a file." << std::endl;
return;
}
posBkgndTree_[bkgndID] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = posBkgndTree_[bkgndID]->findBranches();
if (!dataOK) {
delete posBkgndTree_[bkgndID]; posBkgndTree_[bkgndID] = 0;
std::cerr << "ERROR in LauCPFitModel::embedPosBkgnd : Problem creating data tree for embedding." << std::endl;
return;
}
reuseBkgnd_[bkgndID] = reuseEventsWithinEnsemble;
if (this->enableEmbedding() == kFALSE) {this->enableEmbedding(kTRUE);}
}
void LauCPFitModel::weightEvents( const TString& dataFileName, const TString& dataTreeName )
{
// Routine to provide weights for events that are uniformly distributed
// in the DP (or square DP) so as to reproduce the given DP model
const Bool_t squareDP { posKinematics_->squareDP() || negKinematics_->squareDP() };
if ( squareDP ) {
std::cout << "INFO in LauCPFitModel::weightEvents : will store DP model weights and the square DP jacobian\n";
std::cout << " : the DP model weights can be used on their own to weight events that were generated flat in phase space\n";
std::cout << " : or they can be multiplied by the jacobian to weight events that were generated flat in the square DP\n";
std::cout << " : or they can be multiplied by max(1.0, jacobian) to weight events that were generated quasi-flat in the square DP" << std::endl;
} else {
std::cout << "INFO in LauCPFitModel::weightEvents : will store DP model weights suitable for weighting events that were generated flat in phase space" << std::endl;
}
// This reads in the given dataFile and creates an input
// fit data tree that stores them for all events and experiments.
const Bool_t dataOK { this->verifyFitData( dataFileName, dataTreeName ) };
if ( ! dataOK ) {
std::cerr << "ERROR in LauCPFitModel::weightEvents : Problem caching the data." << std::endl;
return;
}
LauFitDataTree* inputFitData { this->fitData() };
if ( ! inputFitData->haveBranch( "m13Sq_MC" ) || ! inputFitData->haveBranch( "m23Sq_MC" ) ) {
std::cerr << "WARNING in LauCPFitModel::weightEvents : Cannot find MC truth DP coordinate branches in supplied data, aborting." << std::endl;
return;
}
if ( ! inputFitData->haveBranch( "charge" ) ) {
std::cerr << "WARNING in LauCPFitModel::weightEvents : Cannot find branch specifying event charge in supplied data, aborting." << std::endl;
return;
}
// Create the ntuple to hold the DP weights
TString weightsFileName{ dataFileName };
const Ssiz_t index { weightsFileName.Last('.') };
weightsFileName.Insert( index, "_DPweights" );
LauGenNtuple weightsTuple { weightsFileName, dataTreeName };
weightsTuple.addIntegerBranch( "iExpt" );
weightsTuple.addIntegerBranch( "iEvtWithinExpt" );
weightsTuple.addDoubleBranch( "dpModelWeight" );
if ( squareDP ) {
weightsTuple.addDoubleBranch( "sqDPJacobian" );
}
const UInt_t nExpmt { this->nExpt() };
const UInt_t firstExpmt { this->firstExpt() };
for (UInt_t iExpmt {firstExpmt}; iExpmt < (firstExpmt+nExpmt); ++iExpmt) {
inputFitData->readExperimentData(iExpmt);
const UInt_t nEvents { inputFitData->nEvents() };
if ( nEvents < 1 ) {
std::cerr << "WARNING in LauCPFitModel::weightEvents : Zero events in experiment " << iExpmt << ", skipping..." << std::endl;
continue;
}
weightsTuple.setIntegerBranchValue( "iExpt", iExpmt );
// Calculate and store the weights for the events in this experiment
for ( UInt_t iEvent{0}; iEvent < nEvents; ++iEvent ) {
weightsTuple.setIntegerBranchValue( "iEvtWithinExpt", iEvent );
const LauFitData& evtData = inputFitData->getData( iEvent );
const auto m13Sq_MC { evtData.at( "m13Sq_MC" ) };
const auto m23Sq_MC { evtData.at( "m23Sq_MC" ) };
const auto charge { static_cast<Int_t>( evtData.at( "charge" ) ) };
Double_t dpModelWeight{0.0};
Double_t jacobian{1.0};
LauKinematics * kinematics{nullptr};
LauIsobarDynamics * dpModel{nullptr};
if (charge > 0) {
kinematics = posKinematics_;
dpModel = posSigModel_;
} else {
kinematics = negKinematics_;
dpModel = negSigModel_;
}
if ( kinematics->withinDPLimits( m13Sq_MC, m23Sq_MC ) ) {
kinematics->updateKinematics( m13Sq_MC, m23Sq_MC );
dpModelWeight = dpModel->getEventWeight();
if ( squareDP ) {
jacobian = kinematics->calcSqDPJacobian();
}
const Double_t norm { 0.5 * ( negSigModel_->getDPNorm() + posSigModel_->getDPNorm() ) };
dpModelWeight /= norm;
}
weightsTuple.setDoubleBranchValue( "dpModelWeight", dpModelWeight );
if ( squareDP ) {
weightsTuple.setDoubleBranchValue( "sqDPJacobian", jacobian );
}
weightsTuple.fillBranches();
}
}
weightsTuple.buildIndex( "iExpt", "iEvtWithinExpt" );
weightsTuple.addFriendTree( dataFileName, dataTreeName );
weightsTuple.writeOutGenResults();
}
void LauCPFitModel::savePDFPlots(const TString& label)
{
savePDFPlotsWave(label, 0);
savePDFPlotsWave(label, 1);
savePDFPlotsWave(label, 2);
std::cout << "LauCPFitModel::plot" << std::endl;
// ((LauIsobarDynamics*)negSigModel_)->plot();
//Double_t minm13 = negSigModel_->getKinematics()->getm13Min();
Double_t minm13 = 0.0;
Double_t maxm13 = negSigModel_->getKinematics()->getm13Max();
//Double_t minm23 = negSigModel_->getKinematics()->getm23Min();
Double_t minm23 = 0.0;
Double_t maxm23 = negSigModel_->getKinematics()->getm23Max();
Double_t mins13 = minm13*minm13;
Double_t maxs13 = maxm13*maxm13;
Double_t mins23 = minm23*minm23;
Double_t maxs23 = maxm23*maxm23;
Double_t s13, s23, posChPdf, negChPdf;
TString xLabel = "s13";
TString yLabel = "s23";
if (negSigModel_->getDaughters()->gotSymmetricalDP()) { xLabel = "sHigh"; yLabel = "sLow";}
Int_t n13=200.00, n23=200.00;
Double_t delta13, delta23;
delta13 = (maxs13 - mins13)/n13;
delta23 = (maxs23 - mins23)/n23;
UInt_t nAmp = negSigModel_->getnCohAmp();
for (UInt_t resID = 0; resID <= nAmp; ++resID)
{
TGraph2D *posDt = new TGraph2D();
TGraph2D *negDt = new TGraph2D();
TGraph2D *acpDt = new TGraph2D();
TString resName = "TotalAmp";
if (resID != nAmp){
TString tStrResID = Form("%d", resID);
const LauIsobarDynamics* model = negSigModel_;
const LauAbsResonance* resonance = model->getResonance(resID);
resName = resonance->getResonanceName();
std::cout << "resName = " << resName << std::endl;
}
resName.ReplaceAll("(", "");
resName.ReplaceAll(")", "");
resName.ReplaceAll("*", "Star");
posDt->SetName(resName+label);
posDt->SetTitle(resName+" ("+label+") Positive");
negDt->SetName(resName+label);
negDt->SetTitle(resName+" ("+label+") Negative");
acpDt->SetName(resName+label);
acpDt->SetTitle(resName+" ("+label+") Asymmetry");
Int_t count=0;
for (Int_t i=0; i<n13; i++) {
s13 = mins13 + i*delta13;
for (Int_t j=0; j<n23; j++) {
s23 = mins23 + j*delta23;
if (negSigModel_->getKinematics()->withinDPLimits2(s23, s13))
{
if (negSigModel_->getDaughters()->gotSymmetricalDP() && (s13>s23) ) continue;
negSigModel_->calcLikelihoodInfo(s13, s23);
posSigModel_->calcLikelihoodInfo(s13, s23);
LauComplex negChAmp = negSigModel_->getEvtDPAmp();
LauComplex posChAmp = posSigModel_->getEvtDPAmp();
if (resID != nAmp){
negChAmp = negSigModel_->getFullAmplitude(resID);
posChAmp = posSigModel_->getFullAmplitude(resID);
}
negChPdf = negChAmp.abs2();
posChPdf = posChAmp.abs2();
negDt->SetPoint(count,s23,s13,negChPdf); // s23=sHigh, s13 = sLow
posDt->SetPoint(count,s23,s13,posChPdf); // s23=sHigh, s13 = sLow
acpDt->SetPoint(count,s23,s13, negChPdf - posChPdf); // s23=sHigh, s13 = sLow
count++;
}
}
}
gStyle->SetPalette(1);
TCanvas *posC = new TCanvas("c"+resName+label + "Positive",resName+" ("+label+") Positive",0,0,600,400);
posDt->GetXaxis()->SetTitle(xLabel);
posDt->GetYaxis()->SetTitle(yLabel);
posDt->Draw("SURF1");
posDt->GetXaxis()->SetTitle(xLabel);
posDt->GetYaxis()->SetTitle(yLabel);
posC->SaveAs("plot_2D_"+resName + "_"+label+"Positive.C");
TCanvas *negC = new TCanvas("c"+resName+label + "Negative",resName+" ("+label+") Negative",0,0,600,400);
negDt->GetXaxis()->SetTitle(xLabel);
negDt->GetYaxis()->SetTitle(yLabel);
negDt->Draw("SURF1");
negDt->GetXaxis()->SetTitle(xLabel);
negDt->GetYaxis()->SetTitle(yLabel);
negC->SaveAs("plot_2D_"+resName + "_"+label+"Negative.C");
TCanvas *acpC = new TCanvas("c"+resName+label + "Asymmetry",resName+" ("+label+") Asymmetry",0,0,600,400);
acpDt->GetXaxis()->SetTitle(xLabel);
acpDt->GetYaxis()->SetTitle(yLabel);
acpDt->Draw("SURF1");
acpDt->GetXaxis()->SetTitle(xLabel);
acpDt->GetYaxis()->SetTitle(yLabel);
acpC->SaveAs("plot_2D_"+resName + "_"+label+"Asymmetry.C");
}
}
void LauCPFitModel::savePDFPlotsWave(const TString& label, const Int_t& spin)
{
std::cout << "label = "<< label << ", spin = "<< spin << std::endl;
TString tStrResID = "S_Wave";
if (spin == 1) tStrResID = "P_Wave";
if (spin == 2) tStrResID = "D_Wave";
TString xLabel = "s13";
TString yLabel = "s23";
std::cout << "LauCPFitModel::savePDFPlotsWave: "<< tStrResID << std::endl;
Double_t minm13 = 0.0;
Double_t maxm13 = negSigModel_->getKinematics()->getm13Max();
Double_t minm23 = 0.0;
Double_t maxm23 = negSigModel_->getKinematics()->getm23Max();
Double_t mins13 = minm13*minm13;
Double_t maxs13 = maxm13*maxm13;
Double_t mins23 = minm23*minm23;
Double_t maxs23 = maxm23*maxm23;
Double_t s13, s23, posChPdf, negChPdf;
TGraph2D *posDt = new TGraph2D();
TGraph2D *negDt = new TGraph2D();
TGraph2D *acpDt = new TGraph2D();
posDt->SetName(tStrResID+label);
posDt->SetTitle(tStrResID+" ("+label+") Positive");
negDt->SetName(tStrResID+label);
negDt->SetTitle(tStrResID+" ("+label+") Negative");
acpDt->SetName(tStrResID+label);
acpDt->SetTitle(tStrResID+" ("+label+") Asymmetry");
Int_t n13=200.00, n23=200.00;
Double_t delta13, delta23;
delta13 = (maxs13 - mins13)/n13;
delta23 = (maxs23 - mins23)/n23;
UInt_t nAmp = negSigModel_->getnCohAmp();
Int_t count=0;
for (Int_t i=0; i<n13; i++)
{
s13 = mins13 + i*delta13;
for (Int_t j=0; j<n23; j++)
{
s23 = mins23 + j*delta23;
if (negSigModel_->getKinematics()->withinDPLimits2(s23, s13))
{
if (negSigModel_->getDaughters()->gotSymmetricalDP() && (s13>s23) ) continue;
LauComplex negChAmp(0,0);
LauComplex posChAmp(0,0);
Bool_t noWaveRes = kTRUE;
negSigModel_->calcLikelihoodInfo(s13, s23);
for (UInt_t resID = 0; resID < nAmp; ++resID)
{
const LauIsobarDynamics* model = negSigModel_;
const LauAbsResonance* resonance = model->getResonance(resID);
Int_t spin_res = resonance->getSpin();
if (spin != spin_res) continue;
noWaveRes = kFALSE;
negChAmp += negSigModel_->getFullAmplitude(resID);
posChAmp += posSigModel_->getFullAmplitude(resID);
}
if (noWaveRes) return;
negChPdf = negChAmp.abs2();
posChPdf = posChAmp.abs2();
negDt->SetPoint(count,s23,s13,negChPdf); // s23=sHigh, s13 = sLow
posDt->SetPoint(count,s23,s13,posChPdf); // s23=sHigh, s13 = sLow
acpDt->SetPoint(count,s23,s13, negChPdf - posChPdf); // s23=sHigh, s13 = sLow
count++;
}
}
}
gStyle->SetPalette(1);
TCanvas *posC = new TCanvas("c"+tStrResID+label + "Positive",tStrResID+" ("+label+") Positive",0,0,600,400);
posDt->GetXaxis()->SetTitle(xLabel);
posDt->GetYaxis()->SetTitle(yLabel);
posDt->Draw("SURF1");
posDt->GetXaxis()->SetTitle(xLabel);
posDt->GetYaxis()->SetTitle(yLabel);
posC->SaveAs("plot_2D_"+tStrResID + "_"+label+"Positive.C");
TCanvas *negC = new TCanvas("c"+tStrResID+label + "Negative",tStrResID+" ("+label+") Negative",0,0,600,400);
negDt->GetXaxis()->SetTitle(xLabel);
negDt->GetYaxis()->SetTitle(yLabel);
negDt->Draw("SURF1");
negDt->GetXaxis()->SetTitle(xLabel);
negDt->GetYaxis()->SetTitle(yLabel);
negC->SaveAs("plot_2D_"+tStrResID + "_"+label+"Negative.C");
TCanvas *acpC = new TCanvas("c"+tStrResID+label + "Asymmetry",tStrResID+" ("+label+") Asymmetry",0,0,600,400);
acpDt->GetXaxis()->SetTitle(xLabel);
acpDt->GetYaxis()->SetTitle(yLabel);
acpDt->Draw("SURF1");
acpDt->GetXaxis()->SetTitle(xLabel);
acpDt->GetYaxis()->SetTitle(yLabel);
acpC->SaveAs("plot_2D_"+tStrResID + "_"+label+"Asymmetry.C");
}
Double_t LauCPFitModel::getParamFromTree( TTree& tree, const TString& name )
{
TBranch* branch{tree.FindBranch( name )};
if ( branch ) {
TLeaf* leaf{branch->GetLeaf( name )};
if ( leaf ) {
tree.GetEntry(0);
return leaf->GetValue();
} else {
std::cerr << "ERROR in LauCPFitModel::getParamFromTree : Leaf name " + name + " not found in parameter file!" << std::endl;
}
} else {
std::cerr << "ERROR in LauCPFitModel::getParamFromTree : Branch name " + name + " not found in parameter file!" << std::endl;
}
return -1.1;
}
void LauCPFitModel::fixParam( LauParameter* param, const Double_t val, const Bool_t fix)
{
std::cout << "INFO in LauCPFitModel::fixParam : Setting " << param->name() << " to " << val << std::endl;
param->value(val);
param->genValue(val);
param->initValue(val);
if (fix) {
param->fixed(kTRUE);
} else if (!param->fixed()){
// Add parameter name to list to indicate that this should not be randomised by randomiseInitFitPars
// (otherwise only those that are fixed are not randomised).
// This is only done to those that are not already fixed (see randomiseInitFitPars).
allImportedFreeParams_.insert(param);
}
}
void LauCPFitModel::fixParams( std::vector<LauParameter*>& params )
{
const Bool_t fix{fixParams_};
// TODO: Allow some parameters to be fixed and some to remain floating (but initialised)
if ( !fixParamFileName_.IsNull() ) {
// Take param values from a file
TFile * paramFile = TFile::Open(fixParamFileName_, "READ");
if (!paramFile) {
std::cerr << "ERROR in LauCPFitModel::fixParams : File '" + fixParamFileName_ + "' could not be opened for reading!" << std::endl;
return;
}
TTree * paramTree = dynamic_cast<TTree*>(paramFile->Get(fixParamTreeName_));
if (!paramTree) {
std::cerr << "ERROR in LauCPFitModel::fixParams : Tree '" + fixParamTreeName_ + "' not found in parameter file!" << std::endl;
return;
}
if ( !fixParamNames_.empty() ) {
// Fix params from file, according to vector of names
for( auto itr = params.begin(); itr != params.end(); ++itr ) {
auto itrName = fixParamNames_.find( (*itr)->name() );
if ( itrName != fixParamNames_.end() ) {
this->fixParam(*itr, this->getParamFromTree(*paramTree, *itrName), fix);
}
}
} else {
// Fix some (unspecified) parameters from file, prioritising the map (if it exists)
for( auto itr = params.begin(); itr != params.end(); ++itr) {
const TString& name = (*itr)->name();
if ( ! fixParamMap_.empty() ) {
auto nameValItr = fixParamMap_.find(name);
if ( nameValItr != fixParamMap_.end() ) {
this->fixParam(*itr, nameValItr->second, fix);
}
} else {
this->fixParam(*itr, this->getParamFromTree(*paramTree, name), fix);
}
}
} // Vector of names?
} else {
// Fix param names fom map, leave others floating
for( auto itr = params.begin(); itr != params.end(); ++itr ) {
auto nameValItr = this->fixParamMap_.find( (*itr)->name() );
if ( nameValItr != this->fixParamMap_.end() ) {
this->fixParam(*itr, nameValItr->second, fix);
}
}
}
}
diff --git a/src/LauFitNtuple.cc b/src/LauFitNtuple.cc
index 3b8fc6f..9aa5b86 100644
--- a/src/LauFitNtuple.cc
+++ b/src/LauFitNtuple.cc
@@ -1,342 +1,350 @@
/*
Copyright 2004 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauFitNtuple.cc
\brief File containing implementation of LauFitNtuple class.
*/
#include <iostream>
#include "TFile.h"
#include "TMath.h"
#include "TMatrixD.h"
#include "TSystem.h"
#include "TTree.h"
#include "LauAbsFitter.hh"
#include "LauFitNtuple.hh"
#include "LauFitter.hh"
#include "LauParameter.hh"
#include "LauParamFixed.hh"
ClassImp(LauFitNtuple)
LauFitNtuple::LauFitNtuple(const TString& fileName, Bool_t storeAsymErrors) :
rootFileName_(fileName),
rootFile_(0),
fitResults_(0),
definedFitTree_(kFALSE),
storeAsymErrors_(storeAsymErrors),
fitStatus_({-1,0.0,0.0}),
nFitPars_(0),
nFreePars_(0),
nExtraPars_(0),
iExpt_(0)
{
rootFile_ = TFile::Open(rootFileName_, "recreate");
rootFile_->cd();
fitResults_ = new TTree("fitResults", "fitResults");
fitResults_->SetDirectory(rootFile_);
- fitVars_.clear(); extraVars_.clear();
+ fitVars_.clear(); constrainedVars_.clear(); extraVars_.clear();
}
LauFitNtuple::~LauFitNtuple()
{
if (rootFile_ && rootFile_->IsOpen()) {
delete fitResults_; fitResults_ = 0;
}
delete rootFile_; rootFile_ = 0;
}
void LauFitNtuple::storeCorrMatrix(const UInt_t iExpt, const LauAbsFitter::FitStatus& fitStatus, const TMatrixD& covMatrix)
{
// store the minimised NLL value, correlation matrix status and experiment number
iExpt_ = iExpt;
fitStatus_ = fitStatus;
// make the correlation matrix the correct dimensions
if (definedFitTree_ == kFALSE) {
corrMatrix_.clear();
corrMatrix_.resize(nFitPars_);
for (UInt_t i = 0; i < nFitPars_; ++i) {corrMatrix_[i].resize(nFitPars_);}
}
// under certain circumstances, e.g. if the fit has failed in the first
// stage of a two-stage fit, the covariance matrix might not have the
// expected dimensions, or it might even be empty
Bool_t needsPadding = kFALSE;
const UInt_t nElements = covMatrix.GetNoElements();
if ( nElements == 0 ) {
// if it's empty we can just make a diagonal matrix
std::cerr << "WARNING in LauFitNtuple::storeCorrMatrix : received empty covariance matrix - will store diagonal correlation matrix" << std::endl;
for (UInt_t i = 0; i < nFitPars_; ++i) {
for (UInt_t j = 0; j < nFitPars_; ++j) {
if (i == j) {
corrMatrix_[i][j] = 1.0;
} else {
corrMatrix_[i][j] = 0.0;
}
}
}
return;
} else if ( nElements != nFreePars_*nFreePars_ ) {
UInt_t dimension = covMatrix.GetNrows();
UInt_t nSecondStage = 0;
for (UInt_t i = 0; i < nFitPars_; ++i) {
if ( fitVars_[i]->secondStage() ) {
++nSecondStage;
}
}
if ( (dimension + nSecondStage) == nFreePars_ ) {
needsPadding = kTRUE;
std::cerr << "WARNING in LauFitNtuple::storeCorrMatrix : received smaller covariance matrix than expected, likely due to failure of first stage fit - will pad the correlation matrix" << std::endl;
std::cerr << " : nFitPars_ = " << nFitPars_ << std::endl;
std::cerr << " : nFreePars_ = " << nFreePars_ << std::endl;
std::cerr << " : nSecondStage = " << nSecondStage << std::endl;
std::cerr << " : covMatrix size = " << dimension << std::endl;
} else {
std::cerr << "WARNING in LauFitNtuple::storeCorrMatrix : received smaller covariance matrix than expected, for unknown reasons - will store diagonal correlation matrix" << std::endl;
std::cerr << " : nFitPars_ = " << nFitPars_ << std::endl;
std::cerr << " : nFreePars_ = " << nFreePars_ << std::endl;
std::cerr << " : nSecondStage = " << nSecondStage << std::endl;
std::cerr << " : covMatrix size = " << dimension << std::endl;
for (UInt_t i = 0; i < nFitPars_; ++i) {
for (UInt_t j = 0; j < nFitPars_; ++j) {
if (i == j) {
corrMatrix_[i][j] = 1.0;
} else {
corrMatrix_[i][j] = 0.0;
}
}
}
return;
}
}
// calculate the correlation matrix information from the fitter covariance matrix
Bool_t iFixed(kFALSE);
Bool_t jFixed(kFALSE);
Bool_t iSecondStage(kFALSE);
Bool_t jSecondStage(kFALSE);
UInt_t iFree(0);
UInt_t jFree(0);
for (UInt_t i = 0; i < nFitPars_; ++i) {
iFixed = fitVars_[i]->fixed();
iSecondStage = fitVars_[i]->secondStage();
// reset the "j" free parameter counter
jFree = 0;
// NB the supplied covariance matrix is of dimension nFreePars_ x nFreePars_
for (UInt_t j = 0; j < nFitPars_; ++j) {
jFixed = fitVars_[j]->fixed();
jSecondStage = fitVars_[j]->secondStage();
if (i == j) {
corrMatrix_[i][j] = 1.0;
} else if (iFixed == kTRUE || jFixed == kTRUE) {
corrMatrix_[i][j] = 0.0;
} else if ( needsPadding && ( iSecondStage || jSecondStage ) ) {
corrMatrix_[i][j] = 0.0;
} else {
Double_t r_ij = covMatrix(iFree,jFree);
Double_t r_ii = covMatrix(iFree,iFree);
Double_t r_jj = covMatrix(jFree,jFree);
Double_t denom = r_ii * r_jj;
if (denom < 0.0) {
r_ij = 0.0;
denom = 1.0;
}
denom = TMath::Sqrt(denom);
if (denom < 1e-30) {
r_ij = 0.0;
denom = 1.0;
}
corrMatrix_[i][j] = r_ij / denom;
}
if ( !jFixed && !(needsPadding && jSecondStage) ) {
++jFree;
}
}
if ( !iFixed && !(needsPadding && iSecondStage) ) {
++iFree;
}
}
}
-void LauFitNtuple::storeParsAndErrors(const std::vector<LauParameter*>& fitVars, const std::vector<LauParameter>& extraVars)
+void LauFitNtuple::storeParsAndErrors(const std::vector<LauParameter*>& fitVars, const std::set<TString>& constrainedVars, const std::vector<LauParameter>& extraVars)
{
fitVars_ = fitVars;
UInt_t nFitPars = fitVars_.size();
// the number of parameters being given to us should be the same as the number from the last fit
// OR it's the first time so the "last" number is zero
if (nFitPars_ != 0 && nFitPars_ != nFitPars) {
std::cerr << "ERROR in LauFitNtuple::storeParsAndErrors : expected total number of parameters (" << nFitPars_
<< ") not the same as the number provided (" << nFitPars << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
LauParamFixed pred;
UInt_t nFreePars = nFitPars - std::count_if(fitVars_.begin(),fitVars_.end(),pred);
// the number of free parameters being given to us should be the same as the number from the last fit
// OR it's the first time so the "last" number is zero
// (NB we check whether nFitPars_ is zero for this since it is possible to have zero free parameters, albeit rather daft)
if (nFitPars_ != 0 && nFreePars_ != nFreePars) {
std::cerr << "ERROR in LauFitNtuple::storeParsAndErrors : expected number of free parameters (" << nFreePars_
<< ") not the same as the number provided (" << nFreePars << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
nFitPars_ = nFitPars;
nFreePars_ = nFreePars;
+ constrainedVars_ = constrainedVars;
+
extraVars_ = extraVars;
nExtraPars_ = extraVars_.size();
}
void LauFitNtuple::updateFitNtuple()
{
// Now create and fill the stored fit results into an ntuple (TTree)
if (definedFitTree_ == kFALSE) {
std::cout << "INFO in LauFitNtuple::updateFitNtuple : totNoPars = " << nFitPars_ << std::endl;
// Add experiment number as a branch
fitResults_->Branch("iExpt", &iExpt_, "iExpt/I");
fitResults_->Branch("fitStatus", &fitStatus_.status, "fitStatus/I");
// Add NLL (negative log-likelihood) and EDM values from fit
fitResults_->Branch("NLL", &fitStatus_.NLL, "NLL/D");
fitResults_->Branch("EDM", &fitStatus_.EDM, "EDM/D");
for (UInt_t i = 0; i < nFitPars_; i++) {
TString parName = fitVars_[i]->name();
TString parNameD(parName); parNameD += "/D";
fitResults_->Branch(parName.Data(), &fitVars_[i]->value_, parNameD.Data());
TString parInitName(parName); parInitName += "_True";
TString parInitNameD(parInitName); parInitNameD += "/D";
fitResults_->Branch(parInitName.Data(), &fitVars_[i]->genValue_, parInitNameD.Data());
if (!fitVars_[i]->fixed()) {
TString parErrName(parName); parErrName += "_Error";
TString parErrNameD(parErrName); parErrNameD += "/D";
fitResults_->Branch(parErrName.Data(), &fitVars_[i]->error_, parErrNameD.Data());
if ( storeAsymErrors_ ) {
TString parNegErrName(parName); parNegErrName += "_NegError";
TString parNegErrNameD(parNegErrName); parNegErrNameD += "/D";
fitResults_->Branch(parNegErrName.Data(), &fitVars_[i]->negError_, parNegErrNameD.Data());
TString parPosErrName(parName); parPosErrName += "_PosError";
TString parPosErrNameD(parPosErrName); parPosErrNameD += "/D";
fitResults_->Branch(parPosErrName.Data(), &fitVars_[i]->posError_, parPosErrNameD.Data());
}
TString parPullName(parName); parPullName += "_Pull";
TString parPullNameD(parPullName); parPullNameD += "/D";
fitResults_->Branch(parPullName.Data(), &fitVars_[i]->pull_, parPullNameD.Data());
+
+ if ( fitVars_[i]->gaussConstraint() || constrainedVars_.find( parName ) != constrainedVars_.end() ) {
+ TString parConstMeanName(parName); parConstMeanName += "_ConstraintMean";
+ TString parConstMeanNameD(parConstMeanName); parConstMeanNameD += "/D";
+ fitResults_->Branch(parConstMeanName.Data(), &fitVars_[i]->constraintMean_, parConstMeanNameD.Data());
+ }
}
// Now add in the correlation matrix values (only for floating parameters)
if (!fitVars_[i]->fixed()) {
// First the global correlation coeffs
TString parGCCName(parName); parGCCName += "_GCC";
TString parGCCNameD(parGCCName); parGCCNameD += "/D";
fitResults_->Branch(parGCCName.Data(), &fitVars_[i]->gcc_, parGCCNameD.Data());
if ( ! corrMatrix_.empty() ) {
// Then the rest
for (UInt_t j = 0; j < nFitPars_; j++) {
if (!fitVars_[j]->fixed() && i!=j) {
TString parName2 = fitVars_[j]->name();
TString corrName("corr__");
corrName += parName; corrName += "__"; corrName += parName2;
TString corrNameD(corrName); corrNameD += "/D";
fitResults_->Branch(corrName.Data(), &corrMatrix_[i][j], corrNameD.Data());
}
}
}
}
}
// Update extra parameter values...
for (UInt_t i = 0; i < nExtraPars_; i++) {
TString parName = extraVars_[i].name();
TString parNameD(parName); parNameD += "/D";
fitResults_->Branch(parName.Data(), &extraVars_[i].value_, parNameD.Data());
TString parInitName(parName); parInitName += "_True";
TString parInitNameD(parInitName); parInitNameD += "/D";
fitResults_->Branch(parInitName.Data(), &extraVars_[i].genValue_, parInitNameD.Data());
//TString parErrName(parName); parErrName += "_Error";
//TString parErrNameD(parErrName); parErrNameD += "/D";
//fitResults_->Branch(parErrName.Data(), &extraVars_[i].error_, parErrNameD.Data());
// Also find the fit fraction pull and store it
//TString pullName(parName); pullName += "_Pull";
//TString pullNameD(pullName); pullNameD += "/D";
//fitResults_->Branch(pullName.Data(), &extraVars_[i].pull_, pullNameD.Data());
}
definedFitTree_ = kTRUE;
}
std::cout << "INFO in LauFitNtuple::updateFitNtuple : Stored fit values for experiment " << iExpt_ << "\n";
std::cout << " : fitStatus = " << fitStatus_.status << std::endl;
std::cout << " : NLL = " << fitStatus_.NLL << std::endl;
std::cout << " : EDM = " << fitStatus_.EDM << std::endl;
fitResults_->Fill();
}
void LauFitNtuple::writeOutFitResults()
{
// Write out the fit ntuple to the appropriate root file
rootFile_->cd();
fitResults_->Write("",TObject::kOverwrite);
rootFile_->Close();
delete rootFile_; rootFile_ = 0;
}
diff --git a/src/LauFitObject.cc b/src/LauFitObject.cc
index a165f20..8b2d9a3 100644
--- a/src/LauFitObject.cc
+++ b/src/LauFitObject.cc
@@ -1,233 +1,238 @@
/*
Copyright 2017 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauFitObject.cc
\brief File containing implementation of LauFitObject class.
*/
#include <iostream>
#include "TDecompChol.h"
#include "TMatrixD.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TVectorD.h"
#include "LauFitObject.hh"
#include "LauParameter.hh"
#include "LauRandom.hh"
ClassImp(LauFitObject)
void LauFitObject::setNExpts(UInt_t nExperiments, UInt_t firstExperiment, Bool_t toyExpts)
{
nExpt_ = nExperiments;
firstExpt_ = firstExperiment;
toyExpts_ = toyExpts;
if ( ! toyExpts && ( nExperiments > 1 || firstExperiment > 0 ) ) {
std::cerr << "WARNING in LauFitObject::setNExpts : toyExpts is set to kFALSE but the values of nExperiments and firstExperiment indicate otherwise, please check" << std::endl;
} else if ( toyExpts && nExperiments == 1 && firstExperiment == 0 ) {
std::cerr << "WARNING in LauFitObject::setNExpts : toyExpts is set to kTRUE but the values of nExperiments and firstExperiment perhaps indicate otherwise, please check" << std::endl;
}
}
void LauFitObject::resetFitCounters()
{
numberOKFits_ = 0;
numberBadFits_ = 0;
fitStatus_ = { -1, 0.0, 0.0 };
}
void LauFitObject::startNewFit( const UInt_t nPars, const UInt_t nFreePars )
{
// Reset the worst likelihood found to its catch-all value
worstLogLike_ = std::numeric_limits<Double_t>::max();
// Store the number of fit parameters (total and floating)
nParams_ = nPars;
nFreeParams_ = nFreePars;
}
void LauFitObject::storeFitStatus( const LauAbsFitter::FitStatus& status, const TMatrixD& covMatrix )
{
fitStatus_ = status;
covMatrix_.Clear();
covMatrix_.ResizeTo( covMatrix.GetNrows(), covMatrix.GetNcols() );
covMatrix_.SetMatrixArray( covMatrix.GetMatrixArray() );
// Keep track of how many fits worked or failed
// NB values of fitStatus_ indicate the status of the error matrix:
// 0= not calculated at all
// 1= approximation only, not accurate
// 2= full matrix, but forced positive-definite
// 3= full accurate covariance matrix
if (fitStatus_.status == 3) {
++numberOKFits_;
} else {
++numberBadFits_;
}
}
void LauFitObject::addConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width)
{
std::cerr << "WARNING in LauFitObject::addConstraint : This function is deprecated, please switch to addFormulaConstraint!" << std::endl;
this->addFormulaConstraint( formula, pars, mean, width );
}
void LauFitObject::addFormulaConstraint(const TString& formula, const std::vector<TString>& pars, const Double_t mean, const Double_t width)
{
- if ( ! this->checkRepetition(pars) ){
+ if ( ! this->checkRepetition(pars, ConstraintType::Formula) ){
std::cerr << "WARNING in LauFitObject::addFormulaConstraint : Parameter(s) added to multiple constraints!" << std::endl;
}
formulaConstraints_.emplace_back( FormulaConstraint{formula, pars, mean, width, nullptr} );
std::cout << "INFO in LauFitObject::addFormulaConstraint : Added formula constraint" << std::endl;
}
void LauFitObject::addMultiDimConstraint( const std::vector<TString>& pars, const TVectorD& means, const TMatrixD& covMat)
{
- if ( ! this->checkRepetition(pars) ){
+ if ( ! this->checkRepetition(pars, ConstraintType::MultDim) ){
std::cerr << "WARNING in LauFitObject::addMultiDimConstraint : Parameter(s) added to multiple constraints!" << std::endl;
}
multiDimConstraints_.emplace_back( pars, means, covMat );
std::cout << "INFO in LauFitObject::addMultiDimConstraint : Added multi-dimensional constraint" << std::endl;
}
void LauFitObject::generateConstraintMeans( std::vector<LauAbsRValue*>& conVars )
{
if ( ! this->toyExpts() ) {
return;
}
// For reproducibility, set a seed based on the experiment number
// First, store the current seed, so that it can be restored afterwards
const UInt_t oldSeed { LauRandom::randomFun()->GetSeed() };
LauRandom::randomFun()->SetSeed( 827375 + this->iExpt() );
for ( LauAbsRValue* par : conVars ) {
par->generateConstraintMean();
}
for ( auto& constraint : multiDimConstraints_ ) {
constraint.generateConstraintMeans();
}
// Restore the old random seed
LauRandom::randomFun()->SetSeed( oldSeed );
}
-Bool_t LauFitObject::checkRepetition( const std::vector<TString>& names )
+Bool_t LauFitObject::checkRepetition( const std::vector<TString>& names, const ConstraintType conType )
{
- Bool_t allOK(kTRUE);
+ Bool_t allOK{kTRUE};
+
+ for ( auto& newname : names ){
+ // Check in formula constraints
+ if ( formulaConstrainedPars_.find(newname) != formulaConstrainedPars_.end() ){
+ std::cerr << "WARNING in LauFitObject::checkRepetition: named parameter " << newname << " already used in a formula constraint" << std::endl;
+ allOK = kFALSE;
+ }
+
+ // Check in ND constraints
+ if ( multiDimConstrainedPars_.find(newname) != multiDimConstrainedPars_.end() ){
+ std::cerr << "WARNING in LauFitObject::checkRepetition: named parameter " << newname << " already used in a multi-dimensional constraint" << std::endl;
+ allOK = kFALSE;
+ }
+
+ // Add the new names to the appropriate set
+ switch ( conType ) {
+ case ConstraintType::Formula :
+ formulaConstrainedPars_.insert(newname);
+ break;
+ case ConstraintType::MultDim :
+ multiDimConstrainedPars_.insert(newname);
+ break;
+ }
+ }
- if ( formulaConstraints_.size()==0 && multiDimConstraints_.size()==0 ) {
- return allOK;
- }
-
- //Check in formula constraints
- for ( auto& constraint : formulaConstraints_ ){
- for ( auto& parname : constraint.conPars_ ){
- for ( auto& newname : names ){
- if ( parname == newname ){
- std::cerr << "WARNING in LauFitObject::checkRepetition: named parameter " << newname << " already used in a constraint" << std::endl;
- allOK = kFALSE;
- }
- }
- }
- }
- //Check in ND constraints
- for ( auto& constraint : multiDimConstraints_ ){
- for ( auto& parname : constraint.conPars_ ){
- for ( auto& newname : names ){
- if ( parname == newname ){
- std::cerr << "WARNING in LauFitObject::checkRepetition: named parameter " << newname << " already used in a constraint" << std::endl;
- allOK = kFALSE;
- }
- }
- }
- }
return allOK;
}
LauFitObject::MultiDimConstraint::MultiDimConstraint( const std::vector<TString>& parNames, const TVectorD& means, const TMatrixD& covMat ) :
conPars_{parNames},
trueMeans_{means},
means_{means},
invCovMat_{covMat.GetNrows(),covMat.GetNcols()},
sqrtCovMat_{covMat.GetNrows(),covMat.GetNcols()}
{
if ( covMat.GetNcols() != covMat.GetNrows() ){
std::cerr << "ERROR in LauFitObject::MultiDimConstraint : Covariance matrix is not square!" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
if ( ( parNames.size() != static_cast<std::size_t>(means.GetNrows()) ) || ( parNames.size() != static_cast<std::size_t>(covMat.GetNrows()) ) ){
std::cerr << "ERROR in LauFitObject::MultiDimConstraint : Different number of elements in vectors/covariance matrix!" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
// Check invertion of the covariance matrix was successful
TMatrixD invCovMat {TMatrixD::kInverted, covMat};
if ( invCovMat == covMat ){
std::cerr << "ERROR in LauFitObject::MultiDimConstraint : covariance matrix inversion failed, check your input!" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
invCovMat_ = invCovMat;
// Check invertion of the covariance matrix was successful
TDecompChol cholDecomp {covMat};
if ( ! cholDecomp.Decompose() ) {
std::cerr << "ERROR in LauFitObject::MultiDimConstraint : covariance matrix decomposition failed, check your input!" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
sqrtCovMat_ = TMatrixD{TMatrixD::kTransposed, cholDecomp.GetU()};
}
Double_t LauFitObject::MultiDimConstraint::constraintPenalty() const
{
TVectorD diff{ means_.GetNrows() };
for ( ULong_t j {0}; j < conLauPars_.size(); ++j ) {
LauParameter* param = conLauPars_[j];
diff[j] = param->unblindValue();
}
diff -= means_;
return 0.5 * invCovMat_.Similarity( diff );
}
void LauFitObject::MultiDimConstraint::generateConstraintMeans()
{
TRandom* random = LauRandom::randomFun();
for ( Int_t j {0}; j < trueMeans_.GetNrows(); ++j ) {
means_[j] = random->Gaus(0.0, 1.0);
}
means_ *= sqrtCovMat_;
means_ += trueMeans_;
+
+ // Store the new mean in the parameter itself to enable writing it out to the results ntuple
+ // (this is safe because a parameter cannot have both a 1D constraint and an ND constraint on it at the same time)
+ for ( Int_t j {0}; j < trueMeans_.GetNrows(); ++j ) {
+ conLauPars_[j]->constraintMean_ = means_[j];
+ std::cout << "INFO in LauFitObject::MultiDimConstraint::generateConstraintMeans : set constraint mean for parameter \"" << conLauPars_[j]->name() << "\" to " << conLauPars_[j]->constraintMean_ << std::endl;
+ }
}
diff --git a/src/LauRooFitTask.cc b/src/LauRooFitTask.cc
index 150af2b..2e5dd74 100644
--- a/src/LauRooFitTask.cc
+++ b/src/LauRooFitTask.cc
@@ -1,394 +1,393 @@
/*
Copyright 2017 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauRooFitTask.cc
\brief File containing implementation of LauRooFitTask class.
*/
#include <iostream>
#include <vector>
#include "RooFormulaVar.h"
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "TFile.h"
#include "TString.h"
#include "TSystem.h"
#include "TTree.h"
#include "LauFitNtuple.hh"
#include "LauParameter.hh"
#include "LauSimFitTask.hh"
#include "LauRooFitTask.hh"
ClassImp(LauRooFitTask)
LauRooFitTask::LauRooFitTask( RooAbsPdf& model, const Bool_t extended, const RooArgSet& vars, const TString& weightVarName ) :
LauSimFitTask(),
model_(model),
dataVars_(vars),
weightVarName_(weightVarName),
dataFile_(0),
dataTree_(0),
exptData_(0),
extended_(extended),
iExptSet_(),
nllVar_(0)
{
}
LauRooFitTask::~LauRooFitTask()
{
delete nllVar_; nllVar_ = 0;
this->cleanData();
}
void LauRooFitTask::cleanData()
{
if ( dataFile_ != 0 ) {
dataFile_->Close();
delete dataFile_;
dataTree_ = 0;
dataFile_ = 0;
}
delete exptData_;
exptData_ = 0;
}
void LauRooFitTask::initialise()
{
if ( weightVarName_ != "" ) {
Bool_t weightVarFound = kFALSE;
RooFIter argset_iter = dataVars_.fwdIterator();
RooAbsArg* param(0);
while ( (param = argset_iter.next()) ) {
TString name = param->GetName();
if ( name == weightVarName_ ) {
weightVarFound = kTRUE;
break;
}
}
if ( ! weightVarFound ) {
std::cerr << "ERROR in LauRooFitTask::initialise : The set of data variables does not contain the weighting variable \"" << weightVarName_ << std::endl;
std::cerr << " : Weighting will be disabled." << std::endl;
weightVarName_ = "";
}
}
}
Bool_t LauRooFitTask::verifyFitData(const TString& dataFileName, const TString& dataTreeName)
{
// Clean-up from any previous runs
if ( dataFile_ != 0 ) {
this->cleanData();
}
// Open the data file
dataFile_ = TFile::Open( dataFileName );
if ( ! dataFile_ ) {
std::cerr << "ERROR in LauRooFitTask::verifyFitData : Problem opening data file \"" << dataFileName << "\"" << std::endl;
return kFALSE;
}
// Retrieve the tree
dataTree_ = dynamic_cast<TTree*>( dataFile_->Get( dataTreeName ) );
if ( ! dataTree_ ) {
std::cerr << "ERROR in LauRooFitTask::verifyFitData : Problem retrieving tree \"" << dataTreeName << "\" from data file \"" << dataFileName << "\"" << std::endl;
dataFile_->Close();
delete dataFile_;
dataFile_ = 0;
return kFALSE;
}
// Check that the tree contains branches for all the fit variables
RooFIter argset_iter = dataVars_.fwdIterator();
RooAbsArg* param(0);
Bool_t allOK(kTRUE);
while ( (param = argset_iter.next()) ) {
TString name = param->GetName();
TBranch* branch = dataTree_->GetBranch( name );
if ( branch == 0 ) {
std::cerr << "ERROR in LauRooFitTask::verifyFitData : The data tree does not contain a branch for fit variable \"" << name << std::endl;
allOK = kFALSE;
}
}
if ( ! allOK ) {
return kFALSE;
}
// Check whether the tree has the branch iExpt
TBranch* branch = dataTree_->GetBranch("iExpt");
if ( branch == 0 ) {
std::cerr << "WARNING in LauRooFitTask::verifyFitData : Cannot find branch \"iExpt\" in the tree, will treat all data as being from a single experiment" << std::endl;
} else {
// Define the valid values for iExpt
iExptSet_.clear();
const UInt_t firstExp = dataTree_->GetMinimum("iExpt");
const UInt_t lastExp = dataTree_->GetMaximum("iExpt");
for ( UInt_t iExp = firstExp; iExp <= lastExp; ++iExp ) {
iExptSet_.insert( iExp );
}
}
return kTRUE;
}
void LauRooFitTask::prepareInitialParArray( TObjArray& array )
{
// Check that the NLL variable has been initialised
if ( ! nllVar_ ) {
std::cerr << "ERROR in LauRooFitTask::prepareInitialParArray : NLL var not initialised" << std::endl;
return;
}
// If we already prepared the entries in the fitPars_ vector then we only need to add the contents to the array
if ( ! fitPars_.empty() ) {
for ( std::vector<LauParameter*>::iterator iter = fitPars_.begin(); iter != fitPars_.end(); ++iter ) {
array.Add(*iter);
}
return;
}
// Store the set of parameters and the total number of parameters
RooArgSet* varSet = nllVar_->getParameters( exptData_ );
UInt_t nFreePars(0);
// Loop through the fit parameters
RooFIter argset_iter = varSet->fwdIterator();
RooAbsArg* param(0);
while ( (param = argset_iter.next()) ) {
// Only consider the free parameters
if ( ! param->isConstant() ) {
// Add the parameter
RooRealVar* rrvar = dynamic_cast<RooRealVar*>( param );
if ( rrvar != 0 ) {
// Count the number of free parameters
++nFreePars;
// Do the conversion and add it to the array
LauParameter* lpar = this->convertToLauParameter( rrvar );
fitVars_.push_back( rrvar );
fitPars_.push_back( lpar );
array.Add( lpar );
} else {
RooFormulaVar* rfvar = dynamic_cast<RooFormulaVar*>( param );
if ( rfvar == 0 ) {
std::cerr << "ERROR in LauRooFitTask::prepareInitialParArray : The parameter is neither a RooRealVar nor a RooFormulaVar, don't know what to do" << std::endl;
continue;
}
std::vector< std::pair<RooRealVar*,LauParameter*> > lpars = this->convertToLauParameters( rfvar );
for ( std::vector< std::pair<RooRealVar*,LauParameter*> >::iterator iter = lpars.begin(); iter != lpars.end(); ++iter ) {
RooRealVar* rrv = iter->first;
LauParameter* lpar = iter->second;
if ( ! rrv->isConstant() ) {
continue;
}
// Count the number of free parameters
++nFreePars;
// Add the parameter to the array
fitVars_.push_back( rrvar );
fitPars_.push_back( lpar );
array.Add( lpar );
}
}
}
}
delete varSet;
this->startNewFit( nFreePars, nFreePars );
}
LauParameter* LauRooFitTask::convertToLauParameter( const RooRealVar* rooParameter ) const
{
return new LauParameter( rooParameter->GetName(), rooParameter->getVal(), rooParameter->getMin(), rooParameter->getMax(), rooParameter->isConstant() );
}
std::vector< std::pair<RooRealVar*,LauParameter*> > LauRooFitTask::convertToLauParameters( const RooFormulaVar* rooFormula ) const
{
// Create the empty vector
std::vector< std::pair<RooRealVar*,LauParameter*> > lauParameters;
Int_t parIndex(0);
RooAbsArg* rabsarg(0);
RooRealVar* rrvar(0);
RooFormulaVar* rfvar(0);
// Loop through all the parameters of the formula
while ( (rabsarg = rooFormula->getParameter(parIndex)) ) {
// First try converting to a RooRealVar
rrvar = dynamic_cast<RooRealVar*>( rabsarg );
if ( rrvar ) {
// Do the conversion and add it to the array
LauParameter* lpar = this->convertToLauParameter( rrvar );
lauParameters.push_back( std::make_pair(rrvar,lpar) );
continue;
}
// If that didn't work, try converting to a RooFormulaVar
rfvar = dynamic_cast<RooFormulaVar*>( rabsarg );
if ( rfvar ) {
// Do the conversion and add these to the array
std::vector< std::pair<RooRealVar*,LauParameter*> > lpars = this->convertToLauParameters( rfvar );
for ( std::vector< std::pair<RooRealVar*,LauParameter*> >::iterator iter = lpars.begin(); iter != lpars.end(); ++iter ) {
lauParameters.push_back( *iter );
}
continue;
}
// If neither of those worked we don't know what to do, so print an error message and continue
std::cerr << "ERROR in LauRooFitTask::convertToLauParameters : One of the parameters is not a RooRealVar nor a RooFormulaVar, it is a: " << rabsarg->ClassName() << std::endl;
std::cerr << " : Do not know how to process that - it will be skipped." << std::endl;
}
return lauParameters;
}
Double_t LauRooFitTask::getTotNegLogLikelihood()
{
Double_t nLL = (nllVar_ != 0) ? nllVar_->getVal() : 0.0;
return nLL;
}
void LauRooFitTask::setParsFromMinuit(Double_t* par, Int_t npar)
{
// This function sets the internal parameters based on the values
// that Minuit is using when trying to minimise the total likelihood function.
// MINOS reports different numbers of free parameters depending on the
// situation, so disable this check
const UInt_t nFreePars = this->nFreeParams();
if ( ! this->withinAsymErrorCalc() ) {
if (static_cast<UInt_t>(npar) != nFreePars) {
std::cerr << "ERROR in LauRooFitTask::setParsFromMinuit : Unexpected number of free parameters: " << npar << ".\n";
std::cerr << " Expected: " << nFreePars << ".\n" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Despite npar being the number of free parameters
// the par array actually contains all the parameters,
// free and floating...
// Update all the floating ones with their new values
for (UInt_t i(0); i<nFreePars; ++i) {
if (!fitPars_[i]->fixed()) {
// Set both the RooRealVars and the LauParameters
fitPars_[i]->value(par[i]);
fitVars_[i]->setVal(par[i]);
}
}
}
UInt_t LauRooFitTask::readExperimentData()
{
// check that we're being asked to read a valid index
const UInt_t exptIndex = this->iExpt();
if ( iExptSet_.empty() && exptIndex != 0 ) {
std::cerr << "ERROR in LauRooFitTask::readExperimentData : Invalid experiment number " << exptIndex << ", data contains only one experiment" << std::endl;
return 0;
} else if ( iExptSet_.find( exptIndex ) == iExptSet_.end() ) {
std::cerr << "ERROR in LauRooFitTask::readExperimentData : Invalid experiment number " << exptIndex << std::endl;
return 0;
}
// cleanup the data from any previous experiment
delete exptData_;
// retrieve the data and find out how many events have been read
if ( iExptSet_.empty() ) {
exptData_ = new RooDataSet( TString::Format("expt%dData",exptIndex), "", dataTree_, dataVars_, "", (weightVarName_ != "") ? weightVarName_.Data() : 0 );
} else {
const TString selectionString = TString::Format("iExpt==%d",exptIndex);
TTree* exptTree = dataTree_->CopyTree(selectionString);
exptData_ = new RooDataSet( TString::Format("expt%dData",exptIndex), "", exptTree, dataVars_, "", (weightVarName_ != "") ? weightVarName_.Data() : 0 );
delete exptTree;
}
const UInt_t nEvent = exptData_->numEntries();
this->eventsPerExpt( nEvent );
return nEvent;
}
void LauRooFitTask::cacheInputFitVars()
{
// cleanup the old NLL info
delete nllVar_;
// construct the new NLL variable for this dataset
nllVar_ = new RooNLLVar("nllVar", "", model_, *exptData_, extended_);
}
void LauRooFitTask::finaliseExperiment( const LauAbsFitter::FitStatus& fitStat, const TObjArray* parsFromCoordinator, const TMatrixD* covMat, TObjArray& parsToCoordinator )
{
// Copy the fit status information
this->storeFitStatus( fitStat, *covMat );
// Now process the parameters
const UInt_t nFreePars = this->nFreeParams();
UInt_t nPars = parsFromCoordinator->GetEntries();
if ( nPars != nFreePars ) {
std::cerr << "ERROR in LauRooFitTask::finaliseExperiment : Unexpected number of parameters received from coordinator" << std::endl;
std::cerr << " : Received " << nPars << " when expecting " << nFreePars << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
LauParameter* parameter = dynamic_cast<LauParameter*>( (*parsFromCoordinator)[iPar] );
if ( ! parameter ) {
std::cerr << "ERROR in LauRooFitTask::finaliseExperiment : Error reading parameter from coordinator" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
if ( parameter->name() != fitPars_[iPar]->name() ) {
std::cerr << "ERROR in LauRooFitTask::finaliseExperiment : Error reading parameter from coordinator" << std::endl;
gSystem->Exit( EXIT_FAILURE );
}
*(fitPars_[iPar]) = *parameter;
RooRealVar* rrv = fitVars_[iPar];
rrv->setVal( parameter->value() );
rrv->setError( parameter->error() );
rrv->setAsymError( parameter->negError(), parameter->posError() );
}
// Update the pulls and add each finalised fit parameter to the list to
// send back to the coordinator
for ( std::vector<LauParameter*>::iterator iter = fitPars_.begin(); iter != fitPars_.end(); ++iter ) {
(*iter)->updatePull();
parsToCoordinator.Add( *iter );
}
// Write the results into the ntuple
- std::vector<LauParameter> extraVars;
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitPars_, extraVars);
+ ntuple->storeParsAndErrors(fitPars_, this->multiDimConstrainedPars(), {});
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
}
diff --git a/src/LauSimFitCoordinator.cc b/src/LauSimFitCoordinator.cc
index eddb02c..59de318 100644
--- a/src/LauSimFitCoordinator.cc
+++ b/src/LauSimFitCoordinator.cc
@@ -1,1065 +1,1069 @@
/*
Copyright 2013 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauSimFitCoordinator.cc
\brief File containing implementation of LauSimFitCoordinator class.
*/
#include <cstdlib>
#include <iostream>
#include <limits>
#include "TMath.h"
#include "TMatrixD.h"
#include "TMessage.h"
#include "TMonitor.h"
#include "TObjArray.h"
#include "TObjString.h"
#include "TServerSocket.h"
#include "TSocket.h"
#include "TSystem.h"
#include "LauAbsFitter.hh"
#include "LauFitNtuple.hh"
#include "LauFitter.hh"
#include "LauFormulaPar.hh"
#include "LauParameter.hh"
#include "LauParamFixed.hh"
#include "LauSimFitCoordinator.hh"
ClassImp(LauSimFitCoordinator)
LauSimFitCoordinator::LauSimFitCoordinator( UInt_t numTasks, UInt_t port ) :
nTasks_(numTasks),
reqPort_(port),
socketMonitor_(0),
messageFromTask_(0),
fitNtuple_(0)
{
messagesToTasks_.resize( nTasks_ );
for ( UInt_t iTask(0); iTask < nTasks_; ++iTask ) {
messagesToTasks_[iTask] = new TMessage();
}
}
LauSimFitCoordinator::~LauSimFitCoordinator()
{
delete socketMonitor_; socketMonitor_ = 0;
// Tell all tasks that they are finished and delete corresponding socket
TString msgStr("Finish");
TMessage message( kMESS_STRING );
message.WriteTString(msgStr);
for ( std::vector<TSocket*>::iterator iter = socketTasks_.begin(); iter != socketTasks_.end(); ++iter ) {
(*iter)->Send(message);
(*iter)->Close();
delete (*iter);
}
socketTasks_.clear();
// Remove all fit parameters
for ( std::vector<LauParameter*>::iterator iter = params_.begin(); iter != params_.end(); ++iter ) {
delete *iter;
}
params_.clear();
for ( std::vector<Double_t*>::iterator iter = vectorPar_.begin(); iter != vectorPar_.end(); ++iter ) {
delete[] (*iter);
}
vectorPar_.clear();
delete messageFromTask_; messageFromTask_ = 0;
for ( std::vector<TMessage*>::iterator iter = messagesToTasks_.begin(); iter != messagesToTasks_.end(); ++iter ) {
delete (*iter);
}
messagesToTasks_.clear();
delete fitNtuple_;
}
void LauSimFitCoordinator::initSockets()
{
if ( socketMonitor_ != 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::initSockets : Sockets already initialised." << std::endl;
return;
}
//initialise socket connection, then accept a connection and return a full-duplex communication socket.
socketMonitor_ = new TMonitor();
TServerSocket *ss = new TServerSocket( reqPort_, kFALSE );
UInt_t actual_port = ss->GetLocalPort();
std::cout << "INFO in LauSimFitCoordinator::initSockets : Waiting for connection with " << nTasks_ << " tasks on port " << actual_port << std::endl;
socketTasks_.resize(nTasks_);
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
socketTasks_[iTask] = ss->Accept();
std::cout << " : Added task " << iTask << std::endl;
}
// tell the clients to start
std::cout << "INFO in LauSimFitCoordinator::initSockets : Initialising tasks" << std::endl;
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
TMessage message( kMESS_ANY );
message.WriteUInt(iTask);
message.WriteUInt(nTasks_);
message.WriteBool(this->useAsymmFitErrors());
socketTasks_[iTask]->Send(message);
socketMonitor_->Add(socketTasks_[iTask]);
}
std::cout << " : Now start fit\n" << std::endl;
ss->Close();
delete ss;
}
/*
* OLD VERSION THAT JUST GETS THE NAMES - COULD HAVE A SERIES OF EXCHANGES TO GET THE NAMES, INIT VALUES, RANGES, ETC. INSTEAD OF PASSING PARAMETERS
* THIS INCREASES THE GENERALITY OF THE CODE, I.E. THERE IS NO NEED FOR THE TASKS TO KNOW ANY LAURA++ CLASS BUT THIS ONE, BUT MAKES IT RATHER MORE DENSE
* FOR THE MOMENT I WILL STICK WITH THE METHOD OF PASSING LAUPARAMETER OBJECTS AROUND AND CONSIDER GOING BACK TO THIS GENERAL METHOD ONCE EVERYTHING IS WORKING
*
void LauSimFitCoordinator::getParametersFromTasksFirstTime()
{
taskIndices_.resize( nTasks_ );
TSocket* sActive(0);
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
// Send a message to the task, requesting the list of parameter names
TString msgStr = "Parameter Names";
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
socketTasks_[iTask]->Send(message);
// Wait to receive the response and check that it has come from the task we just requested from
sActive = socketMonitor_->Select();
if ( sActive != socketTasks_[iTask] ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Received message from a different task than expected!" << std::endl;
gSystem->Exit(1);
}
// Read the object and extract the parameter names
socketTasks_[iTask]->Recv( messageFromTask_ );
TObjArray * objarray = dynamic_cast<TObjArray*>( messageFromTask_->ReadObject( messageFromTask_->GetClass() ) );
if ( ! objarray ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Error reading parameter names from task" << std::endl;
gSystem->Exit(1);
}
Int_t nPars = objarray->GetEntries();
for ( Int_t iPar(0); iPar < nPars; ++iPar ) {
TObjString* objstring = dynamic_cast<TObjString*>( (*objarray)[iPar] );
if ( ! objstring ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Error reading parameter names from task" << std::endl;
gSystem->Exit(1);
}
TString parname = objstring->GetString();
std::map< TString, UInt_t >::iterator iter = parIndices_.find( parname );
if ( iter != parIndices_.end() ) {
UInt_t index = iter->second;
taskIndices_[iTask].push_back( index );
} else {
UInt_t index = parIndices_.size();
parIndices_.insert( std::make_pair( parname, index ) );
parNames_.insert( std::make_pair( index, parname ) );
taskIndices_[iTask].push_back( index );
}
}
delete objarray; objarray = 0;
delete messageFromTask_; messageFromTask_ = 0;
}
UInt_t nPars = parNames_.size();
parValues_.resize( nPars );
}
*/
void LauSimFitCoordinator::getParametersFromTasks()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasks : Sockets not initialised." << std::endl;
return;
}
if ( params_.empty() ) {
this->getParametersFromTasksFirstTime();
// Add variables to Gaussian constrain to a list
this->addConParameters();
} else {
this->updateParametersFromTasks();
}
}
void LauSimFitCoordinator::updateParametersFromTasks()
{
TSocket* sActive(0);
// Construct a message, requesting the list of parameter names
TString msgStr = "Send Parameters";
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
// Send the message to the task
socketTasks_[iTask]->Send(message);
// Wait to receive the response and check that it has come from the task we just requested from
sActive = socketMonitor_->Select();
if ( sActive != socketTasks_[iTask] ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Received message from a different task than expected!" << std::endl;
gSystem->Exit(1);
}
// Read the object and extract the parameter names
socketTasks_[iTask]->Recv( messageFromTask_ );
TObjArray * objarray = dynamic_cast<TObjArray*>( messageFromTask_->ReadObject( messageFromTask_->GetClass() ) );
if ( ! objarray ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Error reading parameter names from task" << std::endl;
gSystem->Exit(1);
}
// We want to auto-delete the supplied parameters since we only copy their values in this case
objarray->SetOwner(kTRUE);
const UInt_t nPars = objarray->GetEntries();
if ( nPars != taskIndices_[iTask].size() ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Unexpected number of parameters received from task" << std::endl;
gSystem->Exit(1);
}
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
LauParameter* parameter = dynamic_cast<LauParameter*>( (*objarray)[iPar] );
if ( ! parameter ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Error reading parameter from task" << std::endl;
gSystem->Exit(1);
}
TString parname = parameter->name();
Double_t parvalue = parameter->initValue();
std::map< TString, UInt_t >::iterator iter = parIndices_.find( parname );
if ( iter == parIndices_.end() ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Unexpected parameter name received from task" << std::endl;
gSystem->Exit(1);
}
const UInt_t index = iter->second;
if ( taskIndices_[iTask][iPar] != index ) {
std::cerr << "ERROR in LauSimFitCoordinator::updateParametersFromTasks : Unexpected parameter received from task" << std::endl;
gSystem->Exit(1);
}
params_[index]->initValue( parvalue );
parValues_[index] = parvalue;
vectorPar_[iTask][iPar] = parvalue;
this->checkParameter( parameter, index );
}
delete objarray; objarray = 0;
delete messageFromTask_; messageFromTask_ = 0;
}
}
void LauSimFitCoordinator::getParametersFromTasksFirstTime()
{
taskIndices_.resize( nTasks_ );
taskFreeIndices_.resize( nTasks_ );
vectorPar_.resize( nTasks_ );
vectorRes_.resize( nTasks_ );
TSocket* sActive(0);
// Construct a message, requesting the list of parameter names
TString msgStr = "Send Parameters";
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
// Send the message to the task
socketTasks_[iTask]->Send(message);
// Wait to receive the response and check that it has come from the task we just requested from
sActive = socketMonitor_->Select();
if ( sActive != socketTasks_[iTask] ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Received message from a different task than expected!" << std::endl;
gSystem->Exit(1);
}
// Read the object and extract the parameter names
socketTasks_[iTask]->Recv( messageFromTask_ );
TObjArray * objarray = dynamic_cast<TObjArray*>( messageFromTask_->ReadObject( messageFromTask_->GetClass() ) );
if ( ! objarray ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Error reading parameters from task" << std::endl;
gSystem->Exit(1);
}
const UInt_t nPars = objarray->GetEntries();
vectorPar_[iTask] = new Double_t[nPars];
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
LauParameter* parameter = dynamic_cast<LauParameter*>( (*objarray)[iPar] );
if ( ! parameter ) {
std::cerr << "ERROR in LauSimFitCoordinator::getParametersFromTasksFirstTime : Error reading parameter from task" << std::endl;
gSystem->Exit(1);
}
TString parname = parameter->name();
Double_t parvalue = parameter->initValue();
Bool_t parfixed = parameter->fixed();
std::map< TString, UInt_t >::iterator iter = parIndices_.find( parname );
if ( iter != parIndices_.end() ) {
UInt_t index = iter->second;
taskIndices_[iTask].push_back( index );
if ( ! parfixed ) {
taskFreeIndices_[iTask].push_back( index );
}
this->checkParameter( parameter, index );
} else {
UInt_t index = parIndices_.size();
parIndices_.insert( std::make_pair( parname, index ) );
parNames_.insert( std::make_pair( index, parname ) );
taskIndices_[iTask].push_back( index );
if ( ! parfixed ) {
taskFreeIndices_[iTask].push_back( index );
}
params_.push_back( parameter );
parValues_.push_back( parvalue );
}
vectorPar_[iTask][iPar] = parvalue;
}
delete objarray; objarray = 0;
delete messageFromTask_; messageFromTask_ = 0;
}
}
void LauSimFitCoordinator::printParInfo() const
{
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
const std::vector<UInt_t>& indices = taskIndices_[iTask];
std::cout << "INFO in LauSimFitCoordinator::printParInfo : Task " << iTask << " has the following parameters:\n";
for ( std::vector<UInt_t>::const_iterator iter = indices.begin(); iter != indices.end(); ++iter ) {
const TString& parName = parNames_.find(*iter)->second;
Double_t parValue = parValues_[*iter];
const LauParameter* par = params_[*iter];
if ( par->name() != parName || par->initValue() != parValue ) {
std::cerr << "ERROR in LauSimFitCoordinator::printParInfo : Discrepancy in parameter name and value records, this is very strange!!" << std::endl;
}
std::cout << " : " << parName << " = " << parValue << " and has index " << *iter << "\n";
}
std::cout << std::endl;
}
std::cout << "INFO in LauSimFitCoordinator::printParInfo : " << "There are " << params_.size() << " parameters in total" << std::endl;
}
void LauSimFitCoordinator::checkParameter( const LauParameter* param, UInt_t index ) const
{
const LauParameter* storedPar = params_[index];
TString parName = storedPar->name();
if ( param->name() != parName ) {
std::cerr << "ERROR in LauSimFitCoordinator::checkParameter : Parameter name is different!! This shouldn't happen!!" << std::endl;
}
if ( param->initValue() != storedPar->initValue() ) {
std::cerr << "WARNING in LauSimFitCoordinator::checkParameter : Initial value for parameter " << parName << " is different, will use the value first set: " << storedPar->initValue() << std::endl;
}
if ( param->minValue() != storedPar->minValue() ) {
std::cerr << "WARNING in LauSimFitCoordinator::checkParameter : Minimum allowed value for parameter " << parName << " is different, will use the value first set: " << storedPar->minValue() << std::endl;
}
if ( param->maxValue() != storedPar->maxValue() ) {
std::cerr << "WARNING in LauSimFitCoordinator::checkParameter : Maximum allowed value for parameter " << parName << " is different, will use the value first set: " << storedPar->maxValue() << std::endl;
}
if ( param->fixed() != storedPar->fixed() ) {
std::cerr << "WARNING in LauSimFitCoordinator::checkParameter : Fixed/floating property of parameter " << parName << " is different, will use the value first set: " << (storedPar->fixed() ? "fixed" : "floating") << std::endl;
}
if ( param->secondStage() != storedPar->secondStage() ) {
std::cerr << "WARNING in LauSimFitCoordinator::checkParameter : Second stage property of parameter " << parName << " is different, will use the value first set: " << (storedPar->secondStage() ? "true" : "false") << std::endl;
}
}
void LauSimFitCoordinator::initialise()
{
this->initSockets();
}
-void LauSimFitCoordinator::runSimFit( const TString& fitNtupleFileName, const UInt_t nExp, const UInt_t firstExp, const Bool_t useAsymmErrors, const Bool_t doTwoStageFit )
+void LauSimFitCoordinator::runSimFit( const TString& fitNtupleFileName, const Bool_t useAsymmErrors, const Bool_t doTwoStageFit )
{
// Routine to perform the total fit.
// First, initialise
this->useAsymmFitErrors(useAsymmErrors);
this->twoStageFit(doTwoStageFit);
this->initialise();
+ const UInt_t nExp { this->nExpt() };
+ const UInt_t firstExp { this->firstExpt() };
+ const Bool_t isToy { this->toyExpts() };
+
std::cout << "INFO in LauSimFitCoordinator::runSimFit : First experiment = " << firstExp << std::endl;
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Number of experiments = " << nExp << std::endl;
+ std::cout << "INFO in LauSimFitCoordinator::runSimFit : Is toy MC = " << (isToy ? "True" : "False") << std::endl;
// Start the cumulative timer
cumulTimer_.Start();
this->resetFitCounters();
// Create and setup the fit results ntuple
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Creating fit ntuple." << std::endl;
if (fitNtuple_ != 0) {delete fitNtuple_; fitNtuple_ = 0;}
fitNtuple_ = new LauFitNtuple(fitNtupleFileName, useAsymmErrors);
// Loop over the number of experiments
for (UInt_t iExp = firstExp; iExp < (firstExp+nExp); ++iExp) {
// Start the timer to see how long each fit takes
timer_.Start();
this->setCurrentExperiment( iExp );
// Instruct the tasks to read the data for this experiment
Bool_t readOK = this->readData();
if ( ! readOK ) {
std::cerr << "ERROR in LauSimFitCoordinator::runSimFit : One or more tasks reported problems with reading data for experiment " << iExp << ", skipping..." << std::endl;
timer_.Stop();
continue;
}
// Instruct the tasks to perform the caching
this->cacheInputData();
- // If we're fitting toy experiments then re-generate the means of any constraints
- this->generateConstraintMeans( conVars_ );
-
// Do the fit
this->fitExpt();
// Stop the timer and see how long the program took so far
timer_.Stop();
timer_.Print();
// Instruct the tasks to finalise the results
this->finalise();
}
// Print out total timing info.
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Cumulative timing:" << std::endl;
cumulTimer_.Stop();
cumulTimer_.Print();
// Print out stats on OK fits.
const UInt_t nOKFits = this->numberOKFits();
const UInt_t nBadFits = this->numberBadFits();
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Number of OK Fits = " << nOKFits << std::endl;
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Number of Failed Fits = " << nBadFits << std::endl;
Double_t fitEff(0.0);
if (nExp != 0) {fitEff = nOKFits/(1.0*nExp);}
std::cout << "INFO in LauSimFitCoordinator::runSimFit : Fit efficiency = " << fitEff*100.0 << "%." << std::endl;
// Instruct the tasks to write out any fit results (ntuples etc...).
this->writeOutResults();
}
void LauSimFitCoordinator::withinAsymErrorCalc(const Bool_t inAsymErrCalc)
{
this->LauFitObject::withinAsymErrorCalc(inAsymErrCalc);
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::withinAsymErrorCalc : Sockets not initialised." << std::endl;
return;
}
// Construct a message, informing the tasks whether or not we are now within the asymmetric error calculation
TString msgStr("Asym Error Calc");
const Bool_t asymErrorCalc( this->withinAsymErrorCalc() );
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
message.WriteBool( asymErrorCalc );
// Send the message to the tasks
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
socketTasks_[iTask]->Send(message);
}
TSocket* sActive(0);
UInt_t responsesReceived(0);
while ( responsesReceived != nTasks_ ) {
// Get the next queued response
sActive = socketMonitor_->Select();
// Extract from the message the ID of the task and the number of events read
Bool_t response(kTRUE);
UInt_t iTask(0);
sActive->Recv( messageFromTask_ );
messageFromTask_->ReadUInt( iTask );
messageFromTask_->ReadBool( response );
if ( response != asymErrorCalc ) {
std::cerr << "WARNING in LauSimFitCoordinator::withinAsymErrorCalc : Problem informing task " << iTask << std::endl;
}
++responsesReceived;
}
}
Bool_t LauSimFitCoordinator::readData()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::readData : Sockets not initialised." << std::endl;
return kFALSE;
}
// Construct a message, requesting to read the data for the given experiment
TString msgStr("Read Expt");
const UInt_t iExp( this->iExpt() );
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
message.WriteUInt( iExp );
// Send the message to the tasks
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
socketTasks_[iTask]->Send(message);
}
TSocket* sActive(0);
UInt_t responsesReceived(0);
Bool_t ok(kTRUE);
while ( responsesReceived != nTasks_ ) {
// Get the next queued response
sActive = socketMonitor_->Select();
// Extract from the message the ID of the task and the number of events read
sActive->Recv( messageFromTask_ );
UInt_t iTask(0);
UInt_t nEvents(0);
messageFromTask_->ReadUInt( iTask );
messageFromTask_->ReadUInt( nEvents );
if ( nEvents <= 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::readData : Task " << iTask << " reports no events found for experiment " << iExp << std::endl;
ok = kFALSE;
} else {
std::cerr << "INFO in LauSimFitCoordinator::readData : Task " << iTask << " reports " << nEvents << " events found for experiment " << iExp << std::endl;
}
++responsesReceived;
}
return ok;
}
Bool_t LauSimFitCoordinator::cacheInputData()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::cacheInputData : Sockets not initialised." << std::endl;
return kFALSE;
}
// Construct a message, requesting it to read the data for the given experiment
TString msgStr("Cache");
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
// Send the message to the task
socketTasks_[iTask]->Send(message);
}
TSocket* sActive(0);
UInt_t responsesReceived(0);
Bool_t allOK(kTRUE);
while ( responsesReceived != nTasks_ ) {
// Get the next queued response
sActive = socketMonitor_->Select();
// Extract from the message the ID of the task and the success/failure flag
sActive->Recv( messageFromTask_ );
UInt_t iTask(0);
Bool_t ok(kTRUE);
messageFromTask_->ReadUInt( iTask );
messageFromTask_->ReadBool( ok );
if ( ! ok ) {
std::cerr << "ERROR in LauSimFitCoordinator::cacheInputData : Task " << iTask << " reports an error performing caching" << std::endl;
allOK = kFALSE;
}
++responsesReceived;
}
return allOK;
}
void LauSimFitCoordinator::checkInitFitParams()
{
this->getParametersFromTasks();
this->printParInfo();
}
void LauSimFitCoordinator::fitExpt()
{
// Routine to perform the actual fit for the given experiment
// Instruct the tasks to update initial fit parameters if required (e.g. if using random numbers).
this->checkInitFitParams();
+ // If we're fitting toy experiments then re-generate the means of any constraints
+ this->generateConstraintMeans( conVars_ );
+
// Initialise the fitter
LauFitter::fitter().useAsymmFitErrors( this->useAsymmFitErrors() );
LauFitter::fitter().twoStageFit( this->twoStageFit() );
LauFitter::fitter().initialise( this, params_ );
this->startNewFit( LauFitter::fitter().nParameters(), LauFitter::fitter().nFreeParameters() );
// Now ready for minimisation step
std::cout << "\nINFO in LauSimFitCoordinator::fitExpt : Start minimisation...\n";
LauAbsFitter::FitStatus fitResult = LauFitter::fitter().minimise();
// If we're doing a two stage fit we can now release (i.e. float)
// the 2nd stage parameters and re-fit
if (this->twoStageFit()) {
if ( fitResult.status != 3 ) {
std::cerr << "ERROR in LauSimFitCoordinator:fitExpt : Not running second stage fit since first stage failed." << std::endl;
LauFitter::fitter().releaseSecondStageParameters();
} else {
LauFitter::fitter().releaseSecondStageParameters();
this->startNewFit( LauFitter::fitter().nParameters(), LauFitter::fitter().nFreeParameters() );
fitResult = LauFitter::fitter().minimise();
}
}
const TMatrixD& covMat = LauFitter::fitter().covarianceMatrix();
this->storeFitStatus( fitResult, covMat );
// Store the final fit results and errors into protected internal vectors that
// all sub-classes can use within their own finalFitResults implementation
// used below (e.g. putting them into an ntuple in a root file)
LauFitter::fitter().updateParameters();
}
void LauSimFitCoordinator::setParsFromMinuit(Double_t* par, Int_t npar)
{
// This function sets the internal parameters based on the values
// that Minuit is using when trying to minimise the total likelihood function.
// MINOS reports different numbers of free parameters depending on the
// situation, so disable this check
if ( ! this->withinAsymErrorCalc() ) {
const UInt_t nFreePars = this->nFreeParams();
if (static_cast<UInt_t>(npar) != nFreePars) {
std::cerr << "ERROR in LauSimFitCoordinator::setParsFromMinuit : Unexpected number of free parameters: " << npar << ".\n";
std::cerr << " Expected: " << nFreePars << ".\n" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Despite npar being the number of free parameters
// the par array actually contains all the parameters,
// free and floating...
// Update all the parameters with their new values.
// Change the value in the array to be sent out to the tasks and the
// parameters themselves (so that constraints are correctly calculated)
for (UInt_t i(0); i<this->nTotParams(); ++i) {
if (!params_[i]->fixed()) {
parValues_[i] = par[i];
params_[i]->value(par[i]);
}
}
}
Double_t LauSimFitCoordinator::getTotNegLogLikelihood()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::getTotNegLogLikelihood : Sockets not initialised." << std::endl;
return 0.0;
}
// Send current values of the parameters to the tasks.
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
std::vector<UInt_t>& indices = taskIndices_[iTask];
std::vector<UInt_t>& freeIndices = taskFreeIndices_[iTask];
UInt_t nPars = indices.size();
UInt_t nFreePars = freeIndices.size();
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
vectorPar_[iTask][iPar] = parValues_[ indices[iPar] ];
}
TMessage* message = messagesToTasks_[iTask];
message->Reset( kMESS_ANY );
message->WriteUInt( nPars );
message->WriteUInt( nFreePars );
message->WriteFastArray( vectorPar_[iTask], nPars );
socketTasks_[iTask]->Send(*message);
}
Double_t negLogLike(0.0);
TSocket *sActive(0);
UInt_t responsesReceived(0);
Bool_t allOK(kTRUE);
while ( responsesReceived != nTasks_ ) {
sActive = socketMonitor_->Select();
sActive->Recv(messageFromTask_);
messageFromTask_->ReadDouble( vectorRes_[responsesReceived] );
Double_t& nLL = vectorRes_[responsesReceived];
if ( nLL == 0.0 || TMath::IsNaN(nLL) || !TMath::Finite(nLL) ) {
allOK = kFALSE;
}
negLogLike += vectorRes_[responsesReceived];
++responsesReceived;
}
// Calculate any penalty terms from Gaussian constrained variables
const auto& multiDimCons = this->multiDimConstraints();
if ( ! conVars_.empty() || ! multiDimCons.empty() ){
negLogLike += this->getLogLikelihoodPenalty();
}
const Double_t worstNegLogLike = -1.0*this->worstLogLike();
if ( ! allOK ) {
std::cerr << "WARNING in LauSimFitCoordinator::getTotNegLogLikelihood : Strange NLL value returned by one or more tasks\n";
std::cerr << " : Returning worst NLL found so far to force MINUIT out of this region." << std::endl;
negLogLike = worstNegLogLike;
} else if ( negLogLike > worstNegLogLike ) {
this->worstLogLike( -negLogLike );
}
return negLogLike;
}
Double_t LauSimFitCoordinator::getLogLikelihoodPenalty()
{
Double_t penalty{0.0};
for ( LauAbsRValue* par : conVars_ ) {
penalty += par->constraintPenalty();
}
auto& multiDimCons = this->multiDimConstraints();
for ( auto& constraint : multiDimCons ) {
penalty += constraint.constraintPenalty();
}
return penalty;
}
void LauSimFitCoordinator::addConParameters()
{
// Add penalties from the constraints to fit parameters
// First, constraints on the fit parameters themselves
for ( LauParameter* param : params_ ) {
if ( param->gaussConstraint() ) {
conVars_.push_back( param );
std::cout << "INFO in LauSimFitCoordinator::addConParameters : Added Gaussian constraint to parameter "<< param->name() << std::endl;
}
}
// Second, constraints on arbitrary combinations
auto& conStore = this->formulaConstraints();
for ( auto& constraint : conStore ) {
std::vector<LauParameter*> params;
for ( const auto& name : constraint.conPars_ ) {
for ( LauParameter* par : params_ ) {
if ( par->name() == name ){
params.push_back( par );
}
}
}
// If the parameters are not found, skip it
if ( params.size() != constraint.conPars_.size() ) {
std::cerr << "WARNING in LauSimFitCoordinator::addConParameters: Could not find parameters to constrain in the formula... skipping" << std::endl;
continue;
}
constraint.formulaPar_ = std::make_unique<LauFormulaPar>( constraint.formula_, constraint.formula_, params );
constraint.formulaPar_->addGaussianConstraint( constraint.mean_, constraint.width_ );
conVars_.push_back( constraint.formulaPar_.get() );
std::cout << "INFO in LauSimFitCoordinator::addConParameters : Added Gaussian constraint to formula\n";
std::cout << " : Formula: " << constraint.formula_ << std::endl;
for ( LauParameter* param : params ) {
std::cout << " : Parameter: " << param->name() << std::endl;
}
}
// Thirdly, add n-dimensional constraints
auto& multiDimCons = this->multiDimConstraints();
for ( auto& constraint : multiDimCons ){
for ( auto& parname : constraint.conPars_ ){
for ( auto& fitPar : params_ ){
if ( parname == fitPar->name() ){
// Check parameters do not have a 1D Gaussian constraint applied
if ( fitPar->gaussConstraint() ){
- std::cerr << "ERROR in LauAbsFitModel::addConParameters: parameter in n-dimensional constraint already has a 1d constraint applied" << std::endl;
+ std::cerr << "ERROR in LauSimFitCoordinator::addConParameters: parameter in n-dimensional constraint already has a 1d constraint applied" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
constraint.conLauPars_.push_back(fitPar);
}
}
}
if ( constraint.conLauPars_.size() != constraint.conPars_.size() ){
- std::cerr << "Error in LauAbsFitModel::addConParameters : Could not match parameter names for n-dimensional constraint" << std::endl;
+ std::cerr << "Error in LauSimFitCoordinator::addConParameters : Could not match parameter names for n-dimensional constraint" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
}
Bool_t LauSimFitCoordinator::finalise()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Sockets not initialised." << std::endl;
return kFALSE;
}
// Prepare the covariance matrices
const TMatrixD& covMatrix = this->covarianceMatrix();
covMatrices_.resize( nTasks_ );
LauParamFixed pred;
std::map<UInt_t,UInt_t> freeParIndices;
UInt_t counter(0);
for ( UInt_t iPar(0); iPar < this->nTotParams(); ++iPar ) {
const LauParameter* par = params_[iPar];
if ( ! pred(par) ) {
freeParIndices.insert( std::make_pair(iPar,counter) );
++counter;
}
}
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
const UInt_t nPar = taskIndices_[iTask].size();
std::vector<UInt_t> freeIndices;
freeIndices.reserve( nPar );
for ( UInt_t iPar(0); iPar < nPar; ++iPar ) {
UInt_t index = taskIndices_[iTask][iPar];
std::map<UInt_t,UInt_t>::iterator freeIter = freeParIndices.find(index);
if ( freeIter == freeParIndices.end() ) {
continue;
}
UInt_t freeIndex = freeIter->second;
freeIndices.push_back( freeIndex );
}
const UInt_t nFreePars = freeIndices.size();
TMatrixD& covMat = covMatrices_[iTask];
covMat.ResizeTo( nFreePars, nFreePars );
for ( UInt_t iPar(0); iPar < nFreePars; ++iPar ) {
for ( UInt_t jPar(0); jPar < nFreePars; ++jPar ) {
UInt_t i = freeIndices[iPar];
UInt_t j = freeIndices[jPar];
covMat( iPar, jPar ) = covMatrix( i, j );
}
}
}
// The array to hold the parameters
TObjArray array;
// Send messages to all tasks containing the final parameters and fit status, NLL
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
array.Clear();
std::vector<UInt_t>& indices = taskIndices_[iTask];
UInt_t nPars = indices.size();
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
array.Add( params_[ indices[iPar] ] );
}
const Int_t status = this->statusCode();
const Double_t NLL = this->nll();
const Double_t EDM = this->edm();
TMatrixD& covMat = covMatrices_[iTask];
TMessage* message = messagesToTasks_[iTask];
message->Reset( kMESS_OBJECT );
message->WriteInt( status );
message->WriteDouble( NLL );
message->WriteDouble( EDM );
message->WriteObject( &array );
message->WriteObject( &covMat );
socketTasks_[iTask]->Send(*message);
}
TSocket *sActive(0);
UInt_t responsesReceived(0);
Bool_t allOK(kTRUE);
while ( responsesReceived != nTasks_ ) {
// Get the next queued response
sActive = socketMonitor_->Select();
// Extract from the message the ID of the task and the number of events read
sActive->Recv( messageFromTask_ );
UInt_t iTask(0);
Bool_t ok(kTRUE);
messageFromTask_->ReadUInt( iTask );
messageFromTask_->ReadBool( ok );
if ( ok ) {
TObjArray * objarray = dynamic_cast<TObjArray*>( messageFromTask_->ReadObject( messageFromTask_->GetClass() ) );
if ( ! objarray ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Error reading finalised parameters from task" << std::endl;
allOK = kFALSE;
} else {
// We want to auto-delete the supplied parameters since we only copy their values in this case
objarray->SetOwner(kTRUE);
const UInt_t nPars = objarray->GetEntries();
if ( nPars != taskIndices_[iTask].size() ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Unexpected number of finalised parameters received from task" << std::endl;
allOK = kFALSE;
} else {
for ( UInt_t iPar(0); iPar < nPars; ++iPar ) {
LauParameter* parameter = dynamic_cast<LauParameter*>( (*objarray)[iPar] );
if ( ! parameter ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Error reading parameter from task" << std::endl;
allOK = kFALSE;
continue;
}
TString parname = parameter->name();
std::map< TString, UInt_t >::iterator iter = parIndices_.find( parname );
if ( iter == parIndices_.end() ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Unexpected parameter name received from task" << std::endl;
allOK = kFALSE;
continue;
}
const UInt_t index = iter->second;
if ( taskIndices_[iTask][iPar] != index ) {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Unexpected parameter received from task" << std::endl;
allOK = kFALSE;
continue;
}
Double_t parvalue = parameter->value();
params_[index]->value( parvalue );
parValues_[index] = parvalue;
vectorPar_[iTask][iPar] = parvalue;
}
}
delete objarray;
}
} else {
std::cerr << "ERROR in LauSimFitCoordinator::finalise : Task " << iTask << " reports an error performing finalisation" << std::endl;
allOK = kFALSE;
}
++responsesReceived;
}
// Fill our ntuple as well
if ( fitNtuple_ != 0 ) {
for ( std::vector<LauParameter*>::iterator iter = params_.begin(); iter != params_.end(); ++iter ) {
if (!(*iter)->fixed()) {
(*iter)->updatePull();
}
}
- std::vector<LauParameter> extraVars;
- fitNtuple_->storeParsAndErrors( params_, extraVars );
+ fitNtuple_->storeParsAndErrors(params_, this->multiDimConstrainedPars(), {});
fitNtuple_->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
fitNtuple_->updateFitNtuple();
}
return allOK;
}
Bool_t LauSimFitCoordinator::writeOutResults()
{
if ( socketMonitor_ == 0 ) {
std::cerr << "ERROR in LauSimFitCoordinator::writeOutResults : Sockets not initialised." << std::endl;
return kFALSE;
}
// Construct a message, requesting to write out the fit results
TString msgStr("Write Results");
TMessage message( kMESS_STRING );
message.WriteTString( msgStr );
// Send the message to the tasks
for ( UInt_t iTask(0); iTask<nTasks_; ++iTask ) {
socketTasks_[iTask]->Send(message);
}
TSocket *sActive(0);
UInt_t responsesReceived(0);
Bool_t allOK(kTRUE);
while ( responsesReceived != nTasks_ ) {
// Get the next queued response
sActive = socketMonitor_->Select();
// Extract from the message the ID of the task and the number of events read
sActive->Recv( messageFromTask_ );
UInt_t iTask(0);
Bool_t ok(kTRUE);
messageFromTask_->ReadUInt( iTask );
messageFromTask_->ReadBool( ok );
if ( ! ok ) {
std::cerr << "ERROR in LauSimFitCoordinator::writeOutResults : Task " << iTask << " reports an error performing finalisation" << std::endl;
allOK = kFALSE;
}
++responsesReceived;
}
// Write out our ntuple as well
if (fitNtuple_ != 0) {
fitNtuple_->writeOutFitResults();
}
return allOK;
}
diff --git a/src/LauSimpleFitModel.cc b/src/LauSimpleFitModel.cc
index c2f9eee..1a432b1 100644
--- a/src/LauSimpleFitModel.cc
+++ b/src/LauSimpleFitModel.cc
@@ -1,2337 +1,2335 @@
/*
Copyright 2004 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauSimpleFitModel.cc
\brief File containing implementation of LauSimpleFitModel class.
*/
#include "LauSimpleFitModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDaughters.hh"
#include "LauEffModel.hh"
#include "LauEmbeddedData.hh"
#include "LauFitNtuple.hh"
#include "LauGenNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TCanvas.h"
#include "TFile.h"
#include "TGraph.h"
#include "TGraph2D.h"
#include "TH2.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TStyle.h"
#include "TSystem.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <typeinfo>
ClassImp(LauSimpleFitModel)
LauSimpleFitModel::LauSimpleFitModel(LauIsobarDynamics* sigModel) : LauAbsFitModel(),
sigDPModel_(sigModel),
kinematics_(sigModel ? sigModel->getKinematics() : 0),
usingBkgnd_(kFALSE),
nSigComp_(0),
nSigDPPar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
meanEff_("meanEff",0.0,0.0,1.0),
dpRate_("dpRate",0.0,0.0,100.0),
//signalEvents_("signalEvents",1.0,0.0,1.0),
signalEvents_(0),
useSCF_(kFALSE),
useSCFHist_(kFALSE),
scfFrac_("scfFrac",0.0,0.0,1.0),
scfFracHist_(0),
scfMap_(0),
compareFitData_(kFALSE),
signalTree_(0),
reuseSignal_(kFALSE),
useReweighting_(kFALSE),
sigDPLike_(0.0),
scfDPLike_(0.0),
sigExtraLike_(0.0),
scfExtraLike_(0.0),
sigTotalLike_(0.0),
scfTotalLike_(0.0)
{
}
LauSimpleFitModel::~LauSimpleFitModel()
{
delete signalTree_;
for (LauBkgndEmbDataList::iterator iter = bkgndTree_.begin(); iter != bkgndTree_.end(); ++iter) {
delete (*iter);
}
delete scfFracHist_;
}
void LauSimpleFitModel::setupBkgndVectors()
{
UInt_t nBkgnds = this->nBkgndClasses();
bkgndDPModels_.resize( nBkgnds );
bkgndPdfs_.resize( nBkgnds );
bkgndEvents_.resize( nBkgnds );
bkgndTree_.resize( nBkgnds );
reuseBkgnd_.resize( nBkgnds );
bkgndDPLike_.resize( nBkgnds );
bkgndExtraLike_.resize( nBkgnds );
bkgndTotalLike_.resize( nBkgnds );
}
void LauSimpleFitModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauSimpleFitModel::setNSigEvents : The signal yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauSimpleFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
TString name = nSigEvents->name();
if ( ! name.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauSimpleFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
}
void LauSimpleFitModel::setNBkgndEvents(LauAbsRValue* nBkgndEvents)
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauSimpleFitModel::setNBkgndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauSimpleFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauSimpleFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
}
void LauSimpleFitModel::splitSignalComponent( const TH2* dpHisto, const Bool_t upperHalf, const Bool_t fluctuateBins, LauScfMap* scfMap )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauSimpleFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
if ( dpHisto == 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::splitSignalComponent : The histogram pointer is null." << std::endl;
return;
}
const LauDaughters* daughters = sigDPModel_->getDaughters();
scfFracHist_ = new LauEffModel( daughters, 0 );
scfFracHist_->setEffHisto( dpHisto, kTRUE, fluctuateBins, 0.0, 0.0, upperHalf, daughters->squareDP() );
scfMap_ = scfMap;
useSCF_ = kTRUE;
useSCFHist_ = kTRUE;
}
void LauSimpleFitModel::splitSignalComponent( const Double_t scfFrac, const Bool_t fixed )
{
if ( useSCF_ == kTRUE ) {
std::cerr << "ERROR in LauSimpleFitModel::splitSignalComponent : Have already setup SCF." << std::endl;
return;
}
scfFrac_.range( 0.0, 1.0 );
scfFrac_.value( scfFrac ); scfFrac_.initValue( scfFrac ); scfFrac_.genValue( scfFrac );
scfFrac_.fixed( fixed );
useSCF_ = kTRUE;
useSCFHist_ = kFALSE;
}
void LauSimpleFitModel::setBkgndDPModel(const TString& bkgndClass, LauAbsBkgndDPModel* bkgndDPModel)
{
if (bkgndDPModel == 0) {
std::cerr << "ERROR in LauSimpleFitModel::setBkgndDPModel : The model pointer is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauSimpleFitModel::setBkgndDPModel : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
bkgndDPModels_[bkgndID] = bkgndDPModel;
usingBkgnd_ = kTRUE;
}
void LauSimpleFitModel::setSignalPdf(LauAbsPdf* pdf)
{
if (pdf==0) {
std::cerr << "ERROR in LauSimpleFitModel::setSignalPdf : The PDF pointer is null." << std::endl;
return;
}
signalPdfs_.push_back(pdf);
}
void LauSimpleFitModel::setSCFPdf(LauAbsPdf* pdf)
{
if (pdf==0) {
std::cerr << "ERROR in LauSimpleFitModel::setSCFPdf : The PDF pointer is null." << std::endl;
return;
}
scfPdfs_.push_back(pdf);
}
void LauSimpleFitModel::setBkgndPdf(const TString& bkgndClass, LauAbsPdf* pdf)
{
if (pdf == 0) {
std::cerr << "ERROR in LauSimpleFitModel::setBkgndPdf : The PDF pointer is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauSimpleFitModel::setBkgndPdf : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
bkgndPdfs_[bkgndID].push_back(pdf);
usingBkgnd_ = kTRUE;
}
void LauSimpleFitModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
// Resize the coeffPars vector if not already done
if ( coeffPars_.empty() ) {
coeffPars_.resize( sigDPModel_->getnTotAmp() );
}
// Is there a component called compName in the signal model?
const TString compName{ coeffSet->name() };
const Int_t index { sigDPModel_->resonanceIndex(compName) };
if ( index < 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::setAmpCoeffSet : Signal DP model doesn't contain component \"" << compName << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
if ( coeffPars_[index] != nullptr && coeffPars_[index]->name() == compName) {
std::cerr << "ERROR in LauSimpleFitModel::setAmpCoeffSet : Have already set coefficients for \"" << compName << "\"." << std::endl;
return;
}
coeffSet->index(index);
std::cout << "INFO in LauSimpleFitModel::setAmpCoeffSet : Added coefficients for component A"<< index << ": \"" << compName << "\" to the fit model." << std::endl;
coeffSet->printParValues();
coeffPars_[index] = std::move(coeffSet);
++nSigComp_;
}
std::vector<const LauAbsCoeffSet*> LauSimpleFitModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_.size(), nullptr );
std::transform( coeffPars_.begin(), coeffPars_.end(), coeffs.begin(), [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); } );
return coeffs;
}
void LauSimpleFitModel::initialise()
{
// Initialisation
if (!this->useDP() && signalPdfs_.empty()) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : Signal model doesn't exist for any variable." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (this->useDP()) {
// Check that we have all the Dalitz-plot models
if ( sigDPModel_ == 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : The pointer to the signal DP model is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
}
if ( usingBkgnd_ ) {
for (LauBkgndDPModelList::const_iterator dpmodel_iter = bkgndDPModels_.begin(); dpmodel_iter != bkgndDPModels_.end(); ++dpmodel_iter ) {
if ( (*dpmodel_iter) == 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : The pointer to one of the background DP models is null.\n";
std::cerr << " : Removing the Dalitz Plot from the model." << std::endl;
this->useDP(kFALSE);
break;
}
}
}
// Need to check that the number of components we have and that the dynamics has matches up
UInt_t nAmp = sigDPModel_->getnTotAmp();
if (nAmp != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : Number of signal DP components with magnitude and phase set not right." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// If all is well, go ahead and initialise them
this->initialiseDPModels();
}
// Next check that, if a given component is being used we've got the
// right number of PDFs for all the variables involved
// TODO - should probably check variable names and so on as well
UInt_t nsigpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nsigpdfvars;
}
}
}
if (useSCF_) {
UInt_t nscfpdfvars(0);
for ( LauPdfPList::const_iterator pdf_iter = scfPdfs_.begin(); pdf_iter != scfPdfs_.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nscfpdfvars;
}
}
}
if (nscfpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : There are " << nsigpdfvars << " TM signal PDF variables but " << nscfpdfvars << " SCF signal PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
if (usingBkgnd_) {
for (LauBkgndPdfsList::const_iterator bgclass_iter = bkgndPdfs_.begin(); bgclass_iter != bkgndPdfs_.end(); ++bgclass_iter) {
UInt_t nbkgndpdfvars(0);
const LauPdfPList& pdfList = (*bgclass_iter);
for ( LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter ) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nbkgndpdfvars;
}
}
}
if (nbkgndpdfvars != nsigpdfvars) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : There are " << nsigpdfvars << " signal PDF variables but " << nbkgndpdfvars << " bkgnd PDF variables." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
}
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the various extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nExtraPdfPar_ + nNormPar_)) {
std::cerr << "ERROR in LauSimpleFitModel::initialise : Number of fit parameters not of expected size." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauSimpleFitModel::initialiseDPModels()
{
std::cout << "INFO in LauSimpleFitModel::initialiseDPModels : Initialising DP models" << std::endl;
sigDPModel_->initialise(coeffs_);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = bkgndDPModels_.begin(); iter != bkgndDPModels_.end(); ++iter) {
(*iter)->initialise();
}
}
}
void LauSimpleFitModel::recalculateNormalisation()
{
//std::cout << "INFO in LauSimpleFitModel::recalculateNormalizationInDPModels : Recalc Norm in DP model" << std::endl;
sigDPModel_->recalculateNormalisation();
sigDPModel_->modifyDataTree();
}
void LauSimpleFitModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauSimpleFitModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; i++) {
LauParameterPList pars = coeffPars_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit variables and in a separate vector
LauParameterPList& sigDPPars = sigDPModel_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( sigDPPars );
}
void LauSimpleFitModel::setExtraPdfParameters()
{
// Include all the parameters of the various PDFs in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
nExtraPdfPar_ += this->addFitParameters(signalPdfs_);
if (useSCF_ == kTRUE) {
nExtraPdfPar_ += this->addFitParameters(scfPdfs_);
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
nExtraPdfPar_ += this->addFitParameters(*iter);
}
}
}
void LauSimpleFitModel::setFitNEvents()
{
if ( signalEvents_ == 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::setFitNEvents : Signal yield not defined." << std::endl;
return;
}
nNormPar_ = 0;
// initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
nTotEvts += (*iter)->value();
if ( (*iter) == 0 ) {
std::cerr << "ERROR in LauSimpleFitModel::setFitNEvents : Background yield not defined." << std::endl;
return;
}
}
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the number of signal events into the fit parameters
if (this->doEMLFit()) {
std::cout << "INFO in LauSimpleFitModel::setFitNEvents : Initialising number of events for signal and background components..." << std::endl;
// add the signal events to the list of fit parameters
nNormPar_ += this->addFitParameters( signalEvents_ );
} else {
std::cout << "INFO in LauSimpleFitModel::setFitNEvents : Initialising number of events for background components (and hence signal)..." << std::endl;
}
if (useSCF_ && !useSCFHist_) {
nNormPar_ += this->addFitParameters( &scfFrac_ );
}
if (usingBkgnd_ == kTRUE) {
nNormPar_ += this->addFitParameters( bkgndEvents_ );
}
}
void LauSimpleFitModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add a fit fraction for each signal component
fitFrac_ = sigDPModel_->getFitFractions();
if (fitFrac_.size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << fitFrac_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFrac_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << fitFrac_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Add the fit fraction that has not been corrected for the efficiency for each signal component
fitFracEffUnCorr_ = sigDPModel_->getFitFractionsEfficiencyUncorrected();
if (fitFracEffUnCorr_.size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << fitFracEffUnCorr_.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracEffUnCorr_[i].size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: " << fitFracEffUnCorr_[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i = 0; i < nSigComp_; i++) {
for (UInt_t j = i; j < nSigComp_; j++) {
extraVars.push_back(fitFrac_[i][j]);
extraVars.push_back(fitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> extraParams = sigDPModel_->getExtraParameters();
std::vector<LauParameter>::iterator extraIter;
for (extraIter = extraParams.begin(); extraIter != extraParams.end(); ++extraIter) {
LauParameter extraParameter = (*extraIter);
extraVars.push_back(extraParameter);
}
// Now add in the DP efficiency value
Double_t initMeanEff = sigDPModel_->getMeanEff().initValue();
meanEff_.value(initMeanEff); meanEff_.initValue(initMeanEff); meanEff_.genValue(initMeanEff);
extraVars.push_back(meanEff_);
// Also add in the DP rate
Double_t initDPRate = sigDPModel_->getDPRate().initValue();
dpRate_.value(initDPRate); dpRate_.initValue(initDPRate); dpRate_.genValue(initDPRate);
extraVars.push_back(dpRate_);
}
void LauSimpleFitModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// e.g. to make mag > 0.0 and phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; i++) {
// Check whether we have mag > 0.0, and phase in the right range
coeffPars_[i]->finaliseValues();
}
}
// update the pulls on the events
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (useSCF_ && !useSCFHist_) {
scfFrac_.updatePull();
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndYieldList::iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
std::vector<LauParameter*> parameters = (*iter)->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
}
// Update the pulls on all the extra PDFs' parameters
this->updateFitParameters(signalPdfs_);
if (useSCF_ == kTRUE) {
this->updateFitParameters(scfPdfs_);
}
if (usingBkgnd_ == kTRUE) {
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->updateFitParameters(*iter);
}
}
// Fill the fit results to the ntuple for current experiment
// update the coefficients and then calculate the fit fractions
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigDPModel_->updateCoeffs(coeffs_);
sigDPModel_->calcExtraInfo();
LauParArray fitFrac = sigDPModel_->getFitFractions();
if (fitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << fitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << fitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracEffUnCorr = sigDPModel_->getFitFractionsEfficiencyUncorrected();
if (fitFracEffUnCorr.size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << fitFracEffUnCorr.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracEffUnCorr[i].size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: " << fitFracEffUnCorr[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFrac_[i][j].value(fitFrac[i][j].value());
fitFracEffUnCorr_[i][j].value(fitFracEffUnCorr[i][j].value());
}
}
meanEff_.value(sigDPModel_->getMeanEff().value());
dpRate_.value(sigDPModel_->getDPRate().value());
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions)
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFrac_[i][j]);
extraVars.push_back(fitFracEffUnCorr_[i][j]);
}
}
// Store any extra parameters/quantities from the DP model (e.g. K-matrix total fit fractions)
std::vector<LauParameter> extraParams = sigDPModel_->getExtraParameters();
std::vector<LauParameter>::iterator extraIter;
for (extraIter = extraParams.begin(); extraIter != extraParams.end(); ++extraIter) {
LauParameter extraParameter = (*extraIter);
extraVars.push_back(extraParameter);
}
// Now add in the DP efficiency value
extraVars.push_back(meanEff_);
// Also add in the DP rate
extraVars.push_back(dpRate_);
this->printFitFractions(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauSimpleFitModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
for (UInt_t i = 0; i < nSigComp_; i++) {
output << "FitFraction for component " << i << " (" << coeffPars_[i]->name() << ") = " << fitFrac_[i][i].value() << std::endl;
}
output << "Overall DP rate (integral of matrix element squared) = " << dpRate_.value() << std::endl;
output << "Average efficiency weighted by whole DP dynamics = " << meanEff_.value() << std::endl;
}
void LauSimpleFitModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
// TODO - need to include the yields in this table
std::ofstream fout(outputFile);
LauPrint print;
std::cout << "INFO in LauSimpleFitModel::writeOutTable : Writing out results of the fit to the tex file " << outputFile << std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->printTableRow(fout);
}
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
// print the fit fractions in another
fout << "\\begin{tabular}{|l|c|}" << std::endl;
fout << "\\hline" << std::endl;
fout << "Component & FitFraction \\\\" << std::endl;
fout << "\\hline" << std::endl;
Double_t fitFracSum(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
fout << resName << " & $";
Double_t fitFrac = fitFrac_[i][i].value();
fitFracSum += fitFrac;
print.printFormat(fout, fitFrac);
fout << "$ \\\\" << std::endl;
}
fout << "\\hline" << std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum & $";
print.printFormat(fout, fitFracSum);
fout << "$ \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "DP rate & $";
print.printFormat(fout, dpRate_.value());
fout << "$ \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "$< \\varepsilon >$ & $";
print.printFormat(fout, meanEff_.value());
fout << "$ \\\\" << std::endl;
fout << "\\hline" << std::endl;
fout << "\\end{tabular}" << std::endl << std::endl;
}
if (!signalPdfs_.empty()) {
fout << "\\begin{tabular}{|l|c|}" << std::endl;
fout << "\\hline" << std::endl;
if (useSCF_ == kTRUE) {
fout << "\\Extra TM Signal PDFs' Parameters: & \\\\" << std::endl;
} else {
fout << "\\Extra Signal PDFs' Parameters: & \\\\" << std::endl;
}
this->printFitParameters(signalPdfs_, fout);
if (useSCF_ == kTRUE && !scfPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra SCF Signal PDFs' Parameters: & \\\\" << std::endl;
this->printFitParameters(scfPdfs_, fout);
}
if (usingBkgnd_ == kTRUE && !bkgndPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra Background PDFs' Parameters: & \\\\" << std::endl;
for (LauBkgndPdfsList::const_iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->printFitParameters(*iter, fout);
}
}
fout << "\\hline \n\\end{tabular}" << std::endl << std::endl;
}
}
void LauSimpleFitModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
std::cout << "INFO in LauSimpleFitModel::checkInitFitParams : Setting random parameters for the signal DP model" << std::endl;
this->randomiseInitFitPars();
}
}
void LauSimpleFitModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout << "INFO in LauSimpleFitModel::randomiseInitFitPars : Randomising the initial fit magnitudes and phases of the resonances..." << std::endl;
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
}
std::pair<LauSimpleFitModel::LauGenInfo,Bool_t> LauSimpleFitModel::eventsToGenerate()
{
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
LauGenInfo nEvtsGen;
// Keep track of whether any yield or asymmetry parameters are blinded
Bool_t blind = kFALSE;
// Signal
if ( signalEvents_->blind() ) {
blind = kTRUE;
}
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
Int_t nEvtsToGen { static_cast<Int_t>(nEvts) };
if (this->doPoissonSmearing()) {
nEvtsToGen = LauRandom::randomFun()->Poisson(nEvts);
}
nEvtsGen["signal"] = std::make_pair( nEvtsToGen, evtWeight );
// Backgrounds
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const LauAbsRValue* evtsPar = bkgndEvents_[bkgndID];
if ( evtsPar->blind() ) {
blind = kTRUE;
}
evtWeight = 1.0;
nEvts = evtsPar->genValue();
if ( nEvts < 0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
nEvtsToGen = static_cast<Int_t>(nEvts);
if (this->doPoissonSmearing()) {
nEvtsToGen = LauRandom::randomFun()->Poisson(nEvts);
}
const TString& bkgndClass = this->bkgndClassName(bkgndID);
nEvtsGen[bkgndClass] = std::make_pair( nEvtsToGen, evtWeight );
}
return std::make_pair( nEvtsGen, blind );
}
Bool_t LauSimpleFitModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
std::pair<LauGenInfo,Bool_t> info = this->eventsToGenerate();
LauGenInfo nEvts = info.first;
const Bool_t blind = info.second;
Bool_t genOK(kTRUE);
Int_t evtNum(0);
const UInt_t nBkgnds = this->nBkgndClasses();
std::vector<TString> bkgndClassNames(nBkgnds);
std::vector<TString> bkgndClassNamesGen(nBkgnds);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
bkgndClassNames[iBkgnd] = name;
bkgndClassNamesGen[iBkgnd] = "gen"+name;
}
const Bool_t storeSCFTruthInfo = ( useSCF_ || ( this->enableEmbedding() && signalTree_ != 0 && signalTree_->haveBranch("mcMatch") ) );
// Loop over the hypotheses and generate the requested number of events for each one
for (LauGenInfo::const_iterator iter = nEvts.begin(); iter != nEvts.end(); ++iter) {
const TString& type(iter->first);
Int_t nEvtsGen( iter->second.first );
Double_t evtWeight( iter->second.second );
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
// Add efficiency information
this->setGenNtupleDoubleBranchValue( "efficiency", 1 );
if (type == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], 0 );
}
genOK = this->generateSignalEvent();
this->setGenNtupleDoubleBranchValue( "efficiency", sigDPModel_->getEvtEff() );
} else {
this->setGenNtupleIntegerBranchValue("genSig",0);
if ( storeSCFTruthInfo ) {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
}
UInt_t bkgndID(0);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
Int_t gen(0);
if ( bkgndClassNames[iBkgnd] == type ) {
gen = 1;
bkgndID = iBkgnd;
}
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], gen );
}
genOK = this->generateBkgndEvent(bkgndID);
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPBranchValues();
}
// Store the event number (within this experiment)
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
// and then increment it
++evtNum;
this->fillGenNtupleBranches();
if ( !blind && (iEvt%500 == 0) ) {
std::cout << "INFO in LauSimpleFitModel::genExpt : Generated event number " << iEvt << " out of " << nEvtsGen << " " << type << " events." << std::endl;
}
}
if (!genOK) {
break;
}
}
if (this->useDP() && genOK) {
sigDPModel_->checkToyMC(kTRUE,kTRUE);
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray fitFrac = sigDPModel_->getFitFractions();
if (fitFrac.size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::genExpt : Fit Fraction array of unexpected dimension: " << fitFrac.size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFrac[i].size() != nSigComp_) {
std::cerr << "ERROR in LauSimpleFitModel::genExpt : Fit Fraction array of unexpected dimension: " << fitFrac[i].size() << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
fitFrac_[i][j].value(fitFrac[i][j].value());
}
}
meanEff_.value(sigDPModel_->getMeanEff().value());
dpRate_.value(sigDPModel_->getDPRate().value());
}
}
// If we're reusing embedded events or if the generation is being
// reset then clear the lists of used events
if (reuseSignal_ || !genOK) {
if (signalTree_) {
signalTree_->clearUsedList();
}
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
LauEmbeddedData* data = bkgndTree_[bkgndID];
if (reuseBkgnd_[bkgndID] || !genOK) {
if (data) {
data->clearUsedList();
}
}
}
return genOK;
}
Bool_t LauSimpleFitModel::generateSignalEvent()
{
// Generate signal event
Bool_t genOK(kTRUE);
Bool_t genSCF(kFALSE);
if (this->useDP()) {
if (this->enableEmbedding() && signalTree_) {
if (useReweighting_) {
// Select a (random) event from the generated data. Then store the
// reconstructed DP co-ords, together with other pdf information,
// as the embedded data.
genOK = signalTree_->getReweightedEvent(sigDPModel_);
} else {
signalTree_->getEmbeddedEvent(kinematics_);
}
if (signalTree_->haveBranch("mcMatch")) {
Int_t match = static_cast<Int_t>(signalTree_->getValue("mcMatch"));
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
}
} else {
genOK = sigDPModel_->generate();
if ( genOK && useSCF_ ) {
Double_t frac(0.0);
if ( useSCFHist_ ) {
frac = scfFracHist_->calcEfficiency( kinematics_ );
} else {
frac = scfFrac_.genValue();
}
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
// Optionally smear the DP position
// of the SCF event
if ( scfMap_ != 0 ) {
Double_t xCoord(0.0), yCoord(0.0);
if ( kinematics_->squareDP() ) {
xCoord = kinematics_->getmPrime();
yCoord = kinematics_->getThetaPrime();
} else {
xCoord = kinematics_->getm13Sq();
yCoord = kinematics_->getm23Sq();
}
// Find the bin number where this event is generated
Int_t binNo = scfMap_->binNumber( xCoord, yCoord );
// Retrieve the migration histogram
TH2* histo = scfMap_->trueHist( binNo );
const LauAbsEffModel * effModel = sigDPModel_->getEffModel();
do {
// Get a random point from the histogram
histo->GetRandom2( xCoord, yCoord );
// Update the kinematics
if ( kinematics_->squareDP() ) {
kinematics_->updateSqDPKinematics( xCoord, yCoord );
} else {
kinematics_->updateKinematics( xCoord, yCoord );
}
} while ( ! effModel->passVeto( kinematics_ ) );
}
}
}
}
} else {
if (this->enableEmbedding() && signalTree_) {
signalTree_->getEmbeddedEvent(0);
if (signalTree_->haveBranch("mcMatch")) {
Int_t match = static_cast<Int_t>(signalTree_->getValue("mcMatch"));
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
}
} else if ( useSCF_ ) {
Double_t frac = scfFrac_.genValue();
if ( frac < LauRandom::randomFun()->Rndm() ) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
genSCF = kFALSE;
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
genSCF = kTRUE;
}
}
}
if (genOK) {
if ( useSCF_ ) {
if ( genSCF ) {
this->generateExtraPdfValues(&scfPdfs_, signalTree_);
} else {
this->generateExtraPdfValues(&signalPdfs_, signalTree_);
}
} else {
this->generateExtraPdfValues(&signalPdfs_, signalTree_);
}
}
// Check for problems with the embedding
if (this->enableEmbedding() && signalTree_ && (signalTree_->nEvents() == signalTree_->nUsedEvents())) {
std::cerr << "WARNING in LauSimpleFitModel::generateSignalEvent : Source of embedded signal events used up, clearing the list of used events." << std::endl;
signalTree_->clearUsedList();
}
return genOK;
}
Bool_t LauSimpleFitModel::generateBkgndEvent(UInt_t bkgndID)
{
// Generate background event
Bool_t genOK(kTRUE);
LauAbsBkgndDPModel* model = bkgndDPModels_[bkgndID];
LauEmbeddedData* embeddedData(0);
if (this->enableEmbedding()) {
embeddedData = bkgndTree_[bkgndID];
}
LauPdfPList* extraPdfs = &bkgndPdfs_[bkgndID];
if (this->useDP()) {
if (embeddedData) {
embeddedData->getEmbeddedEvent(kinematics_);
} else {
if (model == 0) {
std::cerr << "ERROR in LauSimpleFitModel::generateBkgndEvent : Can't find the DP model for background class \"" << this->bkgndClassName(bkgndID) << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
genOK = model->generate();
}
} else {
if (embeddedData) {
embeddedData->getEmbeddedEvent(0);
}
}
if (genOK) {
this->generateExtraPdfValues(extraPdfs, embeddedData);
}
// Check for problems with the embedding
if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
std::cerr << "WARNING in LauSimpleFitModel::generateBkgndEvent : Source of embedded " << this->bkgndClassName(bkgndID) << " events used up, clearing the list of used events." << std::endl;
embeddedData->clearUsedList();
}
return genOK;
}
void LauSimpleFitModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleDoubleBranch("efficiency");
if ( useSCF_ || ( this->enableEmbedding() && signalTree_ != 0 && signalTree_->haveBranch("mcMatch") ) ) {
this->addGenNtupleIntegerBranch("genTMSig");
this->addGenNtupleIntegerBranch("genSCFSig");
}
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name.Prepend("gen");
this->addGenNtupleIntegerBranch(name);
}
if (this->useDP() == kTRUE) {
this->addGenNtupleDoubleBranch("m12");
this->addGenNtupleDoubleBranch("m23");
this->addGenNtupleDoubleBranch("m13");
this->addGenNtupleDoubleBranch("m12Sq");
this->addGenNtupleDoubleBranch("m23Sq");
this->addGenNtupleDoubleBranch("m13Sq");
this->addGenNtupleDoubleBranch("cosHel12");
this->addGenNtupleDoubleBranch("cosHel23");
this->addGenNtupleDoubleBranch("cosHel13");
if (kinematics_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime");
this->addGenNtupleDoubleBranch("thPrime");
}
}
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
this->addGenNtupleDoubleBranch( (*var_iter) );
}
}
}
}
void LauSimpleFitModel::setDPBranchValues()
{
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12", kinematics_->getm12());
this->setGenNtupleDoubleBranchValue("m23", kinematics_->getm23());
this->setGenNtupleDoubleBranchValue("m13", kinematics_->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq", kinematics_->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq", kinematics_->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq", kinematics_->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12", kinematics_->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23", kinematics_->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13", kinematics_->getc13());
if (kinematics_->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime", kinematics_->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime", kinematics_->getThetaPrime());
}
}
void LauSimpleFitModel::generateExtraPdfValues(LauPdfPList* extraPdfs, LauEmbeddedData* embeddedData)
{
// Generate from the extra PDFs
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
LauFitData genValues;
if (embeddedData) {
genValues = embeddedData->getValues( (*pdf_iter)->varNames() );
} else {
genValues = (*pdf_iter)->generate(kinematics_);
}
for ( LauFitData::const_iterator var_iter = genValues.begin(); var_iter != genValues.end(); ++var_iter ) {
TString varName = var_iter->first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var_iter->second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
}
void LauSimpleFitModel::propagateParUpdates()
{
// Update the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigDPModel_->updateCoeffs(coeffs_);
}
// Update the signal fraction from the background fractions if not doing an extended fit
this->updateSigEvents();
}
void LauSimpleFitModel::updateSigEvents()
{
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
// Subtract background events (if any) from signal.
if ( usingBkgnd_ == kTRUE ) {
for ( LauAbsRValue* bkgndYield : bkgndEvents_ ) {
signalEvents -= bkgndYield->value();
}
}
signalEvents_->value(signalEvents);
}
void LauSimpleFitModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and backgrond models.
LauFitDataTree* inputFitData = this->fitData();
// First the Dalitz plot variables (m_ij^2)
if (this->useDP() == kTRUE) {
// need to append SCF smearing bins before caching DP amplitudes
if ( scfMap_ != 0 ) {
this->appendBinCentres( inputFitData );
}
sigDPModel_->fillDataTree(*inputFitData);
if (usingBkgnd_ == kTRUE) {
for (LauBkgndDPModelList::iterator iter = bkgndDPModels_.begin(); iter != bkgndDPModels_.end(); ++iter) {
(*iter)->fillDataTree(*inputFitData);
}
}
}
// ...and then the extra PDFs
this->cacheInfo(signalPdfs_, *inputFitData);
this->cacheInfo(scfPdfs_, *inputFitData);
for (LauBkgndPdfsList::iterator iter = bkgndPdfs_.begin(); iter != bkgndPdfs_.end(); ++iter) {
this->cacheInfo((*iter), *inputFitData);
}
// and finally the SCF fractions and jacobians
if ( useSCF_ && useSCFHist_ ) {
if ( !inputFitData->haveBranch( "m13Sq" ) || !inputFitData->haveBranch( "m23Sq" ) ) {
std::cerr << "ERROR in LauSimpleFitModel::cacheInputFitVars : Input data does not contain DP branches and so can't cache the SCF fraction." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t nEvents = inputFitData->nEvents();
recoSCFFracs_.clear();
recoSCFFracs_.reserve( nEvents );
if ( kinematics_->squareDP() ) {
recoJacobians_.clear();
recoJacobians_.reserve( nEvents );
}
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
LauFitData::const_iterator m13_iter = dataValues.find("m13Sq");
LauFitData::const_iterator m23_iter = dataValues.find("m23Sq");
kinematics_->updateKinematics( m13_iter->second, m23_iter->second );
Double_t scfFrac = scfFracHist_->calcEfficiency( kinematics_ );
recoSCFFracs_.push_back( scfFrac );
if ( kinematics_->squareDP() ) {
recoJacobians_.push_back( kinematics_->calcSqDPJacobian() );
}
}
}
}
void LauSimpleFitModel::appendBinCentres( LauFitDataTree* inputData )
{
// We'll be caching the DP amplitudes and efficiencies of the centres of the true bins.
// To do so, we attach some fake points at the end of inputData, the number of the entry
// minus the total number of events corresponding to the number of the histogram for that
// given true bin in the LauScfMap object. (What this means is that when Laura is provided with
// the LauScfMap object by the user, it's the latter who has to make sure that it contains the
// right number of histograms and in exactly the right order!)
// Get the x and y co-ordinates of the bin centres
std::vector<Double_t> binCentresXCoords;
std::vector<Double_t> binCentresYCoords;
scfMap_->listBinCentres(binCentresXCoords, binCentresYCoords);
// The SCF histograms could be in square Dalitz plot histograms.
// The dynamics takes normal Dalitz plot coords, so we might have to convert them back.
Bool_t sqDP = kinematics_->squareDP();
UInt_t nBins = binCentresXCoords.size();
fakeSCFFracs_.clear();
fakeSCFFracs_.reserve( nBins );
if ( sqDP ) {
fakeJacobians_.clear();
fakeJacobians_.reserve( nBins );
}
for (UInt_t iBin = 0; iBin < nBins; ++iBin) {
if ( sqDP ) {
kinematics_->updateSqDPKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
binCentresXCoords[iBin] = kinematics_->getm13Sq();
binCentresYCoords[iBin] = kinematics_->getm23Sq();
fakeJacobians_.push_back( kinematics_->calcSqDPJacobian() );
} else {
kinematics_->updateKinematics(binCentresXCoords[iBin],binCentresYCoords[iBin]);
}
fakeSCFFracs_.push_back( scfFracHist_->calcEfficiency( kinematics_ ) );
}
// Set up inputFitVars_ object to hold the fake events
inputData->appendFakePoints(binCentresXCoords,binCentresYCoords);
}
Double_t LauSimpleFitModel::getTotEvtLikelihood(UInt_t iEvt)
{
// Get the DP likelihood for signal and backgrounds
this->getEvtDPLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal and backgrounds
this->getEvtExtraLikelihoods(iEvt);
// If appropriate, combine the TM and SCF likelihoods
Double_t sigLike = sigDPLike_ * sigExtraLike_;
if ( useSCF_ ) {
Double_t scfFrac(0.0);
if (useSCFHist_) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
sigLike *= (1.0 - scfFrac);
if ( (scfMap_ != 0) && (this->useDP() == kTRUE) ) {
// if we're smearing the SCF DP PDF then the SCF frac
// is already included in the SCF DP likelihood
sigLike += (scfDPLike_ * scfExtraLike_);
} else {
sigLike += (scfFrac * scfDPLike_ * scfExtraLike_);
}
}
// Construct the total event likelihood
Double_t likelihood = signalEvents_->unblindValue() * sigLike;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
likelihood += (bkgndEvents_[bkgndID]->unblindValue() * bkgndDPLike_[bkgndID] * bkgndExtraLike_[bkgndID]);
}
return likelihood;
}
Double_t LauSimpleFitModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
for (LauBkgndYieldList::const_iterator iter = bkgndEvents_.begin(); iter != bkgndEvents_.end(); ++iter) {
eventSum += (*iter)->unblindValue();
}
return eventSum;
}
void LauSimpleFitModel::getEvtDPLikelihood(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
if (this->useDP() == kTRUE) {
sigDPModel_->calcLikelihoodInfo(iEvt);
sigDPLike_ = sigDPModel_->getEvtLikelihood();
if ( useSCF_ == kTRUE ) {
if ( scfMap_ == 0 ) {
// we're not smearing the SCF DP position
// so the likelihood is the same as the TM
scfDPLike_ = sigDPLike_;
} else {
// calculate the smeared SCF DP likelihood
scfDPLike_ = this->getEvtSCFDPLikelihood(iEvt);
}
}
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = bkgndDPModels_[bkgndID]->getLikelihood(iEvt);
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
} else {
// There's always going to be a term in the likelihood for the
// signal, so we'd better not zero it.
sigDPLike_ = 1.0;
scfDPLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = 1.0;
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
}
}
Double_t LauSimpleFitModel::getEvtSCFDPLikelihood(UInt_t iEvt)
{
Double_t scfDPLike(0.0);
Double_t recoJacobian(1.0);
Double_t xCoord(0.0);
Double_t yCoord(0.0);
Bool_t squareDP = kinematics_->squareDP();
if ( squareDP ) {
xCoord = sigDPModel_->getEvtmPrime();
yCoord = sigDPModel_->getEvtthPrime();
recoJacobian = recoJacobians_[iEvt];
} else {
xCoord = sigDPModel_->getEvtm13Sq();
yCoord = sigDPModel_->getEvtm23Sq();
}
// Find the bin that our reco event falls in
Int_t recoBin = scfMap_->binNumber( xCoord, yCoord );
// Find out which true Bins contribute to the given reco bin
const std::vector<Int_t>* trueBins = scfMap_->trueBins(recoBin);
Int_t nDataEvents = this->eventsPerExpt();
// Loop over the true bins
for (std::vector<Int_t>::const_iterator iter = trueBins->begin(); iter != trueBins->end(); ++iter)
{
Int_t trueBin = (*iter);
// prob of a true event in the given true bin migrating to the reco bin
Double_t pRecoGivenTrue = scfMap_->prob( recoBin, trueBin );
// We've cached the DP amplitudes and the efficiency for the
// true bin centres, just after the data points
sigDPModel_->calcLikelihoodInfo( nDataEvents + trueBin );
Double_t pTrue = sigDPModel_->getEvtLikelihood();
// Get the cached SCF fraction (and jacobian if we're using the square DP)
Double_t scfFraction = fakeSCFFracs_[ trueBin ];
Double_t jacobian(1.0);
if ( squareDP ) {
jacobian = fakeJacobians_[ trueBin ];
}
scfDPLike += pTrue * jacobian * scfFraction * pRecoGivenTrue;
}
// Divide by the reco jacobian
scfDPLike /= recoJacobian;
return scfDPLike;
}
void LauSimpleFitModel::getEvtExtraLikelihoods(UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0;
for (LauPdfPList::iterator iter = signalPdfs_.begin(); iter != signalPdfs_.end(); ++iter) {
(*iter)->calcLikelihoodInfo(iEvt);
sigExtraLike_ *= (*iter)->getLikelihood();
}
if (useSCF_) {
scfExtraLike_ = 1.0;
for (LauPdfPList::iterator iter = scfPdfs_.begin(); iter != scfPdfs_.end(); ++iter) {
(*iter)->calcLikelihoodInfo(iEvt);
scfExtraLike_ *= (*iter)->getLikelihood();
}
}
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = 1.0;
LauPdfPList& pdfList = bkgndPdfs_[bkgndID];
for (LauPdfPList::iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter) {
(*pdf_iter)->calcLikelihoodInfo(iEvt);
bkgndExtraLike_[bkgndID] *= (*pdf_iter)->getLikelihood();
}
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
}
void LauSimpleFitModel::updateCoeffs()
{
coeffs_.clear();
coeffs_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffs_.push_back(coeffPars_[i]->particleCoeff());
}
}
void LauSimpleFitModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
if ( sigDPModel_->usingScfModel() ) {
this->addSPlotNtupleDoubleBranch("scffraction");
}
}
// Store the total event likelihood for each species.
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFTotalLike");
this->addSPlotNtupleDoubleBranch("sigSCFFrac");
} else {
this->addSPlotNtupleDoubleBranch("sigTotalLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "TotalLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
// Store the DP likelihoods
if (this->useDP()) {
if (useSCF_) {
this->addSPlotNtupleDoubleBranch("sigTMDPLike");
this->addSPlotNtupleDoubleBranch("sigSCFDPLike");
} else {
this->addSPlotNtupleDoubleBranch("sigDPLike");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "DPLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
}
// Store the likelihoods for each extra PDF
if (useSCF_) {
this->addSPlotNtupleBranches(&signalPdfs_, "sigTM");
this->addSPlotNtupleBranches(&scfPdfs_, "sigSCF");
} else {
this->addSPlotNtupleBranches(&signalPdfs_, "sig");
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList* pdfList = &(bkgndPdfs_[iBkgnd]);
this->addSPlotNtupleBranches(pdfList, bkgndClass);
}
}
}
void LauSimpleFitModel::addSPlotNtupleBranches(const LauPdfPList* extraPdfs, const TString& prefix)
{
if (extraPdfs) {
// Loop through each of the PDFs
for (LauPdfPList::const_iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
TString name(prefix);
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr << "WARNING in LauSimpleFitModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
}
Double_t LauSimpleFitModel::setSPlotNtupleBranchValues(LauPdfPList* extraPdfs, const TString& prefix, UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
// calculate the likelihood for this event
(*pdf_iter)->calcLikelihoodInfo(iEvt);
extraLike = (*pdf_iter)->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString varName = (*pdf_iter)->varName();
TString name(prefix);
name += varName;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars("");
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
allVars += (*var_iter);
TString name(prefix);
name += (*var_iter);
name += "Like";
Double_t indivLike = (*pdf_iter)->getLikelihood( (*var_iter) );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
TString name(prefix);
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr << "WARNING in LauSimpleFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs." << std::endl;
}
}
}
return totalLike;
}
LauSPlot::NameSet LauSimpleFitModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
// Loop through all the signal PDFs
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
// Loop over the variables involved in each PDF
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
// If they are not DP coordinates then add them
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
nameSet.insert( (*var_iter) );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauSimpleFitModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (!par->fixed()) {
numbMap[bkgndClass] = par->genValue();
if ( ! par->isLValue() ) {
std::cerr << "WARNING in LauSimpleFitModel::freeSpeciesNames : \"" << par->name() << "\" is a LauFormulaPar, which implies it is perhaps not entirely free to float in the fit, so the sWeight calculation may not be reliable" << std::endl;
}
}
}
}
return numbMap;
}
LauSPlot::NumbMap LauSimpleFitModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (par->fixed()) {
numbMap[bkgndClass] = par->genValue();
}
}
}
return numbMap;
}
LauSPlot::TwoDMap LauSimpleFitModel::twodimPDFs() const
{
LauSPlot::TwoDMap twodimMap;
for (LauPdfPList::const_iterator pdf_iter = signalPdfs_.begin(); pdf_iter != signalPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
if (useSCF_) {
twodimMap.insert( std::make_pair( "sigTM", std::make_pair( varNames[0], varNames[1] ) ) );
} else {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if ( useSCF_ ) {
for (LauPdfPList::const_iterator pdf_iter = scfPdfs_.begin(); pdf_iter != scfPdfs_.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sigSCF", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauPdfPList& pdfList = bkgndPdfs_[iBkgnd];
for (LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
std::vector<TString> varNames = (*pdf_iter)->varNames();
for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( bkgndClass, std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
}
return twodimMap;
}
void LauSimpleFitModel::storePerEvtLlhds()
{
std::cout << "INFO in LauSimpleFitModel::storePerEvtLlhds : Storing per-event likelihood values..." << std::endl;
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
LauFitDataTree* inputFitData = this->fitData();
if (!this->useDP() && this->storeDPEff()) {
sigDPModel_->initialise(coeffs_);
sigDPModel_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
this->setSPlotNtupleIntegerBranchValue("iExpt",this->iExpt());
this->setSPlotNtupleIntegerBranchValue("iEvtWithinExpt",iEvt);
// the DP information
this->getEvtDPLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
sigDPModel_->calcLikelihoodInfo(iEvt);
}
this->setSPlotNtupleDoubleBranchValue("efficiency",sigDPModel_->getEvtEff());
if ( sigDPModel_->usingScfModel() ) {
this->setSPlotNtupleDoubleBranchValue("scffraction",sigDPModel_->getEvtScfFraction());
}
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
if (useSCF_) {
scfTotalLike_ = scfDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigTMDPLike",sigDPLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFDPLike",scfDPLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "DPLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndDPLike_[iBkgnd]);
}
}
} else {
sigTotalLike_ = 1.0;
if (useSCF_) {
scfTotalLike_ = 1.0;
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
bkgndTotalLike_[iBkgnd] = 1.0;
}
}
}
// the signal PDF values
if ( useSCF_ ) {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(&signalPdfs_, "sigTM", iEvt);
scfTotalLike_ *= this->setSPlotNtupleBranchValues(&scfPdfs_, "sigSCF", iEvt);
} else {
sigTotalLike_ *= this->setSPlotNtupleBranchValues(&signalPdfs_, "sig", iEvt);
}
// the background PDF values
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
LauPdfPList& pdfs = bkgndPdfs_[iBkgnd];
bkgndTotalLike_[iBkgnd] *= this->setSPlotNtupleBranchValues(&(pdfs), bkgndClass, iEvt);
}
}
// the total likelihoods
if (useSCF_) {
Double_t scfFrac(0.0);
if ( useSCFHist_ ) {
scfFrac = recoSCFFracs_[iEvt];
} else {
scfFrac = scfFrac_.unblindValue();
}
this->setSPlotNtupleDoubleBranchValue("sigSCFFrac",scfFrac);
sigTotalLike_ *= ( 1.0 - scfFrac );
if ( scfMap_ == 0 ) {
scfTotalLike_ *= scfFrac;
}
this->setSPlotNtupleDoubleBranchValue("sigTMTotalLike",sigTotalLike_);
this->setSPlotNtupleDoubleBranchValue("sigSCFTotalLike",scfTotalLike_);
} else {
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "TotalLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndTotalLike_[iBkgnd]);
}
}
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout << "INFO in LauSimpleFitModel::storePerEvtLlhds : Finished storing per-event likelihood values." << std::endl;
}
void LauSimpleFitModel::embedSignal(const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment,
Bool_t useReweighting)
{
if (signalTree_) {
std::cerr << "ERROR in LauSimpleFitModel::embedSignal : Already embedding signal from a file." << std::endl;
return;
}
signalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = signalTree_->findBranches();
if (!dataOK) {
delete signalTree_; signalTree_ = 0;
std::cerr << "ERROR in LauSimpleFitModel::embedSignal : Problem creating data tree for embedding." << std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
useReweighting_ = useReweighting;
if (this->enableEmbedding() == kFALSE) {
this->enableEmbedding(kTRUE);
}
}
void LauSimpleFitModel::embedBkgnd(const TString& bkgndClass, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
if (bkgndTree_[bkgndID]) {
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Already embedding background from a file." << std::endl;
return;
}
bkgndTree_[bkgndID] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = bkgndTree_[bkgndID]->findBranches();
if (!dataOK) {
delete bkgndTree_[bkgndID]; bkgndTree_[bkgndID] = 0;
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Problem creating data tree for embedding." << std::endl;
return;
}
reuseBkgnd_[bkgndID] = reuseEventsWithinEnsemble;
if (this->enableEmbedding() == kFALSE) {
this->enableEmbedding(kTRUE);
}
}
void LauSimpleFitModel::weightEvents( const TString& dataFileName, const TString& dataTreeName )
{
// Routine to provide weights for events that are uniformly distributed
// in the DP (or square DP) so as to reproduce the given DP model
const Bool_t squareDP { kinematics_->squareDP() };
if ( squareDP ) {
std::cout << "INFO in LauSimpleFitModel::weightEvents : will store DP model weights and the square DP jacobian\n";
std::cout << " : the DP model weights can be used on their own to weight events that were generated flat in phase space\n";
std::cout << " : or they can be multiplied by the jacobian to weight events that were generated flat in the square DP\n";
std::cout << " : or they can be multiplied by max(1.0, jacobian) to weight events that were generated quasi-flat in the square DP" << std::endl;
} else {
std::cout << "INFO in LauSimpleFitModel::weightEvents : will store DP model weights suitable for weighting events that were generated flat in phase space" << std::endl;
}
// This reads in the given dataFile and creates an input
// fit data tree that stores them for all events and experiments.
const Bool_t dataOK { this->verifyFitData( dataFileName, dataTreeName ) };
if ( ! dataOK ) {
std::cerr << "ERROR in LauSimpleFitModel::weightEvents : Problem caching the data." << std::endl;
return;
}
LauFitDataTree* inputFitData { this->fitData() };
if ( ! inputFitData->haveBranch( "m13Sq_MC" ) || ! inputFitData->haveBranch( "m23Sq_MC" ) ) {
std::cerr << "ERROR in LauSimpleFitModel::weightEvents : Cannot find MC truth DP coordinate branches in supplied data, aborting." << std::endl;
return;
}
// Create the ntuple to hold the DP weights
TString weightsFileName{ dataFileName };
const Ssiz_t index { weightsFileName.Last( '.' ) };
weightsFileName.Insert( index, "_DPweights" );
LauGenNtuple weightsTuple{ weightsFileName, dataTreeName };
weightsTuple.addIntegerBranch( "iExpt" );
weightsTuple.addIntegerBranch( "iEvtWithinExpt" );
weightsTuple.addDoubleBranch( "dpModelWeight" );
if ( squareDP ) {
weightsTuple.addDoubleBranch( "sqDPJacobian" );
}
const UInt_t nExpmt { this->nExpt() };
const UInt_t firstExpmt { this->firstExpt() };
for (UInt_t iExpmt {firstExpmt}; iExpmt < (firstExpmt+nExpmt); ++iExpmt) {
inputFitData->readExperimentData(iExpmt);
const UInt_t nEvents { inputFitData->nEvents() };
if ( nEvents < 1 ) {
std::cerr << "WARNING in LauSimpleFitModel::weightEvents : Zero events in experiment " << iExpmt << ", skipping..." << std::endl;
continue;
}
weightsTuple.setIntegerBranchValue( "iExpt", iExpmt );
// Calculate and store the weights for the events in this experiment
for ( UInt_t iEvent{0}; iEvent < nEvents; ++iEvent ) {
weightsTuple.setIntegerBranchValue( "iEvtWithinExpt", iEvent );
const LauFitData& evtData { inputFitData->getData( iEvent ) };
const Double_t m13Sq_MC { evtData.at( "m13Sq_MC" ) };
const Double_t m23Sq_MC { evtData.at( "m23Sq_MC" ) };
Double_t dpModelWeight{0.0};
Double_t jacobian{1.0};
if ( kinematics_->withinDPLimits( m13Sq_MC, m23Sq_MC ) ) {
kinematics_->updateKinematics( m13Sq_MC, m23Sq_MC );
dpModelWeight = sigDPModel_->getEventWeight();
if ( squareDP ) {
jacobian = kinematics_->calcSqDPJacobian();
}
dpModelWeight /= sigDPModel_->getDPNorm();
}
weightsTuple.setDoubleBranchValue( "dpModelWeight", dpModelWeight );
if ( squareDP ) {
weightsTuple.setDoubleBranchValue( "sqDPJacobian", jacobian );
}
weightsTuple.fillBranches();
}
}
weightsTuple.buildIndex( "iExpt", "iEvtWithinExpt" );
weightsTuple.addFriendTree( dataFileName, dataTreeName );
weightsTuple.writeOutGenResults();
}
void LauSimpleFitModel::savePDFPlots(const TString& label)
{
savePDFPlotsWave(label, 0);
savePDFPlotsWave(label, 1);
savePDFPlotsWave(label, 2);
std::cout << "LauCPFitModel::plot" << std::endl;
// ((LauIsobarDynamics*)sigDPModel_)->plot();
//Double_t minm13 = sigDPModel_->getKinematics()->getm13Min();
Double_t minm13 = 0.0;
Double_t maxm13 = sigDPModel_->getKinematics()->getm13Max();
//Double_t minm23 = sigDPModel_->getKinematics()->getm23Min();
Double_t minm23 = 0.0;
Double_t maxm23 = sigDPModel_->getKinematics()->getm23Max();
Double_t mins13 = minm13*minm13;
Double_t maxs13 = maxm13*maxm13;
Double_t mins23 = minm23*minm23;
Double_t maxs23 = maxm23*maxm23;
Double_t s13, s23, chPdf;
Int_t n13=200.00, n23=200.00;
Double_t delta13, delta23;
delta13 = (maxs13 - mins13)/n13;
delta23 = (maxs23 - mins23)/n23;
UInt_t nAmp = sigDPModel_->getnCohAmp();
for (UInt_t resID = 0; resID <= nAmp; ++resID)
{
TGraph2D *dt = new TGraph2D();
TString resName = "TotalAmp";
if (resID != nAmp){
TString tStrResID = Form("%d", resID);
const LauIsobarDynamics* model = sigDPModel_;
const LauAbsResonance* resonance = model->getResonance(resID);
resName = resonance->getResonanceName();
std::cout << "resName = " << resName << std::endl;
}
resName.ReplaceAll("(", "");
resName.ReplaceAll(")", "");
resName.ReplaceAll("*", "Star");
TCanvas *c = new TCanvas("c"+resName+label,resName+" ("+label+")",0,0,600,400);
dt->SetName(resName+label);
dt->SetTitle(resName+" ("+label+")");
Int_t count=0;
for (Int_t i=0; i<n13; i++) {
s13 = mins13 + i*delta13;
for (Int_t j=0; j<n23; j++) {
s23 = mins23 + j*delta23;
if (sigDPModel_->getKinematics()->withinDPLimits2(s23, s13))
{
//if (s13 > 4) continue;
sigDPModel_->calcLikelihoodInfo(s13, s23);
LauComplex chAmp = sigDPModel_->getEvtDPAmp();
if (resID != nAmp){
chAmp = sigDPModel_->getFullAmplitude(resID);
}
chPdf = chAmp.abs2();
//if ((z > 0.04)||(z < -0.03)) continue;
//z = TMath::Log(z);
if (sigDPModel_->getDaughters()->gotSymmetricalDP()){
Double_t sLow = s13;
Double_t sHigh = s23;
if (sLow>sHigh) {
continue;
//sLow = s23;
//sHigh = s13;
}
//if (sLow > 3.5) continue;
//if (i<10) std::cout << "SymmetricalDP" << std::endl;
//if (z>0.02) z = 0.02;
//if (z<-0.02) z = -0.02;
dt->SetPoint(count,sHigh,sLow,chPdf);
count++;
}
else {
dt->SetPoint(count,s13,s23,chPdf);
count++;
}
}
}
}
gStyle->SetPalette(1);
dt->GetXaxis()->SetTitle("m_{K#pi}(low)");
dt->GetYaxis()->SetTitle("m_{K#pi}(high)");
dt->Draw("SURF1");
dt->GetXaxis()->SetTitle("m_{K#pi}(low)");
dt->GetYaxis()->SetTitle("m_{K#pi}(high)");
c->SaveAs("plot_2D_"+resName + "_"+label+".C");
}
}
void LauSimpleFitModel::savePDFPlotsWave(const TString& label, const Int_t& spin)
{
std::cout << "label = "<< label << ", spin = "<< spin << std::endl;
TString tStrResID = "S_Wave";
if (spin == 1) tStrResID = "P_Wave";
if (spin == 2) tStrResID = "D_Wave";
std::cout << "LauSimpleFitModel::savePDFPlotsWave: "<< tStrResID << std::endl;
TCanvas *c = new TCanvas("c"+tStrResID+label,tStrResID+" ("+label+")",0,0,600,400);
Double_t minm13 = 0.0;
Double_t maxm13 = sigDPModel_->getKinematics()->getm13Max();
Double_t minm23 = 0.0;
Double_t maxm23 = sigDPModel_->getKinematics()->getm23Max();
Double_t mins13 = minm13*minm13;
Double_t maxs13 = maxm13*maxm13;
Double_t mins23 = minm23*minm23;
Double_t maxs23 = maxm23*maxm23;
Double_t s13, s23, chPdf;
TGraph2D *dt = new TGraph2D();
dt->SetName(tStrResID+label);
dt->SetTitle(tStrResID+" ("+label+")");
Int_t n13=200.00, n23=200.00;
Double_t delta13, delta23;
delta13 = (maxs13 - mins13)/n13;
delta23 = (maxs23 - mins23)/n23;
UInt_t nAmp = sigDPModel_->getnCohAmp();
Int_t count=0;
for (Int_t i=0; i<n13; i++) {
s13 = mins13 + i*delta13;
for (Int_t j=0; j<n23; j++) {
s23 = mins23 + j*delta23;
if (sigDPModel_->getKinematics()->withinDPLimits2(s23, s13))
{
//if (s13 > 4) continue;
LauComplex chAmp(0,0);
Bool_t noWaveRes = kTRUE;
sigDPModel_->calcLikelihoodInfo(s13, s23);
for (UInt_t resID = 0; resID < nAmp; ++resID)
{
const LauIsobarDynamics* model = sigDPModel_;
const LauAbsResonance* resonance = model->getResonance(resID);
Int_t spin_res = resonance->getSpin();
if (spin != spin_res) continue;
noWaveRes = kFALSE;
chAmp += sigDPModel_->getFullAmplitude(resID);
}
if (noWaveRes) return;
chPdf = chAmp.abs2();
if (sigDPModel_->getDaughters()->gotSymmetricalDP()){
Double_t sLow = s13;
Double_t sHigh = s23;
if (sLow>sHigh) {
continue;
//sLow = s23;
//sHigh = s13;
}
dt->SetPoint(count,sHigh,sLow,chPdf);
count++;
}
else {
dt->SetPoint(count,s13,s23,chPdf);
count++;
}
}
}
}
gStyle->SetPalette(1);
dt->GetXaxis()->SetTitle("pipi");
dt->GetYaxis()->SetTitle("pipi");
dt->Draw("SURF1");
dt->GetXaxis()->SetTitle("pipi");
dt->GetYaxis()->SetTitle("pipi");
c->SaveAs("plot_2D_"+tStrResID+"_"+label+".C");
}
diff --git a/src/LauTimeDepFitModel.cc b/src/LauTimeDepFitModel.cc
index b8f13ee..133d47c 100644
--- a/src/LauTimeDepFitModel.cc
+++ b/src/LauTimeDepFitModel.cc
@@ -1,3296 +1,3294 @@
/*
Copyright 2006 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauTimeDepFitModel.cc
\brief File containing implementation of LauTimeDepFitModel class.
*/
#include "LauTimeDepFitModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauAsymmCalc.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDPPartialIntegralInfo.hh"
#include "LauDaughters.hh"
#include "LauDecayTimePdf.hh"
#include "LauFitNtuple.hh"
#include "LauFlavTag.hh"
#include "LauGenNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauParamFixed.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TFile.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
ClassImp(LauTimeDepFitModel)
LauTimeDepFitModel::LauTimeDepFitModel(LauIsobarDynamics* modelB0bar, LauIsobarDynamics* modelB0, LauFlavTag* flavTag) : LauAbsFitModel(),
sigModelB0bar_(modelB0bar),
sigModelB0_(modelB0),
kinematicsB0bar_(modelB0bar ? modelB0bar->getKinematics() : 0),
kinematicsB0_(modelB0 ? modelB0->getKinematics() : 0),
usingBkgnd_(kFALSE),
flavTag_(flavTag),
curEvtTrueTagFlv_(LauFlavTag::Flavour::Unknown),
curEvtDecayFlv_(LauFlavTag::Flavour::Unknown),
nSigComp_(0),
nSigDPPar_(0),
nDecayTimePar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
nCalibPar_(0),
nTagEffPar_(0),
nEffiPar_(0),
nAsymPar_(0),
coeffsB0bar_(0),
coeffsB0_(0),
coeffPars_(0),
fitFracB0bar_(0),
fitFracB0_(0),
fitFracAsymm_(0),
acp_(0),
meanEffB0bar_("meanEffB0bar",0.0,0.0,1.0),
meanEffB0_("meanEffB0",0.0,0.0,1.0),
DPRateB0bar_("DPRateB0bar",0.0,0.0,100.0),
DPRateB0_("DPRateB0",0.0,0.0,100.0),
signalEvents_(0),
signalAsym_(0),
cpevVarName_(""),
cpEigenValue_(CPEven),
evtCPEigenVals_(0),
deltaM_("deltaM",0.0),
deltaGamma_("deltaGamma",0.0),
tau_("tau",LauConstants::tauB0),
phiMix_("phiMix", 2.0*LauConstants::beta, -LauConstants::threePi, LauConstants::threePi, kFALSE),
sinPhiMix_("sinPhiMix", TMath::Sin(2.0*LauConstants::beta), -2.0, 2.0, kFALSE),
cosPhiMix_("cosPhiMix", TMath::Cos(2.0*LauConstants::beta), -2.0, 2.0, kFALSE),
useSinCos_(kFALSE),
phiMixComplex_(TMath::Cos(-2.0*LauConstants::beta),TMath::Sin(-2.0*LauConstants::beta)),
signalDecayTimePdf_(),
BkgndTypes_(flavTag_->getBkgndTypes()),
BkgndDecayTimePdfs_(),
curEvtDecayTime_(0.0),
curEvtDecayTimeErr_(0.0),
sigExtraPdf_(),
AProd_("AProd",0.0,-1.0,1.0,kTRUE),
iterationsMax_(100000000),
nGenLoop_(0),
ASq_(0.0),
aSqMaxVar_(0.0),
aSqMaxSet_(1.25),
storeGenAmpInfo_(kFALSE),
signalTree_(),
reuseSignal_(kFALSE),
sigDPLike_(0.0),
sigExtraLike_(0.0),
sigTotalLike_(0.0)
{
// Set up ftag here?
this->setBkgndClassNames(flavTag_->getBkgndNames());
const std::size_t nBkgnds { this->nBkgndClasses() };
if ( nBkgnds > 0 ){
usingBkgnd_ = kTRUE;
for ( std::size_t iBkgnd{0}; iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass { this->bkgndClassName( iBkgnd ) };
AProdBkgnd_[iBkgnd] = new LauParameter("AProd_"+bkgndClass,0.0,-1.0,1.0,kTRUE);
}
}
// Make sure that the integration scheme will be symmetrised
sigModelB0bar_->forceSymmetriseIntegration(kTRUE);
sigModelB0_->forceSymmetriseIntegration(kTRUE);
}
LauTimeDepFitModel::~LauTimeDepFitModel()
{
for ( LauAbsPdf* pdf : sigExtraPdf_ ) {
delete pdf;
}
for(auto& data : bkgndTree_){
delete data;
}
}
void LauTimeDepFitModel::setupBkgndVectors()
{
UInt_t nBkgnds { this->nBkgndClasses() };
AProdBkgnd_.resize( nBkgnds );
BkgndDPModelsB_.resize( nBkgnds );
BkgndDPModelsBbar_.resize( nBkgnds );
BkgndDecayTimePdfs_.resize( nBkgnds );
BkgndPdfs_.resize( nBkgnds );
bkgndEvents_.resize( nBkgnds );
bkgndAsym_.resize( nBkgnds );
bkgndTree_.resize( nBkgnds );
reuseBkgnd_.resize( nBkgnds );
bkgndDPLike_.resize( nBkgnds );
bkgndExtraLike_.resize( nBkgnds );
bkgndTotalLike_.resize( nBkgnds );
}
void LauTimeDepFitModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : The LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_ = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauTimeDepFitModel::setNSigEvents(LauParameter* nSigEvents, LauParameter* sigAsym)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : The event LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( sigAsym == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : The asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
signalAsym_ = sigAsym;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_->name(asymName);
}
void LauTimeDepFitModel::setNBkgndEvents(LauAbsRValue* nBkgndEvents)
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBgkndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
if ( bkgndAsym_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : You are trying to overwrite the background asymmetry." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
bkgndAsym_[bkgndID] = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauTimeDepFitModel::setNBkgndEvents(LauAbsRValue* nBkgndEvents, LauAbsRValue* bkgndAsym)
{
if ( nBkgndEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : The background yield LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( bkgndAsym == nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : The background asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
TString yieldName = nBkgndEvents->name();
TString bkgndClassName = yieldName;
if ( bkgndClassName.Contains("Events") ) {
bkgndClassName.Remove( bkgndClassName.Index("Events") );
} else {
yieldName += "Events";
nBkgndEvents->name( yieldName );
}
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
bkgndAsym->name( asymName );
if ( ! this->validBkgndClass( bkgndClassName ) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : Invalid background class \"" << bkgndClassName << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
UInt_t bkgndID = this->bkgndClassID( bkgndClassName );
if ( bkgndEvents_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : You are trying to overwrite the background yield." << std::endl;
return;
}
if ( bkgndAsym_[bkgndID] != nullptr ) {
std::cerr << "ERROR in LauTimeDepFitModel::setNBkgndEvents : You are trying to overwrite the background asymmetry." << std::endl;
return;
}
bkgndEvents_[bkgndID] = nBkgndEvents;
bkgndAsym_[bkgndID] = bkgndAsym;
}
void LauTimeDepFitModel::setSignalDtPdf(LauDecayTimePdf* pdf)
{
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepFitModel::setSignalDtPdf : The PDF pointer is null, not adding it."<<std::endl;
return;
}
signalDecayTimePdf_ = pdf;
}
void LauTimeDepFitModel::setBkgndDtPdf(const TString& bkgndClass, LauDecayTimePdf* pdf)
{
// TODO If these are all histograms shouldn't need to add much more code in other functions
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepFitModel::setBkgndDtPdf : The PDF pointer is null, not adding it."<<std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndDtPdf : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
BkgndDecayTimePdfs_[bkgndID] = pdf;
usingBkgnd_ = kTRUE;
}
void LauTimeDepFitModel::setBkgndDPModels(const TString& bkgndClass, LauAbsBkgndDPModel* BModel, LauAbsBkgndDPModel* BbarModel)
{
if (BModel==nullptr) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndDPModels : the model pointer is null for the particle model." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndDPModels : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
BkgndDPModelsB_[bkgndID] = BModel;
if (BbarModel==nullptr) {
std::cout << "INFO in LauTimeDepFitModel::setBkgndDPModels : the model pointer is null for the anti-particle model. Using only the particle model." << std::endl;
BkgndDPModelsBbar_[bkgndID] = nullptr;
} else {
BkgndDPModelsBbar_[bkgndID] = BbarModel;
}
usingBkgnd_ = kTRUE;
}
void LauTimeDepFitModel::setSignalPdfs(LauAbsPdf* pdf)
{
// These "extra variables" are assumed to be purely kinematical, like mES and DeltaE
//or making use of Rest of Event information, and therefore independent of whether
//the parent is a B0 or a B0bar. If this assupmtion doesn't hold, do modify this part!
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepFitModel::setSignalPdfs : The PDF pointer is null."<<std::endl;
return;
}
sigExtraPdf_.push_back(pdf);
}
void LauTimeDepFitModel::setBkgndPdf(const TString& bkgndClass, LauAbsPdf* pdf)
{
if (pdf==0) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndPdf : PDF pointer is null." << std::endl;
return;
}
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndPdf : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
BkgndPdfs_[bkgndID].push_back(pdf);
usingBkgnd_ = kTRUE;
}
void LauTimeDepFitModel::setPhiMix(const Double_t phiMix, const Bool_t fixPhiMix, const Bool_t useSinCos)
{
phiMix_.value(phiMix); phiMix_.initValue(phiMix); phiMix_.genValue(phiMix); phiMix_.fixed(fixPhiMix);
const Double_t sinPhiMix = TMath::Sin(phiMix);
sinPhiMix_.value(sinPhiMix); sinPhiMix_.initValue(sinPhiMix); sinPhiMix_.genValue(sinPhiMix); sinPhiMix_.fixed(fixPhiMix);
const Double_t cosPhiMix = TMath::Cos(phiMix);
cosPhiMix_.value(cosPhiMix); cosPhiMix_.initValue(cosPhiMix); cosPhiMix_.genValue(cosPhiMix); cosPhiMix_.fixed(fixPhiMix);
useSinCos_ = useSinCos;
phiMixComplex_.setRealPart(cosPhiMix);
phiMixComplex_.setImagPart(-1.0*sinPhiMix);
}
void LauTimeDepFitModel::blindPhiMix(const TString& blindingString, const Double_t width, const Bool_t flipSign)
{
phiMix_.range( -2.0*LauConstants::threePi, 2.0*LauConstants::threePi );
phiMix_.blindParameter( blindingString, width, flipSign );
}
void LauTimeDepFitModel::blindPhiMix(const TString& sinBlindingString, const TString& cosBlindingString, const Double_t width, const Bool_t flipSign)
{
if ( ! useSinCos_ ) {
std::cerr << "WARNING in LauTimeDepFitModel::blindPhiMix : not using sine/cosine parameters, so using the sine blinding string to blind the phase itself." << std::endl;
this->blindPhiMix(sinBlindingString);
return;
}
sinPhiMix_.range( -3.0, 3.0 );
cosPhiMix_.range( -3.0, 3.0 );
sinPhiMix_.blindParameter( sinBlindingString, width, flipSign );
cosPhiMix_.blindParameter( cosBlindingString, width, flipSign );
}
void LauTimeDepFitModel::initialise()
{
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// Initialisation
if (this->useDP() == kTRUE) {
this->initialiseDPModels();
}
// Flavour tagging
//flavTag_->initialise();
// Decay-time PDFs
signalDecayTimePdf_->initialise();
//Initialise for backgrounds if necessary
for (auto& pdf : BkgndDecayTimePdfs_){
pdf->initialise();
}
if (!this->useDP() && sigExtraPdf_.empty()) {
std::cerr<<"ERROR in LauTimeDepFitModel::initialise : Signal model doesn't exist for any variable."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (this->useDP() == kTRUE) {
// Check that we have all the Dalitz-plot models
if ((sigModelB0bar_ == 0) || (sigModelB0_ == 0)) {
std::cerr<<"ERROR in LauTimeDepFitModel::initialise : the pointer to one (particle or anti-particle) of the signal DP models is null."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Next check that, if a given component is being used we've got the
// right number of PDFs for all the variables involved
// TODO - should probably check variable names and so on as well
//UInt_t nsigpdfvars(0);
//for ( LauPdfPList::const_iterator pdf_iter = sigExtraPdf_.begin(); pdf_iter != sigExtraPdf_.end(); ++pdf_iter ) {
// std::vector<TString> varNames = (*pdf_iter)->varNames();
// for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
// if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
// ++nsigpdfvars;
// }
// }
//}
//if (usingBkgnd_) {
// for (LauBkgndPdfsList::const_iterator bgclass_iter = BkgndPdfsB0_.begin(); bgclass_iter != BkgndPdfsB0_.end(); ++bgclass_iter) {
// UInt_t nbkgndpdfvars(0);
// const LauPdfPList& pdfList = (*bgclass_iter);
// for ( LauPdfPList::const_iterator pdf_iter = pdfList.begin(); pdf_iter != pdfList.end(); ++pdf_iter ) {
// std::vector<TString> varNames = (*pdf_iter)->varNames();
// for ( std::vector<TString>::const_iterator var_iter = varNames.begin(); var_iter != varNames.end(); ++var_iter ) {
// if ( (*var_iter) != "m13Sq" && (*var_iter) != "m23Sq" ) {
// ++nbkgndpdfvars;
// }
// }
// }
// if (nbkgndpdfvars != nsigpdfvars) {
// std::cerr << "ERROR in LauTimeDepFitModel::initialise : There are " << nsigpdfvars << " signal PDF variables but " << nbkgndpdfvars << " bkgnd PDF variables." << std::endl;
// gSystem->Exit(EXIT_FAILURE);
// }
// }
//}
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the decay time models
this->setDecayTimeParameters();
// Set the fit parameters for the extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Handle flavour-tagging calibration parameters
this->setCalibParams();
// Add tagging efficiency parameters
this->setTagEffParams();
//Asymmetry terms AProd and in setAsymmetries()?
this->setAsymParams();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nDecayTimePar_ + nExtraPdfPar_ + nNormPar_ + nCalibPar_ + nTagEffPar_ + nEffiPar_ + nAsymPar_)) {
std::cerr<<"ERROR in LauTimeDepFitModel::initialise : Number of fit parameters not of expected size."<<std::endl;
std::cout<< fitVars.size() << " != " << nSigDPPar_ << nDecayTimePar_ << nExtraPdfPar_ << nNormPar_ << nCalibPar_ << nTagEffPar_ << nEffiPar_ << nAsymPar_ << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauTimeDepFitModel::recalculateNormalisation()
{
sigModelB0bar_->recalculateNormalisation();
sigModelB0_->recalculateNormalisation();
sigModelB0bar_->modifyDataTree();
sigModelB0_->modifyDataTree();
this->calcInterferenceTermIntegrals();
}
void LauTimeDepFitModel::initialiseDPModels()
{
if (sigModelB0bar_ == 0) {
std::cerr<<"ERROR in LauTimeDepFitModel::initialiseDPModels : B0bar signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (sigModelB0_ == 0) {
std::cerr<<"ERROR in LauTimeDepFitModel::initialiseDPModels : B0 signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
// Need to check that the number of components we have and that the dynamics has matches up
const UInt_t nAmpB0bar = sigModelB0bar_->getnTotAmp();
const UInt_t nAmpB0 = sigModelB0_->getnTotAmp();
if ( nAmpB0bar != nAmpB0 ) {
std::cerr << "ERROR in LauTimeDepFitModel::initialiseDPModels : Unequal number of signal DP components in the particle and anti-particle models: " << nAmpB0bar << " != " << nAmpB0 << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( nAmpB0bar != nSigComp_ ) {
std::cerr << "ERROR in LauTimeDepFitModel::initialiseDPModels : Number of signal DP components in the model (" << nAmpB0bar << ") not equal to number of coefficients supplied (" << nSigComp_ << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
std::cout<<"INFO in LauTimeDepFitModel::initialiseDPModels : Initialising signal DP model"<<std::endl;
sigModelB0bar_->initialise(coeffsB0bar_);
sigModelB0_->initialise(coeffsB0_);
fifjEffSum_.clear();
fifjEffSum_.resize(nSigComp_);
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
fifjEffSum_[iAmp].resize(nSigComp_);
}
// calculate the integrals of the A*Abar terms
this->calcInterferenceTermIntegrals();
this->calcInterferenceTermNorm();
// Add backgrounds
if (usingBkgnd_ == kTRUE) {
for (auto& model : BkgndDPModelsB_){
model->initialise();
}
for (auto& model : BkgndDPModelsBbar_){
if (model != nullptr) {
model->initialise();
}
}
}
}
void LauTimeDepFitModel::calcInterferenceTermIntegrals()
{
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0bar = sigModelB0bar_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0 = sigModelB0_->getIntegralInfos();
// TODO should check (first time) that they match in terms of number of entries in the vectors and that each entry has the same number of points, ranges, weights etc.
LauComplex A, Abar, fifjEffSumTerm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
fifjEffSum_[iAmp][jAmp].zero();
}
}
const UInt_t nIntegralRegions = integralInfoListB0bar.size();
for ( UInt_t iRegion(0); iRegion < nIntegralRegions; ++iRegion ) {
const LauDPPartialIntegralInfo* integralInfoB0bar = integralInfoListB0bar[iRegion];
const LauDPPartialIntegralInfo* integralInfoB0 = integralInfoListB0[iRegion];
const UInt_t nm13Points = integralInfoB0bar->getnm13Points();
const UInt_t nm23Points = integralInfoB0bar->getnm23Points();
for (UInt_t m13 = 0; m13 < nm13Points; ++m13) {
for (UInt_t m23 = 0; m23 < nm23Points; ++m23) {
const Double_t weight = integralInfoB0bar->getWeight(m13,m23);
const Double_t eff = integralInfoB0bar->getEfficiency(m13,m23);
const Double_t effWeight = eff*weight;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
A = integralInfoB0->getAmplitude(m13, m23, iAmp);
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
Abar = integralInfoB0bar->getAmplitude(m13, m23, jAmp);
fifjEffSumTerm = Abar*A.conj();
fifjEffSumTerm.rescale(effWeight);
fifjEffSum_[iAmp][jAmp] += fifjEffSumTerm;
}
}
}
}
}
}
void LauTimeDepFitModel::calcInterferenceTermNorm()
{
const std::vector<Double_t>& fNormB0bar = sigModelB0bar_->getFNorm();
const std::vector<Double_t>& fNormB0 = sigModelB0_->getFNorm();
LauComplex norm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
LauComplex coeffTerm = coeffsB0bar_[jAmp]*coeffsB0_[iAmp].conj();
coeffTerm *= fifjEffSum_[iAmp][jAmp];
coeffTerm.rescale(fNormB0bar[jAmp] * fNormB0[iAmp]);
norm += coeffTerm;
}
}
norm *= phiMixComplex_;
interTermReNorm_ = 2.0*norm.re();
interTermImNorm_ = 2.0*norm.im();
}
void LauTimeDepFitModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
// Resize the coeffPars vector if not already done
if ( coeffPars_.empty() ) {
const UInt_t nAmpB0bar { sigModelB0bar_->getnTotAmp() };
const UInt_t nAmpB0 { sigModelB0_->getnTotAmp() };
if ( nAmpB0bar != nAmpB0 ) {
std::cerr << "ERROR in LauTimeDepFitModel::setAmpCoeffSet : Unequal number of signal DP components in the B and Bbar models: " << nAmpB0 << " != " << nAmpB0bar << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
coeffPars_.resize( nAmpB0 );
fitFracAsymm_.resize( nAmpB0 );
acp_.resize( nAmpB0 );
}
// Is there a component called compName in the signal models?
TString compName { coeffSet->name() };
TString conjName { sigModelB0bar_->getConjResName(compName) };
const LauDaughters* daughtersB0bar { sigModelB0bar_->getDaughters() };
const LauDaughters* daughtersB0 { sigModelB0_->getDaughters() };
const Bool_t conjugate { daughtersB0bar->isConjugate( daughtersB0 ) };
if ( ! sigModelB0bar_->hasResonance(compName) ) {
if ( ! sigModelB0bar_->hasResonance(conjName) ) {
std::cerr<<"ERROR in LauTimeDepFitModel::setAmpCoeffSet : B0bar signal DP model doesn't contain component \""<<compName<<"\"."<<std::endl;
return;
}
std::cerr<<"WARNING in LauTimeDepFitModel::setAmpCoeffSet : B0bar signal DP model doesn't contain component \""<<compName<<"\" but does contain the conjugate \""<<conjName<<"\", resetting name to use the conjugate."<<std::endl;
std::swap( compName, conjName );
coeffSet->name( compName );
}
Int_t indexB0bar { sigModelB0bar_->resonanceIndex(compName) };
Int_t indexB0 { -1 };
if ( conjugate ) {
if ( ! sigModelB0_->hasResonance(conjName) ) {
std::cerr<<"ERROR in LauTimeDepFitModel::setAmpCoeffSet : B0 signal DP model doesn't contain component \""<<conjName<<"\"."<<std::endl;
return;
}
indexB0 = sigModelB0_->resonanceIndex(conjName);
} else {
if ( ! sigModelB0_->hasResonance(compName) ) {
std::cerr<<"ERROR in LauTimeDepFitModel::setAmpCoeffSet : B0 signal DP model doesn't contain component \""<<compName<<"\"."<<std::endl;
return;
}
indexB0 = sigModelB0_->resonanceIndex(compName);
}
if ( indexB0 != indexB0bar ) {
std::cerr << "ERROR in LauTimeDepFitModel::setAmpCoeffSet : B0 signal DP model and B0bar signal DP model have different indices for component \"" << compName << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
if ( coeffPars_[indexB0] != nullptr && coeffPars_[indexB0]->name() == compName) {
std::cerr<<"ERROR in LauTimeDepFitModel::setAmpCoeffSet : Have already set coefficients for \""<<compName<<"\"."<<std::endl;
return;
}
coeffSet->index(indexB0);
std::cout<<"INFO in LauTimeDepFitModel::setAmpCoeffSet : Added coefficients for component \""<<compName<<"\" to the fit model."<<std::endl;
coeffSet->printParValues();
const TString parName { coeffSet->baseName() + "FitFracAsym" };
fitFracAsymm_[indexB0] = LauParameter{parName, 0.0, -1.0, 1.0};
acp_[indexB0] = coeffSet->acp();
coeffPars_[indexB0] = std::move(coeffSet);
++nSigComp_;
}
std::vector<const LauAbsCoeffSet*> LauTimeDepFitModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_.size(), nullptr );
std::transform( coeffPars_.begin(), coeffPars_.end(), coeffs.begin(), [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); } );
return coeffs;
}
void LauTimeDepFitModel::calcAsymmetries(Bool_t initValues)
{
// Calculate the CP asymmetries
// Also calculate the fit fraction asymmetries
for (UInt_t i = 0; i < nSigComp_; i++) {
acp_[i] = coeffPars_[i]->acp();
LauAsymmCalc asymmCalc(fitFracB0bar_[i][i].value(), fitFracB0_[i][i].value());
Double_t asym = asymmCalc.getAsymmetry();
fitFracAsymm_[i].value(asym);
if (initValues) {
fitFracAsymm_[i].genValue(asym);
fitFracAsymm_[i].initValue(asym);
}
}
}
void LauTimeDepFitModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauTimeDepFitModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; ++i) {
LauParameterPList pars = coeffPars_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit variables and in a separate vector
// Need to make sure that they are unique because some might appear in both DP models
LauParameterPList& sigDPParsB0bar = sigModelB0bar_->getFloatingParameters();
LauParameterPList& sigDPParsB0 = sigModelB0_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0bar );
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0 );
}
UInt_t LauTimeDepFitModel::addFitParameters(LauDecayTimePdf* decayTimePdf)
{
return this->addFitParameters( decayTimePdf->getParameters());
}
UInt_t LauTimeDepFitModel::addFitParameters(std::vector<LauDecayTimePdf*>& decayTimePdfList)
{
UInt_t nParsAdded{0};
for ( auto decayTimePdf : decayTimePdfList ) {
nParsAdded += this->addFitParameters( decayTimePdf );
}
return nParsAdded;
}
void LauTimeDepFitModel::setDecayTimeParameters()
{
nDecayTimePar_ = 0;
std::cout << "INFO in LauTimeDepFitModel::setDecayTimeParameters : Setting the initial fit parameters of the DecayTime Pdfs." << std::endl;
// Loop over the Dt PDFs
nDecayTimePar_ += this->addFitParameters( signalDecayTimePdf_ );
if (usingBkgnd_){
nDecayTimePar_ += this->addFitParameters(BkgndDecayTimePdfs_);
}
if (useSinCos_) {
nDecayTimePar_ += this->addFitParameters( &sinPhiMix_ );
nDecayTimePar_ += this->addFitParameters( &cosPhiMix_ );
} else {
nDecayTimePar_ += this->addFitParameters( &phiMix_ );
}
}
void LauTimeDepFitModel::setExtraPdfParameters()
{
// Include the parameters of the PDF for each tagging category in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
std::cout << "INFO in LauTimeDepFitModel::setExtraPdfParameters : Setting the initial fit parameters of the extra Pdfs." << std::endl;
nExtraPdfPar_ += this->addFitParameters(sigExtraPdf_);
if (usingBkgnd_ == kTRUE) {
for (auto& pdf : BkgndPdfs_){
nExtraPdfPar_ += this->addFitParameters(pdf);
}
}
}
void LauTimeDepFitModel::setFitNEvents()
{
nNormPar_ = 0;
std::cout << "INFO in LauTimeDepFitModel::setFitNEvents : Setting the initial fit parameters of the signal and background yields." << std::endl;
// Initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the signal fraction into the fit parameters
if (this->doEMLFit()) {
std::cout<<"INFO in LauTimeDepFitModel::setFitNEvents : Initialising number of events for signal and background components..."<<std::endl;
nNormPar_ += this->addFitParameters( signalEvents_, sendFixedYieldsToFitter_ );
} else {
std::cout<<"INFO in LauTimeDepFitModel::setFitNEvents : Initialising number of events for background components (and hence signal)..."<<std::endl;
}
// if not using the DP in the model we need an explicit signal asymmetry parameter
if (this->useDP() == kFALSE) {
nNormPar_ += this->addFitParameters( signalAsym_ );
}
// TODO arguably should delegate this
//LauTagCatParamMap& signalTagCatFrac = flavTag_->getSignalTagCatFrac();
// tagging-category fractions for signal events
//for (LauTagCatParamMap::iterator iter = signalTagCatFrac.begin(); iter != signalTagCatFrac.end(); ++iter) {
// if (iter == signalTagCatFrac.begin()) {
// continue;
// }
// LauParameter* par = &((*iter).second);
// fitVars.push_back(par);
// ++nNormPar_;
//}
// Backgrounds
if (usingBkgnd_ == kTRUE) {
nNormPar_ += this->addFitParameters( bkgndEvents_, sendFixedYieldsToFitter_ );
nNormPar_ += this->addFitParameters( bkgndAsym_ );
}
}
void LauTimeDepFitModel::setAsymParams()
{
nAsymPar_ = 0;
//Signal
nAsymPar_ += this->addFitParameters( &AProd_, kTRUE );
//Background(s)
nAsymPar_ += this->addFitParameters( AProdBkgnd_ );
}
void LauTimeDepFitModel::setTagEffParams()
{
nTagEffPar_ = 0;
Bool_t useAltPars = flavTag_->getUseAveDelta();
std::cout << "INFO in LauTimeDepFitModel::setTagEffParams : Setting the initial fit parameters for flavour tagging efficiencies." << std::endl;
if (useAltPars){
std::vector<LauParameter*> tageff_ave = flavTag_->getTagEffAve();
std::vector<LauParameter*> tageff_delta = flavTag_->getTagEffDelta();
nTagEffPar_ += this->addFitParameters( tageff_ave );
nTagEffPar_ += this->addFitParameters( tageff_delta );
} else {
std::vector<LauParameter*> tageff_b0 = flavTag_->getTagEffB0();
std::vector<LauParameter*> tageff_b0bar = flavTag_->getTagEffB0bar();
nTagEffPar_ += this->addFitParameters( tageff_b0 );
nTagEffPar_ += this->addFitParameters( tageff_b0bar );
}
if (usingBkgnd_){
if (useAltPars){
auto tageff_ave = flavTag_->getTagEffBkgndAve();
auto tageff_delta = flavTag_->getTagEffBkgndDelta();
for(auto& innerVec : tageff_ave){
nTagEffPar_ += this->addFitParameters( innerVec );
}
for(auto& innerVec : tageff_delta){
nTagEffPar_ += this->addFitParameters( innerVec );
}
} else {
auto tageff_b0 = flavTag_->getTagEffBkgndB0();
auto tageff_b0bar = flavTag_->getTagEffBkgndB0bar();
for(auto& innerVec : tageff_b0){
nTagEffPar_ += this->addFitParameters( innerVec );
}
for(auto& innerVec : tageff_b0bar){
nTagEffPar_ += this->addFitParameters( innerVec );
}
}
}
}
void LauTimeDepFitModel::setCalibParams()
{
nCalibPar_ = 0;
Bool_t useAltPars = flavTag_->getUseAveDelta();
std::cout << "INFO in LauTimeDepFitModel::setCalibParams : Setting the initial fit parameters of the flavour tagging calibration parameters." << std::endl;
if (useAltPars){
std::vector<LauParameter*> p0pars_ave = flavTag_->getCalibP0Ave();
std::vector<LauParameter*> p0pars_delta = flavTag_->getCalibP0Delta();
std::vector<LauParameter*> p1pars_ave = flavTag_->getCalibP1Ave();
std::vector<LauParameter*> p1pars_delta = flavTag_->getCalibP1Delta();
nCalibPar_ += this->addFitParameters( p0pars_ave );
nCalibPar_ += this->addFitParameters( p0pars_delta );
nCalibPar_ += this->addFitParameters( p1pars_ave );
nCalibPar_ += this->addFitParameters( p1pars_delta );
} else {
std::vector<LauParameter*> p0pars_b0 = flavTag_->getCalibP0B0();
std::vector<LauParameter*> p0pars_b0bar = flavTag_->getCalibP0B0bar();
std::vector<LauParameter*> p1pars_b0 = flavTag_->getCalibP1B0();
std::vector<LauParameter*> p1pars_b0bar = flavTag_->getCalibP1B0bar();
nCalibPar_ += this->addFitParameters( p0pars_b0 );
nCalibPar_ += this->addFitParameters( p0pars_b0bar );
nCalibPar_ += this->addFitParameters( p1pars_b0 );
nCalibPar_ += this->addFitParameters( p1pars_b0bar );
}
}
void LauTimeDepFitModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the B0 and B0bar fit fractions for each signal component
fitFracB0bar_ = sigModelB0bar_->getFitFractions();
if (fitFracB0bar_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0bar_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0bar" );
fitFracB0bar_[i][j].name(name);
extraVars.push_back(fitFracB0bar_[i][j]);
}
}
fitFracB0_ = sigModelB0_->getFitFractions();
if (fitFracB0_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0" );
fitFracB0_[i][j].name(name);
extraVars.push_back(fitFracB0_[i][j]);
}
}
// Calculate the ACPs and FitFrac asymmetries
this->calcAsymmetries(kTRUE);
// Add the Fit Fraction asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(fitFracAsymm_[i]);
}
// Add the calculated CP asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(acp_[i]);
}
// Now add in the DP efficiency values
Double_t initMeanEffB0bar = sigModelB0bar_->getMeanEff().initValue();
meanEffB0bar_.value(initMeanEffB0bar); meanEffB0bar_.initValue(initMeanEffB0bar); meanEffB0bar_.genValue(initMeanEffB0bar);
extraVars.push_back(meanEffB0bar_);
Double_t initMeanEffB0 = sigModelB0_->getMeanEff().initValue();
meanEffB0_.value(initMeanEffB0); meanEffB0_.initValue(initMeanEffB0); meanEffB0_.genValue(initMeanEffB0);
extraVars.push_back(meanEffB0_);
// Also add in the DP rates
Double_t initDPRateB0bar = sigModelB0bar_->getDPRate().initValue();
DPRateB0bar_.value(initDPRateB0bar); DPRateB0bar_.initValue(initDPRateB0bar); DPRateB0bar_.genValue(initDPRateB0bar);
extraVars.push_back(DPRateB0bar_);
Double_t initDPRateB0 = sigModelB0_->getDPRate().initValue();
DPRateB0_.value(initDPRateB0); DPRateB0_.initValue(initDPRateB0); DPRateB0_.genValue(initDPRateB0);
extraVars.push_back(DPRateB0_);
}
void LauTimeDepFitModel::setProductionAsymmetry(const Double_t AProd, const Bool_t AProdFix, const TString& suffix){
AProd_.value(AProd);
AProd_.fixed(AProdFix);
if ( suffix != "" ) {
AProd_.name( AProd_.name() + "_" + suffix );
}
}
void LauTimeDepFitModel::setBkgndAsymmetries(const TString& bkgndClass, const Double_t AProd, const Bool_t AProdFix){
// check that this background name is valid
if ( ! this->validBkgndClass( bkgndClass) ) {
std::cerr << "ERROR in LauTimeDepFitModel::setBkgndAsymmetries : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
AProdBkgnd_[bkgndID]->value( AProd );
AProdBkgnd_[bkgndID]->genValue( AProd );
AProdBkgnd_[bkgndID]->initValue( AProd );
AProdBkgnd_[bkgndID]->fixed( AProdFix );
}
void LauTimeDepFitModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// i.e. to make mag > 0.0, phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; ++i) {
// Check whether we have "a > 0.0", and phases in the right range
coeffPars_[i]->finaliseValues();
}
}
// update the pulls on the event fractions and asymmetries
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (this->useDP() == kFALSE) {
signalAsym_->updatePull();
}
// Finalise the pulls on the decay time parameters
signalDecayTimePdf_->updatePulls();
// and for backgrounds if required
if (usingBkgnd_){
for (auto& pdf : BkgndDecayTimePdfs_){
pdf->updatePulls();
}
}
// Finalise the pulls on the flavour tagging parameters
flavTag_->updatePulls();
if (useSinCos_) {
if ( not sinPhiMix_.fixed() ) {
sinPhiMix_.updatePull();
cosPhiMix_.updatePull();
}
} else {
this->checkMixingPhase();
}
if (usingBkgnd_ == kTRUE) {
for (auto& params : bkgndEvents_){
std::vector<LauParameter*> parameters = params->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
for (auto& params : bkgndAsym_){
std::vector<LauParameter*> parameters = params->getPars();
for ( LauParameter* parameter : parameters ) {
parameter->updatePull();
}
}
}
// Update the pulls on all the extra PDFs' parameters
this->updateFitParameters(sigExtraPdf_);
if (usingBkgnd_ == kTRUE) {
for (auto& pdf : BkgndPdfs_){
this->updateFitParameters(pdf);
}
}
// Fill the fit results to the ntuple
// update the coefficients and then calculate the fit fractions and ACP's
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_->updateCoeffs(coeffsB0bar_); sigModelB0bar_->calcExtraInfo();
sigModelB0_->updateCoeffs(coeffsB0_); sigModelB0_->calcExtraInfo();
LauParArray fitFracB0bar = sigModelB0bar_->getFitFractions();
if (fitFracB0bar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0 = sigModelB0_->getFitFractions();
if (fitFracB0.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_[i][j].value(fitFracB0bar[i][j].value());
fitFracB0_[i][j].value(fitFracB0[i][j].value());
}
}
meanEffB0bar_.value(sigModelB0bar_->getMeanEff().value());
meanEffB0_.value(sigModelB0_->getMeanEff().value());
DPRateB0bar_.value(sigModelB0bar_->getDPRate().value());
DPRateB0_.value(sigModelB0_->getDPRate().value());
this->calcAsymmetries();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions, FF asymmetries, ACPs, mean efficiency and DP rate)
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0bar_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(fitFracAsymm_[i]);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(acp_[i]);
}
extraVars.push_back(meanEffB0bar_);
extraVars.push_back(meanEffB0_);
extraVars.push_back(DPRateB0bar_);
extraVars.push_back(DPRateB0_);
this->printFitFractions(std::cout);
this->printAsymmetries(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauTimeDepFitModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
// First for the B0bar events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"B0bar FitFraction for component "<<i<<" ("<<compName<<") = "<<fitFracB0bar_[i][i].value()<<std::endl;
}
output<<"B0bar overall DP rate (integral of matrix element squared) = "<<DPRateB0bar_.value()<<std::endl;
output<<"B0bar average efficiency weighted by whole DP dynamics = "<<meanEffB0bar_.value()<<std::endl;
// Then for the B0 sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
const TString conjName(sigModelB0bar_->getConjResName(compName));
output<<"B0 FitFraction for component "<<i<<" ("<<conjName<<") = "<<fitFracB0_[i][i].value()<<std::endl;
}
output<<"B0 overall DP rate (integral of matrix element squared) = "<<DPRateB0_.value()<<std::endl;
output<<"B0 average efficiency weighted by whole DP dynamics = "<<meanEffB0_.value()<<std::endl;
}
void LauTimeDepFitModel::printAsymmetries(std::ostream& output)
{
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"Fit Fraction asymmetry for component "<<i<<" ("<<compName<<") = "<<fitFracAsymm_[i].value()<<std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"ACP for component "<<i<<" ("<<compName<<") = "<<acp_[i].value()<<" +- "<<acp_[i].error()<<std::endl;
}
}
void LauTimeDepFitModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
std::ofstream fout(outputFile);
LauPrint print;
std::cout<<"INFO in LauTimeDepFitModel::writeOutTable : Writing out results of the fit to the tex file "<<outputFile<<std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->printTableRow(fout);
}
fout<<"\\hline"<<std::endl;
fout<<"\\end{tabular}"<<std::endl<<std::endl;
// print the fit fractions in another
fout<<"\\begin{tabular}{|l|c|c|c|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"Component & \\Bzb\\ Fit Fraction & \\Bz\\ Fit Fraction & Fit Fraction Asymmetry & $A_{\\CP}$ \\\\"<<std::endl;
fout<<"\\hline"<<std::endl;
Double_t fitFracSumB0bar(0.0);
Double_t fitFracSumB0(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
fout<<resName<<" & $";
Double_t fitFracB0bar = fitFracB0bar_[i][i].value();
fitFracSumB0bar += fitFracB0bar;
print.printFormat(fout, fitFracB0bar);
fout << "$ & $" << std::endl;
Double_t fitFracB0 = fitFracB0_[i][i].value();
fitFracSumB0 += fitFracB0;
print.printFormat(fout, fitFracB0);
fout << "$ & $" << std::endl;
Double_t fitFracAsymm = fitFracAsymm_[i].value();
print.printFormat(fout, fitFracAsymm);
fout << "$ & $" << std::endl;
Double_t acp = acp_[i].value();
Double_t acpErr = acp_[i].error();
print.printFormat(fout, acp);
fout<<" \\pm ";
print.printFormat(fout, acpErr);
fout<<"$ \\\\"<<std::endl;
}
fout<<"\\hline"<<std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum = & $";
print.printFormat(fout, fitFracSumB0bar);
fout << "$ & $";
print.printFormat(fout, fitFracSumB0);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline \n\\hline" << std::endl;
fout << "DP rate = & $";
print.printFormat(fout, DPRateB0bar_.value());
fout << "$ & $";
print.printFormat(fout, DPRateB0_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > =$ & $";
print.printFormat(fout, meanEffB0bar_.value());
fout << "$ & $";
print.printFormat(fout, meanEffB0_.value());
fout << "$ & & \\\\" << std::endl;
if (useSinCos_) {
fout << "$\\sinPhiMix =$ & $";
print.printFormat(fout, sinPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, sinPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
fout << "$\\cosPhiMix =$ & $";
print.printFormat(fout, cosPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, cosPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
} else {
fout << "$\\phiMix =$ & $";
print.printFormat(fout, phiMix_.value());
fout << " \\pm ";
print.printFormat(fout, phiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
}
fout << "\\hline \n\\end{tabular}" << std::endl;
}
if (!sigExtraPdf_.empty()) {
fout<<"\\begin{tabular}{|l|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"\\Extra Signal PDFs' Parameters: & \\\\"<<std::endl;
this->printFitParameters(sigExtraPdf_, fout);
if (usingBkgnd_ == kTRUE && !BkgndPdfs_.empty()) {
fout << "\\hline" << std::endl;
fout << "\\Extra Background PDFs' Parameters: & \\\\" << std::endl;
for (auto& pdf : BkgndPdfs_){
this->printFitParameters(pdf, fout);
}
}
fout<<"\\hline \n\\end{tabular}"<<std::endl<<std::endl;
}
}
void LauTimeDepFitModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
this->randomiseInitFitPars();
}
}
void LauTimeDepFitModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout<<"INFO in LauTimeDepFitModel::randomiseInitFitPars : Randomising the initial values of the coefficients of the DP components (and phiMix)..."<<std::endl;
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
phiMix_.randomiseValue(-LauConstants::pi, LauConstants::pi);
if (useSinCos_) {
sinPhiMix_.initValue(TMath::Sin(phiMix_.initValue()));
cosPhiMix_.initValue(TMath::Cos(phiMix_.initValue()));
}
}
LauTimeDepFitModel::LauGenInfo LauTimeDepFitModel::eventsToGenerate()
{
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
// NB this individual smearing has to be done individually per tagging category as well
LauGenInfo nEvtsGen;
// Signal
// If we're including the DP and decay time we can't decide on the tag
// yet, since it depends on the whole DP+dt PDF, however, if
// we're not then we need to decide.
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0.0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
//TOD sigAysm doesn't do anything here?
Double_t sigAsym(0.0);
if (this->useDP() == kFALSE) {
sigAsym = signalAsym_->genValue();
//TODO fill in here if we care
} else {
Double_t rateB0bar = sigModelB0bar_->getDPRate().value();
Double_t rateB0 = sigModelB0_->getDPRate().value();
if ( rateB0bar+rateB0 > 1e-30) {
sigAsym = (rateB0bar-rateB0)/(rateB0bar+rateB0);
}
//for (LauTagCatParamMap::const_iterator iter = signalTagCatFrac.begin(); iter != signalTagCatFrac.end(); ++iter) {
// const LauParameter& par = iter->second;
// Double_t eventsbyTagCat = par.value() * nEvts;
// if (this->doPoissonSmearing()) {
// eventsbyTagCat = LauRandom::randomFun()->Poisson(eventsbyTagCat);
// }
// eventsB0[iter->first] = std::make_pair( TMath::Nint(eventsbyTagCat), evtWeight );
//}
//nEvtsGen[std::make_pair("signal",0)] = eventsB0; // generate signal event, decide tag later.
if (this->doPoissonSmearing()) {
nEvts = LauRandom::randomFun()->Poisson(signalEvents_->genValue());
}
nEvtsGen["signal"] = std::make_pair( nEvts, evtWeight );
}
std::cout<<"INFO in LauTimeDepFitModel::eventsToGenerate : Generating toy MC with:"<<std::endl;
std::cout<<" : Signal asymmetry = "<<sigAsym<<" and number of signal events = "<<signalEvents_->genValue()<<std::endl;
//TODO backgrounds
if (usingBkgnd_){
for ( UInt_t bkgndID(0); bkgndID < this->nBkgndClasses(); ++bkgndID ) {
if (this->doPoissonSmearing()) {
nEvtsGen[this->bkgndClassName(bkgndID)] = std::make_pair( LauRandom::randomFun()->Poisson(bkgndEvents_[bkgndID]->genValue()), evtWeight);
} else {
nEvtsGen[this->bkgndClassName(bkgndID)] = std::make_pair( bkgndEvents_[bkgndID]->genValue(), evtWeight);
}
std::cout<<" : Number of "<<this->bkgndClassName(bkgndID)<<" background events = "<<bkgndEvents_[bkgndID]->genValue()<<std::endl;
}
}
return nEvtsGen;
}
Bool_t LauTimeDepFitModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
LauGenInfo nEvts = this->eventsToGenerate();
Bool_t genOK(kTRUE);
Int_t evtNum(0);
const UInt_t nBkgnds = this->nBkgndClasses();
std::vector<TString> bkgndClassNames(nBkgnds);
std::vector<TString> bkgndClassNamesGen(nBkgnds);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
bkgndClassNames[iBkgnd] = name;
bkgndClassNamesGen[iBkgnd] = "gen"+name;
}
// Loop over the hypotheses and generate the appropriate number of
// events for each one
for (auto& hypo : nEvts){
// find the category of events (e.g. signal)
const TString& evtCategory(hypo.first);
// Type
const TString& type(hypo.first);
// Number of events
Int_t nEvtsGen( hypo.second.first );
// get the event weight for this category
const Double_t evtWeight( hypo.second.second );
auto t1 = std::chrono::high_resolution_clock::now();
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
if (evtCategory == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], 0 );
}
// All the generate*Event() methods have to fill in curEvtDecayTime_ and curEvtDecayTimeErr_
// In addition, generateSignalEvent has to decide on the tag and fill in curEvtTagFlv_
genOK = this->generateSignalEvent();
} else {
this->setGenNtupleIntegerBranchValue("genSig",0);
UInt_t bkgndID(0);
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
Int_t gen(0);
if ( bkgndClassNames[iBkgnd] == type ) {
gen = 1;
bkgndID = iBkgnd;
}
this->setGenNtupleIntegerBranchValue( bkgndClassNamesGen[iBkgnd], gen );
}
genOK = this->generateBkgndEvent(bkgndID);
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPDtBranchValues(); // store DP, decay time and tagging variables in the ntuple
}
// Store the event's tag and tagging category
this->setGenNtupleIntegerBranchValue("cpEigenvalue", cpEigenValue_);
const TString& trueTagVarName { flavTag_->getTrueTagVarName() };
if ( trueTagVarName != "" ) {
this->setGenNtupleIntegerBranchValue(trueTagVarName, curEvtTrueTagFlv_);
}
if ( cpEigenValue_ == QFS ) {
const TString& decayFlvVarName { flavTag_->getDecayFlvVarName() };
if ( decayFlvVarName == "" ) {
std::cerr<<"ERROR in LauTimeDepFitModel::genExpt : Decay flavour variable not set for QFS decay, see LauFlavTag::setDecayFlvVarName()."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
} else {
this->setGenNtupleIntegerBranchValue(decayFlvVarName, curEvtDecayFlv_);
}
}
const std::vector<TString>& tagVarNames { flavTag_->getTagVarNames() };
const std::vector<TString>& mistagVarNames { flavTag_->getMistagVarNames() };
// Loop over the taggers - values set via generateSignalEvent
const std::size_t nTaggers {flavTag_->getNTaggers()};
for (std::size_t i=0; i<nTaggers; ++i){
this->setGenNtupleIntegerBranchValue(tagVarNames[i], curEvtTagFlv_[i]);
this->setGenNtupleDoubleBranchValue(mistagVarNames[i], curEvtMistag_[i]);
}
// Store the event number (within this experiment)
// and then increment it
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
++evtNum;
// Write the values into the tree
this->fillGenNtupleBranches();
// Print an occasional progress message
if (iEvt%1000 == 0) {std::cout<<"INFO in LauTimeDepFitModel::genExpt : Generated event number "<<iEvt<<" out of "<<nEvtsGen<<" "<<evtCategory<<" events."<<std::endl;}
}
auto t2 = std::chrono::high_resolution_clock::now();
if(nEvtsGen != 0)
{
std::chrono::duration<double, std::milli> ms_double = ( t2 - t1 )/nEvtsGen;
std::cout<<"INFO in LauTimeDepFitModel::genExpt : Average per-event generation time for "<<evtCategory<<": "<<ms_double.count()<<" ms"<<std::endl;
}
} //end of loop over species and tagFlv.
if (this->useDP() && genOK) {
sigModelB0bar_->checkToyMC(kTRUE);
sigModelB0_->checkToyMC(kTRUE);
std::cout<<"aSqMaxSet = "<<aSqMaxSet_<<" and aSqMaxVar = "<<aSqMaxVar_<<std::endl;
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray fitFracB0bar = sigModelB0bar_->getFitFractions();
if (fitFracB0bar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0 = sigModelB0_->getFitFractions();
if (fitFracB0.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFitModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_[i][j].value(fitFracB0bar[i][j].value());
fitFracB0_[i][j].value(fitFracB0[i][j].value());
}
}
meanEffB0bar_.value(sigModelB0bar_->getMeanEff().value());
meanEffB0_.value(sigModelB0_->getMeanEff().value());
DPRateB0bar_.value(sigModelB0bar_->getDPRate().value());
DPRateB0_.value(sigModelB0_->getDPRate().value());
}
}
// If we're reusing embedded events or if the generation is being
// reset then clear the lists of used events
if (reuseSignal_ || !genOK) {
if (signalTree_) {
signalTree_->clearUsedList();
}
}
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
LauEmbeddedData* data = bkgndTree_[bkgndID];
if (reuseBkgnd_[bkgndID] || !genOK) {
if (data) {
data->clearUsedList();
}
}
}
return genOK;
}
Bool_t LauTimeDepFitModel::generateSignalEvent()
{
// Generate signal event, including SCF if necessary.
// DP:DecayTime generation follows.
// If it's ok, we then generate mES, DeltaE, Fisher/NN...
Bool_t genOK(kTRUE);
Bool_t generatedEvent(kFALSE);
if (this->useDP()) {
if (signalTree_) {
signalTree_->getEmbeddedEvent(kinematicsB0bar_);
//curEvtTagFlv_ = TMath::Nint(signalTree_->getValue("tagFlv"));
curEvtDecayTimeErr_ = signalTree_->getValue(signalDecayTimePdf_->varErrName());
curEvtDecayTime_ = signalTree_->getValue(signalDecayTimePdf_->varName());
if (signalTree_->haveBranch("mcMatch")) {
Int_t match = TMath::Nint(signalTree_->getValue("mcMatch"));
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
}
}
} else {
nGenLoop_ = 0;
// Now generate from the combined DP / decay-time PDF
while (generatedEvent == kFALSE && nGenLoop_ < iterationsMax_) {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::Unknown;
curEvtDecayFlv_ = LauFlavTag::Flavour::Unknown;
// First choose the true tag, accounting for the production asymmetry
// CONVENTION WARNING regarding meaning of sign of AProd
Double_t random = LauRandom::randomFun()->Rndm();
if (random <= 0.5 * ( 1.0 - AProd_.unblindValue() ) ) {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::Bbar;
}
// Generate the DP position
Double_t m13Sq{0.0}, m23Sq{0.0};
kinematicsB0bar_->genFlatPhaseSpace(m13Sq, m23Sq);
// Next, calculate the total A and Abar for the given DP position
sigModelB0_->calcLikelihoodInfo(m13Sq, m23Sq);
sigModelB0bar_->calcLikelihoodInfo(m13Sq, m23Sq);
// Generate decay time
const Double_t tMin = signalDecayTimePdf_->minAbscissa();
const Double_t tMax = signalDecayTimePdf_->maxAbscissa();
curEvtDecayTime_ = LauRandom::randomFun()->Uniform(tMin,tMax);
// Generate the decay time error (NB the kTRUE forces the generation of a new value)
curEvtDecayTimeErr_ = signalDecayTimePdf_->generateError(kTRUE);
// Calculate all the decay time info
signalDecayTimePdf_->calcLikelihoodInfo(curEvtDecayTime_,curEvtDecayTimeErr_);
// Retrieve the amplitudes and efficiency from the dynamics
const LauComplex& Abar { sigModelB0bar_->getEvtDPAmp() };
const LauComplex& A { sigModelB0_->getEvtDPAmp() };
const Double_t ASq { A.abs2() };
const Double_t AbarSq { Abar.abs2() };
const Double_t dpEff { sigModelB0bar_->getEvtEff() };
// Also retrieve all the decay time terms
const Double_t dtCos { signalDecayTimePdf_->getCosTerm() };
const Double_t dtSin { signalDecayTimePdf_->getSinTerm() };
const Double_t dtCosh { signalDecayTimePdf_->getCoshTerm() };
const Double_t dtSinh { signalDecayTimePdf_->getSinhTerm() };
// and the decay time acceptance
const Double_t dtEff { signalDecayTimePdf_->getEffiTerm() };
if ( cpEigenValue_ == QFS) {
// Calculate the total intensities for each flavour-specific final state
const Double_t ATotSq { ( ASq * dtCosh + curEvtTrueTagFlv_ * ASq * dtCos ) * dpEff * dtEff };
const Double_t AbarTotSq { ( AbarSq * dtCosh - curEvtTrueTagFlv_ * AbarSq * dtCos ) * dpEff * dtEff };
const Double_t ASumSq { ATotSq + AbarTotSq };
// Finally we throw the dice to see whether this event should be generated (and, if so, which final state)
const Double_t randNum = LauRandom::randomFun()->Rndm();
if (randNum <= ASumSq / aSqMaxSet_ ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ASumSq > aSqMaxVar_) {aSqMaxVar_ = ASumSq;}
if ( randNum <= ATotSq / aSqMaxSet_ ) {
curEvtDecayFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtDecayFlv_ = LauFlavTag::Flavour::Bbar;
}
// Generate the flavour tagging information from the true tag
// (we do this after accepting the event to save time)
flavTag_->generateEventInfo( curEvtTrueTagFlv_, curEvtDecayTime_ );
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
} else {
nGenLoop_++;
}
} else {
// Calculate the DP terms
const Double_t aSqSum { ASq + AbarSq };
const Double_t aSqDif { ASq - AbarSq };
const LauComplex inter { Abar * A.conj() * phiMixComplex_ };
const Double_t interTermIm { ( cpEigenValue_ == CPEven ) ? 2.0 * inter.im() : -2.0 * inter.im() };
const Double_t interTermRe { ( cpEigenValue_ == CPEven ) ? 2.0 * inter.re() : -2.0 * inter.re() };
// Combine DP and decay-time info for all terms
const Double_t coshTerm { aSqSum * dtCosh };
const Double_t sinhTerm { interTermRe * dtSinh };
const Double_t cosTerm { aSqDif * dtCos };
const Double_t sinTerm { interTermIm * dtSin };
// Sum to obtain the total and multiply by the efficiency
// Multiplying the cos and sin terms by the true flavour at production
const Double_t ATotSq { ( coshTerm + sinhTerm + curEvtTrueTagFlv_ * ( cosTerm - sinTerm ) ) * dpEff * dtEff };
//Finally we throw the dice to see whether this event should be generated
const Double_t randNum = LauRandom::randomFun()->Rndm();
if (randNum <= ATotSq/aSqMaxSet_ ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ATotSq > aSqMaxVar_) {aSqMaxVar_ = ATotSq;}
// Generate the flavour tagging information from the true tag
// (we do this after accepting the event to save time)
flavTag_->generateEventInfo( curEvtTrueTagFlv_, curEvtDecayTime_ );
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
} else {
nGenLoop_++;
}
}
} // end of while !generatedEvent loop
} // end of if (signalTree_) else control
} else {
if ( signalTree_ ) {
signalTree_->getEmbeddedEvent(0);
//curEvtTagFlv_ = TMath::Nint(signalTree_->getValue("tagFlv"));
curEvtDecayTimeErr_ = signalTree_->getValue(signalDecayTimePdf_->varErrName());
curEvtDecayTime_ = signalTree_->getValue(signalDecayTimePdf_->varName());
}
}
// Check whether we have generated the toy MC OK.
if (nGenLoop_ >= iterationsMax_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepFitModel::generateSignalEvent : Hit max iterations: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
} else if (aSqMaxVar_ > aSqMaxSet_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepFitModel::generateSignalEvent : Found a larger ASq value: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
}
if (genOK) {
//Some variables, like Fisher or NN, might use m13Sq and m23Sq from the kinematics
//kinematicsB0bar_ is up to date, update kinematicsB0_
kinematicsB0_->updateKinematics(kinematicsB0bar_->getm13Sq(), kinematicsB0bar_->getm23Sq() );
this->generateExtraPdfValues(sigExtraPdf_, signalTree_);
}
// Check for problems with the embedding
if (signalTree_ && (signalTree_->nEvents() == signalTree_->nUsedEvents())) {
std::cerr<<"WARNING in LauTimeDepFitModel::generateSignalEvent : Source of embedded signal events used up, clearing the list of used events."<<std::endl;
signalTree_->clearUsedList();
}
return genOK;
}
Bool_t LauTimeDepFitModel::generateBkgndEvent(UInt_t bkgndID)
{
// Generate Bkgnd event
Bool_t genOK{kTRUE};
//Check necessary ingredients are in place
//TODO these checks should be part of a general sanity check during the initialisation phase
if (BkgndDPModelsB_[bkgndID] == nullptr){
std::cerr << "ERROR in LauTimeDepFitModel::generateBkgndEvent : Dalitz plot model is missing" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (BkgndDecayTimePdfs_[bkgndID] == nullptr){
std::cerr << "ERROR in LauTimeDepFitModel::generateBkgndEvent : Decay time model is missing" << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
//TODO restore the ability to embed events from an external source
//LauAbsBkgndDPModel* model(0);
//LauEmbeddedData* embeddedData(0);
//LauPdfPList* extraPdfs(0);
//LauKinematics* kinematics(0);
//model = BkgndDPModels_[bkgndID];
//if (this->enableEmbedding()) {
// // find the right embedded data for the current tagging category
// LauTagCatEmbDataMap::const_iterator emb_iter = bkgndTree_[bkgndID].find(curEvtTagCat_);
// embeddedData = (emb_iter != bkgndTree_[bkgndID].end()) ? emb_iter->second : 0;
//}
//extraPdfs = &BkgndPdfs_[bkgndID];
//kinematics = kinematicsB0bar_;
//if (this->useDP()) {
// if (embeddedData) {
// embeddedData->getEmbeddedEvent(kinematics);
// } else {
// if (model == 0) {
// const TString& bkgndClass = this->bkgndClassName(bkgndID);
// std::cerr << "ERROR in LauCPFitModel::generateBkgndEvent : Can't find the DP model for background class \"" << bkgndClass << "\"." << std::endl;
// gSystem->Exit(EXIT_FAILURE);
// }
// genOK = model->generate();
// }
//} else {
// if (embeddedData) {
// embeddedData->getEmbeddedEvent(0);
// }
//}
//if (genOK) {
// this->generateExtraPdfValues(extraPdfs, embeddedData);
//}
//// Check for problems with the embedding
//if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
// const TString& bkgndClass = this->bkgndClassName(bkgndID);
// std::cerr << "WARNING in LauCPFitModel::generateBkgndEvent : Source of embedded " << bkgndClass << " events used up, clearing the list of used events." << std::endl;
// embeddedData->clearUsedList();
//}
//
LauKinematics* kinematics{nullptr};
switch ( BkgndTypes_[bkgndID] ) {
case LauFlavTag::BkgndType::Combinatorial:
{
// First choose the true tag, accounting for the production asymmetry
// CONVENTION WARNING regarding meaning of sign of AProd
// NB the true tag doesn't really mean anything for combinatorial background
Double_t random = LauRandom::randomFun()->Rndm();
if ( random <= 0.5 * ( 1.0 - AProdBkgnd_[bkgndID]->unblindValue() ) ) {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::Bbar;
}
if ( cpEigenValue_ == CPEigenvalue::QFS ) {
if ( BkgndDPModelsBbar_[bkgndID] != nullptr ) {
// generate the true decay flavour and the corresponding DP position
// (the supply of two DP models indicates a possible asymmetry)
const Double_t rateB { BkgndDPModelsB_[bkgndID]->getPdfNorm() };
const Double_t rateBbar { BkgndDPModelsBbar_[bkgndID]->getPdfNorm() };
const Double_t ADet { ( rateBbar - rateB ) / ( rateBbar + rateB ) };
random = LauRandom::randomFun()->Rndm();
if ( random <= 0.5 * ( 1.0 - ADet ) ) {
curEvtDecayFlv_ = LauFlavTag::Flavour::B;
BkgndDPModelsB_[bkgndID]->generate();
kinematics = kinematicsB0_;
} else {
curEvtDecayFlv_ = LauFlavTag::Flavour::Bbar;
BkgndDPModelsBbar_[bkgndID]->generate();
kinematics = kinematicsB0bar_;
}
} else {
// generate the true decay flavour
// (the supply of only a single model indicates no asymmetry)
random = LauRandom::randomFun()->Rndm();
if ( random <= 0.5 ) {
curEvtDecayFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtDecayFlv_ = LauFlavTag::Flavour::Bbar;
}
// generate the DP position
BkgndDPModelsB_[bkgndID]->generate();
kinematics = kinematicsB0_;
}
} else {
// mark that the decay flavour is unknown
curEvtDecayFlv_ = LauFlavTag::Flavour::Unknown;
// generate the DP position
BkgndDPModelsB_[bkgndID]->generate();
kinematics = kinematicsB0_;
}
// generate decay time and its error
curEvtDecayTimeErr_ = BkgndDecayTimePdfs_[bkgndID]->generateError(kTRUE);
curEvtDecayTime_ = BkgndDecayTimePdfs_[bkgndID]->generate( kinematics );
// generate the flavour tagging information from the true tag and decay flavour
// (we do this after accepting the event to save time)
flavTag_->generateBkgndEventInfo( bkgndID, curEvtTrueTagFlv_, curEvtDecayFlv_, curEvtDecayTime_ );
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
break;
}
case LauFlavTag::BkgndType::FlavourSpecific:
{
const LauDecayTime::FuncType dtType { BkgndDecayTimePdfs_[bkgndID]->getFuncType() };
if ( dtType == LauDecayTime::FuncType::ExpTrig or dtType == LauDecayTime::FuncType::ExpHypTrig ) {
const Double_t tMin = BkgndDecayTimePdfs_[bkgndID]->minAbscissa();
const Double_t tMax = BkgndDecayTimePdfs_[bkgndID]->maxAbscissa();
const Double_t maxDtEff { BkgndDecayTimePdfs_[bkgndID]->getMaxEfficiency() };
// TODO - do we need the factor 2?
const Double_t ASumSqMax { 2.0 * ( BkgndDPModelsB_[bkgndID]->getMaxHeight() + BkgndDPModelsBbar_[bkgndID]->getMaxHeight() ) * maxDtEff };
nGenLoop_ = 0;
Bool_t generatedEvent{kFALSE};
do {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::Unknown;
curEvtDecayFlv_ = LauFlavTag::Flavour::Unknown;
// First choose the true tag, accounting for the production asymmetry
// CONVENTION WARNING regarding meaning of sign of AProd
Double_t random = LauRandom::randomFun()->Rndm();
if (random <= 0.5 * ( 1.0 - AProdBkgnd_[bkgndID]->unblindValue() ) ) {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtTrueTagFlv_ = LauFlavTag::Flavour::Bbar;
}
// Generate the DP position and calculate the total A^2 and Abar^2
kinematics = BkgndDPModelsB_[bkgndID]->genUniformPoint();
BkgndDPModelsB_[bkgndID]->calcLikelihoodInfo(kinematics);
BkgndDPModelsBbar_[bkgndID]->calcLikelihoodInfo(kinematics);
// Generate decay time
curEvtDecayTime_ = LauRandom::randomFun()->Uniform(tMin,tMax);
// Generate the decay time error (NB the kTRUE forces the generation of a new value)
curEvtDecayTimeErr_ = BkgndDecayTimePdfs_[bkgndID]->generateError(kTRUE);
// Calculate all the decay time info
BkgndDecayTimePdfs_[bkgndID]->calcLikelihoodInfo(curEvtDecayTime_,curEvtDecayTimeErr_);
// Retrieve the DP intensities
const Double_t ASq { BkgndDPModelsB_[bkgndID]->getRawValue() };
const Double_t AbarSq { BkgndDPModelsBbar_[bkgndID]->getRawValue() };
// Also retrieve all the decay time terms
const Double_t dtCos { BkgndDecayTimePdfs_[bkgndID]->getCosTerm() };
const Double_t dtCosh { BkgndDecayTimePdfs_[bkgndID]->getCoshTerm() };
// and the decay time acceptance
const Double_t dtEff { BkgndDecayTimePdfs_[bkgndID]->getEffiTerm() };
// Calculate the total intensities for each flavour-specific final state
const Double_t ATotSq { ( ASq * dtCosh + curEvtTrueTagFlv_ * ASq * dtCos ) * dtEff };
const Double_t AbarTotSq { ( AbarSq * dtCosh - curEvtTrueTagFlv_ * AbarSq * dtCos ) * dtEff };
const Double_t ASumSq { ATotSq + AbarTotSq };
// Finally we throw the dice to see whether this event should be generated (and, if so, which final state)
const Double_t randNum = LauRandom::randomFun()->Rndm();
if (randNum <= ASumSq / ASumSqMax ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ASumSq > ASumSqMax) {
std::cerr << "WARNING in LauTimeDepFitModel::generateBkgndEvent : ASumSq > ASumSqMax" << std::endl;
}
if ( randNum <= ATotSq / ASumSqMax ) {
curEvtDecayFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtDecayFlv_ = LauFlavTag::Flavour::Bbar;
}
//Debug ASumSqMax huge size w.r.t. ASumSq for SDPs
//std::cout<<"ASq "<<ASq<<" AbarSq "<<AbarSq<<" dtCos "<<dtCos<<" dtCosh "<<dtCosh<<" dtEff "<<dtEff<<" ATotSq "<<ATotSq<<" AbarTotSq "<<AbarTotSq<<" ASumSq "<<ASumSq<<" ASumSqMax "<<ASumSqMax<<std::endl;
// Generate the flavour tagging information from the true tag and decay flavour
// (we do this after accepting the event to save time)
flavTag_->generateBkgndEventInfo( bkgndID, curEvtTrueTagFlv_, curEvtDecayFlv_, curEvtDecayTime_ );
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
} else {
nGenLoop_++;
}
} while (generatedEvent == kFALSE && nGenLoop_ < iterationsMax_);
} else {
// Hist, Delta, Exp, DeltaExp decay-time types
// Since there are no oscillations for these decay-time types,
// the true decay flavour must be equal to the true tag flavour
// First choose the true tag and decay flavour, accounting for both the production and detection asymmetries
// CONVENTION WARNING regarding meaning of sign of AProd and ADet
const Double_t AProd { AProdBkgnd_[bkgndID]->unblindValue() };
const Double_t rateB { BkgndDPModelsB_[bkgndID]->getPdfNorm() };
const Double_t rateBbar { BkgndDPModelsBbar_[bkgndID]->getPdfNorm() };
const Double_t ADet { ( rateBbar - rateB ) / ( rateBbar + rateB ) };
const Double_t random = LauRandom::randomFun()->Rndm();
// TODO - is this the correct way to combine the production and detection asymmetries?
if ( random <= 0.5 * ( 1.0 - AProd ) * ( 1.0 - ADet ) ) {
curEvtDecayFlv_ = curEvtTrueTagFlv_ = LauFlavTag::Flavour::B;
} else {
curEvtDecayFlv_ = curEvtTrueTagFlv_ = LauFlavTag::Flavour::Bbar;
}
// generate the DP position
if ( curEvtDecayFlv_ == LauFlavTag::Flavour::B ) {
BkgndDPModelsB_[bkgndID]->generate();
kinematics = kinematicsB0_;
} else {
BkgndDPModelsBbar_[bkgndID]->generate();
kinematics = kinematicsB0bar_;
}
// generate decay time and its error
curEvtDecayTimeErr_ = BkgndDecayTimePdfs_[bkgndID]->generateError(kTRUE);
curEvtDecayTime_ = BkgndDecayTimePdfs_[bkgndID]->generate( kinematics );
// generate the flavour tagging information from the true tag and decay flavour
// (we do this after accepting the event to save time)
flavTag_->generateBkgndEventInfo( bkgndID, curEvtTrueTagFlv_, curEvtDecayFlv_, curEvtDecayTime_ );
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
}
break;
}
case LauFlavTag::BkgndType::SelfConjugate:
// TODO
break;
case LauFlavTag::BkgndType::NonSelfConjugate:
// TODO
break;
}
if ( genOK ) {
// Make sure both kinematics objects are up-to-date
kinematicsB0_->updateKinematics(kinematics->getm13Sq(), kinematics->getm23Sq() );
kinematicsB0bar_->updateKinematics(kinematics->getm13Sq(), kinematics->getm23Sq() );
this->generateExtraPdfValues(BkgndPdfs_[bkgndID], bkgndTree_[bkgndID]);
}
return genOK;
}
void LauTimeDepFitModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleIntegerBranch("cpEigenvalue");
const TString& trueTagVarName { flavTag_->getTrueTagVarName() };
if ( trueTagVarName != "" ) {
this->addGenNtupleIntegerBranch(trueTagVarName);
}
if ( cpEigenValue_ == QFS ) {
const TString& decayFlvVarName { flavTag_->getDecayFlvVarName() };
if ( decayFlvVarName == "" ) {
std::cerr<<"ERROR in LauTimeDepFitModel::setupGenNtupleBranches : Decay flavour variable not set for QFS decay, see LauFlavTag::setDecayFlvVarName()."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
} else {
this->addGenNtupleIntegerBranch(decayFlvVarName);
}
}
const std::vector<TString>& tagVarNames { flavTag_->getTagVarNames() };
const std::vector<TString>& mistagVarNames { flavTag_->getMistagVarNames() };
const std::size_t nTaggers {flavTag_->getNTaggers()};
for (std::size_t taggerID{0}; taggerID<nTaggers; ++taggerID){
this->addGenNtupleIntegerBranch(tagVarNames[taggerID]);
this->addGenNtupleDoubleBranch(mistagVarNames[taggerID]);
}
if (this->useDP() == kTRUE) {
// Let's add the decay time variables.
this->addGenNtupleDoubleBranch(signalDecayTimePdf_->varName());
if ( signalDecayTimePdf_->varErrName() != "" ) {
this->addGenNtupleDoubleBranch(signalDecayTimePdf_->varErrName());
}
this->addGenNtupleDoubleBranch("m12");
this->addGenNtupleDoubleBranch("m23");
this->addGenNtupleDoubleBranch("m13");
this->addGenNtupleDoubleBranch("m12Sq");
this->addGenNtupleDoubleBranch("m23Sq");
this->addGenNtupleDoubleBranch("m13Sq");
this->addGenNtupleDoubleBranch("cosHel12");
this->addGenNtupleDoubleBranch("cosHel23");
this->addGenNtupleDoubleBranch("cosHel13");
if (kinematicsB0bar_->squareDP() && kinematicsB0_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime");
this->addGenNtupleDoubleBranch("thPrime");
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
this->addGenNtupleDoubleBranch("reB0Amp");
this->addGenNtupleDoubleBranch("imB0Amp");
this->addGenNtupleDoubleBranch("reB0barAmp");
this->addGenNtupleDoubleBranch("imB0barAmp");
}
}
// Let's look at the extra variables for signal in one of the tagging categories
for ( const LauAbsPdf* pdf : sigExtraPdf_ ) {
const std::vector<TString> varNames{ pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
this->addGenNtupleDoubleBranch( varName );
}
}
}
}
void LauTimeDepFitModel::setDPDtBranchValues()
{
// Store the decay time variables.
this->setGenNtupleDoubleBranchValue(signalDecayTimePdf_->varName(),curEvtDecayTime_);
if ( signalDecayTimePdf_->varErrName() != "" ) {
this->setGenNtupleDoubleBranchValue(signalDecayTimePdf_->varErrName(),curEvtDecayTimeErr_);
}
// CONVENTION WARNING
// TODO check - for now use B0 for any tags
//LauKinematics* kinematics(0);
//if (curEvtTagFlv_[position]<0) {
LauKinematics* kinematics = kinematicsB0_;
//} else {
// kinematics = kinematicsB0bar_;
//}
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12", kinematics->getm12());
this->setGenNtupleDoubleBranchValue("m23", kinematics->getm23());
this->setGenNtupleDoubleBranchValue("m13", kinematics->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq", kinematics->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq", kinematics->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq", kinematics->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12", kinematics->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23", kinematics->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13", kinematics->getc13());
if (kinematics->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime", kinematics->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime", kinematics->getThetaPrime());
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
if ( this->getGenNtupleIntegerBranchValue("genSig")==1 ) {
LauComplex Abar = sigModelB0bar_->getEvtDPAmp();
LauComplex A = sigModelB0_->getEvtDPAmp();
this->setGenNtupleDoubleBranchValue("reB0Amp", A.re());
this->setGenNtupleDoubleBranchValue("imB0Amp", A.im());
this->setGenNtupleDoubleBranchValue("reB0barAmp", Abar.re());
this->setGenNtupleDoubleBranchValue("imB0barAmp", Abar.im());
} else {
this->setGenNtupleDoubleBranchValue("reB0Amp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0Amp", 0.0);
this->setGenNtupleDoubleBranchValue("reB0barAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0barAmp", 0.0);
}
}
}
void LauTimeDepFitModel::generateExtraPdfValues(LauPdfPList& extraPdfs, LauEmbeddedData* embeddedData)
{
// CONVENTION WARNING
LauKinematics* kinematics = kinematicsB0_;
//LauKinematics* kinematics(0);
//if (curEvtTagFlv_<0) {
// kinematics = kinematicsB0_;
//} else {
// kinematics = kinematicsB0bar_;
//}
// Generate from the extra PDFs
for (auto& pdf : extraPdfs){
LauFitData genValues;
if (embeddedData) {
genValues = embeddedData->getValues( pdf->varNames() );
} else {
genValues = pdf->generate(kinematics);
}
for (auto& var : genValues){
TString varName = var.first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var.second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
void LauTimeDepFitModel::propagateParUpdates()
{
// Update the complex mixing phase
if (useSinCos_) {
phiMixComplex_.setRealPart(cosPhiMix_.unblindValue());
phiMixComplex_.setImagPart(-1.0*sinPhiMix_.unblindValue());
} else {
phiMixComplex_.setRealPart(TMath::Cos(-1.0*phiMix_.unblindValue()));
phiMixComplex_.setImagPart(TMath::Sin(-1.0*phiMix_.unblindValue()));
}
// Update the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_->updateCoeffs(coeffsB0bar_);
sigModelB0_->updateCoeffs(coeffsB0_);
this->calcInterferenceTermNorm();
}
// Update the decay time normalisation
if ( signalDecayTimePdf_ ) {
signalDecayTimePdf_->propagateParUpdates();
}
// TODO
// - maybe also need to add an update of the background decay time PDFs here
// Update the signal events from the background numbers if not doing an extended fit
this->updateSigEvents();
}
void LauTimeDepFitModel::updateSigEvents()
{
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
// Subtract background events (if any) from signal.
if ( usingBkgnd_ == kTRUE ) {
for ( LauAbsRValue* bkgndYield : bkgndEvents_ ) {
signalEvents -= bkgndYield->value();
}
}
signalEvents_->value(signalEvents);
}
void LauTimeDepFitModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and background models.
// Note that we store the events of both charges in both the
// negative and the positive models. It's only later, at the stage
// when the likelihood is being calculated, that we separate them.
LauFitDataTree* inputFitData = this->fitData();
evtCPEigenVals_.clear();
const Bool_t hasCPEV = ( (cpevVarName_ != "") && inputFitData->haveBranch( cpevVarName_ ) );
UInt_t nEvents = inputFitData->nEvents();
evtCPEigenVals_.reserve( nEvents );
LauFitData::const_iterator fitdata_iter;
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
// if the CP-eigenvalue is in the data use those, otherwise use the default
if ( hasCPEV ) {
fitdata_iter = dataValues.find( cpevVarName_ );
const Int_t cpEV = static_cast<Int_t>( fitdata_iter->second );
if ( cpEV == 1 ) {
cpEigenValue_ = CPEven;
} else if ( cpEV == -1 ) {
cpEigenValue_ = CPOdd;
} else if ( cpEV == 0 ) {
cpEigenValue_ = QFS;
} else {
std::cerr<<"WARNING in LauTimeDepFitModel::cacheInputFitVars : Unknown value: "<<cpEV<<" for CP eigenvalue, setting to CP-even"<<std::endl;
cpEigenValue_ = CPEven;
}
}
evtCPEigenVals_.push_back( cpEigenValue_ );
}
// We'll cache the DP amplitudes at the end because we'll
// append some points that the other PDFs won't deal with.
if (this->useDP() == kTRUE) {
// DecayTime and SigmaDecayTime
signalDecayTimePdf_->cacheInfo(*inputFitData);
// cache all the backgrounds too
for(auto& bg : BkgndDecayTimePdfs_)
{bg->cacheInfo(*inputFitData);}
}
// Flavour tagging information
flavTag_->cacheInputFitVars(inputFitData,signalDecayTimePdf_->varName());
// ...and then the extra PDFs
if (not sigExtraPdf_.empty()){
this->cacheInfo(sigExtraPdf_, *inputFitData);
}
if(usingBkgnd_ == kTRUE){
for (auto& pdf : BkgndPdfs_){
this->cacheInfo(pdf, *inputFitData);
}
}
if (this->useDP() == kTRUE) {
sigModelB0bar_->fillDataTree(*inputFitData);
sigModelB0_->fillDataTree(*inputFitData);
if (usingBkgnd_ == kTRUE) {
for (auto& model : BkgndDPModelsB_){
model->fillDataTree(*inputFitData);
}
for (auto& model : BkgndDPModelsBbar_){
if (model != nullptr) {
model->fillDataTree(*inputFitData);
}
}
}
}
}
Double_t LauTimeDepFitModel::getTotEvtLikelihood(const UInt_t iEvt)
{
// Get the CP eigenvalue of the current event
cpEigenValue_ = evtCPEigenVals_[iEvt];
// Get the DP and DecayTime likelihood for signal (TODO and eventually backgrounds)
this->getEvtDPDtLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal (TODO and eventually backgrounds)
this->getEvtExtraLikelihoods(iEvt);
// Construct the total likelihood for signal, qqbar and BBbar backgrounds
Double_t sigLike = sigDPLike_ * sigExtraLike_;
Double_t signalEvents = signalEvents_->unblindValue();
// TODO - consider what to do here - do we even want the option not to use the DP in this model?
//if ( not this->useDP() ) {
//signalEvents *= 0.5 * (1.0 + curEvtTagFlv_ * signalAsym_->unblindValue());
//}
// Construct the total event likelihood
Double_t likelihood { sigLike * signalEvents };
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const Double_t bkgndEvents { bkgndEvents_[bkgndID]->unblindValue() };
likelihood += bkgndEvents*bkgndDPLike_[bkgndID]*bkgndExtraLike_[bkgndID];
}
}
return likelihood;
}
Double_t LauTimeDepFitModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
if (usingBkgnd_) {
for ( const auto& yieldPar : bkgndEvents_ ) {
eventSum += yieldPar->unblindValue();
}
}
return eventSum;
}
void LauTimeDepFitModel::getEvtDPDtLikelihood(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
if ( ! this->useDP() ) {
// There's always going to be a term in the likelihood for the
// signal, so we'd better not zero it.
sigDPLike_ = 1.0;
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_ == kTRUE) {
bkgndDPLike_[bkgndID] = 1.0;
} else {
bkgndDPLike_[bkgndID] = 0.0;
}
}
return;
}
// Calculate event quantities
// Get the DP dynamics, decay time, and flavour tagging to calculate
// everything required for the likelihood calculation
sigModelB0bar_->calcLikelihoodInfo(iEvt);
sigModelB0_->calcLikelihoodInfo(iEvt);
signalDecayTimePdf_->calcLikelihoodInfo(static_cast<std::size_t>(iEvt));
flavTag_->updateEventInfo(iEvt);
// Retrieve the amplitudes and efficiency from the dynamics
LauComplex Abar { sigModelB0bar_->getEvtDPAmp() };
LauComplex A { sigModelB0_->getEvtDPAmp() };
const Double_t dpEff { sigModelB0bar_->getEvtEff() };
// If this is a QFS decay, one of the DP amplitudes needs to be zeroed
curEvtDecayFlv_ = LauFlavTag::Flavour::Unknown;
if (cpEigenValue_ == QFS){
curEvtDecayFlv_ = flavTag_->getCurEvtDecayFlv();
if ( curEvtDecayFlv_ == LauFlavTag::Flavour::B ) {
Abar.zero();
} else if ( curEvtDecayFlv_ == LauFlavTag::Flavour::Bbar ) {
A.zero();
} else {
std::cerr<<"ERROR in LauTimeDepFitModel::getEvtDPDtLikelihood : Decay flavour must be known for QFS decays."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Next calculate the DP terms
const Double_t aSqSum { A.abs2() + Abar.abs2() };
const Double_t aSqDif { A.abs2() - Abar.abs2() };
Double_t interTermRe { 0.0 };
Double_t interTermIm { 0.0 };
if ( cpEigenValue_ != QFS ) {
const LauComplex inter { Abar * A.conj() * phiMixComplex_ };
if ( cpEigenValue_ == CPEven ) {
interTermIm = 2.0 * inter.im();
interTermRe = 2.0 * inter.re();
} else {
interTermIm = -2.0 * inter.im();
interTermRe = -2.0 * inter.re();
}
}
// First get all the decay time terms
// TODO Backgrounds
// Get the decay time acceptance
const Double_t dtEff { signalDecayTimePdf_->getEffiTerm() };
// Get all the decay time terms
const Double_t dtCos { signalDecayTimePdf_->getCosTerm() };
const Double_t dtSin { signalDecayTimePdf_->getSinTerm() };
const Double_t dtCosh { signalDecayTimePdf_->getCoshTerm() };
const Double_t dtSinh { signalDecayTimePdf_->getSinhTerm() };
// Get the decay time error term
const Double_t dtErrLike { signalDecayTimePdf_->getErrTerm() };
// Get flavour tagging terms
Double_t omega{1.0};
Double_t omegabar{1.0};
const std::size_t nTaggers { flavTag_->getNTaggers() };
for (std::size_t taggerID{0}; taggerID<nTaggers; ++taggerID){
omega *= flavTag_->getCapitalOmega(taggerID, LauFlavTag::Flavour::B);
omegabar *= flavTag_->getCapitalOmega(taggerID, LauFlavTag::Flavour::Bbar);
}
const Double_t prodAsym { AProd_.unblindValue() };
const Double_t ftOmegaHyp { ((1.0 - prodAsym)*omega + (1.0 + prodAsym)*omegabar) };
const Double_t ftOmegaTrig { ((1.0 - prodAsym)*omega - (1.0 + prodAsym)*omegabar) };
const Double_t coshTerm { ftOmegaHyp * dtCosh * aSqSum };
const Double_t sinhTerm { ftOmegaHyp * dtSinh * interTermRe };
const Double_t cosTerm { ftOmegaTrig * dtCos * aSqDif };
const Double_t sinTerm { ftOmegaTrig * dtSin * interTermIm };
// Combine all terms to get the total amplitude squared
const Double_t ASq { coshTerm + sinhTerm + cosTerm - sinTerm };
// Calculate the DP and time normalisation
const Double_t normASqSum { sigModelB0_->getDPNorm() + sigModelB0bar_->getDPNorm() };
const Double_t normASqDiff { sigModelB0_->getDPNorm() - sigModelB0bar_->getDPNorm() };
Double_t normInterTermRe { 0.0 };
Double_t normInterTermIm { 0.0 };
if ( cpEigenValue_ != QFS ) {
// TODO - double check this sign flipping here (it's presumably right but...)
normInterTermRe = ( cpEigenValue_ == CPOdd ) ? -1.0 * interTermReNorm_ : interTermReNorm_;
normInterTermIm = ( cpEigenValue_ == CPOdd ) ? -1.0 * interTermImNorm_ : interTermImNorm_;
}
const Double_t normCoshTerm { signalDecayTimePdf_->getNormTermCosh() };
const Double_t normSinhTerm { signalDecayTimePdf_->getNormTermSinh() };
const Double_t normCosTerm { signalDecayTimePdf_->getNormTermCos() };
const Double_t normSinTerm { signalDecayTimePdf_->getNormTermSin() };
const Double_t normHyp { normASqSum * normCoshTerm + normInterTermRe * normSinhTerm };
const Double_t normTrig { prodAsym * ( normASqDiff * normCosTerm - normInterTermIm * normSinTerm ) };
// Combine all terms to get the total normalisation
const Double_t norm { 2.0 * ( normHyp - normTrig ) };
// Multiply the squared-amplitude by the efficiency (DP and decay time) and decay-time error likelihood
// and normalise to obtain the signal likelihood
sigDPLike_ = ( ASq * dpEff * dtEff * dtErrLike ) / norm;
// Background part
// TODO move to new function as getEvtBkgndLikelihoods?
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if ( not usingBkgnd_ ) {
bkgndDPLike_[bkgndID] = 0.0;
continue;
}
Double_t omegaBkgnd{1.0};
Double_t omegaBarBkgnd{1.0};
BkgndDecayTimePdfs_[bkgndID]->calcLikelihoodInfo(static_cast<std::size_t>(iEvt));
// Consider background type
switch ( BkgndTypes_[bkgndID] ) {
case LauFlavTag::BkgndType::Combinatorial :
{
// For combinatorial background the DP and decay-time models factorise completely, just mulitply them
// Start with the DP likelihood...
if ( (cpEigenValue_ == QFS) and BkgndDPModelsBbar_[bkgndID] != nullptr ) { //Flavour specific (with possible detection asymmetry)
// TODO - need to detect CP-eigenstate and two histos case in initialisation and abort since it doesn't make any sense
if ( curEvtDecayFlv_ == LauFlavTag::Flavour::B ) {
bkgndDPLike_[bkgndID] = BkgndDPModelsB_[bkgndID]->getUnNormValue(iEvt);
} else {
bkgndDPLike_[bkgndID] = BkgndDPModelsBbar_[bkgndID]->getUnNormValue(iEvt);
}
bkgndDPLike_[bkgndID] /= ( BkgndDPModelsB_[bkgndID]->getPdfNorm() + BkgndDPModelsBbar_[bkgndID]->getPdfNorm() );
} else {
// No detection asymmetry (could be QFS or CP-eigenstate - which implies slightly different normalisation)
bkgndDPLike_[bkgndID] = BkgndDPModelsB_[bkgndID]->getLikelihood(iEvt);
if ( cpEigenValue_ == QFS ) {
bkgndDPLike_[bkgndID] *= 0.5;
}
}
// ...include the decay time...
switch( BkgndDecayTimePdfs_[bkgndID]->getFuncType() ) {
case LauDecayTime::FuncType::Hist :
bkgndDPLike_[bkgndID] *= BkgndDecayTimePdfs_[bkgndID]->getHistTerm();
break;
case LauDecayTime::FuncType::Exp :
bkgndDPLike_[bkgndID] *= ( BkgndDecayTimePdfs_[bkgndID]->getExpTerm() / BkgndDecayTimePdfs_[bkgndID]->getNormTermExp() );
break;
// TODO - any other decay time function types that make sense for combinatorial?
// - should also have a set of checks in initialise that we have everything we need for the backgrounds and that the various settings make sense
default :
// TODO as per comment just above, once the above mentioned checks are implemented this error message can be removed
std::cerr << "WARNING in LauTimeDepFitModel::getEvtDPDtLikelihood : bkgnd types other than Hist and Exp don't make sense for combinatorial!" << std::endl;
break;
}
// ...include flavour tagging
for (std::size_t taggerID{0}; taggerID<nTaggers; ++taggerID){
// Only need omega==omegabar for combinatorial
omegaBkgnd *= flavTag_->getCapitalOmegaBkgnd(bkgndID, taggerID, LauFlavTag::Flavour::B, curEvtDecayFlv_);
}
bkgndDPLike_[bkgndID] *= omegaBkgnd;
break;
}
case LauFlavTag::BkgndType::FlavourSpecific :
{
// DP terms needed by all decay-time cases
Double_t Asq { BkgndDPModelsB_[bkgndID]->getUnNormValue(iEvt) };
Double_t Asqbar { BkgndDPModelsBbar_[bkgndID]->getUnNormValue(iEvt) };
if ( cpEigenValue_ == QFS ){
// If the signal is flavour-specific we can know which DP to use, so zero the other one
if ( curEvtDecayFlv_ == LauFlavTag::Flavour::B ) {
Asqbar = 0.0;
} else if ( curEvtDecayFlv_ == LauFlavTag::Flavour::Bbar ) {
Asq = 0.0;
}
}
const Double_t AsqSum { Asq + Asqbar };
// DP norm terms needed by all decay-time cases
const Double_t AsqNorm { BkgndDPModelsB_[bkgndID]->getPdfNorm() };
const Double_t AsqbarNorm { BkgndDPModelsBbar_[bkgndID]->getPdfNorm() };
const Double_t AsqNormSum { AsqNorm + AsqbarNorm };
// FT terms needed by all decay-time cases
omegaBkgnd = omegaBarBkgnd = 1.0;
for (std::size_t taggerID{0}; taggerID<nTaggers; ++taggerID){
omegaBkgnd *= flavTag_->getCapitalOmegaBkgnd(bkgndID, taggerID, LauFlavTag::Flavour::B, curEvtDecayFlv_);
omegaBarBkgnd *= flavTag_->getCapitalOmegaBkgnd(bkgndID, taggerID, LauFlavTag::Flavour::Bbar, curEvtDecayFlv_);
}
const Double_t AProd { AProdBkgnd_[bkgndID]->unblindValue() };
const Double_t ftOmegaHypBkgnd { (1.0 - AProd)*omegaBkgnd + (1.0 + AProd)*omegaBarBkgnd };
switch( BkgndDecayTimePdfs_[bkgndID]->getFuncType() )
{
case LauDecayTime::FuncType::Hist: // DP and decay-time still factorise
{
// Start with the DP terms...
bkgndDPLike_[bkgndID] = AsqSum / AsqNormSum;
// ...include the decay time...
bkgndDPLike_[bkgndID] *= BkgndDecayTimePdfs_[bkgndID]->getHistTerm();
// ...include flavour tagging
bkgndDPLike_[bkgndID] *= ( 0.5 * ftOmegaHypBkgnd );
break;
}
case LauDecayTime::FuncType::Exp : // DP and decay-time still factorise
{
// Start with the DP terms...
bkgndDPLike_[bkgndID] = AsqSum / AsqNormSum;
// ...include the decay time...
bkgndDPLike_[bkgndID] *= ( BkgndDecayTimePdfs_[bkgndID]->getExpTerm() / BkgndDecayTimePdfs_[bkgndID]->getNormTermExp() );
// ...include flavour tagging
bkgndDPLike_[bkgndID] *= ( 0.5 * ftOmegaHypBkgnd );
break;
}
case LauDecayTime::FuncType::ExpTrig: // DP and decay-time don't factorise
case LauDecayTime::FuncType::ExpHypTrig:
{
// DP and FT terms specific to this case
const Double_t AsqDiff { Asq - Asqbar };
const Double_t AsqNormDiff { AsqNorm - AsqbarNorm }; //TODO check this shouldn't be `fabs`ed
const Double_t ftOmegaTrigBkgnd { (1.0 - AProd)*omegaBkgnd - (1.0 + AProd)*omegaBarBkgnd };
// decay time terms
// Sin and Sinh terms are ignored: the FS modes can't exhibit TD CPV
const Double_t dtCoshBkgnd { BkgndDecayTimePdfs_[bkgndID]->getCoshTerm() };
const Double_t dtCosBkgnd { BkgndDecayTimePdfs_[bkgndID]->getCosTerm() };
const Double_t normCoshTermBkgnd { BkgndDecayTimePdfs_[bkgndID]->getNormTermCosh() };
const Double_t normCosTermBkgnd { BkgndDecayTimePdfs_[bkgndID]->getNormTermCos() };
// Combine the DP, FT, and decay time terms
const Double_t coshTermBkgnd { ftOmegaHypBkgnd * dtCoshBkgnd * AsqSum };
const Double_t cosTermBkgnd { ftOmegaTrigBkgnd * dtCosBkgnd * AsqDiff };
// Similarly for the normalisation, see Laura note eq. 41
const Double_t normBkgnd { 2.0 * ( (normCoshTermBkgnd * AsqNormSum) - AProd*(normCosTermBkgnd * AsqNormDiff) ) };
bkgndDPLike_[bkgndID] = (coshTermBkgnd + cosTermBkgnd) / normBkgnd;
break;
}
case LauDecayTime::FuncType::Delta:
case LauDecayTime::FuncType::DeltaExp:
// TODO as per comment above, once the checks in initialise are implemented this error message can be removed
std::cerr << "WARNING in LauTimeDepFitModel::getEvtDPDtLikelihood : bkgnd types Delta and DeltaExp don't make sense!" << std::endl;
break;
}
break;
}
case LauFlavTag::BkgndType::SelfConjugate :
//Copy this from the CPeigenstate signal case
std::cerr << "WARNING in LauTimeDepFitModel::getEvtDPDtLikelihood : SelfConjugate states aren't implemented yet!" << std::endl;
bkgndDPLike_[bkgndID] = 0.0;
break;
case LauFlavTag::BkgndType::NonSelfConjugate :
// TODO this has been ignored for now since it's not used in the B->Dpipi case
std::cerr << "WARNING in LauTimeDepFitModel::getEvtDPDtLikelihood : NonSelfConjugate states aren't implemented yet!" << std::endl;
bkgndDPLike_[bkgndID] = 0.0;
break;
}
// Get the decay time acceptance
const Double_t dtEffBkgnd { BkgndDecayTimePdfs_[bkgndID]->getEffiTerm() };
// Get the decay time error term
const Double_t dtErrLikeBkgnd { BkgndDecayTimePdfs_[bkgndID]->getErrTerm() };
// Include these terms in the background likelihood
bkgndDPLike_[bkgndID] *= ( dtEffBkgnd * dtErrLikeBkgnd );
}
}
void LauTimeDepFitModel::getEvtExtraLikelihoods(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0; //There's always a likelihood term for signal, so we better not zero it.
// First, those independent of the tagging of the event:
// signal
if ( not sigExtraPdf_.empty() ) {
sigExtraLike_ = this->prodPdfValue( sigExtraPdf_, iEvt );
}
// Background
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
if (usingBkgnd_) {
bkgndExtraLike_[bkgndID] = this->prodPdfValue( BkgndPdfs_[bkgndID], iEvt );
} else {
bkgndExtraLike_[bkgndID] = 0.0;
}
}
}
void LauTimeDepFitModel::updateCoeffs()
{
coeffsB0bar_.clear(); coeffsB0_.clear();
coeffsB0bar_.reserve(nSigComp_); coeffsB0_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; ++i) {
coeffsB0bar_.push_back(coeffPars_[i]->antiparticleCoeff());
coeffsB0_.push_back(coeffPars_[i]->particleCoeff());
}
}
void LauTimeDepFitModel::checkMixingPhase()
{
Double_t phase = phiMix_.value();
Double_t genPhase = phiMix_.genValue();
// Check now whether the phase lies in the right range (-pi to pi).
Bool_t withinRange(kFALSE);
while (withinRange == kFALSE) {
if (phase > -LauConstants::pi && phase < LauConstants::pi) {
withinRange = kTRUE;
} else {
// Not within the specified range
if (phase > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (phase < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
}
}
// A further problem can occur when the generated phase is close to -pi or pi.
// The phase can wrap over to the other end of the scale -
// this leads to artificially large pulls so we wrap it back.
Double_t diff = phase - genPhase;
if (diff > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (diff < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
// finally store the new value in the parameter
// and update the pull
phiMix_.value(phase);
phiMix_.updatePull();
}
void LauTimeDepFitModel::embedSignal(const TString& fileName, const TString& treeName,
const Bool_t reuseEventsWithinEnsemble, const Bool_t reuseEventsWithinExperiment, const Bool_t useReweighting)
{
if (signalTree_) {
std::cerr<<"ERROR in LauTimeDepFitModel::embedSignal : Already embedding signal from file."<<std::endl;
return;
}
signalTree_ = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = signalTree_->findBranches();
if (!dataOK) {
delete signalTree_; signalTree_ = 0;
std::cerr<<"ERROR in LauTimeDepFitModel::embedSignal : Problem creating data tree for embedding."<<std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
useReweighting_ = useReweighting;
this->enableEmbedding(kTRUE);
}
void LauTimeDepFitModel::embedBkgnd(const TString& bkgndClass, const TString& fileName, const TString& treeName,
const Bool_t reuseEventsWithinEnsemble, const Bool_t reuseEventsWithinExperiment, const Bool_t useReweighting)
{
if ( ! this->validBkgndClass( bkgndClass ) ) {
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Invalid background class \"" << bkgndClass << "\"." << std::endl;
std::cerr << " : Background class names must be provided in \"setBkgndClassNames\" before any other background-related actions can be performed." << std::endl;
return;
}
UInt_t bkgndID = this->bkgndClassID( bkgndClass );
LauEmbeddedData* bkgTree = bkgndTree_[bkgndID];
if (bkgTree) {
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Already embedding background from a file." << std::endl;
return;
}
bkgTree = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = bkgTree->findBranches();
if (!dataOK) {
delete bkgTree; bkgTree = 0;
std::cerr << "ERROR in LauSimpleFitModel::embedBkgnd : Problem creating data tree for embedding." << std::endl;
return;
}
reuseBkgnd_[bkgndID] = reuseEventsWithinEnsemble;
useReweighting_ = useReweighting;
this->enableEmbedding(kTRUE);
}
void LauTimeDepFitModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
}
// Store the total event likelihood for each species.
this->addSPlotNtupleDoubleBranch("sigTotalLike");
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "TotalLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
// Store the DP likelihoods
if (this->useDP()) {
this->addSPlotNtupleDoubleBranch("sigDPLike");
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name( this->bkgndClassName(iBkgnd) );
name += "DPLike";
this->addSPlotNtupleDoubleBranch(name);
}
}
}
// Store the likelihoods for each extra PDF
this->addSPlotNtupleBranches(sigExtraPdf_, "sig");
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
this->addSPlotNtupleBranches(BkgndPdfs_[iBkgnd], bkgndClass);
}
}
}
void LauTimeDepFitModel::addSPlotNtupleBranches(const LauPdfPList& extraPdfs, const TString& prefix)
{
// Loop through each of the PDFs
for ( const LauAbsPdf* pdf : extraPdfs ) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars{0};
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString name{prefix};
name += pdf->varName();
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars{""};
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
allVars += varName;
TString name{prefix};
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
}
TString name{prefix};
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr<<"WARNING in LauTimeDepFitModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs."<<std::endl;
}
}
}
Double_t LauTimeDepFitModel::setSPlotNtupleBranchValues(LauPdfPList& extraPdfs, const TString& prefix, const UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if ( extraPdfs.empty() ) {
return totalLike;
}
for ( LauAbsPdf* pdf : extraPdfs ) {
// calculate the likelihood for this event
pdf->calcLikelihoodInfo(iEvt);
extraLike = pdf->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars{0};
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString name{prefix};
name += pdf->varName();
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars{""};
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
allVars += varName;
TString name{prefix};
name += varName;
name += "Like";
const Double_t indivLike = pdf->getLikelihood( varName );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
}
TString name{prefix};
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr<<"WARNING in LauAllFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs."<<std::endl;
}
}
return totalLike;
}
LauSPlot::NameSet LauTimeDepFitModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
for ( const LauAbsPdf* pdf : sigExtraPdf_ ) {
// Loop over the variables involved in each PDF
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
// If they are not DP coordinates then add them
if ( varName != "m13Sq" && varName != "m23Sq" ) {
nameSet.insert( varName );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauTimeDepFitModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (!par->fixed()) {
numbMap[bkgndClass] = par->genValue();
if ( ! par->isLValue() ) {
std::cerr << "WARNING in LauTimeDepFitModel::freeSpeciesNames : \"" << par->name() << "\" is a LauFormulaPar, which implies it is perhaps not entirely free to float in the fit, so the sWeight calculation may not be reliable" << std::endl;
}
}
}
}
return numbMap;
}
LauSPlot::NumbMap LauTimeDepFitModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
const LauAbsRValue* par = bkgndEvents_[iBkgnd];
if (par->fixed()) {
numbMap[bkgndClass] = par->genValue();
}
}
}
return numbMap;
}
LauSPlot::TwoDMap LauTimeDepFitModel::twodimPDFs() const
{
LauSPlot::TwoDMap twodimMap;
for ( const LauAbsPdf* pdf : sigExtraPdf_ ) {
// Count the number of input variables that are not DP variables
UInt_t nVars{0};
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
for ( const LauAbsPdf* pdf : BkgndPdfs_[iBkgnd] ) {
// Count the number of input variables that are not DP variables
UInt_t nVars{0};
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( bkgndClass, std::make_pair( varNames[0], varNames[1] ) ) );
}
}
}
}
return twodimMap;
}
void LauTimeDepFitModel::storePerEvtLlhds()
{
std::cout<<"INFO in LauTimeDepFitModel::storePerEvtLlhds : Storing per-event likelihood values..."<<std::endl;
LauFitDataTree* inputFitData = this->fitData();
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
if (!this->useDP() && this->storeDPEff()) {
sigModelB0bar_->initialise(coeffsB0bar_);
sigModelB0_->initialise(coeffsB0_);
sigModelB0bar_->fillDataTree(*inputFitData);
sigModelB0_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
LauIsobarDynamics* sigModel(sigModelB0bar_);
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
// Find out whether we have B0bar or B0
flavTag_->updateEventInfo(iEvt);
curEvtTagFlv_ = flavTag_->getCurEvtTagFlv();
curEvtMistag_ = flavTag_->getCurEvtMistag();
// the DP information
this->getEvtDPDtLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
sigModel->calcLikelihoodInfo(iEvt);
}
this->setSPlotNtupleDoubleBranchValue("efficiency",sigModel->getEvtEff());
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "DPLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndDPLike_[iBkgnd]);
}
}
} else {
sigTotalLike_ = 1.0;
}
// the signal PDF values
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigExtraPdf_, "sig", iEvt);
// the background PDF values
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
const TString& bkgndClass = this->bkgndClassName(iBkgnd);
LauPdfPList& pdfs = BkgndPdfs_[iBkgnd];
bkgndTotalLike_[iBkgnd] *= this->setSPlotNtupleBranchValues(pdfs, bkgndClass, iEvt);
}
}
// the total likelihoods
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
if (usingBkgnd_) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t iBkgnd(0); iBkgnd < nBkgnds; ++iBkgnd ) {
TString name = this->bkgndClassName(iBkgnd);
name += "TotalLike";
this->setSPlotNtupleDoubleBranchValue(name,bkgndTotalLike_[iBkgnd]);
}
}
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout<<"INFO in LauTimeDepFitModel::storePerEvtLlhds : Finished storing per-event likelihood values."<<std::endl;
}
void LauTimeDepFitModel::weightEvents( const TString& /*dataFileName*/, const TString& /*dataTreeName*/ )
{
std::cerr << "ERROR in LauTimeDepFitModel::weightEvents : Method not available for this fit model." << std::endl;
return;
}
void LauTimeDepFitModel::savePDFPlots(const TString& /*label*/)
{
}
void LauTimeDepFitModel::savePDFPlotsWave(const TString& /*label*/, const Int_t& /*spin*/)
{
}
void LauTimeDepFitModel::printEventLikelihoods() const
{
std::cerr << "WARNING in LauTimeDepFitModel::printEventLikelihoods : breakdown of likelihood for this event:" << std::endl;
// Print the signal information
std::cerr << " Signal DP+t likelihood = " << sigDPLike_ << "\n";
std::cerr << " Signal DP eff = " << sigModelB0bar_->getEvtEff() << "\n";
std::cerr << " Signal t eff = " << signalDecayTimePdf_->getEffiTerm() << "\n";
this->printPdfValues( sigExtraPdf_, " Signal" );
// Print the background information
if ( usingBkgnd_ ) {
const UInt_t nBkgnds = this->nBkgndClasses();
for ( UInt_t bkgndID(0); bkgndID < nBkgnds; ++bkgndID ) {
const TString& bkgndName { this->bkgndClassName( bkgndID ) };
std::cerr << " " << bkgndName << " DP+t likelihood = " << bkgndDPLike_[bkgndID] << "\n";
/* TODO - not general so won't leave in - can we easily generalise?
if ( bkgndName == "comb" ) {
std::cerr << " comb DP likelihood = " << BkgndDPModelsB_[bkgndID]->getUnNormValue() / BkgndDPModelsB_[bkgndID]->getPdfNorm() << "\n";
std::cerr << " comb t likelihood = " << BkgndDecayTimePdfs_[bkgndID]->getHistTerm() << "\n";
std::cerr << " comb FT likelihood = " << flavTag_->getCapitalOmegaBkgnd(bkgndID, 0, LauFlavTag::Flavour::B, curEvtDecayFlv_) << "\n";
}
*/
this->printPdfValues( BkgndPdfs_[bkgndID], " "+bkgndName );
}
}
}
diff --git a/src/LauTimeDepFlavModel.cc b/src/LauTimeDepFlavModel.cc
index 37b1edf..89771c2 100644
--- a/src/LauTimeDepFlavModel.cc
+++ b/src/LauTimeDepFlavModel.cc
@@ -1,2264 +1,2262 @@
/*
Copyright 2015 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauTimeDepFlavModel.cc
\brief File containing implementation of LauTimeDepFlavModel class.
*/
#include "LauTimeDepFlavModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauAsymmCalc.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDPPartialIntegralInfo.hh"
#include "LauDaughters.hh"
#include "LauDecayTimePdf.hh"
#include "LauFitNtuple.hh"
#include "LauGenNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TFile.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
ClassImp(LauTimeDepFlavModel)
LauTimeDepFlavModel::LauTimeDepFlavModel(LauIsobarDynamics* modelB0bar, LauIsobarDynamics* modelB0, const Bool_t useUntaggedEvents, const TString& tagVarName, const TString& tagCatVarName) : LauAbsFitModel(),
sigModelB0bar_(modelB0bar),
sigModelB0_(modelB0),
kinematicsB0bar_(modelB0bar ? modelB0bar->getKinematics() : 0),
kinematicsB0_(modelB0 ? modelB0->getKinematics() : 0),
useUntaggedEvents_(useUntaggedEvents),
nSigComp_(0),
nSigDPPar_(0),
nDecayTimePar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
coeffsB0bar_(0),
coeffsB0_(0),
coeffPars_(0),
fitFracB0bar_(0),
fitFracB0_(0),
fitFracAsymm_(0),
acp_(0),
meanEffB0bar_("meanEffB0bar",0.0,0.0,1.0),
meanEffB0_("meanEffB0",0.0,0.0,1.0),
DPRateB0bar_("DPRateB0bar",0.0,0.0,100.0),
DPRateB0_("DPRateB0",0.0,0.0,100.0),
signalEvents_(0),
signalAsym_(0),
signalTagCatFrac_(),
tagVarName_(tagVarName),
tagCatVarName_(tagCatVarName),
cpevVarName_(""),
validTagCats_(),
curEvtTagFlv_(0),
curEvtTagCat_(0),
cpEigenValue_(CPEven),
evtTagFlvVals_(0),
evtTagCatVals_(0),
evtCPEigenVals_(0),
dilution_(),
deltaDilution_(),
//deltaM_("deltaM",LauConstants::deltaMd),
deltaM_("deltaM",0.0),
deltaGamma_("deltaGamma",0.0),
tau_("tau",LauConstants::tauB0),
phiMix_("phiMix", 2.0*LauConstants::beta, -LauConstants::threePi, LauConstants::threePi, kFALSE),
sinPhiMix_("sinPhiMix", TMath::Sin(2.0*LauConstants::beta), -3.0, 3.0, kFALSE),
cosPhiMix_("cosPhiMix", TMath::Cos(2.0*LauConstants::beta), -3.0, 3.0, kFALSE),
useSinCos_(kFALSE),
phiMixComplex_(TMath::Cos(-2.0*LauConstants::beta),TMath::Sin(-2.0*LauConstants::beta)),
signalDecayTimePdfs_(),
curEvtDecayTime_(0.0),
curEvtDecayTimeErr_(0.0),
sigExtraPdf_(),
iterationsMax_(500000),
nGenLoop_(0),
ASq_(0.0),
aSqMaxVar_(0.0),
aSqMaxSet_(1.25),
storeGenAmpInfo_(kFALSE),
signalTree_(),
reuseSignal_(kFALSE),
sigDPLike_(0.0),
sigExtraLike_(0.0),
sigTotalLike_(0.0)
{
// Add the untagged category as a valid category
this->addValidTagCat(0);
// Set the fraction, average dilution and dilution difference for the untagged category
this->setSignalTagCatPars(0, 1.0, 0.0, 0.0, kTRUE);
}
LauTimeDepFlavModel::~LauTimeDepFlavModel()
{
// TODO - need to delete the various embedded data structures here
}
void LauTimeDepFlavModel::setupBkgndVectors()
{
}
void LauTimeDepFlavModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : The LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_ = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauTimeDepFlavModel::setNSigEvents(LauParameter* nSigEvents, LauParameter* sigAsym)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : The event LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( sigAsym == nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : The asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
signalAsym_ = sigAsym;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_->name(asymName);
}
void LauTimeDepFlavModel::setNBkgndEvents(LauAbsRValue* /*nBkgndEvents*/)
{
std::cerr << "WARNING in LauTimeDepFlavModel::setNBkgndEvents : This model does not yet support backgrounds" << std::endl;
}
void LauTimeDepFlavModel::addValidTagCats(const std::vector<Int_t>& tagCats)
{
for (std::vector<Int_t>::const_iterator iter = tagCats.begin(); iter != tagCats.end(); ++iter) {
this->addValidTagCat(*iter);
}
}
void LauTimeDepFlavModel::addValidTagCat(Int_t tagCat)
{
validTagCats_.insert(tagCat);
}
void LauTimeDepFlavModel::setSignalTagCatPars(const Int_t tagCat, const Double_t tagCatFrac, const Double_t dilution, const Double_t deltaDilution, const Bool_t fixTCFrac)
{
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setSignalTagCatPars : Tagging category \""<<tagCat<<"\" not valid, not changing the tagging categories parameters."<<std::endl;
return;
}
TString tagCatFracName("signalTagCatFrac"); tagCatFracName += tagCat;
signalTagCatFrac_[tagCat].name(tagCatFracName);
signalTagCatFrac_[tagCat].range(0.0,1.0);
signalTagCatFrac_[tagCat].value(tagCatFrac); signalTagCatFrac_[tagCat].initValue(tagCatFrac); signalTagCatFrac_[tagCat].genValue(tagCatFrac);
signalTagCatFrac_[tagCat].fixed(fixTCFrac);
TString dilutionName("dilution"); dilutionName += tagCat;
dilution_[tagCat].name(dilutionName);
dilution_[tagCat].range(0.0,1.0);
dilution_[tagCat].value(dilution); dilution_[tagCat].initValue(dilution); dilution_[tagCat].genValue(dilution);
TString deltaDilutionName("deltaDilution"); dilutionName += tagCat;
deltaDilution_[tagCat].name(deltaDilutionName);
deltaDilution_[tagCat].range(-2.0,2.0);
deltaDilution_[tagCat].value(deltaDilution); deltaDilution_[tagCat].initValue(deltaDilution); deltaDilution_[tagCat].genValue(deltaDilution);
// We check they're set up correctly with this->checkSignalTagCatFractions(); only when the user has
//set them all up, in this->initialise();
}
void LauTimeDepFlavModel::checkSignalTagCatFractions()
{
Double_t totalTaggedFrac(0.0);
for (LauTagCatParamMap::const_iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
if (iter->first != 0) {
const LauParameter& par = iter->second;
totalTaggedFrac += par.value();
}
}
if ( ((totalTaggedFrac < (1.0-1.0e-8))&&!useUntaggedEvents_) || (totalTaggedFrac > (1.0+1.0e-8)) ) {
std::cerr<<"WARNING in LauTimeDepFlavModel::checkSignalTagCatFractions : Tagging category fractions add up to "<<totalTaggedFrac<<", not 1.0; normalizing them."<<std::endl;
for (LauTagCatParamMap::iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
LauParameter& par = iter->second;
Double_t newVal = par.value() / totalTaggedFrac;
par.value(newVal); par.initValue(newVal); par.genValue(newVal);
}
} else if (useUntaggedEvents_) {
Double_t tagCatFrac = 1.0 - totalTaggedFrac;
TString tagCatFracName("signalTagCatFrac0");
signalTagCatFrac_[0].name(tagCatFracName);
signalTagCatFrac_[0].range(0.0,1.0);
signalTagCatFrac_[0].value(tagCatFrac); signalTagCatFrac_[0].initValue(tagCatFrac); signalTagCatFrac_[0].genValue(tagCatFrac);
signalTagCatFrac_[0].fixed(kTRUE);
TString dilutionName("dilution0");
dilution_[0].name(dilutionName);
dilution_[0].range(0.0,1.0);
dilution_[0].value(0.0); dilution_[0].initValue(0.0); dilution_[0].genValue(0.0);
TString deltaDilutionName("deltaDilution0");
deltaDilution_[0].name(deltaDilutionName);
deltaDilution_[0].range(-2.0,2.0);
deltaDilution_[0].value(0.0); deltaDilution_[0].initValue(0.0); deltaDilution_[0].genValue(0.0);
}
for (LauTagCatParamMap::const_iterator iter=dilution_.begin(); iter!=dilution_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepFlavModel::checkSignalTagCatFractions : Setting dilution for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
for (LauTagCatParamMap::const_iterator iter=deltaDilution_.begin(); iter!=deltaDilution_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepFlavModel::checkSignalTagCatFractions : Setting Delta(dilution) for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
for (LauTagCatParamMap::const_iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepFlavModel::checkSignalTagCatFractions : Setting (signal) fraction for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
}
void LauTimeDepFlavModel::setSignalDtPdf(Int_t tagCat, LauDecayTimePdf* pdf)
{
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setSignalDtPdf : Tagging category \""<<tagCat<<"\" not valid, not adding PDF."<<std::endl;
return;
}
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setSignalDtPdf : The PDF pointer is null, not adding it."<<std::endl;
return;
}
signalDecayTimePdfs_[tagCat] = pdf;
}
void LauTimeDepFlavModel::setSignalPdfs(Int_t tagCat, LauAbsPdf* pdf)
{
// These "extra variables" are assumed to be purely kinematical, like mES and DeltaE
//or making use of Rest of Event information, and therefore independent of whether
//the parent is a B0 or a B0bar. If this assupmtion doesn't hold, do modify this part!
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setSignalPdfs : Tagging category \""<<tagCat<<"\" not valid, not adding models."<<std::endl;
return;
}
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setSignalPdfs : The PDF pointer is null."<<std::endl;
return;
}
sigExtraPdf_[tagCat].push_back(pdf);
}
void LauTimeDepFlavModel::setPhiMix(Double_t phiMix, Bool_t fixPhiMix, Bool_t useSinCos)
{
phiMix_.value(phiMix); phiMix_.initValue(phiMix); phiMix_.genValue(phiMix); phiMix_.fixed(fixPhiMix);
Double_t sinPhiMix = TMath::Sin(phiMix);
sinPhiMix_.value(sinPhiMix); sinPhiMix_.initValue(sinPhiMix); sinPhiMix_.genValue(sinPhiMix); sinPhiMix_.fixed(fixPhiMix);
Double_t cosPhiMix = TMath::Cos(phiMix);
cosPhiMix_.value(cosPhiMix); cosPhiMix_.initValue(cosPhiMix); cosPhiMix_.genValue(cosPhiMix); cosPhiMix_.fixed(fixPhiMix);
useSinCos_ = useSinCos;
phiMixComplex_.setRealPart(cosPhiMix);
phiMixComplex_.setImagPart(-1.0*sinPhiMix);
}
void LauTimeDepFlavModel::initialise()
{
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// Initialisation
if (this->useDP() == kTRUE) {
this->initialiseDPModels();
}
if (!this->useDP() && sigExtraPdf_.empty()) {
std::cerr<<"ERROR in LauTimeDepFlavModel::initialise : Signal model doesn't exist for any variable."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (this->useDP() == kTRUE) {
// Check that we have all the Dalitz-plot models
if ((sigModelB0bar_ == 0) || (sigModelB0_ == 0)) {
std::cerr<<"ERROR in LauTimeDepFlavModel::initialise : the pointer to one (particle or anti-particle) of the signal DP models is null."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Check here that the tagging category fractions add up to 1, otherwise "normalise". Also set up the untagged cat.
// NB this has to be done early in the initialization as other methods access the tagCats map.
this->checkSignalTagCatFractions();
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the decay time models
this->setDecayTimeParameters();
// Set the fit parameters for the extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nDecayTimePar_ + nExtraPdfPar_ + nNormPar_)) {
std::cerr<<"ERROR in LauTimeDepFlavModel::initialise : Number of fit parameters not of expected size."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauTimeDepFlavModel::recalculateNormalisation()
{
sigModelB0bar_->recalculateNormalisation();
sigModelB0_->recalculateNormalisation();
sigModelB0bar_->modifyDataTree();
sigModelB0_->modifyDataTree();
this->calcInterferenceTermIntegrals();
}
void LauTimeDepFlavModel::initialiseDPModels()
{
if (sigModelB0bar_ == 0) {
std::cerr<<"ERROR in LauTimeDepFlavModel::initialiseDPModels : B0bar signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (sigModelB0_ == 0) {
std::cerr<<"ERROR in LauTimeDepFlavModel::initialiseDPModels : B0 signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
// Need to check that the number of components we have and that the dynamics has matches up
//const UInt_t nAmpB0bar = sigModelB0bar_->getnAmp();
//const UInt_t nAmpB0 = sigModelB0_->getnAmp();
const UInt_t nAmpB0bar = sigModelB0bar_->getnTotAmp();
const UInt_t nAmpB0 = sigModelB0_->getnTotAmp();
if ( nAmpB0bar != nAmpB0 ) {
std::cerr << "ERROR in LauTimeDepFlavModel::initialiseDPModels : Unequal number of signal DP components in the particle and anti-particle models: " << nAmpB0bar << " != " << nAmpB0 << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( nAmpB0bar != nSigComp_ ) {
std::cerr << "ERROR in LauTimeDepFlavModel::initialiseDPModels : Number of signal DP components in the model (" << nAmpB0bar << ") not equal to number of coefficients supplied (" << nSigComp_ << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
std::cout<<"INFO in LauTimeDepFlavModel::initialiseDPModels : Initialising signal DP model"<<std::endl;
sigModelB0bar_->initialise(coeffsB0bar_);
sigModelB0_->initialise(coeffsB0_);
fifjEffSum_.clear();
fifjEffSum_.resize(nSigComp_);
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
fifjEffSum_[iAmp].resize(nSigComp_);
}
// calculate the integrals of the A*Abar terms
this->calcInterferenceTermIntegrals();
this->calcInterTermNorm();
}
void LauTimeDepFlavModel::calcInterferenceTermIntegrals()
{
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0bar = sigModelB0bar_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0 = sigModelB0_->getIntegralInfos();
// TODO should check (first time) that they match in terms of number of entries in the vectors and that each entry has the same number of points, ranges, weights etc.
LauComplex A, Abar, fifjEffSumTerm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
fifjEffSum_[iAmp][jAmp].zero();
}
}
const UInt_t nIntegralRegions = integralInfoListB0bar.size();
for ( UInt_t iRegion(0); iRegion < nIntegralRegions; ++iRegion ) {
const LauDPPartialIntegralInfo* integralInfoB0bar = integralInfoListB0bar[iRegion];
const LauDPPartialIntegralInfo* integralInfoB0 = integralInfoListB0[iRegion];
const UInt_t nm13Points = integralInfoB0bar->getnm13Points();
const UInt_t nm23Points = integralInfoB0bar->getnm23Points();
for (UInt_t m13 = 0; m13 < nm13Points; ++m13) {
for (UInt_t m23 = 0; m23 < nm23Points; ++m23) {
const Double_t weight = integralInfoB0bar->getWeight(m13,m23);
const Double_t eff = integralInfoB0bar->getEfficiency(m13,m23);
const Double_t effWeight = eff*weight;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
A = integralInfoB0->getAmplitude(m13, m23, iAmp);
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
Abar = integralInfoB0bar->getAmplitude(m13, m23, jAmp);
fifjEffSumTerm = Abar*A.conj();
fifjEffSumTerm.rescale(effWeight);
fifjEffSum_[iAmp][jAmp] += fifjEffSumTerm;
}
}
}
}
}
}
void LauTimeDepFlavModel::calcInterTermNorm()
{
const std::vector<Double_t> fNormB0bar = sigModelB0bar_->getFNorm();
const std::vector<Double_t> fNormB0 = sigModelB0_->getFNorm();
LauComplex norm;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
LauComplex coeffTerm = coeffsB0bar_[jAmp]*coeffsB0_[iAmp].conj();
coeffTerm *= fifjEffSum_[iAmp][jAmp];
coeffTerm.rescale(fNormB0bar[jAmp] * fNormB0[iAmp]);
norm += coeffTerm;
}
}
norm *= phiMixComplex_;
interTermReNorm_ = 2.0*norm.re();
interTermImNorm_ = 2.0*norm.im();
}
void LauTimeDepFlavModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
// Resize the coeffPars vector if not already done
if ( coeffPars_.empty() ) {
const UInt_t nAmpB0bar { sigModelB0bar_->getnTotAmp() };
const UInt_t nAmpB0 { sigModelB0_->getnTotAmp() };
if ( nAmpB0bar != nAmpB0 ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setAmpCoeffSet : Unequal number of signal DP components in the B and Bbar models: " << nAmpB0 << " != " << nAmpB0bar << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
coeffPars_.resize( nAmpB0 );
fitFracAsymm_.resize( nAmpB0 );
acp_.resize( nAmpB0 );
}
// Is there a component called compName in the signal models?
TString compName { coeffSet->name() };
TString conjName { sigModelB0bar_->getConjResName(compName) };
const LauDaughters* daughtersB0bar { sigModelB0bar_->getDaughters() };
const LauDaughters* daughtersB0 { sigModelB0_->getDaughters() };
const Bool_t conjugate { daughtersB0bar->isConjugate( daughtersB0 ) };
if ( ! sigModelB0bar_->hasResonance(compName) ) {
if ( ! sigModelB0bar_->hasResonance(conjName) ) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setAmpCoeffSet : B0bar signal DP model doesn't contain component \""<<compName<<"\"."<<std::endl;
return;
}
std::cerr<<"WARNING in LauTimeDepFlavModel::setAmpCoeffSet : B0bar signal DP model doesn't contain component \""<<compName<<"\" but does contain the conjugate \""<<conjName<<"\", restting name to use the conjugate."<<std::endl;
std::swap( compName, conjName );
coeffSet->name( compName );
}
Int_t indexB0bar { sigModelB0bar_->resonanceIndex(compName) };
Int_t indexB0 { -1 };
if ( conjugate ) {
if ( ! sigModelB0_->hasResonance(conjName) ) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setAmpCoeffSet : B0 signal DP model doesn't contain component \""<<conjName<<"\"."<<std::endl;
return;
}
indexB0 = sigModelB0_->resonanceIndex(conjName);
} else {
if ( ! sigModelB0_->hasResonance(compName) ) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setAmpCoeffSet : B0 signal DP model doesn't contain component \""<<compName<<"\"."<<std::endl;
return;
}
indexB0 = sigModelB0_->resonanceIndex(compName);
}
if ( indexB0 != indexB0bar ) {
std::cerr << "ERROR in LauTimeDepFlavModel::setAmpCoeffSet : B0 signal DP model and B0bar signal DP model have different indices for component \"" << compName << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
if ( coeffPars_[indexB0] != nullptr && coeffPars_[indexB0]->name() == compName) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setAmpCoeffSet : Have already set coefficients for \""<<compName<<"\"."<<std::endl;
return;
}
coeffSet->index(indexB0);
std::cout<<"INFO in LauTimeDepFlavModel::setAmpCoeffSet : Added coefficients for component \""<<compName<<"\" to the fit model."<<std::endl;
coeffSet->printParValues();
const TString parName { coeffSet->baseName() + "FitFracAsym" };
fitFracAsymm_[indexB0] = LauParameter{parName, 0.0, -1.0, 1.0};
acp_[indexB0] = coeffSet->acp();
coeffPars_[indexB0] = std::move(coeffSet);
++nSigComp_;
}
std::vector<const LauAbsCoeffSet*> LauTimeDepFlavModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_.size(), nullptr );
std::transform( coeffPars_.begin(), coeffPars_.end(), coeffs.begin(), [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); } );
return coeffs;
}
void LauTimeDepFlavModel::calcAsymmetries(Bool_t initValues)
{
// Calculate the CP asymmetries
// Also calculate the fit fraction asymmetries
for (UInt_t i = 0; i < nSigComp_; i++) {
acp_[i] = coeffPars_[i]->acp();
LauAsymmCalc asymmCalc(fitFracB0bar_[i][i].value(), fitFracB0_[i][i].value());
Double_t asym = asymmCalc.getAsymmetry();
fitFracAsymm_[i].value(asym);
if (initValues) {
fitFracAsymm_[i].genValue(asym);
fitFracAsymm_[i].initValue(asym);
}
}
}
void LauTimeDepFlavModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauTimeDepFlavModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; i++) {
LauParameterPList pars = coeffPars_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit variables and in a separate vector
// Need to make sure that they are unique because some might appear in both DP models
LauParameterPList& sigDPParsB0bar = sigModelB0bar_->getFloatingParameters();
LauParameterPList& sigDPParsB0 = sigModelB0_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0bar );
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0 );
}
UInt_t LauTimeDepFlavModel::addParametersToFitList(LauTagCatDtPdfMap& theMap)
{
UInt_t counter(0);
// loop through the map
for (LauTagCatDtPdfMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
// grab the pdf and then its parameters
LauDecayTimePdf* thePdf = (*iter).second; // The first one is the tagging category
LauAbsRValuePList& rvalues = thePdf->getParameters();
counter += this->addFitParameters(rvalues);
}
return counter;
}
UInt_t LauTimeDepFlavModel::addParametersToFitList(LauTagCatPdfMap& theMap)
{
UInt_t counter(0);
// loop through the map
for (LauTagCatPdfMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
counter += this->addFitParameters(iter->second); // first is the tagging category
}
return counter;
}
void LauTimeDepFlavModel::setDecayTimeParameters()
{
nDecayTimePar_ = 0;
// Loop over the Dt PDFs
nDecayTimePar_ += this->addParametersToFitList(signalDecayTimePdfs_);
if (useSinCos_) {
nDecayTimePar_ += this->addFitParameters( &sinPhiMix_ );
nDecayTimePar_ += this->addFitParameters( &cosPhiMix_ );
} else {
nDecayTimePar_ += this->addFitParameters( &phiMix_ );
}
}
void LauTimeDepFlavModel::setExtraPdfParameters()
{
// Include the parameters of the PDF for each tagging category in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
nExtraPdfPar_ += this->addParametersToFitList(sigExtraPdf_);
}
void LauTimeDepFlavModel::setFitNEvents()
{
nNormPar_ = 0;
// Initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the signal fraction into the fit parameters
if (this->doEMLFit()) {
std::cout<<"INFO in LauTimeDepFlavModel::setFitNEvents : Initialising number of events for signal and background components..."<<std::endl;
nNormPar_ += this->addFitParameters( signalEvents_ );
} else {
std::cout<<"INFO in LauTimeDepFlavModel::setFitNEvents : Initialising number of events for background components (and hence signal)..."<<std::endl;
}
// if not using the DP in the model we need an explicit signal asymmetry parameter
if (this->useDP() == kFALSE) {
nNormPar_ += this->addFitParameters( signalAsym_ );
}
// tagging-category fractions for signal events
for (LauTagCatParamMap::iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
if (iter == signalTagCatFrac_.begin()) {
continue;
}
LauParameter* par = &((*iter).second);
nNormPar_ += this->addFitParameters( par );
}
}
void LauTimeDepFlavModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the B0 and B0bar fit fractions for each signal component
fitFracB0bar_ = sigModelB0bar_->getFitFractions();
if (fitFracB0bar_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0bar_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0bar" );
fitFracB0bar_[i][j].name(name);
extraVars.push_back(fitFracB0bar_[i][j]);
}
}
fitFracB0_ = sigModelB0_->getFitFractions();
if (fitFracB0_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0" );
fitFracB0_[i][j].name(name);
extraVars.push_back(fitFracB0_[i][j]);
}
}
// Calculate the ACPs and FitFrac asymmetries
this->calcAsymmetries(kTRUE);
// Add the Fit Fraction asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(fitFracAsymm_[i]);
}
// Add the calculated CP asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(acp_[i]);
}
// Now add in the DP efficiency values
Double_t initMeanEffB0bar = sigModelB0bar_->getMeanEff().initValue();
meanEffB0bar_.value(initMeanEffB0bar); meanEffB0bar_.initValue(initMeanEffB0bar); meanEffB0bar_.genValue(initMeanEffB0bar);
extraVars.push_back(meanEffB0bar_);
Double_t initMeanEffB0 = sigModelB0_->getMeanEff().initValue();
meanEffB0_.value(initMeanEffB0); meanEffB0_.initValue(initMeanEffB0); meanEffB0_.genValue(initMeanEffB0);
extraVars.push_back(meanEffB0_);
// Also add in the DP rates
Double_t initDPRateB0bar = sigModelB0bar_->getDPRate().initValue();
DPRateB0bar_.value(initDPRateB0bar); DPRateB0bar_.initValue(initDPRateB0bar); DPRateB0bar_.genValue(initDPRateB0bar);
extraVars.push_back(DPRateB0bar_);
Double_t initDPRateB0 = sigModelB0_->getDPRate().initValue();
DPRateB0_.value(initDPRateB0); DPRateB0_.initValue(initDPRateB0); DPRateB0_.genValue(initDPRateB0);
extraVars.push_back(DPRateB0_);
}
void LauTimeDepFlavModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// i.e. to make mag > 0.0, phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; ++i) {
// Check whether we have "a > 0.0", and phases in the right range
coeffPars_[i]->finaliseValues();
}
}
// update the pulls on the event fractions and asymmetries
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (this->useDP() == kFALSE) {
signalAsym_->updatePull();
}
// Finalise the pulls on the decay time parameters
for (LauTagCatDtPdfMap::iterator iter = signalDecayTimePdfs_.begin(); iter != signalDecayTimePdfs_.end(); ++iter) {
LauDecayTimePdf* pdf = (*iter).second;
pdf->updatePulls();
}
if (useSinCos_) {
cosPhiMix_.updatePull();
sinPhiMix_.updatePull();
} else {
this->checkMixingPhase();
}
// Update the pulls on all the extra PDFs' parameters
for (LauTagCatPdfMap::iterator iter = sigExtraPdf_.begin(); iter != sigExtraPdf_.end(); ++iter) {
this->updateFitParameters(iter->second);
}
// Tagging-category fractions for signal and background events
Double_t firstCatFrac(1.0);
Int_t firstCat(0);
for (LauTagCatParamMap::iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
if (iter == signalTagCatFrac_.begin()) {
firstCat = iter->first;
continue;
}
LauParameter& par = (*iter).second;
firstCatFrac -= par.value();
// update the parameter pull
par.updatePull();
}
signalTagCatFrac_[firstCat].value(firstCatFrac);
signalTagCatFrac_[firstCat].updatePull();
// Fill the fit results to the ntuple
// update the coefficients and then calculate the fit fractions and ACP's
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_->updateCoeffs(coeffsB0bar_); sigModelB0bar_->calcExtraInfo();
sigModelB0_->updateCoeffs(coeffsB0_); sigModelB0_->calcExtraInfo();
LauParArray fitFracB0bar = sigModelB0bar_->getFitFractions();
if (fitFracB0bar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0 = sigModelB0_->getFitFractions();
if (fitFracB0.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_[i][j].value(fitFracB0bar[i][j].value());
fitFracB0_[i][j].value(fitFracB0[i][j].value());
}
}
meanEffB0bar_.value(sigModelB0bar_->getMeanEff().value());
meanEffB0_.value(sigModelB0_->getMeanEff().value());
DPRateB0bar_.value(sigModelB0bar_->getDPRate().value());
DPRateB0_.value(sigModelB0_->getDPRate().value());
this->calcAsymmetries();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions, FF asymmetries, ACPs, mean efficiency and DP rate)
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0bar_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(fitFracAsymm_[i]);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(acp_[i]);
}
extraVars.push_back(meanEffB0bar_);
extraVars.push_back(meanEffB0_);
extraVars.push_back(DPRateB0bar_);
extraVars.push_back(DPRateB0_);
this->printFitFractions(std::cout);
this->printAsymmetries(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauTimeDepFlavModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
// First for the B0bar events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"B0bar FitFraction for component "<<i<<" ("<<compName<<") = "<<fitFracB0bar_[i][i].value()<<std::endl;
}
output<<"B0bar overall DP rate (integral of matrix element squared) = "<<DPRateB0bar_.value()<<std::endl;
output<<"B0bar average efficiency weighted by whole DP dynamics = "<<meanEffB0bar_.value()<<std::endl;
// Then for the B0 sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
const TString conjName(sigModelB0bar_->getConjResName(compName));
output<<"B0 FitFraction for component "<<i<<" ("<<conjName<<") = "<<fitFracB0_[i][i].value()<<std::endl;
}
output<<"B0 overall DP rate (integral of matrix element squared) = "<<DPRateB0_.value()<<std::endl;
output<<"B0 average efficiency weighted by whole DP dynamics = "<<meanEffB0_.value()<<std::endl;
}
void LauTimeDepFlavModel::printAsymmetries(std::ostream& output)
{
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"Fit Fraction asymmetry for component "<<i<<" ("<<compName<<") = "<<fitFracAsymm_[i].value()<<std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_[i]->name());
output<<"ACP for component "<<i<<" ("<<compName<<") = "<<acp_[i].value()<<" +- "<<acp_[i].error()<<std::endl;
}
}
void LauTimeDepFlavModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
std::ofstream fout(outputFile);
LauPrint print;
std::cout<<"INFO in LauTimeDepFlavModel::writeOutTable : Writing out results of the fit to the tex file "<<outputFile<<std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->printTableRow(fout);
}
fout<<"\\hline"<<std::endl;
fout<<"\\end{tabular}"<<std::endl<<std::endl;
// print the fit fractions in another
fout<<"\\begin{tabular}{|l|c|c|c|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"Component & \\Bzb\\ Fit Fraction & \\Bz\\ Fit Fraction & Fit Fraction Asymmetry & $A_{\\CP}$ \\\\"<<std::endl;
fout<<"\\hline"<<std::endl;
Double_t fitFracSumB0bar(0.0);
Double_t fitFracSumB0(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
fout<<resName<<" & $";
Double_t fitFracB0bar = fitFracB0bar_[i][i].value();
fitFracSumB0bar += fitFracB0bar;
print.printFormat(fout, fitFracB0bar);
fout << "$ & $" << std::endl;
Double_t fitFracB0 = fitFracB0_[i][i].value();
fitFracSumB0 += fitFracB0;
print.printFormat(fout, fitFracB0);
fout << "$ & $" << std::endl;
Double_t fitFracAsymm = fitFracAsymm_[i].value();
print.printFormat(fout, fitFracAsymm);
fout << "$ & $" << std::endl;
Double_t acp = acp_[i].value();
Double_t acpErr = acp_[i].error();
print.printFormat(fout, acp);
fout<<" \\pm ";
print.printFormat(fout, acpErr);
fout<<"$ \\\\"<<std::endl;
}
fout<<"\\hline"<<std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum = & $";
print.printFormat(fout, fitFracSumB0bar);
fout << "$ & $";
print.printFormat(fout, fitFracSumB0);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline \n\\hline" << std::endl;
fout << "DP rate = & $";
print.printFormat(fout, DPRateB0bar_.value());
fout << "$ & $";
print.printFormat(fout, DPRateB0_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > =$ & $";
print.printFormat(fout, meanEffB0bar_.value());
fout << "$ & $";
print.printFormat(fout, meanEffB0_.value());
fout << "$ & & \\\\" << std::endl;
if (useSinCos_) {
fout << "$\\sinPhiMix =$ & $";
print.printFormat(fout, sinPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, sinPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
fout << "$\\cosPhiMix =$ & $";
print.printFormat(fout, cosPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, cosPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
} else {
fout << "$\\phiMix =$ & $";
print.printFormat(fout, phiMix_.value());
fout << " \\pm ";
print.printFormat(fout, phiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
}
fout << "\\hline \n\\end{tabular}" << std::endl;
}
if (!sigExtraPdf_.empty()) {
fout<<"\\begin{tabular}{|l|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"\\Extra Signal PDFs' Parameters: & \\\\"<<std::endl;
for (LauTagCatPdfMap::const_iterator iter = sigExtraPdf_.begin(); iter != sigExtraPdf_.end(); ++iter) {
this->printFitParameters(iter->second, fout);
}
fout<<"\\hline \n\\end{tabular}"<<std::endl<<std::endl;
}
}
void LauTimeDepFlavModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
this->randomiseInitFitPars();
}
}
void LauTimeDepFlavModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout<<"INFO in LauTimeDepFlavModel::randomiseInitFitPars : Randomising the initial values of the coefficients of the DP components (and phiMix)..."<<std::endl;
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_[i]->randomiseInitValues();
}
phiMix_.randomiseValue(-LauConstants::pi, LauConstants::pi);
if (useSinCos_) {
sinPhiMix_.initValue(TMath::Sin(phiMix_.initValue()));
cosPhiMix_.initValue(TMath::Cos(phiMix_.initValue()));
}
}
LauTimeDepFlavModel::LauGenInfo LauTimeDepFlavModel::eventsToGenerate()
{
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
// NB this individual smearing has to be done individually per tagging category as well
LauGenInfo nEvtsGen;
LauTagCatGenInfo eventsB0, eventsB0bar;
// Signal
// If we're including the DP and decay time we can't decide on the tag
// yet, since it depends on the whole DP+dt PDF, however, if
// we're not then we need to decide.
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0.0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
Double_t sigAsym(0.0);
if (this->useDP() == kFALSE) {
sigAsym = signalAsym_->genValue();
for (LauTagCatParamMap::const_iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
const LauParameter& par = iter->second;
Double_t eventsbyTagCat = par.value() * nEvts;
Double_t eventsB0byTagCat = TMath::Nint(eventsbyTagCat/2.0 * (1.0 - sigAsym));
Double_t eventsB0barbyTagCat = TMath::Nint(eventsbyTagCat/2.0 * (1.0 + sigAsym));
if (this->doPoissonSmearing()) {
eventsB0byTagCat = LauRandom::randomFun()->Poisson(eventsB0byTagCat);
eventsB0barbyTagCat = LauRandom::randomFun()->Poisson(eventsB0barbyTagCat);
}
eventsB0[iter->first] = std::make_pair( TMath::Nint(eventsB0byTagCat), evtWeight );
eventsB0bar[iter->first] = std::make_pair( TMath::Nint(eventsB0barbyTagCat), evtWeight );
}
nEvtsGen[std::make_pair("signal",-1)] = eventsB0;
nEvtsGen[std::make_pair("signal",+1)] = eventsB0bar;
} else {
Double_t rateB0bar = sigModelB0bar_->getDPRate().value();
Double_t rateB0 = sigModelB0_->getDPRate().value();
if ( rateB0bar+rateB0 > 1e-30) {
sigAsym = (rateB0bar-rateB0)/(rateB0bar+rateB0);
}
for (LauTagCatParamMap::const_iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
const LauParameter& par = iter->second;
Double_t eventsbyTagCat = par.value() * nEvts;
if (this->doPoissonSmearing()) {
eventsbyTagCat = LauRandom::randomFun()->Poisson(eventsbyTagCat);
}
eventsB0[iter->first] = std::make_pair( TMath::Nint(eventsbyTagCat), evtWeight );
}
nEvtsGen[std::make_pair("signal",0)] = eventsB0; // generate signal event, decide tag later.
}
std::cout<<"INFO in LauTimeDepFlavModel::eventsToGenerate : Generating toy MC with:"<<std::endl;
std::cout<<" : Signal asymmetry = "<<sigAsym<<" and number of signal events = "<<signalEvents_->genValue()<<std::endl;
return nEvtsGen;
}
Bool_t LauTimeDepFlavModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
LauGenInfo nEvts = this->eventsToGenerate();
Bool_t genOK(kTRUE);
Int_t evtNum(0);
// Loop over the hypotheses and generate the appropriate number of
// events for each one
for (LauGenInfo::const_iterator iter = nEvts.begin(); iter != nEvts.end(); ++iter) {
// find the category of events (e.g. signal)
const TString& evtCategory(iter->first.first);
// find the true flavour
curEvtTagFlv_ = iter->first.second;
// loop through each tagging category
const LauTagCatGenInfo& eventsByTagCat = iter->second;
for (LauTagCatGenInfo::const_iterator tagCatIter = eventsByTagCat.begin(); tagCatIter != eventsByTagCat.end(); ++tagCatIter) {
// get the tagging category
curEvtTagCat_ = tagCatIter->first;
// get the event weight for this category
const Double_t evtWeight( tagCatIter->second.second );
// get the number of events to generate in this configuration
const Int_t nEvtsGen( tagCatIter->second.first );
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
if (evtCategory == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
// All the generate*Event() methods have to fill in curEvtDecayTime_ and curEvtDecayTimeErr_
// In addition, generateSignalEvent has to decide on the tag and fill in curEvtTagFlv_
genOK = this->generateSignalEvent();
} else {
genOK = kFALSE;
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPDtBranchValues(); // store DP, decay time and tagging variables in the ntuple
}
// Store the event's tag and tagging category
this->setGenNtupleIntegerBranchValue("cpEigenvalue", cpEigenValue_);
this->setGenNtupleIntegerBranchValue("tagCat",curEvtTagCat_);
this->setGenNtupleIntegerBranchValue("tagFlv",curEvtTagFlv_);
// Store the event number (within this experiment)
// and then increment it
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
++evtNum;
// Write the values into the tree
this->fillGenNtupleBranches();
// Print an occasional progress message
if (iEvt%1000 == 0) {std::cout<<"INFO in LauTimeDepFlavModel::genExpt : Generated event number "<<iEvt<<" out of "<<nEvtsGen<<" "<<evtCategory<<" events."<<std::endl;}
} // end of loop over events for given species, tagCat and tagFlv
if (!genOK) {
break;
}
} // end of loop over tagCats for the given species and tagFlv
if (!genOK) {
break;
}
} //end of loop over species and tagFlv.
if (this->useDP() && genOK) {
sigModelB0bar_->checkToyMC(kTRUE);
sigModelB0_->checkToyMC(kTRUE);
std::cout<<"aSqMaxSet = "<<aSqMaxSet_<<" and aSqMaxVar = "<<aSqMaxVar_<<std::endl;
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray fitFracB0bar = sigModelB0bar_->getFitFractions();
if (fitFracB0bar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0 = sigModelB0_->getFitFractions();
if (fitFracB0.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_[i][j].value(fitFracB0bar[i][j].value());
fitFracB0_[i][j].value(fitFracB0[i][j].value());
}
}
meanEffB0bar_.value(sigModelB0bar_->getMeanEff().value());
meanEffB0_.value(sigModelB0_->getMeanEff().value());
DPRateB0bar_.value(sigModelB0bar_->getDPRate().value());
DPRateB0_.value(sigModelB0_->getDPRate().value());
}
}
// If we're reusing embedded events or if the generation is being
// reset then clear the lists of used events
//if (!signalTree_.empty() && (reuseSignal_ || !genOK)) {
if (reuseSignal_ || !genOK) {
for(LauTagCatEmbDataMap::const_iterator iter = signalTree_.begin(); iter != signalTree_.end(); ++iter) {
(iter->second)->clearUsedList();
}
}
return genOK;
}
Bool_t LauTimeDepFlavModel::generateSignalEvent()
{
// Generate signal event, including SCF if necessary.
// DP:DecayTime generation follows.
// If it's ok, we then generate mES, DeltaE, Fisher/NN...
Bool_t genOK(kTRUE);
Bool_t generatedEvent(kFALSE);
Bool_t doSquareDP = kinematicsB0bar_->squareDP();
doSquareDP &= kinematicsB0_->squareDP();
LauKinematics* kinematics(kinematicsB0bar_);
// find the right decay time PDF for the current tagging category
LauTagCatDtPdfMap::const_iterator dt_iter = signalDecayTimePdfs_.find(curEvtTagCat_);
LauDecayTimePdf* decayTimePdf = (dt_iter != signalDecayTimePdfs_.end()) ? dt_iter->second : 0;
// find the right embedded data for the current tagging category
LauTagCatEmbDataMap::const_iterator emb_iter = signalTree_.find(curEvtTagCat_);
LauEmbeddedData* embeddedData = (emb_iter != signalTree_.end()) ? emb_iter->second : 0;
// find the right extra PDFs for the current tagging category
LauTagCatPdfMap::iterator extra_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* extraPdfs = (extra_iter != sigExtraPdf_.end()) ? &(extra_iter->second) : 0;
if (this->useDP()) {
if (embeddedData) {
embeddedData->getEmbeddedEvent(kinematics);
curEvtTagFlv_ = TMath::Nint(embeddedData->getValue("tagFlv"));
curEvtDecayTimeErr_ = embeddedData->getValue(decayTimePdf->varErrName());
curEvtDecayTime_ = embeddedData->getValue(decayTimePdf->varName());
if (embeddedData->haveBranch("mcMatch")) {
Int_t match = TMath::Nint(embeddedData->getValue("mcMatch"));
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
}
}
} else {
nGenLoop_ = 0;
// generate the decay time error (NB the kTRUE forces the generation of a new value)
curEvtDecayTimeErr_ = decayTimePdf->generateError(kTRUE);
while (generatedEvent == kFALSE && nGenLoop_ < iterationsMax_) {
// Calculate the unnormalised truth-matched signal likelihood
// First let define the tag flavour
Double_t randNo = LauRandom::randomFun()->Rndm();
if (randNo < 0.5) {
curEvtTagFlv_ = +1; // B0 tag
} else {
curEvtTagFlv_ = -1; // B0bar tag
}
// Calculate event quantities that depend only on the tagCat and tagFlv
Double_t qD = curEvtTagFlv_*dilution_[curEvtTagCat_].unblindValue();
Double_t qDDo2 = curEvtTagFlv_*0.5*deltaDilution_[curEvtTagCat_].unblindValue();
// Generate the DP position
Double_t m13Sq(0.0), m23Sq(0.0);
kinematicsB0bar_->genFlatPhaseSpace(m13Sq, m23Sq);
// Next, calculate the total A and Abar for the given DP position
sigModelB0_->calcLikelihoodInfo(m13Sq, m23Sq);
sigModelB0bar_->calcLikelihoodInfo(m13Sq, m23Sq);
// Retrieve the amplitudes and efficiency from the dynamics
const LauComplex& Abar = sigModelB0bar_->getEvtDPAmp();
const LauComplex& A = sigModelB0_->getEvtDPAmp();
Double_t eff = sigModelB0bar_->getEvtEff();
// Next calculate the DP terms
Double_t aSqSum = A.abs2() + Abar.abs2();
Double_t aSqDif = A.abs2() - Abar.abs2();
LauComplex inter = Abar * A.conj() * phiMixComplex_;
Double_t interTermIm = 2.0 * inter.im();
Double_t interTermRe = 2.0 * inter.re();
// Generate decay time
const Double_t tMin = decayTimePdf->minAbscissa();
const Double_t tMax = decayTimePdf->maxAbscissa();
curEvtDecayTime_ = LauRandom::randomFun()->Rndm()*(tMax-tMin) + tMin;
// Calculate all the decay time info
decayTimePdf->calcLikelihoodInfo(curEvtDecayTime_, curEvtDecayTimeErr_);
// First get all the decay time terms
//Double_t dtExp = decayTimePdf->getExpTerm();
Double_t dtCos = decayTimePdf->getCosTerm();
Double_t dtSin = decayTimePdf->getSinTerm();
Double_t dtCosh = decayTimePdf->getCoshTerm();
Double_t dtSinh = decayTimePdf->getSinhTerm();
// Combine all terms
Double_t cosTerm = dtCos * qD * aSqDif;
Double_t sinTerm = dtSin * qD * interTermIm;
Double_t coshTerm = dtCosh * (1.0 + qDDo2) * aSqSum;
Double_t sinhTerm = dtSinh * (1.0 + qDDo2) * interTermRe;
if ( cpEigenValue_ == CPOdd ) {
sinTerm *= -1.0;
sinhTerm *= -1.0;
}
// ... to get the total and multiply by the efficiency
Double_t ASq = coshTerm + cosTerm - sinTerm + sinhTerm;
//ASq /= decayTimePdf->getNormTerm();
ASq *= eff;
//Finally we throw the dice to see whether this event should be generated
//We make a distinction between the likelihood of TM and SCF to tag the SCF events as such
randNo = LauRandom::randomFun()->Rndm();
if (randNo <= ASq/aSqMaxSet_ ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ASq > aSqMaxVar_) {aSqMaxVar_ = ASq;}
} else {
nGenLoop_++;
}
} // end of while !generatedEvent loop
} // end of if (embeddedData) else control
} else {
if ( embeddedData ) {
embeddedData->getEmbeddedEvent(0);
curEvtTagFlv_ = TMath::Nint(embeddedData->getValue("tagFlv"));
curEvtDecayTimeErr_ = embeddedData->getValue(decayTimePdf->varErrName());
curEvtDecayTime_ = embeddedData->getValue(decayTimePdf->varName());
}
}
// Check whether we have generated the toy MC OK.
if (nGenLoop_ >= iterationsMax_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepFlavModel::generateSignalEvent : Hit max iterations: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
} else if (aSqMaxVar_ > aSqMaxSet_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepFlavModel::generateSignalEvent : Found a larger ASq value: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
}
if (genOK) {
//Some variables, like Fisher or NN, might use m13Sq and m23Sq from the kinematics
//kinematicsB0bar_ is up to date, update kinematicsB0_
kinematicsB0_->updateKinematics(kinematicsB0bar_->getm13Sq(), kinematicsB0bar_->getm23Sq() );
this->generateExtraPdfValues(extraPdfs, embeddedData);
}
// Check for problems with the embedding
if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
std::cerr<<"WARNING in LauTimeDepFlavModel::generateSignalEvent : Source of embedded signal events used up, clearing the list of used events."<<std::endl;
embeddedData->clearUsedList();
}
return genOK;
}
void LauTimeDepFlavModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleIntegerBranch("cpEigenvalue");
this->addGenNtupleIntegerBranch("tagFlv");
this->addGenNtupleIntegerBranch("tagCat");
if (this->useDP() == kTRUE) {
// Let's add the decay time variables.
if (signalDecayTimePdfs_.begin() != signalDecayTimePdfs_.end()) {
LauDecayTimePdf* pdf = signalDecayTimePdfs_.begin()->second;
this->addGenNtupleDoubleBranch(pdf->varName());
this->addGenNtupleDoubleBranch(pdf->varErrName());
}
this->addGenNtupleDoubleBranch("m12");
this->addGenNtupleDoubleBranch("m23");
this->addGenNtupleDoubleBranch("m13");
this->addGenNtupleDoubleBranch("m12Sq");
this->addGenNtupleDoubleBranch("m23Sq");
this->addGenNtupleDoubleBranch("m13Sq");
this->addGenNtupleDoubleBranch("cosHel12");
this->addGenNtupleDoubleBranch("cosHel23");
this->addGenNtupleDoubleBranch("cosHel13");
if (kinematicsB0bar_->squareDP() && kinematicsB0_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime");
this->addGenNtupleDoubleBranch("thPrime");
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
this->addGenNtupleDoubleBranch("reB0Amp");
this->addGenNtupleDoubleBranch("imB0Amp");
this->addGenNtupleDoubleBranch("reB0barAmp");
this->addGenNtupleDoubleBranch("imB0barAmp");
}
}
// Let's look at the extra variables for signal in one of the tagging categories
if ( ! sigExtraPdf_.empty() ) {
const LauPdfPList& oneTagCatPdfList { sigExtraPdf_.begin()->second };
for ( const LauAbsPdf* pdf : oneTagCatPdfList ) {
const std::vector<TString> varNames{ pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
this->addGenNtupleDoubleBranch( varName );
}
}
}
}
}
void LauTimeDepFlavModel::setDPDtBranchValues()
{
// Store the decay time variables.
if (signalDecayTimePdfs_.begin() != signalDecayTimePdfs_.end()) {
LauDecayTimePdf* pdf = signalDecayTimePdfs_.begin()->second;
this->setGenNtupleDoubleBranchValue(pdf->varName(),curEvtDecayTime_);
this->setGenNtupleDoubleBranchValue(pdf->varErrName(),curEvtDecayTimeErr_);
}
LauKinematics* kinematics(0);
if (curEvtTagFlv_<0) {
kinematics = kinematicsB0_;
} else {
kinematics = kinematicsB0bar_;
}
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12", kinematics->getm12());
this->setGenNtupleDoubleBranchValue("m23", kinematics->getm23());
this->setGenNtupleDoubleBranchValue("m13", kinematics->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq", kinematics->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq", kinematics->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq", kinematics->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12", kinematics->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23", kinematics->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13", kinematics->getc13());
if (kinematics->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime", kinematics->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime", kinematics->getThetaPrime());
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
if ( this->getGenNtupleIntegerBranchValue("genSig")==1 ) {
LauComplex Abar = sigModelB0bar_->getEvtDPAmp();
LauComplex A = sigModelB0_->getEvtDPAmp();
this->setGenNtupleDoubleBranchValue("reB0Amp", A.re());
this->setGenNtupleDoubleBranchValue("imB0Amp", A.im());
this->setGenNtupleDoubleBranchValue("reB0barAmp", Abar.re());
this->setGenNtupleDoubleBranchValue("imB0barAmp", Abar.im());
} else {
this->setGenNtupleDoubleBranchValue("reB0Amp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0Amp", 0.0);
this->setGenNtupleDoubleBranchValue("reB0barAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0barAmp", 0.0);
}
}
}
void LauTimeDepFlavModel::generateExtraPdfValues(LauPdfPList* extraPdfs, LauEmbeddedData* embeddedData)
{
LauKinematics* kinematics(0);
if (curEvtTagFlv_<0) {
kinematics = kinematicsB0_;
} else {
kinematics = kinematicsB0bar_;
}
// Generate from the extra PDFs
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
LauFitData genValues;
if (embeddedData) {
genValues = embeddedData->getValues( (*pdf_iter)->varNames() );
} else {
genValues = (*pdf_iter)->generate(kinematics);
}
for ( LauFitData::const_iterator var_iter = genValues.begin(); var_iter != genValues.end(); ++var_iter ) {
TString varName = var_iter->first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var_iter->second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
}
void LauTimeDepFlavModel::propagateParUpdates()
{
// Update the complex mixing phase
if (useSinCos_) {
phiMixComplex_.setRealPart(cosPhiMix_.unblindValue());
phiMixComplex_.setImagPart(-1.0*sinPhiMix_.unblindValue());
} else {
phiMixComplex_.setRealPart(TMath::Cos(-1.0*phiMix_.unblindValue()));
phiMixComplex_.setImagPart(TMath::Sin(-1.0*phiMix_.unblindValue()));
}
// Update the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_->updateCoeffs(coeffsB0bar_);
sigModelB0_->updateCoeffs(coeffsB0_);
this->calcInterTermNorm();
}
// Update the signal events from the background numbers if not doing an extended fit
this->updateSigEvents();
}
void LauTimeDepFlavModel::updateSigEvents()
{
// tagging-category fractions for signal events
this->setFirstTagCatFrac(signalTagCatFrac_);
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
signalEvents_->value(signalEvents);
}
void LauTimeDepFlavModel::setFirstTagCatFrac(LauTagCatParamMap& theMap)
{
Double_t firstCatFrac = 1.0;
Int_t firstCat(0);
for (LauTagCatParamMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
if (iter == theMap.begin()) {
firstCat = iter->first;
continue;
}
LauParameter& par = iter->second;
firstCatFrac -= par.unblindValue();
}
theMap[firstCat].value(firstCatFrac);
}
void LauTimeDepFlavModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and background models.
// Note that we store the events of both charges in both the
// negative and the positive models. It's only later, at the stage
// when the likelihood is being calculated, that we separate them.
LauFitDataTree* inputFitData = this->fitData();
// Start by caching the tagging and CP-eigenstate information
evtTagCatVals_.clear();
evtTagFlvVals_.clear();
evtCPEigenVals_.clear();
if ( ! inputFitData->haveBranch( tagCatVarName_ ) ) {
std::cerr << "ERROR in LauTimeDepFlavModel::cacheInputFitVars : Input data does not contain branch \"" << tagCatVarName_ << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( ! inputFitData->haveBranch( tagVarName_ ) ) {
std::cerr << "ERROR in LauTimeDepFlavModel::cacheInputFitVars : Input data does not contain branch \"" << tagVarName_ << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
const Bool_t hasCPEV = ( (cpevVarName_ != "") && inputFitData->haveBranch( cpevVarName_ ) );
UInt_t nEvents = inputFitData->nEvents();
evtTagCatVals_.reserve( nEvents );
evtTagFlvVals_.reserve( nEvents );
evtCPEigenVals_.reserve( nEvents );
LauFitData::const_iterator fitdata_iter;
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
fitdata_iter = dataValues.find( tagCatVarName_ );
curEvtTagCat_ = static_cast<Int_t>( fitdata_iter->second );
if ( ! this->validTagCat( curEvtTagCat_ ) ) {
std::cerr << "WARNING in LauTimeDepFlavModel::cacheInputFitVars : Invalid tagging category " << curEvtTagCat_ << " for event " << iEvt << ", setting it to untagged" << std::endl;
curEvtTagCat_ = 0;
}
evtTagCatVals_.push_back( curEvtTagCat_ );
fitdata_iter = dataValues.find( tagVarName_ );
curEvtTagFlv_ = static_cast<Int_t>( fitdata_iter->second );
if ( TMath::Abs( curEvtTagFlv_ ) != 1 ) {
if ( curEvtTagFlv_ > 0 ) {
std::cerr << "WARNING in LauTimeDepFlavModel::cacheInputFitVars : Invalid tagging output " << curEvtTagFlv_ << " for event " << iEvt << ", setting it to +1" << std::endl;
curEvtTagFlv_ = +1;
} else {
std::cerr << "WARNING in LauTimeDepFlavModel::cacheInputFitVars : Invalid tagging output " << curEvtTagFlv_ << " for event " << iEvt << ", setting it to -1" << std::endl;
curEvtTagFlv_ = -1;
}
}
evtTagFlvVals_.push_back( curEvtTagFlv_ );
// if the CP-eigenvalue is in the data use those, otherwise use the default
if ( hasCPEV ) {
fitdata_iter = dataValues.find( cpevVarName_ );
const Int_t cpEV = static_cast<Int_t>( fitdata_iter->second );
if ( cpEV == 1 ) {
cpEigenValue_ = CPEven;
} else if ( cpEV == -1 ) {
cpEigenValue_ = CPOdd;
} else {
std::cerr<<"WARNING in LauTimeDepFlavModel::cacheInputFitVars : Unknown value: "<<cpEV<<" for CP eigenvalue, setting to CP-even"<<std::endl;
cpEigenValue_ = CPEven;
}
}
evtCPEigenVals_.push_back( cpEigenValue_ );
}
// We'll cache the DP amplitudes at the end because we'll
// append some points that the other PDFs won't deal with.
if (this->useDP() == kTRUE) {
// DecayTime and SigmaDecayTime
for (LauTagCatDtPdfMap::iterator dt_iter = signalDecayTimePdfs_.begin(); dt_iter != signalDecayTimePdfs_.end(); ++dt_iter) {
(*dt_iter).second->cacheInfo(*inputFitData);
}
}
// ...and then the extra PDFs
for (LauTagCatPdfMap::iterator pdf_iter = sigExtraPdf_.begin(); pdf_iter != sigExtraPdf_.end(); ++pdf_iter) {
this->cacheInfo(pdf_iter->second, *inputFitData);
}
if (this->useDP() == kTRUE) {
sigModelB0bar_->fillDataTree(*inputFitData);
sigModelB0_->fillDataTree(*inputFitData);
}
}
Double_t LauTimeDepFlavModel::getTotEvtLikelihood(const UInt_t iEvt)
{
// Find out whether the tag-side B was a B0 or a B0bar.
curEvtTagFlv_ = evtTagFlvVals_[iEvt];
// Also get the tagging category.
curEvtTagCat_ = evtTagCatVals_[iEvt];
// Get the CP eigenvalue of the current event
cpEigenValue_ = evtCPEigenVals_[iEvt];
// Get the DP and DecayTime likelihood for signal (TODO and eventually backgrounds)
this->getEvtDPDtLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal (TODO and eventually backgrounds)
this->getEvtExtraLikelihoods(iEvt);
// Construct the total likelihood for signal, qqbar and BBbar backgrounds
Double_t sigLike = sigDPLike_ * sigExtraLike_;
Double_t signalEvents = signalEvents_->unblindValue();
if (this->useDP() == kFALSE) {
signalEvents *= 0.5 * (1.0 + curEvtTagFlv_ * signalAsym_->unblindValue());
}
// Construct the total event likelihood
Double_t likelihood(sigLike*signalTagCatFrac_[curEvtTagCat_].unblindValue());
if ( ! signalEvents_->fixed() ) {
likelihood *= signalEvents;
}
return likelihood;
}
Double_t LauTimeDepFlavModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
return eventSum;
}
void LauTimeDepFlavModel::getEvtDPDtLikelihood(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
sigDPLike_ = 1.0; //There's always a likelihood term for signal, so we better not zero it.
if ( this->useDP() == kFALSE ) {
return;
}
// Mistag probabilities. Defined as: omega = prob of the tagging B0 being reported as B0bar
// Whether we want omega or omegaBar depends on q_tag, hence curEvtTagFlv_*... in the previous lines
//Double_t misTagFrac = 0.5 * (1.0 - dilution_[curEvtTagCat_] - qDDo2);
//Double_t misTagFracBar = 0.5 * (1.0 - dilution_[curEvtTagCat_] + qDDo2);
// Calculate event quantities
Double_t qD = curEvtTagFlv_*dilution_[curEvtTagCat_].unblindValue();
Double_t qDDo2 = curEvtTagFlv_*0.5*deltaDilution_[curEvtTagCat_].unblindValue();
// Get the dynamics to calculate everything required for the likelihood calculation
sigModelB0bar_->calcLikelihoodInfo(iEvt);
sigModelB0_->calcLikelihoodInfo(iEvt);
// Retrieve the amplitudes and efficiency from the dynamics
const LauComplex& Abar = sigModelB0bar_->getEvtDPAmp();
const LauComplex& A = sigModelB0_->getEvtDPAmp();
Double_t eff = sigModelB0bar_->getEvtEff();
// Next calculate the DP terms
Double_t aSqSum = A.abs2() + Abar.abs2();
Double_t aSqDif = A.abs2() - Abar.abs2();
LauComplex inter = Abar * A.conj() * phiMixComplex_;
Double_t interTermIm = 2.0 * inter.im();
Double_t interTermRe = 2.0 * inter.re();
// First get all the decay time terms
//LauDecayTimePdf* signalDtPdf = signalDecayTimePdfs_[curEvtTagCat_];
LauDecayTimePdf* decayTimePdf = signalDecayTimePdfs_[curEvtTagCat_];
decayTimePdf->calcLikelihoodInfo(static_cast<std::size_t>(iEvt));
// First get all the decay time terms
Double_t dtCos = decayTimePdf->getCosTerm();
Double_t dtSin = decayTimePdf->getSinTerm();
Double_t dtCosh = decayTimePdf->getCoshTerm();
Double_t dtSinh = decayTimePdf->getSinhTerm();
Double_t cosTerm = dtCos * qD * aSqDif;
Double_t sinTerm = dtSin * qD * interTermIm;
Double_t coshTerm = dtCosh * (1.0 + qDDo2) * aSqSum;
Double_t sinhTerm = dtSinh * (1.0 + qDDo2) * interTermRe;
if ( cpEigenValue_ == CPOdd ) {
sinTerm *= -1.0;
sinhTerm *= -1.0;
}
// ... to get the total and multiply by the efficiency
Double_t ASq = coshTerm + cosTerm - sinTerm + sinhTerm;
ASq *= eff;
// Calculate the DP and time normalisation
Double_t normTermIndep = sigModelB0bar_->getDPNorm() + sigModelB0_->getDPNorm();
Double_t normTermCosh = decayTimePdf->getNormTermCosh();
Double_t normTermDep = interTermReNorm_;
Double_t normTermSinh = decayTimePdf->getNormTermSinh();
Double_t norm = normTermIndep*normTermCosh + normTermDep*normTermSinh;
// Calculate the normalised signal likelihood
sigDPLike_ = ASq / norm;
}
void LauTimeDepFlavModel::getEvtExtraLikelihoods(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0; //There's always a likelihood term for signal, so we better not zero it.
// First, those independent of the tagging of the event:
// signal
LauTagCatPdfMap::iterator sig_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* pdfList = (sig_iter != sigExtraPdf_.end())? &(sig_iter->second) : 0;
if (pdfList) {
sigExtraLike_ = this->prodPdfValue( *pdfList, iEvt );
}
}
void LauTimeDepFlavModel::updateCoeffs()
{
coeffsB0bar_.clear(); coeffsB0_.clear();
coeffsB0bar_.reserve(nSigComp_); coeffsB0_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; ++i) {
coeffsB0bar_.push_back(coeffPars_[i]->antiparticleCoeff());
coeffsB0_.push_back(coeffPars_[i]->particleCoeff());
}
}
Bool_t LauTimeDepFlavModel::validTagCat(Int_t tagCat) const
{
return (validTagCats_.find(tagCat) != validTagCats_.end());
}
Bool_t LauTimeDepFlavModel::checkTagCatFracMap(const LauTagCatParamMap& theMap) const
{
// First check that there is an entry for each tagging category.
// NB an entry won't have been added if it isn't a valid category
// so don't need to check for that here.
if (theMap.size() != signalTagCatFrac_.size()) {
std::cerr<<"ERROR in LauTimeDepFlavModel::checkTagCatFracMap : Not all tagging categories present."<<std::endl;
return kFALSE;
}
// Now check that the fractions sum up to unity.
Double_t tagCatFracSum(0.0);
for (LauTagCatParamMap::const_iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
const LauParameter& par = (*iter).second;
tagCatFracSum += par.unblindValue();
}
if ((tagCatFracSum - 1.0) > 1E-10) {
std::cerr<<"ERROR in LauTimeDepFlavModel::checkTagCatFracMap : Tagging category event fractions do not sum to unity."<<std::endl;
return kFALSE;
}
// If we've got to here then all is OK.
return kTRUE;
}
void LauTimeDepFlavModel::checkMixingPhase()
{
Double_t phase = phiMix_.value();
Double_t genPhase = phiMix_.genValue();
// Check now whether the phase lies in the right range (-pi to pi).
Bool_t withinRange(kFALSE);
while (withinRange == kFALSE) {
if (phase > -LauConstants::pi && phase < LauConstants::pi) {
withinRange = kTRUE;
} else {
// Not within the specified range
if (phase > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (phase < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
}
}
// A further problem can occur when the generated phase is close to -pi or pi.
// The phase can wrap over to the other end of the scale -
// this leads to artificially large pulls so we wrap it back.
Double_t diff = phase - genPhase;
if (diff > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (diff < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
// finally store the new value in the parameter
// and update the pull
phiMix_.value(phase);
phiMix_.updatePull();
}
void LauTimeDepFlavModel::embedSignal(Int_t tagCat, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if (signalTree_[tagCat]) {
std::cerr<<"ERROR in LauTimeDepFlavModel::embedSignal : Already embedding signal from file for tagging category "<<tagCat<<"."<<std::endl;
return;
}
if (!reuseEventsWithinEnsemble && reuseEventsWithinExperiment) {
std::cerr<<"WARNING in LauTimeDepFlavModel::embedSignal : Conflicting options provided, will not reuse events at all."<<std::endl;
reuseEventsWithinExperiment = kFALSE;
}
signalTree_[tagCat] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = signalTree_[tagCat]->findBranches();
if (!dataOK) {
delete signalTree_[tagCat]; signalTree_[tagCat] = 0;
std::cerr<<"ERROR in LauTimeDepFlavModel::embedSignal : Problem creating data tree for embedding."<<std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
}
void LauTimeDepFlavModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
}
// Store the total event likelihood for each species.
this->addSPlotNtupleDoubleBranch("sigTotalLike");
// Store the DP likelihoods
if (this->useDP()) {
this->addSPlotNtupleDoubleBranch("sigDPLike");
}
// Store the likelihoods for each extra PDF
const LauPdfPList* pdfList( &(sigExtraPdf_.begin()->second) );
this->addSPlotNtupleBranches(pdfList, "sig");
}
void LauTimeDepFlavModel::addSPlotNtupleBranches(const LauPdfPList* extraPdfs, const TString& prefix)
{
if (!extraPdfs) {
return;
}
// Loop through each of the PDFs
for ( const LauAbsPdf* pdf : *extraPdfs ) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString varName = pdf->varName();
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars("");
for ( const TString& varName : varNames ) {
allVars += varName;
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
TString name(prefix);
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr<<"WARNING in LauTimeDepFlavModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs."<<std::endl;
}
}
}
Double_t LauTimeDepFlavModel::setSPlotNtupleBranchValues(LauPdfPList* extraPdfs, const TString& prefix, UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if ( !extraPdfs ) {
return totalLike;
}
for ( LauAbsPdf* pdf : *extraPdfs ) {
// calculate the likelihood for this event
pdf->calcLikelihoodInfo(iEvt);
extraLike = pdf->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString varName = pdf->varName();
TString name(prefix);
name += varName;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars("");
for ( const TString& varName : varNames ) {
allVars += varName;
TString name(prefix);
name += varName;
name += "Like";
Double_t indivLike = pdf->getLikelihood( varName );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
TString name(prefix);
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr<<"WARNING in LauAllFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs."<<std::endl;
}
}
return totalLike;
}
LauSPlot::NameSet LauTimeDepFlavModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
LauPdfPList pdfList( (sigExtraPdf_.begin()->second) );
for ( const LauAbsPdf* pdf : pdfList ) {
// Loop over the variables involved in each PDF
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
// If they are not DP coordinates then add them
if ( varName != "m13Sq" && varName != "m23Sq" ) {
nameSet.insert( varName );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauTimeDepFlavModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
return numbMap;
}
LauSPlot::NumbMap LauTimeDepFlavModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
return numbMap;
}
LauSPlot::TwoDMap LauTimeDepFlavModel::twodimPDFs() const
{
LauSPlot::TwoDMap twodimMap;
const LauPdfPList& pdfList { sigExtraPdf_.begin()->second };
for ( const LauAbsPdf* pdf : pdfList ) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
return twodimMap;
}
void LauTimeDepFlavModel::storePerEvtLlhds()
{
std::cout<<"INFO in LauTimeDepFlavModel::storePerEvtLlhds : Storing per-event likelihood values..."<<std::endl;
LauFitDataTree* inputFitData = this->fitData();
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
if (!this->useDP() && this->storeDPEff()) {
sigModelB0bar_->initialise(coeffsB0bar_);
sigModelB0_->initialise(coeffsB0_);
sigModelB0bar_->fillDataTree(*inputFitData);
sigModelB0_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
LauIsobarDynamics* sigModel(sigModelB0bar_);
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
// Find out whether we have B0bar or B0
curEvtTagFlv_ = evtTagFlvVals_[iEvt];
curEvtTagCat_ = evtTagCatVals_[iEvt];
LauTagCatPdfMap::iterator sig_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* sigPdfs = (sig_iter != sigExtraPdf_.end())? &(sig_iter->second) : 0;
// the DP information
this->getEvtDPDtLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
sigModel->calcLikelihoodInfo(iEvt);
}
this->setSPlotNtupleDoubleBranchValue("efficiency",sigModel->getEvtEff());
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
} else {
sigTotalLike_ = 1.0;
}
// the signal PDF values
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sig", iEvt);
// the total likelihoods
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout<<"INFO in LauTimeDepFlavModel::storePerEvtLlhds : Finished storing per-event likelihood values."<<std::endl;
}
void LauTimeDepFlavModel::weightEvents( const TString& /*dataFileName*/, const TString& /*dataTreeName*/ )
{
std::cerr << "ERROR in LauTimeDepFlavModel::weightEvents : Method not available for this fit model." << std::endl;
return;
}
diff --git a/src/LauTimeDepNonFlavModel.cc b/src/LauTimeDepNonFlavModel.cc
index 8631778..d340b41 100644
--- a/src/LauTimeDepNonFlavModel.cc
+++ b/src/LauTimeDepNonFlavModel.cc
@@ -1,2743 +1,2741 @@
/*
Copyright 2015 University of Warwick
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Laura++ package authors:
John Back
Paul Harrison
Thomas Latham
*/
/*! \file LauTimeDepNonFlavModel.cc
\brief File containing implementation of LauTimeDepNonFlavModel class.
*/
#include "LauTimeDepNonFlavModel.hh"
#include "LauAbsBkgndDPModel.hh"
#include "LauAbsCoeffSet.hh"
#include "LauAbsPdf.hh"
#include "LauAsymmCalc.hh"
#include "LauComplex.hh"
#include "LauConstants.hh"
#include "LauDPPartialIntegralInfo.hh"
#include "LauDaughters.hh"
#include "LauDecayTimePdf.hh"
#include "LauFitNtuple.hh"
#include "LauGenNtuple.hh"
#include "LauIsobarDynamics.hh"
#include "LauKinematics.hh"
#include "LauPrint.hh"
#include "LauRandom.hh"
#include "LauScfMap.hh"
#include "TFile.h"
#include "TMinuit.h"
#include "TRandom.h"
#include "TSystem.h"
#include "TVirtualFitter.h"
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
ClassImp(LauTimeDepNonFlavModel)
LauTimeDepNonFlavModel::LauTimeDepNonFlavModel(LauIsobarDynamics* modelB0bar_f, LauIsobarDynamics* modelB0_f, LauIsobarDynamics* modelB0bar_fbar, LauIsobarDynamics* modelB0_fbar, const Bool_t useUntaggedEvents, const TString& tagVarName, const TString& tagCatVarName) : LauAbsFitModel(),
sigModelB0bar_f_(modelB0bar_f),
sigModelB0_f_(modelB0_f),
sigModelB0bar_fbar_(modelB0bar_fbar),
sigModelB0_fbar_(modelB0_fbar),
kinematicsB0bar_f_(modelB0bar_f ? modelB0bar_f->getKinematics() : 0),
kinematicsB0_f_(modelB0_f ? modelB0_f->getKinematics() : 0),
kinematicsB0bar_fbar_(modelB0bar_fbar ? modelB0bar_fbar->getKinematics() : 0),
kinematicsB0_fbar_(modelB0_fbar ? modelB0_fbar->getKinematics() : 0),
useUntaggedEvents_(useUntaggedEvents),
nSigComp_(0),
nSigDPPar_(0),
nDecayTimePar_(0),
nExtraPdfPar_(0),
nNormPar_(0),
coeffsB0bar_f_(0),
coeffsB0_f_(0),
coeffsB0bar_fbar_(0),
coeffsB0_fbar_(0),
coeffPars_B0f_B0barfbar_(0),
coeffPars_B0fbar_B0barf_(0),
interTermReNorm_f_(0),
interTermReNorm_fbar_(0),
interTermImNorm_f_(0),
interTermImNorm_fbar_(0),
fitFracB0bar_f_(0),
fitFracB0_f_(0),
fitFracB0bar_fbar_(0),
fitFracB0_fbar_(0),
fitFracAsymm_B0f_B0barfbar_(0),
fitFracAsymm_B0fbar_B0barf_(0),
acp_B0f_B0barfbar_(0),
acp_B0fbar_B0barf_(0),
meanEffB0bar_f_("meanEffB0bar_f",0.0,0.0,1.0),
meanEffB0_f_("meanEffB0_f",0.0,0.0,1.0),
meanEffB0bar_fbar_("meanEffB0bar_fbar",0.0,0.0,1.0),
meanEffB0_fbar_("meanEffB0_fbar",0.0,0.0,1.0),
DPRateB0bar_f_("DPRateB0bar_f",0.0,0.0,100.0),
DPRateB0_f_("DPRateB0_f",0.0,0.0,100.0),
DPRateB0bar_fbar_("DPRateB0bar_fbar",0.0,0.0,100.0),
DPRateB0_fbar_("DPRateB0_fbar",0.0,0.0,100.0),
signalEvents_(0),
signalAsym_(0),
signalTagCatFrac_(),
tagVarName_(tagVarName),
tagCatVarName_(tagCatVarName),
cpevVarName_(""),
validTagCats_(),
curEvtTagFlv_(0),
curEvtTagCat_(0),
cpEigenValue_(CPEven),
evtTagFlvVals_(0),
evtTagCatVals_(0),
evtCPEigenVals_(0),
dilution_(),
deltaDilution_(),
deltaM_("deltaM",0.0),
deltaGamma_("deltaGamma",0.0),
tau_("tau",LauConstants::tauB0),
phiMix_("phiMix", 2.0*LauConstants::beta, -LauConstants::threePi, LauConstants::threePi, kFALSE),
sinPhiMix_("sinPhiMix", TMath::Sin(2.0*LauConstants::beta), -3.0, 3.0, kFALSE),
cosPhiMix_("cosPhiMix", TMath::Cos(2.0*LauConstants::beta), -3.0, 3.0, kFALSE),
useSinCos_(kFALSE),
phiMixComplex_(TMath::Cos(-2.0*LauConstants::beta),TMath::Sin(-2.0*LauConstants::beta)),
signalDecayTimePdfs_(),
curEvtDecayTime_(0.0),
curEvtDecayTimeErr_(0.0),
qD_(0.0),
qDDo2_(0.0),
sigExtraPdf_(),
finalState_(0.0),
iterationsMax_(500000),
nGenLoop_(0),
ASq_(0.0),
aSqMaxVar_(0.0),
aSqMaxSet_(1.25),
normTimeDP_f_(0.0),
normTimeDP_fbar_(0.0),
storeGenAmpInfo_(kFALSE),
signalTree_(),
reuseSignal_(kFALSE),
sigDPLike_(0.0),
sigExtraLike_(0.0),
sigTotalLike_(0.0)
{
// Add the untagged category as a valid category
this->addValidTagCat(0);
// Set the fraction, average dilution and dilution difference for the untagged category
this->setSignalTagCatPars(0, 1.0, 0.0, 0.0, kTRUE);
}
LauTimeDepNonFlavModel::~LauTimeDepNonFlavModel()
{
// TODO - need to delete the various embedded data structures here
}
void LauTimeDepNonFlavModel::setupBkgndVectors()
{
}
void LauTimeDepNonFlavModel::setNSigEvents(LauParameter* nSigEvents)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : The LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_ = new LauParameter(asymName,0.0,-1.0,1.0,kTRUE);
}
void LauTimeDepNonFlavModel::setNSigEvents(LauParameter* nSigEvents, LauParameter* sigAsym)
{
if ( nSigEvents == nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : The event LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( sigAsym == nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : The asym LauParameter pointer is null." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( signalEvents_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : You are trying to overwrite the signal yield." << std::endl;
return;
}
if ( signalAsym_ != nullptr ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : You are trying to overwrite the signal asymmetry." << std::endl;
return;
}
TString yieldName = nSigEvents->name();
if ( ! yieldName.BeginsWith("signalEvents") ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setNSigEvents : The signal yield parameter name should start with \"signalEvents\" (plus any optional suffix)." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
signalEvents_ = nSigEvents;
signalAsym_ = sigAsym;
TString asymName = yieldName;
asymName.ReplaceAll( "Events", "Asym" );
signalAsym_->name(asymName);
}
void LauTimeDepNonFlavModel::setNBkgndEvents(LauAbsRValue* /*nBkgndEvents*/)
{
std::cerr << "WARNING in LauTimeDepNonFlavModel::setNBkgndEvents : This model does not yet support backgrounds" << std::endl;
}
void LauTimeDepNonFlavModel::addValidTagCats(const std::vector<Int_t>& tagCats)
{
for (std::vector<Int_t>::const_iterator iter = tagCats.begin(); iter != tagCats.end(); ++iter) {
this->addValidTagCat(*iter);
}
}
void LauTimeDepNonFlavModel::addValidTagCat(Int_t tagCat)
{
validTagCats_.insert(tagCat);
}
void LauTimeDepNonFlavModel::setSignalTagCatPars(const Int_t tagCat, const Double_t tagCatFrac, const Double_t dilution, const Double_t deltaDilution, const Bool_t fixTCFrac)
{
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setSignalTagCatPars : Tagging category \""<<tagCat<<"\" not valid, not changing the tagging categories parameters."<<std::endl;
return;
}
TString tagCatFracName("signalTagCatFrac"); tagCatFracName += tagCat;
signalTagCatFrac_[tagCat].name(tagCatFracName);
signalTagCatFrac_[tagCat].range(0.0,1.0);
signalTagCatFrac_[tagCat].value(tagCatFrac); signalTagCatFrac_[tagCat].initValue(tagCatFrac); signalTagCatFrac_[tagCat].genValue(tagCatFrac);
signalTagCatFrac_[tagCat].fixed(fixTCFrac);
TString dilutionName("dilution"); dilutionName += tagCat;
dilution_[tagCat].name(dilutionName);
dilution_[tagCat].range(0.0,1.0);
dilution_[tagCat].value(dilution); dilution_[tagCat].initValue(dilution); dilution_[tagCat].genValue(dilution);
TString deltaDilutionName("deltaDilution"); dilutionName += tagCat;
deltaDilution_[tagCat].name(deltaDilutionName);
deltaDilution_[tagCat].range(-2.0,2.0);
deltaDilution_[tagCat].value(deltaDilution); deltaDilution_[tagCat].initValue(deltaDilution); deltaDilution_[tagCat].genValue(deltaDilution);
// We check they're set up correctly with this->checkSignalTagCatFractions(); only when the user has
//set them all up, in this->initialise();
}
void LauTimeDepNonFlavModel::checkSignalTagCatFractions()
{
Double_t totalTaggedFrac(0.0);
for (LauTagCatParamMap::const_iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
if (iter->first != 0) {
const LauParameter& par = iter->second;
totalTaggedFrac += par.value();
}
}
if ( ((totalTaggedFrac < (1.0-1.0e-8))&&!useUntaggedEvents_) || (totalTaggedFrac > (1.0+1.0e-8)) ) {
std::cerr<<"WARNING in LauTimeDepNonFlavModel::checkSignalTagCatFractions : Tagging category fractions add up to "<<totalTaggedFrac<<", not 1.0; normalizing them."<<std::endl;
for (LauTagCatParamMap::iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
LauParameter& par = iter->second;
Double_t newVal = par.value() / totalTaggedFrac;
par.value(newVal); par.initValue(newVal); par.genValue(newVal);
}
} else if (useUntaggedEvents_) {
Double_t tagCatFrac = 1.0 - totalTaggedFrac;
TString tagCatFracName("signalTagCatFrac0");
signalTagCatFrac_[0].name(tagCatFracName);
signalTagCatFrac_[0].range(0.0,1.0);
signalTagCatFrac_[0].value(tagCatFrac); signalTagCatFrac_[0].initValue(tagCatFrac); signalTagCatFrac_[0].genValue(tagCatFrac);
signalTagCatFrac_[0].fixed(kTRUE);
TString dilutionName("dilution0");
dilution_[0].name(dilutionName);
dilution_[0].range(0.0,1.0);
dilution_[0].value(0.0); dilution_[0].initValue(0.0); dilution_[0].genValue(0.0);
TString deltaDilutionName("deltaDilution0");
deltaDilution_[0].name(deltaDilutionName);
deltaDilution_[0].range(-2.0,2.0);
deltaDilution_[0].value(0.0); deltaDilution_[0].initValue(0.0); deltaDilution_[0].genValue(0.0);
}
for (LauTagCatParamMap::const_iterator iter=dilution_.begin(); iter!=dilution_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepNonFlavModel::checkSignalTagCatFractions : Setting dilution for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
for (LauTagCatParamMap::const_iterator iter=deltaDilution_.begin(); iter!=deltaDilution_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepNonFlavModel::checkSignalTagCatFractions : Setting Delta(dilution) for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
for (LauTagCatParamMap::const_iterator iter=signalTagCatFrac_.begin(); iter!=signalTagCatFrac_.end(); ++iter) {
std::cout<<"INFO in LauTimeDepNonFlavModel::checkSignalTagCatFractions : Setting (signal) fraction for tagging category "<<(*iter).first<<" to "<<(*iter).second.value()<<std::endl;
}
}
void LauTimeDepNonFlavModel::setSignalDtPdf(Int_t tagCat, LauDecayTimePdf* pdf)
{
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setSignalDtPdf : Tagging category \""<<tagCat<<"\" not valid, not adding PDF."<<std::endl;
return;
}
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setSignalDtPdf : The PDF pointer is null, not adding it."<<std::endl;
return;
}
signalDecayTimePdfs_[tagCat] = pdf;
}
void LauTimeDepNonFlavModel::setSignalPdfs(Int_t tagCat, LauAbsPdf* pdf)
{
// These "extra variables" are assumed to be purely kinematical, like mES and DeltaE
//or making use of Rest of Event information, and therefore independent of whether
//the parent is a B0 or a B0bar. If this assupmtion doesn't hold, do modify this part!
if (!this->validTagCat(tagCat)) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setSignalPdfs : Tagging category \""<<tagCat<<"\" not valid, not adding models."<<std::endl;
return;
}
if (pdf==0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setSignalPdfs : The PDF pointer is null."<<std::endl;
return;
}
sigExtraPdf_[tagCat].push_back(pdf);
}
void LauTimeDepNonFlavModel::setPhiMix(Double_t phiMix, Bool_t fixPhiMix, Bool_t useSinCos)
{
phiMix_.value(phiMix); phiMix_.initValue(phiMix); phiMix_.genValue(phiMix); phiMix_.fixed(fixPhiMix);
Double_t sinPhiMix = TMath::Sin(phiMix);
sinPhiMix_.value(sinPhiMix); sinPhiMix_.initValue(sinPhiMix); sinPhiMix_.genValue(sinPhiMix); sinPhiMix_.fixed(fixPhiMix);
Double_t cosPhiMix = TMath::Cos(phiMix);
cosPhiMix_.value(cosPhiMix); cosPhiMix_.initValue(cosPhiMix); cosPhiMix_.genValue(cosPhiMix); cosPhiMix_.fixed(fixPhiMix);
useSinCos_ = useSinCos;
phiMixComplex_.setRealPart(cosPhiMix);
phiMixComplex_.setImagPart(-1.0*sinPhiMix);
}
void LauTimeDepNonFlavModel::initialise()
{
// From the initial parameter values calculate the coefficients
// so they can be passed to the signal model
this->updateCoeffs();
// Initialisation
if (this->useDP() == kTRUE) {
this->initialiseDPModels();
}
if (!this->useDP() && sigExtraPdf_.empty()) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialise : Signal model doesn't exist for any variable."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (this->useDP() == kTRUE) {
// Check that we have all the Dalitz-plot models
if ((sigModelB0bar_f_ == 0) || (sigModelB0_f_ == 0) || (sigModelB0bar_fbar_ == 0) || (sigModelB0bar_fbar_ == 0)) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialise : the pointer to one (particle or anti-particle) of the signal DP models is null."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
// Check here that the tagging category fractions add up to 1, otherwise "normalise". Also set up the untagged cat.
// NB this has to be done early in the initialization as other methods access the tagCats map.
this->checkSignalTagCatFractions();
// Clear the vectors of parameter information so we can start from scratch
this->clearFitParVectors();
// Set the fit parameters for signal and background models
this->setSignalDPParameters();
// Set the fit parameters for the decay time models
this->setDecayTimeParameters();
// Set the fit parameters for the extra PDFs
this->setExtraPdfParameters();
// Set the initial bg and signal events
this->setFitNEvents();
// Check that we have the expected number of fit variables
const LauParameterPList& fitVars = this->fitPars();
if (fitVars.size() != (nSigDPPar_ + nDecayTimePar_ + nExtraPdfPar_ + nNormPar_)) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialise : Number of fit parameters not of expected size."<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
this->setExtraNtupleVars();
}
void LauTimeDepNonFlavModel::recalculateNormalisation()
{
sigModelB0bar_f_->recalculateNormalisation();
sigModelB0_f_->recalculateNormalisation();
sigModelB0bar_fbar_->recalculateNormalisation();
sigModelB0_fbar_->recalculateNormalisation();
sigModelB0bar_f_->modifyDataTree();
sigModelB0_f_->modifyDataTree();
sigModelB0bar_fbar_->modifyDataTree();
sigModelB0_fbar_->modifyDataTree();
this->calcInterferenceTermIntegrals();
}
void LauTimeDepNonFlavModel::initialiseDPModels()
{
if (sigModelB0bar_f_ == 0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialiseDPModels : B0bar -> f signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (sigModelB0_f_ == 0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialiseDPModels : B0 -> f signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (sigModelB0bar_fbar_ == 0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialiseDPModels : B0bar -> fbar signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if (sigModelB0_fbar_ == 0) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::initialiseDPModels : B0 -> fbar signal DP model doesn't exist"<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
// Need to check that the number of components we have and that the dynamics has matches up
//const UInt_t nAmpB0bar_f = sigModelB0bar_f_->getnAmp();
//const UInt_t nAmpB0_f = sigModelB0_f_->getnAmp();
//const UInt_t nAmpB0bar_fbar = sigModelB0bar_fbar_->getnAmp();
//const UInt_t nAmpB0_fbar = sigModelB0_fbar_->getnAmp();
const UInt_t nAmpB0bar_f = sigModelB0bar_f_->getnTotAmp();
const UInt_t nAmpB0_f = sigModelB0_f_->getnTotAmp();
const UInt_t nAmpB0bar_fbar = sigModelB0bar_fbar_->getnTotAmp();
const UInt_t nAmpB0_fbar = sigModelB0_fbar_->getnTotAmp();
if ( nAmpB0bar_f != nAmpB0_f ){
std::cerr << "ERROR in LauTimeDepNonFlavModel::initialiseDPModels : Unequal number of signal DP components in the particle and anti-particle models: " << nAmpB0bar_f << " != " << nAmpB0_f << std::endl;
gSystem->Exit(EXIT_FAILURE);
} else if ( nAmpB0bar_fbar != nAmpB0_fbar ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::initialiseDPModels : Unequal number of signal DP components in the particle and anti-particle models: " << nAmpB0bar_fbar << " != " << nAmpB0_fbar << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( nAmpB0bar_f != nSigComp_ ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::initialiseDPModels : Number of signal DP components in the model (" << nAmpB0bar_f << ") not equal to number of coefficients supplied (" << nSigComp_ << ")." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
std::cout<<"INFO in LauTimeDepNonFlavModel::initialiseDPModels : Initialising signal DP model"<<std::endl;
sigModelB0bar_f_->initialise(coeffsB0bar_f_);
sigModelB0_f_->initialise(coeffsB0_f_);
sigModelB0bar_fbar_->initialise(coeffsB0bar_fbar_);
sigModelB0_fbar_->initialise(coeffsB0_fbar_);
fifjEffSum_f_.clear();
fifjEffSum_fbar_.clear();
fifjEffSum_f_.resize(nSigComp_);
fifjEffSum_fbar_.resize(nSigComp_);
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
fifjEffSum_f_[iAmp].resize(nSigComp_);
fifjEffSum_fbar_[iAmp].resize(nSigComp_);
}
// calculate the integrals of the A*Abar terms
this->calcInterferenceTermIntegrals();
this->calcInterTermNorm();
}
void LauTimeDepNonFlavModel::calcInterferenceTermIntegrals()
{
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0bar_f = sigModelB0bar_f_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0_f = sigModelB0_f_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0bar_fbar = sigModelB0bar_fbar_->getIntegralInfos();
const std::vector<LauDPPartialIntegralInfo*>& integralInfoListB0_fbar = sigModelB0_fbar_->getIntegralInfos();
LauComplex A_f, Abar_f, A_fbar, Abar_fbar, fifjEffSumTerm_f, fifjEffSumTerm_fbar;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
fifjEffSum_f_[iAmp][jAmp].zero();
fifjEffSum_fbar_[iAmp][jAmp].zero();
}
}
const UInt_t nIntegralRegions_f = integralInfoListB0bar_f.size();
for ( UInt_t iRegion(0); iRegion < nIntegralRegions_f; ++iRegion ) {
const LauDPPartialIntegralInfo* integralInfoB0bar_f = integralInfoListB0bar_f[iRegion];
const LauDPPartialIntegralInfo* integralInfoB0_f = integralInfoListB0_f[iRegion];
const UInt_t nm13Points = integralInfoB0bar_f->getnm13Points();
const UInt_t nm23Points = integralInfoB0bar_f->getnm23Points();
for (UInt_t m13 = 0; m13 < nm13Points; ++m13) {
for (UInt_t m23 = 0; m23 < nm23Points; ++m23) {
const Double_t weight_f = integralInfoB0bar_f->getWeight(m13,m23);
const Double_t eff_f = integralInfoB0bar_f->getEfficiency(m13,m23);
const Double_t effWeight_f = eff_f*weight_f;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
A_f = integralInfoB0_f->getAmplitude(m13, m23, iAmp);
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
Abar_f = integralInfoB0bar_f->getAmplitude(m13, m23, jAmp);
fifjEffSumTerm_f = Abar_f*A_f.conj();
fifjEffSumTerm_f.rescale(effWeight_f);
fifjEffSum_f_[iAmp][jAmp] += fifjEffSumTerm_f;
}
}
}
}
}
const UInt_t nIntegralRegions_fbar = integralInfoListB0bar_fbar.size();
for ( UInt_t iRegion(0); iRegion < nIntegralRegions_fbar; ++iRegion ) {
const LauDPPartialIntegralInfo* integralInfoB0bar_fbar = integralInfoListB0bar_fbar[iRegion];
const LauDPPartialIntegralInfo* integralInfoB0_fbar = integralInfoListB0_fbar[iRegion];
const UInt_t nm13Points = integralInfoB0bar_fbar->getnm13Points();
const UInt_t nm23Points = integralInfoB0bar_fbar->getnm23Points();
for (UInt_t m13 = 0; m13 < nm13Points; ++m13) {
for (UInt_t m23 = 0; m23 < nm23Points; ++m23) {
const Double_t weight_fbar = integralInfoB0bar_fbar->getWeight(m13,m23);
const Double_t eff_fbar = integralInfoB0bar_fbar->getEfficiency(m13,m23);
const Double_t effWeight_fbar = eff_fbar*weight_fbar;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
A_fbar = integralInfoB0_fbar->getAmplitude(m13, m23, iAmp);
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
Abar_fbar = integralInfoB0bar_fbar->getAmplitude(m13, m23, jAmp);
fifjEffSumTerm_fbar = Abar_fbar*A_fbar.conj();
fifjEffSumTerm_fbar.rescale(effWeight_fbar);
fifjEffSum_fbar_[iAmp][jAmp] += fifjEffSumTerm_fbar;
}
}
}
}
}
}
void LauTimeDepNonFlavModel::calcInterTermNorm()
{
const std::vector<Double_t> fNormB0bar_f = sigModelB0bar_f_->getFNorm();
const std::vector<Double_t> fNormB0_f = sigModelB0_f_->getFNorm();
const std::vector<Double_t> fNormB0bar_fbar = sigModelB0bar_fbar_->getFNorm();
const std::vector<Double_t> fNormB0_fbar = sigModelB0_fbar_->getFNorm();
LauComplex norm_f;
LauComplex norm_fbar;
for (UInt_t iAmp = 0; iAmp < nSigComp_; ++iAmp) {
for (UInt_t jAmp = 0; jAmp < nSigComp_; ++jAmp) {
LauComplex coeffTerm_f = coeffsB0bar_f_[jAmp]*coeffsB0_f_[iAmp].conj();
LauComplex coeffTerm_fbar = coeffsB0bar_fbar_[jAmp]*coeffsB0_fbar_[iAmp].conj();
coeffTerm_f *= fifjEffSum_f_[iAmp][jAmp];
coeffTerm_fbar *= fifjEffSum_fbar_[iAmp][jAmp];
coeffTerm_f.rescale(fNormB0bar_f[jAmp] * fNormB0_f[iAmp]);
coeffTerm_fbar.rescale(fNormB0bar_fbar[jAmp] * fNormB0_fbar[iAmp]);
norm_f += coeffTerm_f;
norm_fbar += coeffTerm_fbar;
}
}
norm_f *= phiMixComplex_;
norm_fbar *= phiMixComplex_;
interTermReNorm_f_ = 2.0*norm_f.re();
interTermImNorm_f_ = 2.0*norm_f.im();
interTermReNorm_fbar_ = 2.0*norm_fbar.re();
interTermImNorm_fbar_ = 2.0*norm_fbar.im();
}
void LauTimeDepNonFlavModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet)
{
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : Set of coefficients for B0/B0bar -> f,fbar contains only component for f final state \""<<coeffSet->name()<<"\"."<<std::endl;
return;
}
void LauTimeDepNonFlavModel::setAmpCoeffSet(std::unique_ptr<LauAbsCoeffSet> coeffSet_B0f_B0barfbar, std::unique_ptr<LauAbsCoeffSet> coeffSet_B0fbar_B0barf)
{
// Define the signal model for each of the particle/antiparticle and f/fbar final states
const TString compName_B0f_B0barfbar { coeffSet_B0f_B0barfbar->name() };
const TString compName_B0fbar_B0barf { coeffSet_B0fbar_B0barf->name() };
const TString conjName_B0f_B0barfbar { sigModelB0bar_fbar_->getConjResName(compName_B0f_B0barfbar) };
const TString conjName_B0fbar_B0barf { sigModelB0bar_f_->getConjResName(compName_B0fbar_B0barf) };
//std::cout << "Values are: " << std::endl;
//std::cout << "CompName: " << compName_B0f_B0barfbar << " " << compName_B0fbar_B0barf << std::endl;
//std::cout << "ComjName: " << conjName_B0f_B0barfbar << " " << conjName_B0fbar_B0barf << std::endl;
// Define each daughter configuration
const LauDaughters* daughtersB0bar_f { sigModelB0bar_f_->getDaughters() };
const LauDaughters* daughtersB0_f { sigModelB0_f_->getDaughters() };
const LauDaughters* daughtersB0bar_fbar { sigModelB0bar_fbar_->getDaughters() };
const LauDaughters* daughtersB0_fbar { sigModelB0_fbar_->getDaughters() };
const Bool_t conjugateB0_f { daughtersB0_f->isConjugate( daughtersB0bar_fbar ) };
const Bool_t conjugateB0_fbar { daughtersB0_fbar->isConjugate( daughtersB0bar_f ) };
if ( ! sigModelB0_f_->hasResonance(compName_B0f_B0barfbar) ) {
if ( ! sigModelB0_f_->hasResonance(conjName_B0f_B0barfbar) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : B0 -> f signal DP model doesn't contain component \""<< compName_B0f_B0barfbar <<"\"."<<std::endl;
return;
}
}
if ( ! sigModelB0_fbar_->hasResonance(compName_B0fbar_B0barf) ) {
if ( ! sigModelB0_fbar_->hasResonance(conjName_B0fbar_B0barf) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : B0 -> fbar signal DP model doesn't contain component \""<< compName_B0fbar_B0barf<<"\"."<<std::endl;
return;
}
}
if ( conjugateB0_f ){
if ( ! sigModelB0bar_fbar_->hasResonance(conjName_B0f_B0barfbar) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : signal DP model doesn't contain component \""<<conjName_B0f_B0barfbar<<"\"."<<std::endl;
return;
}
}
if ( conjugateB0_fbar ){
if ( ! sigModelB0bar_f_->hasResonance(conjName_B0fbar_B0barf) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : signal DP model doesn't contain component \""<<conjName_B0fbar_B0barf<<"\"."<<std::endl;
return;
}
}
if (( !conjugateB0_f ) && ( !conjugateB0_fbar )) {
if ( ! sigModelB0bar_fbar_->hasResonance(compName_B0f_B0barfbar) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : signal DP model doesn't contain component \""<<compName_B0f_B0barfbar<<"\"."<<std::endl;
return;
} else if ( ! sigModelB0bar_f_->hasResonance(compName_B0fbar_B0barf) ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : signal DP model doesn't contain component \""<<compName_B0fbar_B0barf<<"\"."<<std::endl;
return;
}
}
const Int_t index_B0bar_f { sigModelB0bar_f_->resonanceIndex(compName_B0fbar_B0barf) };
const Int_t index_B0_f { sigModelB0_f_->resonanceIndex(compName_B0f_B0barfbar) };
const Int_t index_B0bar_fbar { sigModelB0bar_fbar_->resonanceIndex(compName_B0f_B0barfbar) };
const Int_t index_B0_fbar { sigModelB0_fbar_->resonanceIndex(compName_B0fbar_B0barf) };
if ( index_B0bar_f != index_B0_f || index_B0bar_f != index_B0bar_fbar || index_B0bar_f != index_B0_fbar ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : signal DP models have different indices for component \"" << compName_B0fbar_B0barf << " / " << compName_B0f_B0barfbar << "\"." << std::endl;
return;
}
// Do we already have it in our list of names?
if ( coeffPars_B0f_B0barfbar_[index_B0_f] != nullptr && coeffPars_B0f_B0barfbar_[index_B0_f]->name() == compName_B0f_B0barfbar ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : Have already set coefficients for \""<<compName_B0f_B0barfbar<<"\"."<<std::endl;
return;
}
if ( coeffPars_B0fbar_B0barf_[index_B0_f] != nullptr && coeffPars_B0fbar_B0barf_[index_B0_f]->name() == compName_B0fbar_B0barf ) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setAmpCoeffSet : Have already set coefficients for \""<<compName_B0fbar_B0barf<<"\"."<<std::endl;
return;
}
coeffSet_B0f_B0barfbar->index(nSigComp_);
coeffSet_B0fbar_B0barf->index(nSigComp_);
std::cout<<"INFO in LauTimeDepNonFlavModel::setAmpCoeffSet : Added coefficients for components \""<<compName_B0f_B0barfbar<<"\" (B0->f, B0bar->fbar) and \""<<compName_B0fbar_B0barf<<"\" (B0->fbar, B0bar->f)"<<std::endl;
coeffSet_B0f_B0barfbar->printParValues();
coeffSet_B0fbar_B0barf->printParValues();
const TString parName_B0f_B0barfbar { coeffSet_B0f_B0barfbar->baseName() + "FitFracAsym" };
const TString parName_B0fbar_B0barf { coeffSet_B0fbar_B0barf->baseName() + "FitFracAsym" };
fitFracAsymm_B0f_B0barfbar_[index_B0_f] = LauParameter{parName_B0f_B0barfbar, 0.0, -1.0, 1.0};
fitFracAsymm_B0fbar_B0barf_[index_B0_fbar] = LauParameter{parName_B0fbar_B0barf, 0.0, -1.0, 1.0};
acp_B0f_B0barfbar_[index_B0_f] = coeffSet_B0f_B0barfbar->acp();
acp_B0fbar_B0barf_[index_B0_fbar] = coeffSet_B0fbar_B0barf->acp();
coeffPars_B0f_B0barfbar_[index_B0_f] = std::move(coeffSet_B0f_B0barfbar);
coeffPars_B0fbar_B0barf_[index_B0_fbar] = std::move(coeffSet_B0fbar_B0barf);
++nSigComp_;
}
std::vector<const LauAbsCoeffSet*> LauTimeDepNonFlavModel::getAmpCoeffs() const
{
std::vector<const LauAbsCoeffSet*> coeffs;
coeffs.assign( coeffPars_B0f_B0barfbar_.size() + coeffPars_B0fbar_B0barf_.size(), nullptr );
auto getPtr = [](const std::unique_ptr<LauAbsCoeffSet>& ptr){ return ptr.get(); };
auto iter = std::transform( coeffPars_B0f_B0barfbar_.begin(), coeffPars_B0f_B0barfbar_.end(), coeffs.begin(), getPtr );
std::transform( coeffPars_B0fbar_B0barf_.begin(), coeffPars_B0fbar_B0barf_.end(), iter, getPtr );
return coeffs;
}
void LauTimeDepNonFlavModel::calcAsymmetries(Bool_t initValues)
{
// Calculate the CP asymmetries between B0_f(B0_fbar) and B0bar_fbar(B0bar_f)
// Also calculate the fit fraction asymmetries
for (UInt_t i = 0; i < nSigComp_; i++) {
acp_B0f_B0barfbar_[i] = coeffPars_B0f_B0barfbar_[i]->acp();
acp_B0fbar_B0barf_[i] = coeffPars_B0fbar_B0barf_[i]->acp();
LauAsymmCalc asymmCalc_B0f_B0barfbar(fitFracB0bar_fbar_[i][i].value(), fitFracB0_f_[i][i].value());
LauAsymmCalc asymmCalc_B0fbar_B0barf(fitFracB0bar_f_[i][i].value(), fitFracB0_fbar_[i][i].value());
Double_t asym_B0f_B0barfbar = asymmCalc_B0f_B0barfbar.getAsymmetry();
Double_t asym_B0fbar_B0barf = asymmCalc_B0fbar_B0barf.getAsymmetry();
fitFracAsymm_B0f_B0barfbar_[i].value(asym_B0f_B0barfbar);
fitFracAsymm_B0fbar_B0barf_[i].value(asym_B0fbar_B0barf);
if (initValues) {
fitFracAsymm_B0f_B0barfbar_[i].genValue(asym_B0f_B0barfbar);
fitFracAsymm_B0fbar_B0barf_[i].genValue(asym_B0fbar_B0barf);
fitFracAsymm_B0f_B0barfbar_[i].initValue(asym_B0f_B0barfbar);
fitFracAsymm_B0fbar_B0barf_[i].initValue(asym_B0fbar_B0barf);
}
}
}
void LauTimeDepNonFlavModel::setSignalDPParameters()
{
// Set the fit parameters for the signal model.
nSigDPPar_ = 0;
if ( ! this->useDP() ) {
return;
}
std::cout << "INFO in LauTimeDepNonFlavModel::setSignalDPParameters : Setting the initial fit parameters for the signal DP model." << std::endl;
// Place isobar coefficient parameters in vector of fit variables
for (UInt_t i = 0; i < nSigComp_; i++) {
LauParameterPList pars_B0f_B0barfbar = coeffPars_B0f_B0barfbar_[i]->getParameters();
LauParameterPList pars_B0fbar_B0barf = coeffPars_B0fbar_B0barf_[i]->getParameters();
nSigDPPar_ += this->addFitParameters( pars_B0f_B0barfbar, kTRUE );
nSigDPPar_ += this->addFitParameters( pars_B0fbar_B0barf, kTRUE );
}
// Obtain the resonance parameters and place them in the vector of fit variables and in a separate vector
// Need to make sure that they are unique because some might appear in both DP models
LauParameterPList& sigDPParsB0bar_f = sigModelB0bar_f_->getFloatingParameters();
LauParameterPList& sigDPParsB0_f = sigModelB0_f_->getFloatingParameters();
LauParameterPList& sigDPParsB0bar_fbar = sigModelB0bar_fbar_->getFloatingParameters();
LauParameterPList& sigDPParsB0_fbar = sigModelB0_fbar_->getFloatingParameters();
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0bar_f );
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0_f );
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0bar_fbar );
nSigDPPar_ += this->addResonanceParameters( sigDPParsB0_fbar );
}
UInt_t LauTimeDepNonFlavModel::addParametersToFitList(LauTagCatDtPdfMap& theMap)
{
UInt_t counter(0);
// loop through the map
for (LauTagCatDtPdfMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
// grab the pdf and then its parameters
LauDecayTimePdf* thePdf = (*iter).second; // The first one is the tagging category
LauAbsRValuePList& rvalues = thePdf->getParameters();
counter += this->addFitParameters(rvalues);
}
return counter;
}
UInt_t LauTimeDepNonFlavModel::addParametersToFitList(LauTagCatPdfMap& theMap)
{
UInt_t counter(0);
// loop through the map
for (LauTagCatPdfMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
counter += this->addFitParameters(iter->second); // first is the tagging category
}
return counter;
}
void LauTimeDepNonFlavModel::setDecayTimeParameters()
{
nDecayTimePar_ = 0;
// Loop over the Dt PDFs
nDecayTimePar_ += this->addParametersToFitList(signalDecayTimePdfs_);
if (useSinCos_) {
nDecayTimePar_ += this->addFitParameters( &sinPhiMix_ );
nDecayTimePar_ += this->addFitParameters( &cosPhiMix_ );
} else {
nDecayTimePar_ += this->addFitParameters( &phiMix_ );
}
}
void LauTimeDepNonFlavModel::setExtraPdfParameters()
{
// Include the parameters of the PDF for each tagging category in the fit
// NB all of them are passed to the fit, even though some have been fixed through parameter.fixed(kTRUE)
// With the new "cloned parameter" scheme only "original" parameters are passed to the fit.
// Their clones are updated automatically when the originals are updated.
nExtraPdfPar_ = 0;
nExtraPdfPar_ += this->addParametersToFitList(sigExtraPdf_);
}
void LauTimeDepNonFlavModel::setFitNEvents()
{
nNormPar_ = 0;
// Initialise the total number of events to be the sum of all the hypotheses
Double_t nTotEvts = signalEvents_->value();
this->eventsPerExpt(TMath::FloorNint(nTotEvts));
// if doing an extended ML fit add the signal fraction into the fit parameters
if (this->doEMLFit()) {
std::cout<<"INFO in LauTimeDepNonFlavModel::setFitNEvents : Initialising number of events for signal and background components..."<<std::endl;
nNormPar_ += this->addFitParameters( signalEvents_ );
} else {
std::cout<<"INFO in LauTimeDepNonFlavModel::setFitNEvents : Initialising number of events for background components (and hence signal)..."<<std::endl;
}
// if not using the DP in the model we need an explicit signal asymmetry parameter
if (this->useDP() == kFALSE) {
nNormPar_ += this->addFitParameters( signalAsym_ );
}
// tagging-category fractions for signal events
for (LauTagCatParamMap::iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
if (iter == signalTagCatFrac_.begin()) {
continue;
}
LauParameter* par = &((*iter).second);
nNormPar_ += this->addFitParameters( par );
}
}
void LauTimeDepNonFlavModel::setExtraNtupleVars()
{
// Set-up other parameters derived from the fit results, e.g. fit fractions.
if (this->useDP() != kTRUE) {
return;
}
// First clear the vectors so we start from scratch
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
// Add the B0 (f/fbar) and B0bar (f/fbar) fit fractions for each signal component
fitFracB0bar_f_ = sigModelB0bar_f_->getFitFractions();
if (fitFracB0bar_f_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_f_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0bar_f_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0bar_f" );
fitFracB0bar_f_[i][j].name(name);
extraVars.push_back(fitFracB0bar_f_[i][j]);
}
}
fitFracB0_f_ = sigModelB0_f_->getFitFractions();
if (fitFracB0_f_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_f_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_f_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_f_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0_f_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0_f" );
fitFracB0_f_[i][j].name(name);
extraVars.push_back(fitFracB0_f_[i][j]);
}
}
fitFracB0bar_fbar_ = sigModelB0bar_fbar_->getFitFractions();
if (fitFracB0bar_fbar_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_fbar_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0bar_fbar_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0bar_fbar" );
fitFracB0bar_fbar_[i][j].name(name);
extraVars.push_back(fitFracB0bar_fbar_[i][j]);
}
}
fitFracB0_fbar_ = sigModelB0_fbar_->getFitFractions();
if (fitFracB0_fbar_.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar_.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_fbar_[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::setExtraNtupleVars : Initial Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar_[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j = i; j < nSigComp_; j++) {
TString name = fitFracB0_fbar_[i][j].name();
name.Insert( name.Index("FitFrac"), "B0_fbar" );
fitFracB0_fbar_[i][j].name(name);
extraVars.push_back(fitFracB0_fbar_[i][j]);
}
}
// Calculate the ACPs and FitFrac asymmetries
this->calcAsymmetries(kTRUE);
// Add the Fit Fraction asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(fitFracAsymm_B0f_B0barfbar_[i]);
extraVars.push_back(fitFracAsymm_B0fbar_B0barf_[i]);
}
// Add the calculated CP asymmetry for each signal component
for (UInt_t i = 0; i < nSigComp_; i++) {
extraVars.push_back(acp_B0f_B0barfbar_[i]);
extraVars.push_back(acp_B0fbar_B0barf_[i]);
}
// Now add in the DP efficiency values
Double_t initMeanEffB0bar_f = sigModelB0bar_f_->getMeanEff().initValue();
meanEffB0bar_f_.value(initMeanEffB0bar_f); meanEffB0bar_f_.initValue(initMeanEffB0bar_f); meanEffB0bar_f_.genValue(initMeanEffB0bar_f);
extraVars.push_back(meanEffB0bar_f_);
Double_t initMeanEffB0_f = sigModelB0_f_->getMeanEff().initValue();
meanEffB0_f_.value(initMeanEffB0_f); meanEffB0_f_.initValue(initMeanEffB0_f); meanEffB0_f_.genValue(initMeanEffB0_f);
extraVars.push_back(meanEffB0_f_);
Double_t initMeanEffB0bar_fbar = sigModelB0bar_fbar_->getMeanEff().initValue();
meanEffB0bar_fbar_.value(initMeanEffB0bar_fbar); meanEffB0bar_fbar_.initValue(initMeanEffB0bar_fbar); meanEffB0bar_fbar_.genValue(initMeanEffB0bar_fbar);
extraVars.push_back(meanEffB0bar_fbar_);
Double_t initMeanEffB0_fbar = sigModelB0_fbar_->getMeanEff().initValue();
meanEffB0_fbar_.value(initMeanEffB0_fbar); meanEffB0_fbar_.initValue(initMeanEffB0_fbar); meanEffB0_fbar_.genValue(initMeanEffB0_fbar);
extraVars.push_back(meanEffB0_fbar_);
// Also add in the DP rates
Double_t initDPRateB0bar_f = sigModelB0bar_f_->getDPRate().initValue();
DPRateB0bar_f_.value(initDPRateB0bar_f); DPRateB0bar_f_.initValue(initDPRateB0bar_f); DPRateB0bar_f_.genValue(initDPRateB0bar_f);
extraVars.push_back(DPRateB0bar_f_);
Double_t initDPRateB0_f = sigModelB0_f_->getDPRate().initValue();
DPRateB0_f_.value(initDPRateB0_f); DPRateB0_f_.initValue(initDPRateB0_f); DPRateB0_f_.genValue(initDPRateB0_f);
extraVars.push_back(DPRateB0_f_);
Double_t initDPRateB0bar_fbar = sigModelB0bar_fbar_->getDPRate().initValue();
DPRateB0bar_fbar_.value(initDPRateB0bar_fbar); DPRateB0bar_fbar_.initValue(initDPRateB0bar_fbar); DPRateB0bar_fbar_.genValue(initDPRateB0bar_fbar);
extraVars.push_back(DPRateB0bar_fbar_);
Double_t initDPRateB0_fbar = sigModelB0_fbar_->getDPRate().initValue();
DPRateB0_fbar_.value(initDPRateB0_fbar); DPRateB0_fbar_.initValue(initDPRateB0_fbar); DPRateB0_fbar_.genValue(initDPRateB0_fbar);
extraVars.push_back(DPRateB0_fbar_);
}
void LauTimeDepNonFlavModel::finaliseFitResults(const TString& tablePrefixName)
{
// Retrieve parameters from the fit results for calculations and toy generation
// and eventually store these in output root ntuples/text files
// Now take the fit parameters and update them as necessary
// i.e. to make mag > 0.0, phase in the right range.
// This function will also calculate any other values, such as the
// fit fractions, using any errors provided by fitParErrors as appropriate.
// Also obtain the pull values: (measured - generated)/(average error)
if (this->useDP() == kTRUE) {
for (UInt_t i = 0; i < nSigComp_; ++i) {
// Check whether we have "a > 0.0", and phases in the right range
coeffPars_B0f_B0barfbar_[i]->finaliseValues();
coeffPars_B0fbar_B0barf_[i]->finaliseValues();
}
}
// update the pulls on the event fractions and asymmetries
if (this->doEMLFit()) {
signalEvents_->updatePull();
}
if (this->useDP() == kFALSE) {
signalAsym_->updatePull();
}
// Finalise the pulls on the decay time parameters
for (LauTagCatDtPdfMap::iterator iter = signalDecayTimePdfs_.begin(); iter != signalDecayTimePdfs_.end(); ++iter) {
LauDecayTimePdf* pdf = (*iter).second;
pdf->updatePulls();
}
if (useSinCos_) {
cosPhiMix_.updatePull();
sinPhiMix_.updatePull();
} else {
this->checkMixingPhase();
}
// Update the pulls on all the extra PDFs' parameters
for (LauTagCatPdfMap::iterator iter = sigExtraPdf_.begin(); iter != sigExtraPdf_.end(); ++iter) {
this->updateFitParameters(iter->second);
}
// Tagging-category fractions for signal and background events
Double_t firstCatFrac(1.0);
Int_t firstCat(0);
for (LauTagCatParamMap::iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
if (iter == signalTagCatFrac_.begin()) {
firstCat = iter->first;
continue;
}
LauParameter& par = (*iter).second;
firstCatFrac -= par.value();
// update the parameter pull
par.updatePull();
}
signalTagCatFrac_[firstCat].value(firstCatFrac);
signalTagCatFrac_[firstCat].updatePull();
// Fill the fit results to the ntuple
// update the coefficients and then calculate the fit fractions and ACP's
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_f_->updateCoeffs(coeffsB0bar_f_); sigModelB0bar_f_->calcExtraInfo();
sigModelB0_f_->updateCoeffs(coeffsB0_f_); sigModelB0_f_->calcExtraInfo();
sigModelB0bar_fbar_->updateCoeffs(coeffsB0bar_fbar_); sigModelB0bar_fbar_->calcExtraInfo();
sigModelB0_fbar_->updateCoeffs(coeffsB0_fbar_); sigModelB0_fbar_->calcExtraInfo();
LauParArray fitFracB0bar_f = sigModelB0bar_f_->getFitFractions();
if (fitFracB0bar_f.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_f[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0_f = sigModelB0_f_->getFitFractions();
if (fitFracB0_f.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0_f.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_f[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0_f[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0bar_fbar = sigModelB0bar_fbar_->getFitFractions();
if (fitFracB0bar_fbar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_fbar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0_fbar = sigModelB0_fbar_->getFitFractions();
if (fitFracB0_fbar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_fbar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::finaliseFitResults : Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_f_[i][j].value(fitFracB0bar_f[i][j].value());
fitFracB0_f_[i][j].value(fitFracB0_f[i][j].value());
fitFracB0bar_fbar_[i][j].value(fitFracB0bar_fbar[i][j].value());
fitFracB0_fbar_[i][j].value(fitFracB0_fbar[i][j].value());
}
}
meanEffB0bar_f_.value(sigModelB0bar_f_->getMeanEff().value());
meanEffB0_f_.value(sigModelB0_f_->getMeanEff().value());
meanEffB0bar_fbar_.value(sigModelB0bar_fbar_->getMeanEff().value());
meanEffB0_fbar_.value(sigModelB0_fbar_->getMeanEff().value());
DPRateB0bar_f_.value(sigModelB0bar_f_->getDPRate().value());
DPRateB0_f_.value(sigModelB0_f_->getDPRate().value());
DPRateB0bar_fbar_.value(sigModelB0bar_fbar_->getDPRate().value());
DPRateB0_fbar_.value(sigModelB0_fbar_->getDPRate().value());
this->calcAsymmetries();
// Then store the final fit parameters, and any extra parameters for
// the signal model (e.g. fit fractions, FF asymmetries, ACPs, mean efficiency and DP rate)
this->clearExtraVarVectors();
LauParameterList& extraVars = this->extraPars();
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0bar_f_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0_f_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0bar_fbar_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
extraVars.push_back(fitFracB0_fbar_[i][j]);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(fitFracAsymm_B0f_B0barfbar_[i]);
extraVars.push_back(fitFracAsymm_B0fbar_B0barf_[i]);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
extraVars.push_back(acp_B0f_B0barfbar_[i]);
extraVars.push_back(acp_B0fbar_B0barf_[i]);
}
extraVars.push_back(meanEffB0bar_f_);
extraVars.push_back(meanEffB0_f_);
extraVars.push_back(meanEffB0bar_fbar_);
extraVars.push_back(meanEffB0_fbar_);
extraVars.push_back(DPRateB0bar_f_);
extraVars.push_back(DPRateB0_f_);
extraVars.push_back(DPRateB0bar_fbar_);
extraVars.push_back(DPRateB0_fbar_);
this->printFitFractions(std::cout);
this->printAsymmetries(std::cout);
}
- const LauParameterPList& fitVars = this->fitPars();
- const LauParameterList& extraVars = this->extraPars();
LauFitNtuple* ntuple = this->fitNtuple();
- ntuple->storeParsAndErrors(fitVars, extraVars);
+ ntuple->storeParsAndErrors(this->fitPars(), this->multiDimConstrainedPars(), this->extraPars());
// find out the correlation matrix for the parameters
ntuple->storeCorrMatrix(this->iExpt(), this->fitStatus(), this->covarianceMatrix());
// Fill the data into ntuple
ntuple->updateFitNtuple();
// Print out the partial fit fractions, phases and the
// averaged efficiency, reweighted by the dynamics (and anything else)
if (this->writeLatexTable()) {
TString sigOutFileName(tablePrefixName);
sigOutFileName += "_"; sigOutFileName += this->iExpt(); sigOutFileName += "Expt.tex";
this->writeOutTable(sigOutFileName);
}
}
void LauTimeDepNonFlavModel::printFitFractions(std::ostream& output)
{
// Print out Fit Fractions, total DP rate and mean efficiency
// B0 -> f events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0f_B0barfbar_[i]->name());
output<<"B0bar FitFraction for component "<<i<<" ("<<compName<<") = "<<fitFracB0_f_[i][i].value()<<std::endl;
}
output<<"B0 -> f overall DP rate (integral of matrix element squared) = "<<DPRateB0_f_.value()<<std::endl;
output<<"B0 -> f average efficiency weighted by whole DP dynamics = "<<meanEffB0_f_.value()<<std::endl;
// B0bar -> fbar sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0f_B0barfbar_[i]->name());
const TString conjName(sigModelB0_f_->getConjResName(compName));
output<<"B0 FitFraction for component "<<i<<" ("<<conjName<<") = "<<fitFracB0bar_fbar_[i][i].value()<<std::endl;
}
output<<"B0bar -> fbar overall DP rate (integral of matrix element squared) = "<<DPRateB0bar_fbar_.value()<<std::endl;
output<<"B0bar -> fbar average efficiency weighted by whole DP dynamics = "<<meanEffB0bar_fbar_.value()<<std::endl;
// B0 -> fbar events
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0fbar_B0barf_[i]->name());
output<<"B0bar FitFraction for component "<<i<<" ("<<compName<<") = "<<fitFracB0_fbar_[i][i].value()<<std::endl;
}
output<<"B0 -> fbar overall DP rate (integral of matrix element squared) = "<<DPRateB0_fbar_.value()<<std::endl;
output<<"B0 -> fbar average efficiency weighted by whole DP dynamics = "<<meanEffB0_fbar_.value()<<std::endl;
// B0bar -> f sample
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0fbar_B0barf_[i]->name());
const TString conjName(sigModelB0_fbar_->getConjResName(compName));
output<<"B0 FitFraction for component "<<i<<" ("<<conjName<<") = "<<fitFracB0bar_f_[i][i].value()<<std::endl;
}
output<<"B0bar -> f overall DP rate (integral of matrix element squared) = "<<DPRateB0bar_f_.value()<<std::endl;
output<<"B0bar -> f average efficiency weighted by whole DP dynamics = "<<meanEffB0bar_f_.value()<<std::endl;
}
void LauTimeDepNonFlavModel::printAsymmetries(std::ostream& output)
{
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0f_B0barfbar_[i]->name());
output<<"Fit Fraction for B0(B0bar) -> f(fbar) asymmetry for component "<<i<<" ("<<compName<<") = "<<fitFracAsymm_B0f_B0barfbar_[i].value()<<std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0fbar_B0barf_[i]->name());
output<<"Fit Fraction for B0(B0bar) -> fbar(f) asymmetry for component "<<i<<" ("<<compName<<") = "<<fitFracAsymm_B0fbar_B0barf_[i].value()<<std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0f_B0barfbar_[i]->name());
output<<"ACP for B0(B0bar) -> f(fbar) component "<<i<<" ("<<compName<<") = "<<acp_B0f_B0barfbar_[i].value()<<" +- "<<acp_B0f_B0barfbar_[i].error()<<std::endl;
}
for (UInt_t i = 0; i < nSigComp_; i++) {
const TString compName(coeffPars_B0fbar_B0barf_[i]->name());
output<<"ACP for B0(B0bar) -> fbar(f) component "<<i<<" ("<<compName<<") = "<<acp_B0fbar_B0barf_[i].value()<<" +- "<<acp_B0fbar_B0barf_[i].error()<<std::endl;
}
}
void LauTimeDepNonFlavModel::writeOutTable(const TString& outputFile)
{
// Write out the results of the fit to a tex-readable table
std::ofstream fout(outputFile);
LauPrint print;
std::cout<<"INFO in LauTimeDepNonFlavModel::writeOutTable : Writing out results of the fit to the tex file "<<outputFile<<std::endl;
if (this->useDP() == kTRUE) {
// print the fit coefficients in one table
coeffPars_B0f_B0barfbar_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_B0f_B0barfbar_[i]->printTableRow(fout);
}
fout<<"\\hline"<<std::endl;
coeffPars_B0fbar_B0barf_.front()->printTableHeading(fout);
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_B0fbar_B0barf_[i]->printTableRow(fout);
}
fout<<"\\hline"<<std::endl;
fout<<"\\end{tabular}"<<std::endl<<std::endl;
// Combination for B0(B0bar) -> f(fbar)
fout<<"\\begin{tabular}{|l|c|c|c|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"Component & \\Bzb -> fbar \\ Fit Fraction & \\Bz ->f \\ Fit Fraction & Fit Fraction Asymmetry & $A_{\\CP}$ \\\\"<<std::endl;
fout<<"\\hline"<<std::endl;
Double_t fitFracSumB0bar_fbar(0.0);
Double_t fitFracSumB0_f(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_B0f_B0barfbar_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
fout<<resName<<" & $";
Double_t fitFracB0bar_fbar = fitFracB0bar_fbar_[i][i].value();
fitFracSumB0bar_fbar += fitFracB0bar_fbar;
print.printFormat(fout, fitFracB0bar_fbar);
fout << "$ & $" << std::endl;
Double_t fitFracB0_f = fitFracB0_f_[i][i].value();
fitFracSumB0_f += fitFracB0_f;
print.printFormat(fout, fitFracB0_f);
fout << "$ & $" << std::endl;
Double_t fitFracAsymm_B0f_B0barfbar = fitFracAsymm_B0f_B0barfbar_[i].value();
print.printFormat(fout, fitFracAsymm_B0f_B0barfbar);
fout << "$ & $" << std::endl;
Double_t acp_B0f_B0barfbar = acp_B0f_B0barfbar_[i].value();
Double_t acpErr_B0f_B0barfbar = acp_B0f_B0barfbar_[i].error();
print.printFormat(fout, acp_B0f_B0barfbar);
fout<<" \\pm ";
print.printFormat(fout, acpErr_B0f_B0barfbar);
fout<<"$ \\\\"<<std::endl;
}
fout<<"\\hline"<<std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum = & $";
print.printFormat(fout, fitFracSumB0bar_fbar);
fout << "$ & $";
print.printFormat(fout, fitFracSumB0_f);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline \n\\hline" << std::endl;
fout << "DP rate = & $";
print.printFormat(fout, DPRateB0bar_fbar_.value());
fout << "$ & $";
print.printFormat(fout, DPRateB0_f_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > =$ & $";
print.printFormat(fout, meanEffB0bar_fbar_.value());
fout << "$ & $";
print.printFormat(fout, meanEffB0_f_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$ & & & & & & & \\\\" << std::endl;
if (useSinCos_) {
fout << "$\\sinPhiMix =$ & $";
print.printFormat(fout, sinPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, sinPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
fout << "$\\cosPhiMix =$ & $";
print.printFormat(fout, cosPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, cosPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
} else {
fout << "$\\phiMix =$ & $";
print.printFormat(fout, phiMix_.value());
fout << " \\pm ";
print.printFormat(fout, phiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
}
fout << "\\hline \n\\end{tabular}" << std::endl;
// Another combination for B0(B0bar) -> fbar(f)
fout<<"\\begin{tabular}{|l|c|c|c|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"Component & \\Bzb -> f \\ Fit Fraction & \\Bz ->fbar \\ Fit Fraction & Fit Fraction Asymmetry & $A_{\\CP}$ \\\\"<<std::endl;
fout<<"\\hline"<<std::endl;
Double_t fitFracSumB0bar_f(0.0);
Double_t fitFracSumB0_fbar(0.0);
for (UInt_t i = 0; i < nSigComp_; i++) {
TString resName = coeffPars_B0fbar_B0barf_[i]->name();
resName = resName.ReplaceAll("_", "\\_");
fout<<resName<<" & $";
Double_t fitFracB0bar_f = fitFracB0bar_f_[i][i].value();
fitFracSumB0bar_f += fitFracB0bar_f;
print.printFormat(fout, fitFracB0bar_f);
fout << "$ & $" << std::endl;
Double_t fitFracB0_fbar = fitFracB0_fbar_[i][i].value();
fitFracSumB0_fbar += fitFracB0_fbar;
print.printFormat(fout, fitFracB0_fbar);
fout << "$ & $" << std::endl;
Double_t fitFracAsymm_B0fbar_B0barf = fitFracAsymm_B0fbar_B0barf_[i].value();
print.printFormat(fout, fitFracAsymm_B0fbar_B0barf);
fout << "$ & $" << std::endl;
Double_t acp_B0fbar_B0barf = acp_B0fbar_B0barf_[i].value();
Double_t acpErr_B0fbar_B0barf = acp_B0fbar_B0barf_[i].error();
print.printFormat(fout, acp_B0fbar_B0barf);
fout<<" \\pm ";
print.printFormat(fout, acpErr_B0fbar_B0barf);
fout<<"$ \\\\"<<std::endl;
}
fout<<"\\hline"<<std::endl;
// Also print out sum of fit fractions
fout << "Fit Fraction Sum = & $";
print.printFormat(fout, fitFracSumB0bar_f);
fout << "$ & $";
print.printFormat(fout, fitFracSumB0_fbar);
fout << "$ & & \\\\" << std::endl;
fout << "\\hline \n\\hline" << std::endl;
fout << "DP rate = & $";
print.printFormat(fout, DPRateB0bar_f_.value());
fout << "$ & $";
print.printFormat(fout, DPRateB0_fbar_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$< \\varepsilon > =$ & $";
print.printFormat(fout, meanEffB0bar_f_.value());
fout << "$ & $";
print.printFormat(fout, meanEffB0_fbar_.value());
fout << "$ & & \\\\" << std::endl;
fout << "$ & & & & & & & \\\\" << std::endl;
if (useSinCos_) {
fout << "$\\sinPhiMix =$ & $";
print.printFormat(fout, sinPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, sinPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
fout << "$\\cosPhiMix =$ & $";
print.printFormat(fout, cosPhiMix_.value());
fout << " \\pm ";
print.printFormat(fout, cosPhiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
} else {
fout << "$\\phiMix =$ & $";
print.printFormat(fout, phiMix_.value());
fout << " \\pm ";
print.printFormat(fout, phiMix_.error());
fout << "$ & & & & & & & \\\\" << std::endl;
}
fout << "\\hline \n\\end{tabular}" << std::endl;
}
if (!sigExtraPdf_.empty()) {
fout<<"\\begin{tabular}{|l|c|}"<<std::endl;
fout<<"\\hline"<<std::endl;
fout<<"\\Extra Signal PDFs' Parameters: & \\\\"<<std::endl;
for (LauTagCatPdfMap::const_iterator iter = sigExtraPdf_.begin(); iter != sigExtraPdf_.end(); ++iter) {
this->printFitParameters(iter->second, fout);
}
fout<<"\\hline \n\\end{tabular}"<<std::endl<<std::endl;
}
}
void LauTimeDepNonFlavModel::checkInitFitParams()
{
// Update the number of signal events to be total-sum(background events)
this->updateSigEvents();
// Check whether we want to have randomised initial fit parameters for the signal model
if (this->useRandomInitFitPars() == kTRUE) {
this->randomiseInitFitPars();
}
}
void LauTimeDepNonFlavModel::randomiseInitFitPars()
{
// Only randomise those parameters that are not fixed!
std::cout<<"INFO in LauTimeDepNonFlavModel::randomiseInitFitPars : Randomising the initial values of the coefficients of the DP components (and phiMix)..."<<std::endl;
for (UInt_t i = 0; i < nSigComp_; i++) {
coeffPars_B0f_B0barfbar_[i]->randomiseInitValues();
coeffPars_B0fbar_B0barf_[i]->randomiseInitValues();
}
phiMix_.randomiseValue(-LauConstants::pi, LauConstants::pi);
if (useSinCos_) {
sinPhiMix_.initValue(TMath::Sin(phiMix_.initValue()));
cosPhiMix_.initValue(TMath::Cos(phiMix_.initValue()));
}
}
LauTimeDepNonFlavModel::LauGenInfo LauTimeDepNonFlavModel::eventsToGenerate()
{
// TODO : Check whether in this bit we keep the same procedure or not
// Determine the number of events to generate for each hypothesis
// If we're smearing then smear each one individually
// NB this individual smearing has to be done individually per tagging category as well
LauGenInfo nEvtsGen;
LauTagCatGenInfo eventsB0, eventsB0bar;
// Signal
// If we're including the DP and decay time we can't decide on the tag
// yet, since it depends on the whole DP+dt PDF, however, if
// we're not then we need to decide.
Double_t evtWeight(1.0);
Double_t nEvts = signalEvents_->genValue();
if ( nEvts < 0.0 ) {
evtWeight = -1.0;
nEvts = TMath::Abs( nEvts );
}
Double_t sigAsym(0.0);
if (this->useDP() == kFALSE) {
sigAsym = signalAsym_->genValue();
for (LauTagCatParamMap::const_iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
const LauParameter& par = iter->second;
Double_t eventsbyTagCat = par.value() * nEvts;
Double_t eventsB0byTagCat = TMath::Nint(eventsbyTagCat/2.0 * (1.0 - sigAsym));
Double_t eventsB0barbyTagCat = TMath::Nint(eventsbyTagCat/2.0 * (1.0 + sigAsym));
if (this->doPoissonSmearing()) {
eventsB0byTagCat = LauRandom::randomFun()->Poisson(eventsB0byTagCat);
eventsB0barbyTagCat = LauRandom::randomFun()->Poisson(eventsB0barbyTagCat);
}
eventsB0[iter->first] = std::make_pair( TMath::Nint(eventsB0byTagCat), evtWeight );
eventsB0bar[iter->first] = std::make_pair( TMath::Nint(eventsB0barbyTagCat), evtWeight );
}
nEvtsGen[std::make_pair("signal",-1)] = eventsB0;
nEvtsGen[std::make_pair("signal",+1)] = eventsB0bar;
} else {
Double_t rateB0bar = sigModelB0bar_f_->getDPRate().value();
Double_t rateB0 = sigModelB0_f_->getDPRate().value();
if ( rateB0bar+rateB0 > 1e-30) {
sigAsym = (rateB0bar-rateB0)/(rateB0bar+rateB0);
}
for (LauTagCatParamMap::const_iterator iter = signalTagCatFrac_.begin(); iter != signalTagCatFrac_.end(); ++iter) {
const LauParameter& par = iter->second;
Double_t eventsbyTagCat = par.value() * nEvts;
if (this->doPoissonSmearing()) {
eventsbyTagCat = LauRandom::randomFun()->Poisson(eventsbyTagCat);
}
eventsB0[iter->first] = std::make_pair( TMath::Nint(eventsbyTagCat), evtWeight );
}
nEvtsGen[std::make_pair("signal",0)] = eventsB0; // generate signal event, decide tag later.
}
std::cout<<"INFO in LauTimeDepNonFlavModel::eventsToGenerate : Generating toy MC with:"<<std::endl;
std::cout<<" : Signal asymmetry = "<<sigAsym<<" and number of signal events = "<<signalEvents_->genValue()<<std::endl;
return nEvtsGen;
}
Bool_t LauTimeDepNonFlavModel::genExpt()
{
// Routine to generate toy Monte Carlo events according to the various models we have defined.
// Determine the number of events to generate for each hypothesis
LauGenInfo nEvts = this->eventsToGenerate();
Bool_t genOK(kTRUE);
Int_t evtNum(0);
// Loop over the hypotheses and generate the appropriate number of
// events for each one
for (LauGenInfo::const_iterator iter = nEvts.begin(); iter != nEvts.end(); ++iter) {
// find the category of events (e.g. signal)
const TString& evtCategory(iter->first.first);
// find the true flavour
curEvtTagFlv_ = iter->first.second;
// loop through each tagging category
const LauTagCatGenInfo& eventsByTagCat = iter->second;
for (LauTagCatGenInfo::const_iterator tagCatIter = eventsByTagCat.begin(); tagCatIter != eventsByTagCat.end(); ++tagCatIter) {
// get the tagging category
curEvtTagCat_ = tagCatIter->first;
// get the event weight for this category
const Double_t evtWeight( tagCatIter->second.second );
// get the number of events to generate in this configuration
const Int_t nEvtsGen( tagCatIter->second.first );
for (Int_t iEvt(0); iEvt<nEvtsGen; ++iEvt) {
this->setGenNtupleDoubleBranchValue( "evtWeight", evtWeight );
if (evtCategory == "signal") {
this->setGenNtupleIntegerBranchValue("genSig",1);
// All the generate*Event() methods have to fill in curEvtDecayTime_ and curEvtDecayTimeErr_
// In addition, generateSignalEvent has to decide on the tag and fill in curEvtTagFlv_
genOK = this->generateSignalEvent();
} else {
genOK = kFALSE;
}
if (!genOK) {
// If there was a problem with the generation then break out and return.
// The problem model will have adjusted itself so that all should be OK next time.
break;
}
if (this->useDP() == kTRUE) {
this->setDPDtBranchValues(); // store DP, decay time and tagging variables in the ntuple
}
// Store the event's tag and tagging category
this->setGenNtupleIntegerBranchValue("cpEigenvalue", cpEigenValue_);
this->setGenNtupleIntegerBranchValue("tagCat",curEvtTagCat_);
this->setGenNtupleIntegerBranchValue("tagFlv",curEvtTagFlv_);
// Store the event number (within this experiment)
// and then increment it
this->setGenNtupleIntegerBranchValue("iEvtWithinExpt",evtNum);
++evtNum;
// Write the values into the tree
this->fillGenNtupleBranches();
// Print an occasional progress message
if (iEvt%1000 == 0) {std::cout<<"INFO in LauTimeDepNonFlavModel::genExpt : Generated event number "<<iEvt<<" out of "<<nEvtsGen<<" "<<evtCategory<<" events."<<std::endl;}
} // end of loop over events for given species, tagCat and tagFlv
if (!genOK) {
break;
}
} // end of loop over tagCats for the given species and tagFlv
if (!genOK) {
break;
}
} //end of loop over species and tagFlv.
if (this->useDP() && genOK) {
sigModelB0bar_f_->checkToyMC(kTRUE);
sigModelB0_f_->checkToyMC(kTRUE);
sigModelB0bar_fbar_->checkToyMC(kTRUE);
sigModelB0_fbar_->checkToyMC(kTRUE);
std::cout<<"aSqMaxSet = "<<aSqMaxSet_<<" and aSqMaxVar = "<<aSqMaxVar_<<std::endl;
// Get the fit fractions if they're to be written into the latex table
if (this->writeLatexTable()) {
LauParArray fitFracB0bar_f = sigModelB0bar_f_->getFitFractions();
if (fitFracB0bar_f.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_f[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_f[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0_f = sigModelB0_f_->getFitFractions();
if (fitFracB0_f.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0_f.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_f[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0_f[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0bar_fbar = sigModelB0bar_fbar_->getFitFractions();
if (fitFracB0bar_fbar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0bar_fbar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0bar_fbar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
LauParArray fitFracB0_fbar = sigModelB0_fbar_->getFitFractions();
if (fitFracB0_fbar.size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar.size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
for (UInt_t i(0); i<nSigComp_; ++i) {
if (fitFracB0_fbar[i].size() != nSigComp_) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::generate : Fit Fraction array of unexpected dimension: "<<fitFracB0_fbar[i].size()<<std::endl;
gSystem->Exit(EXIT_FAILURE);
}
}
for (UInt_t i(0); i<nSigComp_; ++i) {
for (UInt_t j(i); j<nSigComp_; ++j) {
fitFracB0bar_f_[i][j].value(fitFracB0bar_f[i][j].value());
fitFracB0_f_[i][j].value(fitFracB0_f[i][j].value());
fitFracB0bar_fbar_[i][j].value(fitFracB0bar_fbar[i][j].value());
fitFracB0_fbar_[i][j].value(fitFracB0_fbar[i][j].value());
}
}
meanEffB0bar_f_.value(sigModelB0bar_f_->getMeanEff().value());
meanEffB0_f_.value(sigModelB0_f_->getMeanEff().value());
meanEffB0bar_fbar_.value(sigModelB0bar_fbar_->getMeanEff().value());
meanEffB0_fbar_.value(sigModelB0_fbar_->getMeanEff().value());
DPRateB0bar_f_.value(sigModelB0bar_f_->getDPRate().value());
DPRateB0_f_.value(sigModelB0_f_->getDPRate().value());
DPRateB0bar_fbar_.value(sigModelB0bar_fbar_->getDPRate().value());
DPRateB0_fbar_.value(sigModelB0_fbar_->getDPRate().value());
}
}
// If we're reusing embedded events or if the generation is being
// reset then clear the lists of used events
//if (!signalTree_.empty() && (reuseSignal_ || !genOK)) {
if (reuseSignal_ || !genOK) {
for(LauTagCatEmbDataMap::const_iterator iter = signalTree_.begin(); iter != signalTree_.end(); ++iter) {
(iter->second)->clearUsedList();
}
}
return genOK;
}
Bool_t LauTimeDepNonFlavModel::generateSignalEvent()
{
// Generate signal event, including SCF if necessary.
// DP:DecayTime generation follows.
// If it's ok, we then generate mES, DeltaE, Fisher/NN...
Bool_t genOK(kTRUE);
Bool_t generatedEvent(kFALSE);
Bool_t doSquareDP = kinematicsB0bar_f_->squareDP();
doSquareDP &= kinematicsB0_f_->squareDP();
doSquareDP &= kinematicsB0bar_fbar_->squareDP();
doSquareDP &= kinematicsB0_fbar_->squareDP();
LauKinematics* kinematics = 0;
//(kinematicsB0bar_);
// find the right decay time PDF for the current tagging category
LauTagCatDtPdfMap::const_iterator dt_iter = signalDecayTimePdfs_.find(curEvtTagCat_);
LauDecayTimePdf* decayTimePdf = (dt_iter != signalDecayTimePdfs_.end()) ? dt_iter->second : 0;
// find the right embedded data for the current tagging category
LauTagCatEmbDataMap::const_iterator emb_iter = signalTree_.find(curEvtTagCat_);
LauEmbeddedData* embeddedData = (emb_iter != signalTree_.end()) ? emb_iter->second : 0;
// find the right extra PDFs for the current tagging category
LauTagCatPdfMap::iterator extra_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* extraPdfs = (extra_iter != sigExtraPdf_.end()) ? &(extra_iter->second) : 0;
if (this->useDP()) {
if (embeddedData) {
// TODO : correct the kinematic term to the two possible final state
// This option is not allowed in the moment
kinematics = kinematicsB0bar_f_;
embeddedData->getEmbeddedEvent(kinematics);
curEvtTagFlv_ = TMath::Nint(embeddedData->getValue("tagFlv"));
curEvtDecayTimeErr_ = embeddedData->getValue(decayTimePdf->varErrName());
curEvtDecayTime_ = embeddedData->getValue(decayTimePdf->varName());
if (embeddedData->haveBranch("mcMatch")) {
Int_t match = TMath::Nint(embeddedData->getValue("mcMatch"));
if (match) {
this->setGenNtupleIntegerBranchValue("genTMSig",1);
this->setGenNtupleIntegerBranchValue("genSCFSig",0);
} else {
this->setGenNtupleIntegerBranchValue("genTMSig",0);
this->setGenNtupleIntegerBranchValue("genSCFSig",1);
}
}
} else {
nGenLoop_ = 0;
// generate the decay time error (NB the kTRUE forces the generation of a new value)
curEvtDecayTimeErr_ = decayTimePdf->generateError(kTRUE);
while (generatedEvent == kFALSE && nGenLoop_ < iterationsMax_) {
// Calculate the unnormalised truth-matched signal likelihood
// First let define the tag flavour
Double_t randNo = LauRandom::randomFun()->Rndm();
if (randNo < 0.5) {
curEvtTagFlv_ = +1; // B0 tag
} else {
curEvtTagFlv_ = -1; // B0bar tag
}
// Calculate event quantities that depend only on the tagCat and tagFlv
qD_ = curEvtTagFlv_*dilution_[curEvtTagCat_].unblindValue();
qDDo2_ = curEvtTagFlv_*0.5*deltaDilution_[curEvtTagCat_].unblindValue();
// Generate decay time
const Double_t tMin = decayTimePdf->minAbscissa();
const Double_t tMax = decayTimePdf->maxAbscissa();
curEvtDecayTime_ = LauRandom::randomFun()->Rndm()*(tMax-tMin) + tMin;
// Calculate all the decay time info
decayTimePdf->calcLikelihoodInfo(curEvtDecayTime_, curEvtDecayTimeErr_);
// Calculate the relevant amplitude normalisation for the two DP's
this->calculateAmplitudeNorm(decayTimePdf);
// DP variables
Double_t m13Sq(0.0), m23Sq(0.0);
Double_t randNo_finalState = LauRandom::randomFun()->Rndm();
if (randNo_finalState < normTimeDP_f_/(normTimeDP_f_+normTimeDP_fbar_)) {
finalState_ = +1; // A(Abar) -> f
// Generate DP position
kinematicsB0bar_f_->genFlatPhaseSpace(m13Sq, m23Sq);
kinematicsB0_f_->updateKinematics(kinematicsB0bar_f_->getm13Sq(), kinematicsB0bar_f_->getm23Sq() );
// Calculate the total A and Abar for the given DP position
sigModelB0_f_->calcLikelihoodInfo(m13Sq, m23Sq);
sigModelB0bar_f_->calcLikelihoodInfo(m13Sq, m23Sq);
// Calculate DP terms
this->calculateDPterms(decayTimePdf, sigModelB0bar_f_, sigModelB0_f_);
} else {
finalState_ = -1; // A(Abar) -> fbar
// Generate DP position
kinematicsB0bar_fbar_->genFlatPhaseSpace(m13Sq, m23Sq);
kinematicsB0_fbar_->updateKinematics(kinematicsB0bar_fbar_->getm13Sq(), kinematicsB0bar_fbar_->getm23Sq() );
// Calculate the total A and Abar for the given DP position
sigModelB0_fbar_->calcLikelihoodInfo(m13Sq, m23Sq);
sigModelB0bar_fbar_->calcLikelihoodInfo(m13Sq, m23Sq);
// Calculate DP terms
this->calculateDPterms(decayTimePdf, sigModelB0bar_fbar_, sigModelB0_fbar_);
}
//Finally we throw the dice to see whether this event should be generated
//We make a distinction between the likelihood of TM and SCF to tag the SCF events as such
randNo = LauRandom::randomFun()->Rndm();
if (randNo <= ASq_/aSqMaxSet_ ) {
generatedEvent = kTRUE;
nGenLoop_ = 0;
if (ASq_ > aSqMaxVar_) {aSqMaxVar_ = ASq_;}
} else {
nGenLoop_++;
}
} // end of while !generatedEvent loop
} // end of if (embeddedData) else control
} else {
if ( embeddedData ) {
embeddedData->getEmbeddedEvent(0);
curEvtTagFlv_ = TMath::Nint(embeddedData->getValue("tagFlv"));
curEvtDecayTimeErr_ = embeddedData->getValue(decayTimePdf->varErrName());
curEvtDecayTime_ = embeddedData->getValue(decayTimePdf->varName());
}
}
// Check whether we have generated the toy MC OK.
if (nGenLoop_ >= iterationsMax_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepNonFlavModel::generateSignalEvent : Hit max iterations: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
} else if (aSqMaxVar_ > aSqMaxSet_) {
aSqMaxSet_ = 1.01 * aSqMaxVar_;
genOK = kFALSE;
std::cerr<<"WARNING in LauTimeDepNonFlavModel::generateSignalEvent : Found a larger ASq value: setting aSqMaxSet_ to "<<aSqMaxSet_<<std::endl;
}
if (genOK) {
//Some variables, like Fisher or NN, might use m13Sq and m23Sq from the kinematics
//kinematicsB0bar_ is up to date, update kinematicsB0_
// TODO : check whether I should keep the kinemaics update for B0 in here
this->generateExtraPdfValues(extraPdfs, embeddedData);
}
// Check for problems with the embedding
if (embeddedData && (embeddedData->nEvents() == embeddedData->nUsedEvents())) {
std::cerr<<"WARNING in LauTimeDepNonFlavModel::generateSignalEvent : Source of embedded signal events used up, clearing the list of used events."<<std::endl;
embeddedData->clearUsedList();
}
return genOK;
}
void LauTimeDepNonFlavModel::calculateDPterms(LauDecayTimePdf* decayTimePdf, LauIsobarDynamics* sigModelB0bar, LauIsobarDynamics* sigModelB0)
{
// Retrieve the amplitudes and efficiency from the dynamics
const LauComplex& Abar = sigModelB0bar->getEvtDPAmp();
const LauComplex& A = sigModelB0->getEvtDPAmp();
Double_t eff = sigModelB0bar->getEvtEff();
// Calculate the DP terms
Double_t aSqSum = A.abs2() + Abar.abs2();
Double_t aSqDif = A.abs2() - Abar.abs2();
LauComplex inter = Abar * A.conj() * phiMixComplex_;
Double_t interTermIm = 2.0 * inter.im();
Double_t interTermRe = 2.0 * inter.re();
// Decay time pdf terms
Double_t dtCos = decayTimePdf->getCosTerm();
Double_t dtSin = decayTimePdf->getSinTerm();
Double_t dtCosh = decayTimePdf->getCoshTerm();
Double_t dtSinh = decayTimePdf->getSinhTerm();
// Combine all terms
Double_t cosTerm = dtCos * qD_ * aSqDif;
Double_t sinTerm = dtSin * qD_ * interTermIm;
Double_t coshTerm = dtCosh * (1.0 + qDDo2_) * aSqSum;
Double_t sinhTerm = dtSinh * (1.0 + qDDo2_) * interTermRe;
if ( cpEigenValue_ == CPOdd ) {
sinTerm *= -1.0;
sinhTerm *= -1.0;
}
// Total amplitude and multiply by the efficiency
ASq_ = coshTerm + cosTerm - sinTerm + sinhTerm;
ASq_ *= eff;
}
void LauTimeDepNonFlavModel::calculateAmplitudeNorm(LauDecayTimePdf* decayTimePdf)
{
// Integrals of the sum of the ampltudes to the f(fbar) integral( |A|^2 + |Abar|^2 ) dP
Double_t normTermNonDep_f = sigModelB0bar_f_->getDPNorm() + sigModelB0_f_->getDPNorm();
Double_t normTermNonDep_fbar = sigModelB0bar_fbar_->getDPNorm() + sigModelB0_fbar_->getDPNorm();
// Integrals of cross terms |Abar|*|Aconj|
Double_t normTermDep_f = interTermReNorm_f_;
Double_t normTermDep_fbar = interTermReNorm_fbar_;
// Decay time constant integrals
Double_t normTermCosh = decayTimePdf->getNormTermCosh();
Double_t normTermSinh = decayTimePdf->getNormTermSinh();
// Time-dependent DP normalisation terms
normTimeDP_f_ = normTermNonDep_f*normTermCosh + normTermDep_f*normTermSinh;
normTimeDP_fbar_ = normTermNonDep_fbar*normTermCosh + normTermDep_fbar*normTermSinh;
}
void LauTimeDepNonFlavModel::setupGenNtupleBranches()
{
// Setup the required ntuple branches
this->addGenNtupleDoubleBranch("evtWeight");
this->addGenNtupleIntegerBranch("genSig");
this->addGenNtupleIntegerBranch("cpEigenvalue");
this->addGenNtupleIntegerBranch("tagFlv");
this->addGenNtupleIntegerBranch("tagCat");
if (this->useDP() == kTRUE) {
// Let's add the decay time variables.
if (signalDecayTimePdfs_.begin() != signalDecayTimePdfs_.end()) {
LauDecayTimePdf* pdf = signalDecayTimePdfs_.begin()->second;
this->addGenNtupleDoubleBranch(pdf->varName());
this->addGenNtupleDoubleBranch(pdf->varErrName());
}
this->addGenNtupleDoubleBranch("m12_f");
this->addGenNtupleDoubleBranch("m23_f");
this->addGenNtupleDoubleBranch("m13_f");
this->addGenNtupleDoubleBranch("m12Sq_f");
this->addGenNtupleDoubleBranch("m23Sq_f");
this->addGenNtupleDoubleBranch("m13Sq_f");
this->addGenNtupleDoubleBranch("cosHel12_f");
this->addGenNtupleDoubleBranch("cosHel23_f");
this->addGenNtupleDoubleBranch("cosHel13_f");
if (kinematicsB0bar_f_->squareDP() && kinematicsB0_f_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime_f");
this->addGenNtupleDoubleBranch("thPrime_f");
}
this->addGenNtupleDoubleBranch("m12_fbar");
this->addGenNtupleDoubleBranch("m23_fbar");
this->addGenNtupleDoubleBranch("m13_fbar");
this->addGenNtupleDoubleBranch("m12Sq_fbar");
this->addGenNtupleDoubleBranch("m23Sq_fbar");
this->addGenNtupleDoubleBranch("m13Sq_fbar");
this->addGenNtupleDoubleBranch("cosHel12_fbar");
this->addGenNtupleDoubleBranch("cosHel23_fbar");
this->addGenNtupleDoubleBranch("cosHel13_fbar");
if (kinematicsB0bar_fbar_->squareDP() && kinematicsB0_fbar_->squareDP()) {
this->addGenNtupleDoubleBranch("mPrime_fbar");
this->addGenNtupleDoubleBranch("thPrime_fbar");
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
this->addGenNtupleDoubleBranch("reB0fAmp");
this->addGenNtupleDoubleBranch("imB0fAmp");
this->addGenNtupleDoubleBranch("reB0barfAmp");
this->addGenNtupleDoubleBranch("imB0barfAmp");
this->addGenNtupleDoubleBranch("reB0fbarAmp");
this->addGenNtupleDoubleBranch("imB0fbarAmp");
this->addGenNtupleDoubleBranch("reB0barfbarAmp");
this->addGenNtupleDoubleBranch("imB0barfbarAmp");
}
}
// Let's look at the extra variables for signal in one of the tagging categories
if ( ! sigExtraPdf_.empty() ) {
const LauPdfPList& oneTagCatPdfList { sigExtraPdf_.begin()->second };
for ( const LauAbsPdf* pdf : oneTagCatPdfList ) {
const std::vector<TString> varNames{ pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
this->addGenNtupleDoubleBranch( varName );
}
}
}
}
}
void LauTimeDepNonFlavModel::setDPDtBranchValues()
{
// Store the decay time variables.
if (signalDecayTimePdfs_.begin() != signalDecayTimePdfs_.end()) {
LauDecayTimePdf* pdf = signalDecayTimePdfs_.begin()->second;
this->setGenNtupleDoubleBranchValue(pdf->varName(),curEvtDecayTime_);
this->setGenNtupleDoubleBranchValue(pdf->varErrName(),curEvtDecayTimeErr_);
}
LauKinematics* kinematics_f(0);
LauKinematics* kinematics_fbar(0);
if (curEvtTagFlv_<0) {
kinematics_f = kinematicsB0_f_;
kinematics_fbar = kinematicsB0_fbar_;
} else {
kinematics_f = kinematicsB0bar_f_;
kinematics_fbar = kinematicsB0bar_fbar_;
}
// Store all the DP information
this->setGenNtupleDoubleBranchValue("m12_f", kinematics_f->getm12());
this->setGenNtupleDoubleBranchValue("m23_f", kinematics_f->getm23());
this->setGenNtupleDoubleBranchValue("m13_f", kinematics_f->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq_f", kinematics_f->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq_f", kinematics_f->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq_f", kinematics_f->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12_f", kinematics_f->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23_f", kinematics_f->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13_f", kinematics_f->getc13());
if (kinematics_f->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime_f", kinematics_f->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime_f", kinematics_f->getThetaPrime());
}
this->setGenNtupleDoubleBranchValue("m12_fbar", kinematics_fbar->getm12());
this->setGenNtupleDoubleBranchValue("m23_fbar", kinematics_fbar->getm23());
this->setGenNtupleDoubleBranchValue("m13_fbar", kinematics_fbar->getm13());
this->setGenNtupleDoubleBranchValue("m12Sq_fbar", kinematics_fbar->getm12Sq());
this->setGenNtupleDoubleBranchValue("m23Sq_fbar", kinematics_fbar->getm23Sq());
this->setGenNtupleDoubleBranchValue("m13Sq_fbar", kinematics_fbar->getm13Sq());
this->setGenNtupleDoubleBranchValue("cosHel12_fbar", kinematics_fbar->getc12());
this->setGenNtupleDoubleBranchValue("cosHel23_fbar", kinematics_fbar->getc23());
this->setGenNtupleDoubleBranchValue("cosHel13_fbar", kinematics_fbar->getc13());
if (kinematics_fbar->squareDP()) {
this->setGenNtupleDoubleBranchValue("mPrime_fbar", kinematics_fbar->getmPrime());
this->setGenNtupleDoubleBranchValue("thPrime_fbar", kinematics_fbar->getThetaPrime());
}
// Can add the real and imaginary parts of the B0 and B0bar total
// amplitudes seen in the generation (restrict this with a flag
// that defaults to false)
if ( storeGenAmpInfo_ ) {
if ( this->getGenNtupleIntegerBranchValue("genSig")==1 ) {
LauComplex Abar_f = sigModelB0bar_f_->getEvtDPAmp();
LauComplex A_f = sigModelB0_f_->getEvtDPAmp();
LauComplex Abar_fbar = sigModelB0bar_fbar_->getEvtDPAmp();
LauComplex A_fbar = sigModelB0_fbar_->getEvtDPAmp();
this->setGenNtupleDoubleBranchValue("reB0fAmp", A_f.re());
this->setGenNtupleDoubleBranchValue("imB0fAmp", A_f.im());
this->setGenNtupleDoubleBranchValue("reB0barfAmp", Abar_f.re());
this->setGenNtupleDoubleBranchValue("imB0barfAmp", Abar_f.im());
this->setGenNtupleDoubleBranchValue("reB0fbarAmp", A_fbar.re());
this->setGenNtupleDoubleBranchValue("imB0fbarAmp", A_fbar.im());
this->setGenNtupleDoubleBranchValue("reB0barfbarAmp", Abar_fbar.re());
this->setGenNtupleDoubleBranchValue("imB0barfbarAmp", Abar_fbar.im());
} else {
this->setGenNtupleDoubleBranchValue("reB0fAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0fAmp", 0.0);
this->setGenNtupleDoubleBranchValue("reB0barfAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0barfAmp", 0.0);
this->setGenNtupleDoubleBranchValue("reB0fbarAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0fbarAmp", 0.0);
this->setGenNtupleDoubleBranchValue("reB0barfbarAmp", 0.0);
this->setGenNtupleDoubleBranchValue("imB0barfbarAmp", 0.0);
}
}
}
void LauTimeDepNonFlavModel::generateExtraPdfValues(LauPdfPList* extraPdfs, LauEmbeddedData* embeddedData)
{
// TODO : need to add the additional DP
LauKinematics* kinematics_f(0);
//LauKinematics* kinematics_fbar(0);
if (curEvtTagFlv_<0) {
kinematics_f = kinematicsB0_f_;
//kinematics_fbar = kinematicsB0_fbar_;
} else {
kinematics_f = kinematicsB0bar_f_;
//kinematics_fbar = kinematicsB0bar_fbar_;
}
// Generate from the extra PDFs
if (extraPdfs) {
for (LauPdfPList::iterator pdf_iter = extraPdfs->begin(); pdf_iter != extraPdfs->end(); ++pdf_iter) {
LauFitData genValues;
if (embeddedData) {
genValues = embeddedData->getValues( (*pdf_iter)->varNames() );
} else {
genValues = (*pdf_iter)->generate(kinematics_f);
}
for ( LauFitData::const_iterator var_iter = genValues.begin(); var_iter != genValues.end(); ++var_iter ) {
TString varName = var_iter->first;
if ( varName != "m13Sq" && varName != "m23Sq" ) {
Double_t value = var_iter->second;
this->setGenNtupleDoubleBranchValue(varName,value);
}
}
}
}
}
void LauTimeDepNonFlavModel::propagateParUpdates()
{
// Update the complex mixing phase
if (useSinCos_) {
phiMixComplex_.setRealPart(cosPhiMix_.unblindValue());
phiMixComplex_.setImagPart(-1.0*sinPhiMix_.unblindValue());
} else {
phiMixComplex_.setRealPart(TMath::Cos(-1.0*phiMix_.unblindValue()));
phiMixComplex_.setImagPart(TMath::Sin(-1.0*phiMix_.unblindValue()));
}
// Update the total normalisation for the signal likelihood
if (this->useDP() == kTRUE) {
this->updateCoeffs();
sigModelB0bar_f_->updateCoeffs(coeffsB0bar_f_);
sigModelB0_f_->updateCoeffs(coeffsB0_f_);
sigModelB0bar_fbar_->updateCoeffs(coeffsB0bar_fbar_);
sigModelB0_fbar_->updateCoeffs(coeffsB0_fbar_);
this->calcInterTermNorm();
}
// Update the signal events from the background numbers if not doing an extended fit
this->updateSigEvents();
}
void LauTimeDepNonFlavModel::updateSigEvents()
{
// tagging-category fractions for signal events
this->setFirstTagCatFrac(signalTagCatFrac_);
// Only do this for an non-extended fit
// And then only if the signal yield is floating
if ( this->doEMLFit() || signalEvents_->fixed() ) {
return;
}
// Initially set the signal yield to be the
// total number of events in the data sample
Double_t signalEvents = this->eventsPerExpt();
signalEvents_->value(signalEvents);
}
void LauTimeDepNonFlavModel::setFirstTagCatFrac(LauTagCatParamMap& theMap)
{
Double_t firstCatFrac = 1.0;
Int_t firstCat(0);
for (LauTagCatParamMap::iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
if (iter == theMap.begin()) {
firstCat = iter->first;
continue;
}
LauParameter& par = iter->second;
firstCatFrac -= par.unblindValue();
}
theMap[firstCat].value(firstCatFrac);
}
void LauTimeDepNonFlavModel::cacheInputFitVars()
{
// Fill the internal data trees of the signal and background models.
// Note that we store the events of both charges in both the
// negative and the positive models. It's only later, at the stage
// when the likelihood is being calculated, that we separate them.
LauFitDataTree* inputFitData = this->fitData();
// Start by caching the tagging and CP-eigenstate information
evtTagCatVals_.clear();
evtTagFlvVals_.clear();
evtCPEigenVals_.clear();
if ( ! inputFitData->haveBranch( tagCatVarName_ ) ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::cacheInputFitVars : Input data does not contain branch \"" << tagCatVarName_ << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
if ( ! inputFitData->haveBranch( tagVarName_ ) ) {
std::cerr << "ERROR in LauTimeDepNonFlavModel::cacheInputFitVars : Input data does not contain branch \"" << tagVarName_ << "\"." << std::endl;
gSystem->Exit(EXIT_FAILURE);
}
const Bool_t hasCPEV = ( (cpevVarName_ != "") && inputFitData->haveBranch( cpevVarName_ ) );
UInt_t nEvents = inputFitData->nEvents();
evtTagCatVals_.reserve( nEvents );
evtTagFlvVals_.reserve( nEvents );
evtCPEigenVals_.reserve( nEvents );
LauFitData::const_iterator fitdata_iter;
for (UInt_t iEvt = 0; iEvt < nEvents; iEvt++) {
const LauFitData& dataValues = inputFitData->getData(iEvt);
fitdata_iter = dataValues.find( tagCatVarName_ );
curEvtTagCat_ = static_cast<Int_t>( fitdata_iter->second );
if ( ! this->validTagCat( curEvtTagCat_ ) ) {
std::cerr << "WARNING in LauTimeDepNonFlavModel::cacheInputFitVars : Invalid tagging category " << curEvtTagCat_ << " for event " << iEvt << ", setting it to untagged" << std::endl;
curEvtTagCat_ = 0;
}
evtTagCatVals_.push_back( curEvtTagCat_ );
fitdata_iter = dataValues.find( tagVarName_ );
curEvtTagFlv_ = static_cast<Int_t>( fitdata_iter->second );
if ( TMath::Abs( curEvtTagFlv_ ) != 1 ) {
if ( curEvtTagFlv_ > 0 ) {
std::cerr << "WARNING in LauTimeDepNonFlavModel::cacheInputFitVars : Invalid tagging output " << curEvtTagFlv_ << " for event " << iEvt << ", setting it to +1" << std::endl;
curEvtTagFlv_ = +1;
} else {
std::cerr << "WARNING in LauTimeDepNonFlavModel::cacheInputFitVars : Invalid tagging output " << curEvtTagFlv_ << " for event " << iEvt << ", setting it to -1" << std::endl;
curEvtTagFlv_ = -1;
}
}
evtTagFlvVals_.push_back( curEvtTagFlv_ );
// if the CP-eigenvalue is in the data use those, otherwise use the default
if ( hasCPEV ) {
fitdata_iter = dataValues.find( cpevVarName_ );
const Int_t cpEV = static_cast<Int_t>( fitdata_iter->second );
if ( cpEV == 1 ) {
cpEigenValue_ = CPEven;
} else if ( cpEV == -1 ) {
cpEigenValue_ = CPOdd;
} else {
std::cerr<<"WARNING in LauTimeDepNonFlavModel::cacheInputFitVars : Unknown value: "<<cpEV<<" for CP eigenvalue, setting to CP-even"<<std::endl;
cpEigenValue_ = CPEven;
}
}
evtCPEigenVals_.push_back( cpEigenValue_ );
}
// We'll cache the DP amplitudes at the end because we'll
// append some points that the other PDFs won't deal with.
if (this->useDP() == kTRUE) {
// DecayTime and SigmaDecayTime
for (LauTagCatDtPdfMap::iterator dt_iter = signalDecayTimePdfs_.begin(); dt_iter != signalDecayTimePdfs_.end(); ++dt_iter) {
(*dt_iter).second->cacheInfo(*inputFitData);
}
}
// ...and then the extra PDFs
for (LauTagCatPdfMap::iterator pdf_iter = sigExtraPdf_.begin(); pdf_iter != sigExtraPdf_.end(); ++pdf_iter) {
this->cacheInfo(pdf_iter->second, *inputFitData);
}
if (this->useDP() == kTRUE) {
sigModelB0bar_f_->fillDataTree(*inputFitData);
sigModelB0_f_->fillDataTree(*inputFitData);
sigModelB0bar_fbar_->fillDataTree(*inputFitData);
sigModelB0_fbar_->fillDataTree(*inputFitData);
}
}
Double_t LauTimeDepNonFlavModel::getTotEvtLikelihood(const UInt_t iEvt)
{
// Find out whether the tag-side B was a B0 or a B0bar.
curEvtTagFlv_ = evtTagFlvVals_[iEvt];
// Also get the tagging category.
curEvtTagCat_ = evtTagCatVals_[iEvt];
// Get the CP eigenvalue of the current event
cpEigenValue_ = evtCPEigenVals_[iEvt];
// Get the DP and DecayTime likelihood for signal
this->getEvtDPDtLikelihood(iEvt);
// Get the combined extra PDFs likelihood for signal
this->getEvtExtraLikelihoods(iEvt);
// Construct the total likelihood for signal, qqbar and BBbar backgrounds
Double_t sigLike = sigDPLike_ * sigExtraLike_;
Double_t signalEvents = signalEvents_->unblindValue();
if (this->useDP() == kFALSE) {
signalEvents *= 0.5 * (1.0 + curEvtTagFlv_ * signalAsym_->unblindValue());
}
// Construct the total event likelihood
Double_t likelihood(sigLike*signalTagCatFrac_[curEvtTagCat_].unblindValue());
if ( ! signalEvents_->fixed() ) {
likelihood *= signalEvents;
}
return likelihood;
}
Double_t LauTimeDepNonFlavModel::getEventSum() const
{
Double_t eventSum(0.0);
eventSum += signalEvents_->unblindValue();
return eventSum;
}
void LauTimeDepNonFlavModel::getEvtDPDtLikelihood(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// Dalitz plot for the given event evtNo.
sigDPLike_ = 1.0; //There's always a likelihood term for signal, so we better not zero it.
if ( this->useDP() == kFALSE ) {
return;
}
// Mistag probabilities. Defined as: omega = prob of the tagging B0 being reported as B0bar
// Whether we want omega or omegaBar depends on q_tag, hence curEvtTagFlv_*... in the previous lines
//Double_t misTagFrac = 0.5 * (1.0 - dilution_[curEvtTagCat_] - qDDo2);
//Double_t misTagFracBar = 0.5 * (1.0 - dilution_[curEvtTagCat_] + qDDo2);
// Calculate event quantities
qD_ = curEvtTagFlv_*dilution_[curEvtTagCat_].unblindValue();
qDDo2_ = curEvtTagFlv_*0.5*deltaDilution_[curEvtTagCat_].unblindValue();
//LauDecayTimePdf* signalDtPdf = signalDecayTimePdfs_[curEvtTagCat_];
LauDecayTimePdf* decayTimePdf = signalDecayTimePdfs_[curEvtTagCat_];
decayTimePdf->calcLikelihoodInfo(static_cast<std::size_t>(iEvt));
// Calculate the relevant amplitude normalisation for the two DP's
this->calculateAmplitudeNorm(decayTimePdf);
Double_t randNo = LauRandom::randomFun()->Rndm();
if (randNo < normTimeDP_f_/(normTimeDP_f_+normTimeDP_fbar_)) {
finalState_ = +1; // A(Abar) -> f
// Calculate the likelihood for the f final state
sigModelB0bar_f_->calcLikelihoodInfo(iEvt);
sigModelB0_f_->calcLikelihoodInfo(iEvt);
// Calculate DP terms
this->calculateDPterms(decayTimePdf, sigModelB0bar_f_, sigModelB0_f_);
} else {
finalState_ = -1; // A(Abar) -> fbar
// Calculate the likelihood for the fbar final state
sigModelB0bar_fbar_->calcLikelihoodInfo(iEvt);
sigModelB0_fbar_->calcLikelihoodInfo(iEvt);
// Calculate DP terms
this->calculateDPterms(decayTimePdf, sigModelB0bar_fbar_, sigModelB0_fbar_);
}
// Calculate the normalised signal likelihood
sigDPLike_ = ASq_ / (normTimeDP_f_+normTimeDP_fbar_);
}
void LauTimeDepNonFlavModel::getEvtExtraLikelihoods(const UInt_t iEvt)
{
// Function to return the signal and background likelihoods for the
// extra variables for the given event evtNo.
sigExtraLike_ = 1.0; //There's always a likelihood term for signal, so we better not zero it.
// First, those independent of the tagging of the event:
// signal
LauTagCatPdfMap::iterator sig_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* pdfList = (sig_iter != sigExtraPdf_.end())? &(sig_iter->second) : 0;
if (pdfList) {
sigExtraLike_ = this->prodPdfValue( *pdfList, iEvt );
}
}
void LauTimeDepNonFlavModel::updateCoeffs()
{
coeffsB0bar_f_.clear(); coeffsB0_f_.clear(); coeffsB0bar_fbar_.clear(); coeffsB0_fbar_.clear();
coeffsB0bar_f_.reserve(nSigComp_); coeffsB0_f_.reserve(nSigComp_);
coeffsB0bar_fbar_.reserve(nSigComp_); coeffsB0_fbar_.reserve(nSigComp_);
for (UInt_t i = 0; i < nSigComp_; ++i) {
coeffsB0bar_f_.push_back(coeffPars_B0fbar_B0barf_[i]->antiparticleCoeff());
coeffsB0_f_.push_back(coeffPars_B0f_B0barfbar_[i]->particleCoeff());
coeffsB0bar_fbar_.push_back(coeffPars_B0f_B0barfbar_[i]->antiparticleCoeff());
coeffsB0_fbar_.push_back(coeffPars_B0fbar_B0barf_[i]->particleCoeff());
}
}
Bool_t LauTimeDepNonFlavModel::validTagCat(Int_t tagCat) const
{
return (validTagCats_.find(tagCat) != validTagCats_.end());
}
Bool_t LauTimeDepNonFlavModel::checkTagCatFracMap(const LauTagCatParamMap& theMap) const
{
// First check that there is an entry for each tagging category.
// NB an entry won't have been added if it isn't a valid category
// so don't need to check for that here.
if (theMap.size() != signalTagCatFrac_.size()) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::checkTagCatFracMap : Not all tagging categories present."<<std::endl;
return kFALSE;
}
// Now check that the fractions sum up to unity.
Double_t tagCatFracSum(0.0);
for (LauTagCatParamMap::const_iterator iter = theMap.begin(); iter != theMap.end(); ++iter) {
const LauParameter& par = (*iter).second;
tagCatFracSum += par.unblindValue();
}
if ((tagCatFracSum - 1.0) > 1E-10) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::checkTagCatFracMap : Tagging category event fractions do not sum to unity."<<std::endl;
return kFALSE;
}
// If we've got to here then all is OK.
return kTRUE;
}
void LauTimeDepNonFlavModel::checkMixingPhase()
{
Double_t phase = phiMix_.value();
Double_t genPhase = phiMix_.genValue();
// Check now whether the phase lies in the right range (-pi to pi).
Bool_t withinRange(kFALSE);
while (withinRange == kFALSE) {
if (phase > -LauConstants::pi && phase < LauConstants::pi) {
withinRange = kTRUE;
} else {
// Not within the specified range
if (phase > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (phase < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
}
}
// A further problem can occur when the generated phase is close to -pi or pi.
// The phase can wrap over to the other end of the scale -
// this leads to artificially large pulls so we wrap it back.
Double_t diff = phase - genPhase;
if (diff > LauConstants::pi) {
phase -= LauConstants::twoPi;
} else if (diff < -LauConstants::pi) {
phase += LauConstants::twoPi;
}
// finally store the new value in the parameter
// and update the pull
phiMix_.value(phase);
phiMix_.updatePull();
}
void LauTimeDepNonFlavModel::embedSignal(Int_t tagCat, const TString& fileName, const TString& treeName,
Bool_t reuseEventsWithinEnsemble, Bool_t reuseEventsWithinExperiment)
{
if (signalTree_[tagCat]) {
std::cerr<<"ERROR in LauTimeDepNonFlavModel::embedSignal : Already embedding signal from file for tagging category "<<tagCat<<"."<<std::endl;
return;
}
if (!reuseEventsWithinEnsemble && reuseEventsWithinExperiment) {
std::cerr<<"WARNING in LauTimeDepNonFlavModel::embedSignal : Conflicting options provided, will not reuse events at all."<<std::endl;
reuseEventsWithinExperiment = kFALSE;
}
signalTree_[tagCat] = new LauEmbeddedData(fileName,treeName,reuseEventsWithinExperiment);
Bool_t dataOK = signalTree_[tagCat]->findBranches();
if (!dataOK) {
delete signalTree_[tagCat]; signalTree_[tagCat] = 0;
std::cerr<<"ERROR in LauTimeDepNonFlavModel::embedSignal : Problem creating data tree for embedding."<<std::endl;
return;
}
reuseSignal_ = reuseEventsWithinEnsemble;
}
void LauTimeDepNonFlavModel::setupSPlotNtupleBranches()
{
// add branches for storing the experiment number and the number of
// the event within the current experiment
this->addSPlotNtupleIntegerBranch("iExpt");
this->addSPlotNtupleIntegerBranch("iEvtWithinExpt");
// Store the efficiency of the event (for inclusive BF calculations).
if (this->storeDPEff()) {
this->addSPlotNtupleDoubleBranch("efficiency");
}
// Store the total event likelihood for each species.
this->addSPlotNtupleDoubleBranch("sigTotalLike");
// Store the DP likelihoods
if (this->useDP()) {
this->addSPlotNtupleDoubleBranch("sigDPLike");
}
// Store the likelihoods for each extra PDF
const LauPdfPList* pdfList( &(sigExtraPdf_.begin()->second) );
this->addSPlotNtupleBranches(pdfList, "sig");
}
void LauTimeDepNonFlavModel::addSPlotNtupleBranches(const LauPdfPList* extraPdfs, const TString& prefix)
{
if (!extraPdfs) {
return;
}
// Loop through each of the PDFs
for ( const LauAbsPdf* pdf : *extraPdfs ) {
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply add one branch for that variable
TString varName = pdf->varName();
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// need a branch for them both together and
// branches for each
TString allVars("");
for ( const TString& varName : varNames ) {
allVars += varName;
TString name(prefix);
name += varName;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
}
TString name(prefix);
name += allVars;
name += "Like";
this->addSPlotNtupleDoubleBranch(name);
} else {
std::cerr<<"WARNING in LauTimeDepNonFlavModel::addSPlotNtupleBranches : Can't yet deal with 3D PDFs."<<std::endl;
}
}
}
Double_t LauTimeDepNonFlavModel::setSPlotNtupleBranchValues(LauPdfPList* extraPdfs, const TString& prefix, UInt_t iEvt)
{
// Store the PDF value for each variable in the list
Double_t totalLike(1.0);
Double_t extraLike(0.0);
if ( !extraPdfs ) {
return totalLike;
}
for ( LauAbsPdf* pdf : *extraPdfs ) {
// calculate the likelihood for this event
pdf->calcLikelihoodInfo(iEvt);
extraLike = pdf->getLikelihood();
totalLike *= extraLike;
// Count the number of input variables that are not
// DP variables (used in the case where there is DP
// dependence for e.g. MVA)
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 1 ) {
// If the PDF only has one variable then
// simply store the value for that variable
TString varName = pdf->varName();
TString name(prefix);
name += varName;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else if ( nVars == 2 ) {
// If the PDF has two variables then we
// store the value for them both together
// and for each on their own
TString allVars("");
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
allVars += varName;
TString name(prefix);
name += varName;
name += "Like";
Double_t indivLike = pdf->getLikelihood( varName );
this->setSPlotNtupleDoubleBranchValue(name, indivLike);
}
}
TString name(prefix);
name += allVars;
name += "Like";
this->setSPlotNtupleDoubleBranchValue(name, extraLike);
} else {
std::cerr<<"WARNING in LauAllFitModel::setSPlotNtupleBranchValues : Can't yet deal with 3D PDFs."<<std::endl;
}
}
return totalLike;
}
LauSPlot::NameSet LauTimeDepNonFlavModel::variableNames() const
{
LauSPlot::NameSet nameSet;
if (this->useDP()) {
nameSet.insert("DP");
}
LauPdfPList pdfList( (sigExtraPdf_.begin()->second) );
for ( const LauAbsPdf* pdf : pdfList ) {
// Loop over the variables involved in each PDF
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
// If they are not DP coordinates then add them
if ( varName != "m13Sq" && varName != "m23Sq" ) {
nameSet.insert( varName );
}
}
}
return nameSet;
}
LauSPlot::NumbMap LauTimeDepNonFlavModel::freeSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (!signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
return numbMap;
}
LauSPlot::NumbMap LauTimeDepNonFlavModel::fixdSpeciesNames() const
{
LauSPlot::NumbMap numbMap;
if (signalEvents_->fixed() && this->doEMLFit()) {
numbMap["sig"] = signalEvents_->genValue();
}
return numbMap;
}
LauSPlot::TwoDMap LauTimeDepNonFlavModel::twodimPDFs() const
{
LauSPlot::TwoDMap twodimMap;
const LauPdfPList& pdfList { sigExtraPdf_.begin()->second };
for ( const LauAbsPdf* pdf : pdfList ) {
// Count the number of input variables that are not DP variables
UInt_t nVars(0);
const std::vector<TString> varNames { pdf->varNames() };
for ( const TString& varName : varNames ) {
if ( varName != "m13Sq" && varName != "m23Sq" ) {
++nVars;
}
}
if ( nVars == 2 ) {
twodimMap.insert( std::make_pair( "sig", std::make_pair( varNames[0], varNames[1] ) ) );
}
}
return twodimMap;
}
void LauTimeDepNonFlavModel::storePerEvtLlhds()
{
std::cout<<"INFO in LauTimeDepNonFlavModel::storePerEvtLlhds : Storing per-event likelihood values..."<<std::endl;
LauFitDataTree* inputFitData = this->fitData();
// if we've not been using the DP model then we need to cache all
// the info here so that we can get the efficiency from it
if (!this->useDP() && this->storeDPEff()) {
sigModelB0bar_f_->initialise(coeffsB0bar_f_);
sigModelB0_f_->initialise(coeffsB0_f_);
sigModelB0bar_fbar_->initialise(coeffsB0bar_fbar_);
sigModelB0_fbar_->initialise(coeffsB0_fbar_);
sigModelB0bar_f_->fillDataTree(*inputFitData);
sigModelB0_f_->fillDataTree(*inputFitData);
sigModelB0bar_fbar_->fillDataTree(*inputFitData);
sigModelB0_fbar_->fillDataTree(*inputFitData);
}
UInt_t evtsPerExpt(this->eventsPerExpt());
LauIsobarDynamics* sigModel(sigModelB0bar_f_);
for (UInt_t iEvt = 0; iEvt < evtsPerExpt; ++iEvt) {
// Find out whether we have B0bar or B0
curEvtTagFlv_ = evtTagFlvVals_[iEvt];
curEvtTagCat_ = evtTagCatVals_[iEvt];
LauTagCatPdfMap::iterator sig_iter = sigExtraPdf_.find(curEvtTagCat_);
LauPdfPList* sigPdfs = (sig_iter != sigExtraPdf_.end())? &(sig_iter->second) : 0;
// the DP information
this->getEvtDPDtLikelihood(iEvt);
if (this->storeDPEff()) {
if (!this->useDP()) {
sigModel->calcLikelihoodInfo(iEvt);
}
this->setSPlotNtupleDoubleBranchValue("efficiency",sigModel->getEvtEff());
}
if (this->useDP()) {
sigTotalLike_ = sigDPLike_;
this->setSPlotNtupleDoubleBranchValue("sigDPLike",sigDPLike_);
} else {
sigTotalLike_ = 1.0;
}
// the signal PDF values
sigTotalLike_ *= this->setSPlotNtupleBranchValues(sigPdfs, "sig", iEvt);
// the total likelihoods
this->setSPlotNtupleDoubleBranchValue("sigTotalLike",sigTotalLike_);
// fill the tree
this->fillSPlotNtupleBranches();
}
std::cout<<"INFO in LauTimeDepNonFlavModel::storePerEvtLlhds : Finished storing per-event likelihood values."<<std::endl;
}
void LauTimeDepNonFlavModel::weightEvents( const TString& /*dataFileName*/, const TString& /*dataTreeName*/ )
{
std::cerr << "ERROR in LauTimeDepNonFlavModel::weightEvents : Method not available for this fit model." << std::endl;
return;
}
void LauTimeDepNonFlavModel::savePDFPlots(const TString& /*label*/)
{
}
void LauTimeDepNonFlavModel::savePDFPlotsWave(const TString& /*label*/, const Int_t& /*spin*/)
{
}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Nov 19, 8:19 PM (1 d, 5 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3804507
Default Alt Text
(752 KB)

Event Timeline