terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

Hi friends,
Running the following program results in the following messages:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc

Here is the program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define Entry (1e6)
#define fEntry (0)
#define psfile "../prints/WallECalibPrints.ps"
#define datafile "../database/WallEcalibData.dat"
TF1 *fun[21];
TCutG *cutd,*cutp;
TH2D *protonMev[21],*proton[21],*chanp[21],*chan[21],*chane[21];
TH2D *deuteronMev[21],*deuteron[21],*chand[21],*Etheta;
FILE *WCalib;
int counter;
int fit_limit[11][2]={0,0,
		      30,34, /*1*/
		      22,34, /*2*/
		      14,32,
		      11,30,
		      13,30,/*5*/
		      13,30,
		      12,30,
		      14,32,
		      20,32,/*9*/
		      26,34};


void  ElasticWenergyCalibration()
{
  gROOT->Macro("openntuple.cxx");   c1->Divide(2,2);
  gROOT->LoadMacro("2body_subroutines.cxx");
  gROOT->Macro("ElasticKinemaGraphs.cxx");
  TPostScript *ps = new TPostScript(psfile,111); 
  ps->Range(18,27);  //set x,y of printed page
  gStyle->SetOptStat(00);
  gStyle->SetPalette(1);
  TLatex *text= new TLatex();

  if (! define_cuts()) return 0;
  if (! define_histos()) return 0;

  char name[15][255],side[2][10]={"WL","WR"};
  float par[5];

  sprintf(name[0],"MWPCX>0 && abs(abs(Bphi-MWPCphi)-180)<10 ");
  sprintf(name[1],"Fill_histos(ID,WLE,WLdet,WRE,WRdet,MWPCX,MWPCY,MWPCtheta,MWPCphi,BLE,Btheta,Bphi,Time,0)");
  c2->cd();
  counter=0;
  fprintf(stdout,"Number of the Entries are: %2.1e \n%s\n --> Calibrating the  detectors ...\n",Entry,name[1]);
  elasticchain->Draw(name[1],name[0],"",Entry,fEntry);
  c2->Update();    ps->NewPage();

  for (int i=1;i<2;i++)
    {
     fun[i]->SetParameters(6.5,0,0);
     fprintf(WCalib,"%d\t",i);
     slicefit(i,deuteron[i],7,37,4,8);
     for (int co=0;co<3;co++) 
        {
         par[co]=fun[i]->GetParameter(co); 
         fprintf(WCalib," %2.2e\t",par[co]);
        } 
    fprintf(WCalib,"\n");
    fprintf(stdout,"Bar %d fitted.\n",i);
    }

  counter=0;
  fprintf(stdout,"\n --> Filling the  spectras ...\n");
  sprintf(name[1],"Fill_histos(ID,WLE,WLdet,WRE,WRdet,MWPCX,MWPCY,MWPCtheta,MWPCphi,BLE,Btheta,Bphi,Time,1)");
  elasticchain->Draw(name[1],name[0],"",Entry);
  c2->Update(); c1->cd();
  for (int i=1;i<2;i++)
    {
     ps ->NewPage();
     c1->cd(1); chand[i]->Draw("colz");
     c1->cd(2); chanp[i]->Draw("colz");
     c1->cd(3); deuteron[i]->Draw("colz"); fun[i]->Draw("same");
     sprintf(name[1],"E_{d}, Bar No.%d",i);
     deuteronMev[i]->SetTitle(name[1]);
     c1->cd(4); deuteronMev[i]->Draw("colz");
     Edthetad->Draw();
     c1->Update();
     fprintf(stdout,"Bar %d finished.\n",i);
     if (i==8) return;
    }
  c2->cd();
  Etheta->Draw("colz");  
  Edthetad->Draw();
  c2->Update();
  ps->Close();
  fclose(WCalib);
  fprintf(stdout,"\n Deuteron calibrations are saved in %s.\n",datafile);
  fprintf(stdout,"\n and histograms are saved in %s\n",psfile);
}
int Fill_histos(double ID,double WLE,double WLdet,double WRE,double WRdet,double MWPCX,double MWPCY,
		double MWPCtheta,double MWPCphi,double BLE,double Btheta,double Bphi,double Time,int id)
{
  counter++;  
  fprintf(stdout,"\rEntry No: %2.2e\n",counter);
 
   if (WLdet>0) int det=(int)WLdet;
   else {
         fprintf(stdout,"null event %d\n",WLdet);
         return 0;
         }

  /*Fill histograms*/
  chane[det]->Fill(MWPCX,sqrt(WLE*WRE));
  
  if (cutp->IsInside(MWPCtheta,Btheta))                  /*proton forward*/
    {
     chanp[det]->Fill(MWPCtheta,sqrt(WLE*WRE));
     proton[det]->Fill(MWPCtheta,sqrt(WLE*WRE)/Pd_kinema(MWPCtheta,MWPCphi,6));     /* Pd_kinema(MWPCtheta,MWPCphi,6) = Proton lab Energy*/
    }

  //  if (cutd->IsInside(MWPCtheta,Btheta) && Btheta>80){             /*deuteron forward*/
  if (Btheta>80)     /*deuteron forward*/
    {             
      if (id==0)
        {
         chand[det]->Fill(MWPCtheta,sqrt(WLE*WRE)/100);
         chanp[det]->Fill(MWPCtheta,WLE/100);
         deuteron[det]->Fill(MWPCtheta,sqrt(WLE*WRE)/Pd_kinema(MWPCtheta,MWPCphi,-1)); /*Pd_kinema(MWPCtheta,MWPCphi,-1)= Deuteron lab Energy*/
        }
     else if (id==1)
            {
             deuteronMev[det]->Fill(MWPCtheta,sqrt(WLE*WRE)/ function(MWPCtheta,MWPCX,det));
             Etheta->Fill(MWPCtheta,sqrt(WLE*WRE)/ function(MWPCtheta,MWPCX,det));
            }
     else {}
    }
  return det;
}

