Skip to content

Commit c740f9c

Browse files
committed
[RF] Use RooWorkspace accessor functions directly with std::string
Some code modernization, where we profit from the new possibility to pass `std::string` directly to the RooWorkspace accessor functions thanks to the `RooStringView`.
1 parent bc93d89 commit c740f9c

15 files changed

Lines changed: 133 additions & 133 deletions

roofit/histfactory/src/Asimov.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void RooStats::HistFactory::Asimov::ConfigureWorkspace(RooWorkspace* wspace) {
5858
double val = itr->second;
5959

6060
// Try to get the variable in the workspace
61-
RooRealVar* var = wspace->var(param.c_str());
61+
RooRealVar* var = wspace->var(param);
6262
if( !var ) {
6363
std::cout << "Error: Trying to set variable: " << var
6464
<< " to a specific value in creation of asimov dataset: " << fName
@@ -96,7 +96,7 @@ void RooStats::HistFactory::Asimov::ConfigureWorkspace(RooWorkspace* wspace) {
9696
bool isConstant = itr->second;
9797

9898
// Try to get the variable in the workspace
99-
RooRealVar* var = wspace->var(param.c_str());
99+
RooRealVar* var = wspace->var(param);
100100
if( !var ) {
101101
std::cout << "Error: Trying to set variable: " << var
102102
<< " constant in creation of asimov dataset: " << fName

roofit/histfactory/src/HistFactoryNavigation.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace RooStats {
9696
}
9797

9898
// Get the ModelConfig
99-
ModelConfig* mc = (ModelConfig*) wspace->obj(ModelConfigName.c_str());
99+
ModelConfig* mc = (ModelConfig*) wspace->obj(ModelConfigName);
100100
if( !mc ) {
101101
std::cout << "Error: Failed to find ModelConfig: " << ModelConfigName
102102
<< " from workspace: " << WorkspaceName

roofit/histfactory/src/HistoToWorkspaceFactory.cxx

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ namespace HistFactory{
159159
for(Int_t i=lowBin; i<highBin; ++i){
160160
std::stringstream str;
161161
str<<"_"<<i;
162-
RooRealVar* temp = proto->var((prefix+str.str()).c_str());
162+
RooRealVar* temp = proto->var(prefix+str.str());
163163
mean(i) = temp->getVal();
164164
}
165165

@@ -198,16 +198,16 @@ namespace HistFactory{
198198
std::stringstream str;
199199
str<<"_"<<j;
200200

201-
RooRealVar* temp = (RooRealVar*) proto->var(("alpha_"+sourceName.at(j)).c_str());
201+
RooRealVar* temp = (RooRealVar*) proto->var("alpha_"+sourceName.at(j));
202202
if(!temp){
203203
temp = (RooRealVar*) proto->factory(("alpha_"+sourceName.at(j)+range).c_str());
204204

205205
// now add a constraint term for these parameters
206206
string command=("Gaussian::alpha_"+sourceName.at(j)+"Constraint(alpha_"+sourceName.at(j)+",nom_"+sourceName.at(j)+"[0.,-10,10],1.)");
207207
cout << command << endl;
208208
likelihoodTermNames.push_back( proto->factory( command.c_str() )->GetName() );
209-
proto->var(("nom_"+sourceName.at(j)).c_str())->setConstant();
210-
const_cast<RooArgSet*>(proto->set("globalObservables"))->add(*proto->var(("nom_"+sourceName.at(j)).c_str()));
209+
proto->var("nom_"+sourceName.at(j))->setConstant();
210+
const_cast<RooArgSet*>(proto->set("globalObservables"))->add(*proto->var("nom_"+sourceName.at(j)));
211211

212212
}
213213

@@ -391,9 +391,9 @@ namespace HistFactory{
391391
for(Int_t i=lowBin; i<highBin; ++i){
392392
std::stringstream str;
393393
str<<"_"<<i;
394-
RooRealVar* obs = (RooRealVar*) proto->var((obsPrefix+str.str()).c_str());
394+
RooRealVar* obs = (RooRealVar*) proto->var(obsPrefix+str.str());
395395
cout << "expected number of events called: " << expPrefix << endl;
396-
RooAbsReal* exp = proto->function((expPrefix+str.str()).c_str());
396+
RooAbsReal* exp = proto->function(expPrefix+str.str());
397397
if(obs && exp){
398398

399399
//proto->Print();
@@ -490,12 +490,12 @@ namespace HistFactory{
490490
proto->factory(Form("PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1./scale,1./scale));
491491

492492
// set beta const status to be same as alpha
493-
if(proto->var(Form("alpha_%s",it->first.c_str()))->isConstant())
494-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(true);
493+
if(proto->var("alpha_" + it->first)->isConstant())
494+
proto->var("beta_" + it->first)->setConstant(true);
495495
else
496-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(false);
496+
proto->var("beta_" + it->first)->setConstant(false);
497497
// set alpha const status to true
498-
// proto->var(Form("alpha_%s",it->first.c_str()))->setConstant(true);
498+
// proto->var("alpha_" + it->first)->setConstant(true);
499499

500500
// replace alphas with alphaOfBeta and replace constraints
501501
//cout << "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint" << endl;
@@ -505,8 +505,8 @@ namespace HistFactory{
505505
editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
506506

507507
/*
508-
if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
509-
cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
508+
if( proto->pdf("alpha_"+it->first+"Constraint") && proto->var("alpha_"+it->first) )
509+
cout << " checked they are there" << proto->pdf("alpha_"+it->first+"Constraint") << " " << proto->var("alpha_"+it->first) << endl;
510510
else
511511
cout << "NOT THERE" << endl;
512512
*/
@@ -519,7 +519,7 @@ namespace HistFactory{
519519
precede="";
520520
cout << "Going to issue this edit command\n" << edit<< endl;
521521
proto->factory( edit.c_str() );
522-
RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
522+
RooAbsPdf* newOne = proto->pdf(lastPdf);
523523
if(!newOne)
524524
cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
525525

@@ -529,7 +529,7 @@ namespace HistFactory{
529529
// add uniform terms and their constraints
530530
for(it=uniformSyst.begin(); it!=uniformSyst.end(); ++it) {
531531
cout << "edit for " << it->first << "with rel uncert = " << it->second << endl;
532-
if(! proto->var(("alpha_"+it->first).c_str())){
532+
if(! proto->var("alpha_"+it->first)){
533533
cout << "systematic not there" << endl;
534534
nskipped++;
535535
continue;
@@ -542,12 +542,12 @@ namespace HistFactory{
542542
proto->factory(Form("PolyVar::alphaOfBeta_%s(beta_%s,{-1,1})",it->first.c_str(),it->first.c_str()));
543543

544544
// set beta const status to be same as alpha
545-
if(proto->var(Form("alpha_%s",it->first.c_str()))->isConstant())
546-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(true);
545+
if(proto->var("alpha_" + it->first)->isConstant())
546+
proto->var("beta_" + it->first)->setConstant(true);
547547
else
548-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(false);
548+
proto->var("beta_" + it->first)->setConstant(false);
549549
// set alpha const status to true
550-
// proto->var(Form("alpha_%s",it->first.c_str()))->setConstant(true);
550+
// proto->var("alpha_" + it->first)->setConstant(true);
551551

552552
// replace alphas with alphaOfBeta and replace constraints
553553
cout << "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint" << endl;
@@ -556,8 +556,8 @@ namespace HistFactory{
556556
cout << "alpha_"+it->first+"=alphaOfBeta_"+ it->first << endl;
557557
editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
558558

559-
if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
560-
cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
559+
if( proto->pdf("alpha_"+it->first+"Constraint") && proto->var("alpha_"+it->first) )
560+
cout << " checked they are there" << proto->pdf("alpha_"+it->first+"Constraint") << " " << proto->var("alpha_"+it->first) << endl;
561561
else
562562
cout << "NOT THERE" << endl;
563563

@@ -569,7 +569,7 @@ namespace HistFactory{
569569
precede="";
570570
cout << edit<< endl;
571571
proto->factory( edit.c_str() );
572-
RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
572+
RooAbsPdf* newOne = proto->pdf(lastPdf);
573573
if(!newOne)
574574
cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
575575

@@ -583,7 +583,7 @@ namespace HistFactory{
583583
// add lognormal terms and their constraints
584584
for(it=logNormSyst.begin(); it!=logNormSyst.end(); ++it) {
585585
cout << "edit for " << it->first << "with rel uncert = " << it->second << endl;
586-
if(! proto->var(("alpha_"+it->first).c_str())){
586+
if(! proto->var("alpha_"+it->first)){
587587
cout << "systematic not there" << endl;
588588
nskipped++;
589589
continue;
@@ -609,12 +609,12 @@ namespace HistFactory{
609609
// proto->factory(Form("PolyVar::alphaOfBeta_%s(beta_%s,{%f,%f})",it->first.c_str(),it->first.c_str(),-1.,1./scale));
610610

611611
// set beta const status to be same as alpha
612-
if(proto->var(Form("alpha_%s",it->first.c_str()))->isConstant())
613-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(true);
612+
if(proto->var("alpha_" + it->first)->isConstant())
613+
proto->var("beta_" + it->first)->setConstant(true);
614614
else
615-
proto->var(Form("beta_%s",it->first.c_str()))->setConstant(false);
615+
proto->var("beta_" + it->first)->setConstant(false);
616616
// set alpha const status to true
617-
// proto->var(Form("alpha_%s",it->first.c_str()))->setConstant(true);
617+
// proto->var("alpha_" + it->first)->setConstant(true);
618618

619619
// replace alphas with alphaOfBeta and replace constraints
620620
cout << "alpha_"+it->first+"Constraint=beta_" + it->first+ "Constraint" << endl;
@@ -623,8 +623,8 @@ namespace HistFactory{
623623
cout << "alpha_"+it->first+"=alphaOfBeta_"+ it->first << endl;
624624
editList+=precede + "alpha_"+it->first+"=alphaOfBeta_"+ it->first;
625625

626-
if( proto->pdf(("alpha_"+it->first+"Constraint").c_str()) && proto->var(("alpha_"+it->first).c_str()) )
627-
cout << " checked they are there" << proto->pdf(("alpha_"+it->first+"Constraint").c_str()) << " " << proto->var(("alpha_"+it->first).c_str()) << endl;
626+
if( proto->pdf("alpha_"+it->first+"Constraint") && proto->var("alpha_"+it->first) )
627+
cout << " checked they are there" << proto->pdf("alpha_"+it->first+"Constraint") << " " << proto->var("alpha_"+it->first) << endl;
628628
else
629629
cout << "NOT THERE" << endl;
630630

@@ -636,7 +636,7 @@ namespace HistFactory{
636636
precede="";
637637
cout << edit<< endl;
638638
proto->factory( edit.c_str() );
639-
RooAbsPdf* newOne = proto->pdf(lastPdf.c_str());
639+
RooAbsPdf* newOne = proto->pdf(lastPdf);
640640
if(!newOne)
641641
cout << "\n\n ---------------------\n WARNING: failed to make EDIT\n\n" << endl;
642642

@@ -817,7 +817,7 @@ namespace HistFactory{
817817
//////////////////////////////////////
818818
// fix specified parameters
819819
for(unsigned int i=0; i<systToFix.size(); ++i){
820-
RooRealVar* temp = proto->var((systToFix.at(i)).c_str());
820+
RooRealVar* temp = proto->var(systToFix.at(i));
821821
if(temp) temp->setConstant();
822822
else cout << "could not find variable " << systToFix.at(i) << " could not set it to constant" << endl;
823823
}
@@ -887,7 +887,7 @@ namespace HistFactory{
887887
else ss << ',' << channel_name ;
888888
RooWorkspace * ch=chs[i];
889889

890-
RooAbsPdf* model = ch->pdf(("model_"+channel_name).c_str());
890+
RooAbsPdf* model = ch->pdf("model_"+channel_name);
891891
models.push_back(model);
892892
globalObs.add(*ch->set("globalObservables"));
893893

@@ -934,7 +934,7 @@ namespace HistFactory{
934934

935935
for(unsigned int i=0; i<fSystToFix.size(); ++i){
936936
// make sure they are fixed
937-
RooRealVar* temp = combined->var((fSystToFix.at(i)).c_str());
937+
RooRealVar* temp = combined->var(fSystToFix.at(i));
938938
if(temp) {
939939
temp->setConstant();
940940
cout <<"setting " << fSystToFix.at(i) << " constant" << endl;
@@ -961,7 +961,7 @@ namespace HistFactory{
961961
{
962962

963963
ModelConfig * combined_config = (ModelConfig *) combined->obj("ModelConfig");
964-
RooDataSet * simData = (RooDataSet *) combined->obj(data_name.c_str());
964+
RooDataSet * simData = (RooDataSet *) combined->obj(data_name);
965965
// const RooArgSet * constrainedParams=combined_config->GetNuisanceParameters();
966966
const RooArgSet * POIs=combined_config->GetParametersOfInterest();
967967

@@ -979,7 +979,7 @@ namespace HistFactory{
979979
combined->defineSet("constrainedParams", *constrainedParams);
980980
*/
981981

982-
//RooAbsPdf* model=combined->pdf(model_name.c_str());
982+
//RooAbsPdf* model=combined->pdf(model_name);
983983
RooAbsPdf* model=combined_config->GetPdf();
984984
// RooArgSet* allParams = model->getParameters(*simData);
985985

0 commit comments

Comments
 (0)