使用std :: transform()添加四个向量c ++

时间:2019-01-26 15:54:36

标签: c++ std ubuntu-18.04

我在使用std::transform添加四个向量时遇到问题。听起来像是一个简单的问题,但我无法完成。假设我有以下内容:

std::vector<double> No_of_sgn1, No_of_sgn2, No_of_sgn3, No_of_sgn4;

这些向量中的每一个具有4个元素(双精度)。所以我试图只添加两个向量:

std::transform(No_of_sgn1.begin(), No_of_sgn1.end(), No_of_sgn2.begin(), No_of_sgn1.begin(), std::plus<double>());

我假设将No_of_sgn2添加到No_of_sgn1,然后打印以查看结果:

std::cout << No_of_sgn1[0] << std::endl;

但是,代码在这里中断给了我

  

*中断* 违反细分

    TString getlabel(TString fileName, bool nu = false){

 TString g = "";

  if(fileName.Contains("345060"))       g = "ggH";
  if(fileName.Contains("344235"))       g = "VBF";
  if(fileName.Contains("AZNLO_VH"))     g = "VH";
  if(fileName.Contains("ttH125_ZZ4l"))  g = "ttH";
  if(fileName.Contains("Zjet"))         g = "Z+jets";
  if(fileName.Contains("A14_ttbar"))    g = "tt";


 return g;
}

TString getbinName(int xBin,TString type);
map<TString, std::map<int, double>> val;

    int main(){
//this part contains the place where the data imported
TChain *chain = new TChain("tree_relaxIsoD0");

chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/Signal/mc16_13TeV.345060.PowhegPythia8EvtGen_NNLOPS_nnlo_30_ggH125_ZZ4l_bkgCR.root");               //ggH
chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/Signal/mc16_13TeV.344235.PowhegPy8EG_NNPDF30_AZNLOCTEQ6L1_VBFH125_ZZ4lep_notau_bkgCR.root");        //VBF
chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/Signal/mc16a_13TeV.PowhegPythia8EvtGen_NNPDF30_AZNLO_VH_MINLO_bkgCR_merged_3s.root");  //VH
chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/Signal/mc16a_13TeV.PowhegPy8EG_A14NNPDF23_NNPDF30ME_ttH125_ZZ4l_allhad_merged_3s.root");            //ttH
chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/BkgCR/mc16_13TeV.Sherpa_NNPDF30NNLO_Zjet_bkgCR_merged_4s.root"); //Z+jets
chain->Add("~/cernbox/Lepron_Isolation/samples/Prod_v18/mc16a/BkgCR/mc16_13TeV.410289.PhPy8EG_A14_ttbar_hdamp258p75_4lMFilt_40_8_bkgCR.root");                    //ttbar


Histos a(chain);
a.Loop();

return 0;
}

for(int j=2; j<6;j++){
    if(groupName=="ggH" ) No_of_sgn1.push_back(val[groupName][j]);
    if(groupName=="VBF" ) No_of_sgn2.push_back(val[groupName][j]);
    if(groupName=="VH" ) No_of_sgn3.push_back(val[groupName][j]); 
    if(groupName=="ttH" ) No_of_sgn4.push_back(val[groupName][j]);
    if(groupName=="Z+jets") No_of_bkg1.push_back(val[groupName][j]);
    if(groupName=="tt" ) No_of_bkg2.push_back(val[groupName][j]);
 }

就像上面那样。 到这里为止一切都很好。我什至打印出所有向量的值,它们看起来很正确。这是我打印第一个矢量的方法:

 "if(groupName=="ggH") cout<<groupName<<":"<<" 4mu: "<<No_of_sgn1[0]<<" 4e: "<<No_of_sgn1[1]<<" 2mu2e: "<<No_of_sgn1[2]<<" 2e2mu: "<<No_of_sgn1[3]<<endl; ".

输出为:

  

“ ggH:4mu:0.869109 4e:0.452978 2mu2e:0.485973 2e2mu:0.605595

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:-1)

非常感谢大家。我碰巧解决了这个问题。首先,我将所有向量转换为数组,如下所示:

double s1[3]; double s2[3]; double s3[3]; double s4[3];
double b1[3]; double b2[3];

std::copy(No_of_sgn1.begin(), No_of_sgn1.end(), s1);
std::copy(No_of_sgn2.begin(), No_of_sgn2.end(), s2);
std::copy(No_of_sgn3.begin(), No_of_sgn3.end(), s3);
std::copy(No_of_sgn4.begin(), No_of_sgn4.end(), s4);
std::copy(No_of_bkg1.begin(), No_of_bkg1.end(), b1);
std::copy(No_of_bkg2.begin(), No_of_bkg2.end(), b2);

然后我执行了以下操作:

double No_of_sgn[3];
double No_of_bkg[3];

for(int g=0; g<4;g++){
  No_of_sgn[g] =  s1[g]+s2[g]+s3[g]+s4[g];
  No_of_bkg[g] =  b1[g]+b2[g];
}

cout<<" 4mu: "<<No_of_sgn[0]<<" 4e: "<<No_of_sgn[1]<<" 2mu2e: "<<No_of_sgn[2]<<" 2e2mu: "<<No_of_sgn[3]<<endl;

cout的输出是:

4mu: 18.9752 4e: 9.90788 2mu2e: 10.5984 2e2mu: 13.2445

我想要的是什么:)

干杯

Abdualazem。