int define_cuts()
{
  /*Create a graphical cut to seperate elastic deuteron from proton*/
  float xd[6] = {7,8,27,42,42,9};            /*deuteron forward proton backward*/
  float yd[6] = {160,130,80,60,75,155};
  cutd = new TCutG("cutd",6,xd,yd);
  float xp[5] = {7,7,40,40,10};                     /*proton forward deuteon backward*/
  float yp[5] = {95,70,45,65,95};
  cutp = new TCutG("cutp",5,xp,yp);
  return 1;
}
int  define_histos()
{

  char name[15][255];
  for (int i=1;i<11;i++)
    {
    sprintf(name[0],"fun%d",i);
    fun[i] = new TF1(name[0],"pol1",10,40);
    sprintf(name[0],"protonMev%d",i);
    protonMev[i] = new TH2D (name[0],"E_{p};#theta_{MWPC} [deg]  ;E_{p}[MeV]  "
			     ,90,7,37,100,100,200);
    CenterTitle(protonMev[i]);
    sprintf(name[1],"deuteronMev%d",i);
    deuteronMev[i] = new TH2D (name[1],"E_{d};#theta_{MWPC} [deg]  ;E_{d}[MeV]  "
			       ,90,7,37,100,100,200);
    CenterTitle(deuteronMev[i]);
    sprintf(name[2],"proton%d",i);
    proton[i] = new TH2D (name[2],"Chan/E_{p};#theta_{MWPC} [deg];Chan/E_{p} [MeV]"
			  ,90,7,37,80,4,8);
    CenterTitle(proton[i]);
    sprintf(name[3],"deuteron%d",i);
    sprintf(name[2],"Chan/E_{d} No.%d;#theta_{MWPC} [deg]  ;Chan/E_{d} [MeV]",i);
    deuteron[i] = new TH2D (name[3],name[2],90,7,37,80,4,8);
    CenterTitle(deuteron[i]);
    sprintf(name[4],"chane%d",i);
    chane[i] = new TH2D (name[4],"Elastic;#theta_{MWPC} [deg]  ;Chan  "
			 ,90,7,37,200,400,2000);
    CenterTitle(chane[i]);
    sprintf(name[5],"chan%d",i);
    chan[i] = new TH2D (name[5],"Total;#theta_{MWPC} [deg]  ;Chan  "
			,90,7,37,200,400,2000);
    CenterTitle(chan[i]);
    sprintf(name[6],"chansinle%d",i);
    chanp[i] = new TH2D (name[6],"Single Channel;#theta_{MWPC} [deg]; L [chan] "
			 ,90,7,37,200,4,20);
    CenterTitle(chanp[i]);
    sprintf(name[7],"chand%d",i);
    chand[i] = new TH2D (name[7],"deuteron;#theta_{MWPC} [deg]  ;#sqrt{L [chan]#times R [chan]}/100 ",90,7,37,200,4,20);
    CenterTitle(chand[i]);
   }
  Etheta=new TH2D("Etheta","Energy_{d}, #theta_{d};#theta_{d} [deg];E_{d} [MeV]",90,7,37,140,60,200);
  CenterTitle(Etheta);
  if (  WCalib=fopen(datafile,"w+")) fprintf(stdout,"Calibration file %s is open.\n ",datafile);
  else {
       fprintf(stdout," error in opening the file %s \n",datafile);
       return 0;
       }
  fprintf(WCalib,"!-deuteron-p0\n");
  return 1;
}
void slicefit(int det,TH2D *h,float minX,float maxX,float minY,float maxY)
{

  double Yval,Xval,Yerr;
  TH1D *htmp;
  TGraphErrors *gr = new TGraphErrors(40);
  double XBinW = h->GetXaxis()->GetBinWidth(1);
  double YBinW = h->GetYaxis()->GetBinWidth(1);
  double BXmax=(maxX-minX)/XBinW;
  double BXmin=1;
  int Xslicestep=2,k=0;
  TF1 *f3= new TF1("f3","gaus",10,190);
  h->ProjectionY("test",BXmin,BXmax);
  htmp=(TH1D*)gROOT->FindObject("test");
  double peak,hpeak = htmp->GetMaximum();
  for (int j=BXmin;j<BXmax;j=j+Xslicestep)
     {
      h->ProjectionY("test",j-(Xslicestep/2),j+(Xslicestep/2));
      htmp=(TH1D*)gROOT->FindObject("test");

      if (htmp->GetEntries()>5)
        {
         Yval=minY+htmp->GetMaximumBin()*YBinW;
         Xval=minX+j*XBinW;
      if ( Yval<maxY && Yval>minY)
         {
	  fprintf(stdout,"BXmax %f ,BXmin %f Xvalue %f Yvalue %f peak %f hpeak %f \n"
		,BXmax,BXmin,Xval,Yval,peak,hpeak);
	  k++;
	  gr->SetPoint(k,Xval,Yval); 
	  gr->SetPointError(k,0,.1);    
        } 
      }
     }
  char ch[6];
  sprintf(ch,"fun%d",det);
  gr->Fit(ch,"","",fit_limit[det][0],fit_limit[det][1]);
  gr->Draw("same");
}
double function(double t,int x, int det)
{
  return  fun[det]->Eval(t);
}
/*arrange the histogram attributes*/
void CenterTitle(TH2D *h)
{
  h->GetXaxis()->CenterTitle(kTRUE);
  h->GetYaxis()->CenterTitle(kTRUE);
  h->GetXaxis()->SetTitleSize(0.05);
  h->GetYaxis()->SetTitleSize(0.05);
  h->GetXaxis()->SetTitleOffset(0.9);
  h->GetYaxis()->SetTitleOffset(-0.9);
}
void CenterTitle(TH1D *h){
  h->GetXaxis()->CenterTitle(kTRUE);
  h->GetYaxis()->CenterTitle(kTRUE);
  h->GetXaxis()->SetTitleSize(0.05);
  h->GetYaxis()->SetTitleSize(0.05);
  h->GetXaxis()->SetTitleOffset(0.9);
  h->GetYaxis()->SetTitleOffset(0.9);
}

Hope that someone can help me. Thanks in advance!!
std::bad_alloc is thrown when new fails.
Topic archived. No new replies allowed.