本文整理汇总了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);
//.........这里部分代码省略.........