在抛出std :: bad_alloc的实例后终止调用

时间:2015-04-13 06:08:21

标签: c++

我正面对最近几天的当前错误并与之抗争以解决问题,但一切都是徒劳的。我正在使用Pythia8和fastjet事件生成器并运行我的简单代码。代码编译得很好,但会导致运行时错误,如下所示。它在低事件下运行良好,但对于大型迭代(事件),它失败并打印出讨论中的错误:

  

SpaceShower中的PYTHIA警告:: pT2nextQCD:重量超过1       PAYTHIA StringFragmentation :: fragment中的错误:卡在加入中       Pythia中的PYTHIA错误::下一个:hadronLevel失败;再试一次       SpaceShower中的PYTHIA错误:: pT2nearQCDthreshold:卡在循环中       StringFragmentation :: fragmentToJunction中的PYTHIA错误:捕获在联结味道循环中       StringFragmentation :: fragmentToJunction中的PYTHIA警告:收敛连接帧结构不良       MultipleInteractions中的PYTHIA警告:: pTnext:重量超过1       在抛出' std :: bad_alloc'的实例后终止调用       what():std :: bad_alloc

here is code:
[
double Rparam = 0.4;
fastjet::Strategy strategy = fastjet::Best;
fastjet::RecombinationScheme recombScheme = fastjet::Et_scheme;
fastjet::JetDefinition *jetDef = NULL;
jetDef = new fastjet::JetDefinition(fastjet::antikt_algorithm, Rparam,
recombScheme, strategy);
// Fastjet input
std::vector <fastjet::PseudoJet> fjInputs;



//================== event selection efficiency variables ==========

int nev=0;
int passedJetCut=0;
int passedBJetCut=0;
int passedLeptonCut=0;
int passedMtaunuCut=0;
int passedMtaunubCut=0;
int passedWCut=0;
int passedTopCut=0;
int passedMetCut=0;

int njets=2;
int nbjets=1;
int ntaus=1;
double MW=80.38;

// Begin event loop. Generate event. Skip if error. List first one.
for (int iEvent = 0; iEvent < HowManyEvents; ++iEvent) {

nev++;
if(iEvent%1000==0)cout<<"Event number "<<iEvent<<endl;
// cout<<"Event number "<<iEvent<<endl;
//==================== PYTHIA ==================
if(!pythia.next())continue;

// if (iEvent<3)pythia.process.list();
if (iEvent<2)pythia.event.list();
//if (iEvent<2)event.list();
//==================== TAUOLA ==================

// Convert event record to HepMC
HepMC::GenEvent * HepMCEvt = new HepMC::GenEvent();

//Conversion needed if HepMC uses different momentum units
//than Pythia. However, requires HepMC 2.04 or higher.
HepMCEvt->use_units(HepMC::Units::GEV,HepMC::Units::MM);

ToHepMC.fill_next_event(pythia, HepMCEvt);

// if (FirstEvent)event.list();
//Beware this does not reflect tauola effect
//tauola C++ interface only affects HepMC::GenEvent which is made
// from pythia.event

//run TAUOLA on the event
TauolaHepMCEvent * t_event = new TauolaHepMCEvent(HepMCEvt);

//Since we let Pythia decay taus, we have to undecay them first.
t_event->undecayTaus();
t_event->decayTaus();

// Reset Fastjet input for each event
fjInputs.resize(0);

// Keep track of missing ET

//*******************
class IsbFromTop {
public:
bool operator()( const HepMC::GenParticle* p ) {
if ( abs(p->pdg_id()) == 5 ) return 1;
return 0;
}
};
vector<bjet> bjets;
bjets.clear();

//======================= loop over particles ========================
for(HepMC::GenEvent::particle_const_iterator p = HepMCEvt->particles_begin();
p!= HepMCEvt->particles_end(); ++p ){

int pid=(*p)->pdg_id();
double pt=(*p)->momentum().perp();
double px=(*p)->momentum().px();
double py=(*p)->momentum().py();
double pz=(*p)->momentum().pz();
double e=(*p)->momentum().e();
double eta=(*p)->momentum().eta();
double phi=(*p)->momentum().phi();

if(abs(pid)==15){
if ( (*p)->production_vertex() ) {
for ( HepMC::GenVertex::particle_iterator mother =
(*p)->production_vertex()->particles_begin(HepMC::parents);
mother!=(*p)->production_vertex()->particles_end(HepMC::parents);
++mother )
{
if(abs((*mother)->pdg_id())==24){
W2Tau=true;
}
}
}
}

// Final state only
if ((*p)->status()!=1)continue;
if(abs(pid)==11 || abs(pid)==13){
if(!FoundLepton){
hleptonet->Fill(pt);
hleptoneta->Fill(eta);
hleptonphi->Fill(phi);
pxlepton=px;
pylepton=py;
pzlepton=pz;
elepton=e;
FoundLepton=true;
}
if(pt>20. && pt<80. && fabs(eta)<1.5)
nleptons++;
}
// No neutrinos
if (abs(pid)==12 ||
abs(pid)==14 ||
abs(pid)==16){nnu++;continue;}

// Only |eta| < 5
if(fabs(eta)>5.)continue;


// Missing ET
mex -= px;
mey -= py;


// Store as input to Fastjhttps://mail.google.com/mail/u/0/?shva=1#inboxet
fjInputs.push_back(fastjet::PseudoJet (px,py,pz,e));

}//loop over particles in the event

continue.............................

thanks a lot for your help
ijaz 

1 个答案:

答案 0 :(得分:0)

抱歉,我对Pythia一无所知,但在c ++中,新的Operator将返回已分配的内存指针,否则会抛出bad_alloc异常。

使用try catch我们可以解决这个问题 -

像:

while(true)
{
    try
    {
        string s = new string(1024*1024*1024);
        break;
    }
    catch
    {
        // do stuff
    }
}

它将解决崩溃问题,但CPU利用率将上升,直到我们获得所需的内存。(如果CPU达到100%使用率,它将挂起系统)