当前位置: 首页>>代码示例>>C++>>正文


C++ TH2D::GetRandom2方法代码示例

本文整理汇总了C++中TH2D::GetRandom2方法的典型用法代码示例。如果您正苦于以下问题:C++ TH2D::GetRandom2方法的具体用法?C++ TH2D::GetRandom2怎么用?C++ TH2D::GetRandom2使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TH2D的用法示例。


在下文中一共展示了TH2D::GetRandom2方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if

std::pair<std::vector<float>, std::vector<int> >   sample_pdf(TH1* hist, long int nsamples, long int maxsamples)
{
  std::vector<float> events;
  std::vector<int> weights;


  double histint = hist->Integral();
  int totalbins = hist->GetNbinsX()*hist->GetNbinsY()*hist->GetNbinsZ();
  double maxperbin = maxsamples/(1.0*totalbins);

  std::vector<int> histweights(totalbins);
  bool weighted = false;
  if (nsamples > maxsamples){
    weighted = true;
    std::cout << "Creating weighted dataset wherever bincount > " << maxperbin<< std::endl;
    for (int i=0;i<hist->GetNbinsX();i++){
      for (int j=0;j<hist->GetNbinsY();j++){
        for (int k=0;k<hist->GetNbinsZ();k++){
          double height = hist->GetBinContent(i+1,j+1,k+1);
          if (nsamples*(height/histint) > maxperbin){
            int weight = ((nsamples*(height/histint)) / maxperbin) + 1;
            hist->SetBinContent(i+1,j+1,k+1,height/weight);
            std::cout << "Weighting bin " << i << " " << j << " " << k << " (" << k + j*hist->GetNbinsZ() + i*hist->GetNbinsY()*hist->GetNbinsZ() << ") " << ", binheight: " << height << " (" << height*histint << "), Weight: " << weight << ", now " << hist->GetBinContent(i+1,j+1,k+1) << std::endl;
            histweights[k + j*hist->GetNbinsZ() + i*hist->GetNbinsY()*hist->GetNbinsZ()] = weight;
          }else{
            histweights[k + j*hist->GetNbinsZ() + i*hist->GetNbinsY()*hist->GetNbinsZ()] = 1.0;
            //        std::cout << "Not weighting bin " << i << ", binheight: " << height << " (" << height*histint << ")" << std::endl;
          }
        }
      }
    }
  }else{
    std::cout << nsamples << " < " << maxsamples << std::endl;

  }

  int allocatesize = nsamples;
  if (weighted){
    double totalweight = 0;
    for (int i=0;i<hist->GetNbinsX();i++)
      for (int j=0;j<hist->GetNbinsY();j++)
        for (int k=0;k<hist->GetNbinsZ();k++)
          totalweight += hist->GetBinContent(i+1,j+1,k+1)*histweights[k + j*hist->GetNbinsZ() + i*hist->GetNbinsY()*hist->GetNbinsZ()];
    std::cout << "NSAMPLES: " << nsamples << ", TOTALWEIGHT: " << totalweight << std::endl;
    allocatesize = (int) (nsamples * (1.5*hist->Integral()/histint));
  }
  std::cout << "ALLOCATESIZE: " << allocatesize << std::endl;

  if (hist->IsA() == TH1D::Class()) {
    events.reserve(allocatesize);
    weights.reserve(allocatesize);
    TH1D* ht = dynamic_cast<TH1D*>(hist);

    double obs;
    long int j;
    for (j=0; j<nsamples; j++) {
      obs = ht->GetRandom();

      events.push_back(obs);

      if (weighted){
        int ibin = (hist->GetXaxis()->FindBin(obs)-1);
        weights.push_back(histweights[ibin]);
        j += histweights[ibin]-1;
      }else{
        weights.push_back(1);
      }
    }
    if (j > nsamples)
      weights.back() -= j-nsamples;
  }
  else if (hist->IsA() == TH2D::Class()) {
    events.reserve(allocatesize*2);
    weights.reserve(allocatesize);
    TH2D* ht = dynamic_cast<TH2D*>(hist);

    double obs0;
    double obs1;
    long int j;
    for (j=0; j<nsamples; j++) {
      ht->GetRandom2(obs0, obs1);

      events.push_back(obs0);
      events.push_back(obs1);

      if (weighted){
        int ibin = (hist->GetYaxis()->FindBin(obs1)-1) + (hist->GetXaxis()->FindBin(obs0)-1)*hist->GetNbinsY();
        weights.push_back(histweights[ibin]);
        j += histweights[ibin]-1;
      }else{
        weights.push_back(1);
      }
    }
    if (j > nsamples)
      weights.back() -= j-nsamples;
  }
  else if (hist->IsA() == TH3D::Class()) {
    events.reserve(allocatesize*3);
    weights.reserve(allocatesize);
    TH3D* ht = dynamic_cast<TH3D*>(hist);
//.........这里部分代码省略.........
开发者ID:bonventre,项目名称:sxmc,代码行数:101,代码来源:generator.cpp


注:本文中的TH2D::GetRandom2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。