Skip to content

Commit

Permalink
Merge pull request #464 from gundam-organization/feature/toyParInjector
Browse files Browse the repository at this point in the history
(re-)Adding toy parameter injector
  • Loading branch information
nadrino authored Mar 22, 2024
2 parents 31cca7e + 4c21d5e commit 4938a3e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/Applications/src/gundamFitter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main(int argc, char** argv){
clParser.addOption("useDataEntry", {"--use-data-entry"}, "Overrides \"selectedDataEntry\" in dataSet config. Second arg is to select a given dataset");
clParser.addOption("useDataConfig", {"--use-data-config"}, "Add a data entry to the data set definition and use it for the fit");
clParser.addOption("injectParameterConfig", {"--inject-parameters"}, "Inject parameters defined in the provided config file");
clParser.addOption("injectToyParameters", {"--inject-toy-parameter"}, "Inject parameters defined in the provided config file");
clParser.addOption("appendix", {"--appendix"}, "Add appendix to the output file name");

clParser.addDummyOption("Trigger options");
Expand Down Expand Up @@ -140,6 +141,13 @@ int main(int argc, char** argv){
injectParameterPath = clParser.getOptionVal<std::string>("injectParameterConfig");
}

// toy par injector
std::string toyParInjector{};
if( clParser.isOptionTriggered("injectToyParameters") ){
toyParInjector = clParser.getOptionVal<std::string>("injectToyParameters");
LogWarning << "Inject toy parameter: " << toyParInjector << std::endl;
}

// PRNG seed?
gRandom = new TRandom3(0); // Initialize with a UUID;
if( clParser.isOptionTriggered("randomSeed") ){
Expand Down Expand Up @@ -189,6 +197,7 @@ int main(int argc, char** argv){
{"kickMc", "KickMc"},
{"lightOutputMode", "Light"},
{"toyFit", "ToyFit_%s"},
{"injectToyParameters", "InjToyPar_%s"},
{"dry-run", "DryRun"},
{"appendix", "%s"},
};
Expand Down Expand Up @@ -295,6 +304,12 @@ int main(int argc, char** argv){
fitter.getLikelihoodInterface().getDataSetManager().getPropagator().setParameterInjectorConfig(injectConfig);
}

// toyParInjector
if( not toyParInjector.empty() ){
auto injectConfig = ConfigUtils::readConfigFile( toyParInjector );
fitter.getLikelihoodInterface().getDataSetManager().setToyParameterInjector( injectConfig );
}

// Also check app level config options
GenericToolbox::Json::deprecatedAction(configHandler.getConfig(), "generateSamplePlots", [&]{
LogAlert << "Forwarding the option to FitterEngine. Consider moving it into \"fitterEngineConfig:\"" << std::endl;
Expand Down
5 changes: 5 additions & 0 deletions src/DatasetManager/include/DataSetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class DataSetManager : public JsonBaseClass {
public:
DataSetManager() = default;

// setters
void setToyParameterInjector(const JsonType& toyParameterInjector_){ _toyParameterInjector_ = toyParameterInjector_; }

// const-getters
[[nodiscard]] const Propagator& getPropagator() const{ return _propagator_; }
[[nodiscard]] const EventTreeWriter& getTreeWriter() const{ return _treeWriter_; }
Expand All @@ -39,6 +42,8 @@ class DataSetManager : public JsonBaseClass {
EventTreeWriter _treeWriter_{};
std::vector<DatasetDefinition> _dataSetList_{};

JsonType _toyParameterInjector_{};

};


Expand Down
31 changes: 21 additions & 10 deletions src/DatasetManager/src/DataSetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void DataSetManager::loadData(){
bool cacheManagerState = GundamGlobals::getEnableCacheManager();
GundamGlobals::setEnableCacheManager(false);


// make sure everything is ready for loading
_propagator_.clearContent();

Expand Down Expand Up @@ -92,7 +93,6 @@ void DataSetManager::loadData(){
// Copy to data container
if( usedMcContainer ){
if( _propagator_.isThrowAsimovToyParameters() ){
LogWarning << "Will throw toy parameters..." << std::endl;

if( _propagator_.isShowEventBreakdown() ){
LogInfo << "Propagating prior parameters on the initially loaded events..." << std::endl;
Expand All @@ -117,17 +117,24 @@ void DataSetManager::loadData(){
}
}

_propagator_.getParametersManager().throwParameters();
if( _toyParameterInjector_.empty() ){
LogWarning << "Will throw toy parameters..." << std::endl;
_propagator_.getParametersManager().throwParameters();

// Handling possible masks
for( auto& parSet : _propagator_.getParametersManager().getParameterSetsList() ){
if( not parSet.isEnabled() ) continue;
// Handling possible masks
for( auto& parSet : _propagator_.getParametersManager().getParameterSetsList() ){
if( not parSet.isEnabled() ) continue;

if( parSet.isMaskForToyGeneration() ){
LogWarning << parSet.getName() << " will be masked for the toy generation." << std::endl;
parSet.setMaskedForPropagation( true );
if( parSet.isMaskForToyGeneration() ){
LogWarning << parSet.getName() << " will be masked for the toy generation." << std::endl;
parSet.setMaskedForPropagation( true );
}
}
}
else{
LogWarning << "Injecting parameters..." << std::endl;
_propagator_.getParametersManager().injectParameterValues( _toyParameterInjector_ );
}

} // throw asimov?

Expand Down Expand Up @@ -191,6 +198,9 @@ void DataSetManager::loadData(){
}
#endif

LogInfo << "Propagating prior parameters on events..." << std::endl;
_propagator_.reweightMcEvents();

LogInfo << "Filling up sample bin caches..." << std::endl;
GundamGlobals::getParallelWorker().runJob([this](int iThread){
LogInfoIf(iThread <= 0) << "Updating sample per bin event lists..." << std::endl;
Expand Down Expand Up @@ -236,8 +246,9 @@ void DataSetManager::loadData(){
/// Now caching the event for the plot generator
_propagator_.getPlotGenerator().defineHistogramHolders();

GundamGlobals::setEnableCacheManager(cacheManagerState);

/// Propagator needs to be fast, let the workers wait for the signal
GundamGlobals::getParallelWorker().setCpuTimeSaverIsEnabled(false);

/// restoring state
GundamGlobals::setEnableCacheManager(cacheManagerState);
}
1 change: 1 addition & 0 deletions src/Propagator/include/Propagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Propagator : public JsonBaseClass {
// Core
void buildDialCache();
void propagateParameters();
void resetEventWeights();
void reweightMcEvents();
void refillMcHistograms();
void clearContent();
Expand Down
11 changes: 6 additions & 5 deletions src/Propagator/src/Propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,15 @@ void Propagator::propagateParameters(){
this->refillMcHistograms();

}
void Propagator::resetEventWeights(){
std::for_each(_dialCollectionList_.begin(), _dialCollectionList_.end(), [&]( DialCollection& dc_){
dc_.updateInputBuffers();
});
}
void Propagator::reweightMcEvents() {
reweightTimer.start();

std::for_each(_dialCollectionList_.begin(), _dialCollectionList_.end(), [&]( DialCollection& dc_ ){
dc_.updateInputBuffers();
});
resetEventWeights();

bool usedGPU{false};
#ifdef GUNDAM_USING_CACHE_MANAGER
Expand Down Expand Up @@ -258,8 +261,6 @@ void Propagator::printBreakdowns(){
}

this->reweightMcEvents();

// no reweight
for( size_t iSample = 0 ; iSample < _sampleSet_.getSampleList().size() ; iSample++ ){
stageBreakdownList[iSample][iStage] = _sampleSet_.getSampleList()[iSample].getMcContainer().getSumWeights();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ void LikelihoodInterface::initializeImpl() {
_nbSampleBins_ += int(sample.getBinning().getBinList().size() );
}

LogInfo << "Move back MC parameters to prior..." << std::endl;
_dataSetManager_.getPropagator().getParametersManager().moveParametersToPrior();

/// some joint fit probability might need to save the value of the nominal histogram.
/// here we know every parameter is at its nominal value
LogInfo << "First evaluation of the LLH at the nominal value..." << std::endl;
_dataSetManager_.getPropagator().getParametersManager().moveParametersToPrior();
this->propagateAndEvalLikelihood();
LogInfo << this->getSummary() << std::endl;

/// move the parameter away from the prior if needed
if( not _dataSetManager_.getPropagator().getParameterInjectorMc().empty() ){
Expand Down

0 comments on commit 4938a3e

Please sign in to comment.