static void improveEvents(vegasGrid * vegPtr,double (*func)(double *,double,double*)) { int mode; void * pscr_=NULL; double eff0; for(mode=1;mode!=4; ) { char strmen[]="\030" " sub-cubes = N1 " " random search = N2 " " simplex search= N3 " " Start search of maxima "; improveStr(strmen,"N1","%d",n_cube); improveStr(strmen,"N2","%d",nRandom); improveStr(strmen,"N3","%d",nSimplex1); menu1(54,14,"Preparing of generator",strmen,"n_prep_gen_*",&pscr_,&mode); switch(mode) { case 0: return; case 1: if(correctLong(50,15,"Number of sub-cubes:",&n_cube,1)) { free(vegPtr->fMax); vegPtr->fMax=NULL; vegPtr->nCubes=0; } break; case 2: correctLong(50,15,"Random search:",&nRandom,1);break; case 3: correctLong(50,15,"Simplex steps :",&nSimplex1,1); break; /* " milk = N3 " improveStr(strmen,"N3","%.1f",milk); case 4: correctDouble(50,15,"Content of milk:",&milk,1); break; */ case 4: if(n_cube < 1 ) n_cube=1; { int mCheck=vegas_max(vegPtr,n_cube, nRandom,nSimplex1, milk, func,&eff0); if(mCheck==0) { char mess[50]; sprintf(mess,"Expected efficiency %f",eff0/max); messanykey(25,15,mess); put_text(&pscr_); } else { mode=1; if(mCheck==2) messanykey(25,15,"Not enough memory.\n" "Decrease the number of sub-cubes"); } } } } }
int runVegas(void) { int i; double sd; double avgi; char mess[25]; FILE * iprt = NULL; int mode=1; void * pscr=NULL; static int n_Line=7; i=imkmom(inP1,inP2); if(veg_Ptr&&veg_Ptr->ndim!=i)clearGrid(); if(!veg_Ptr) veg_Ptr=vegas_init(i,50); if(nin_int == 2) strcpy(mess, "Cross section[pb]"); else strcpy(mess, " Width[Gev] "); /* ** save current session parameters */ w_sess__(NULL); /* ** open protocol and resulting files */ { char fname[50]; sprintf(fname,"%sprt_%d",outputDir,nSess); iprt=fopen(fname,"a"); if(ftell(iprt)==0) { fprintf(iprt," CalcHEP kinematics module \n The session parameters:\n"); w_sess__(iprt); fprintf(iprt,"===================================\n"); } } /* ** initkinematics */ while(correctHistList()) editHist(); /* *** Main cycle */ if(!integral.old || n_Line==7) { n_Line=7; scrcolor(Blue, BGmain); // printLn(iprt,&n_Line," #IT %20s Error %% nCall chi**2",mess); printLn(iprt,&n_Line," #IT %s Error[%%] nCalls Eff. chi^2",mess); } for(;;) { static int worn=1; char strmen[]="\030" " nSess = N2_1 " " nCalls = N1_1 " " Set Distributions " "*Start integration " " Display Distributions " " Clear statistic " " Freeze grid OFF " " Clear grid " " Event Cubes NCUBE " " Generate Events "; improveStr(strmen,"N1_1","%d",integral.ncall[0]); improveStr(strmen,"N2_1","%d",integral.itmx[0]); improveStr(strmen,"NCUBE","%d",EventGrid); if(integral.freeze) improveStr(strmen,"OFF","ON"); menu1(54,7,"",strmen,"n_veg_*",&pscr,&mode); switch(mode) { case 0: if(iprt) fclose(iprt); return 0; case 1: correctInt(50,12,"Enter new value ",&integral.itmx[0],1); break; case 2: correctLong(50,12,"Enter new value ",&integral.ncall[0],1); break; case 3: editHist(); break; case 4: if(veg_Ptr->fMax && !integral.freeze) { if(!mess_y_n(15,15,"You have event generator prepared.\n" " The answer 'Y' will start Vegas session \nwhich destroys it." " To save the event generator answer 'N' \nand set " " ' Freeze grid' ON")) break; else { free(veg_Ptr->fMax); veg_Ptr->fMax=NULL; veg_Ptr->nCubes=0;} } for (i = 1; i <= integral.itmx[0]; ++i) { double sum; char errtxt[100]=""; int k; if(integral.ncall[0]==0) break; nCall=0; negPoints=0; badPoints=0; hFill=1; if(vegas_int(veg_Ptr, integral.ncall[0],1.5*(!integral.freeze), func_, &avgi, &sd) ) break; integral.old=1; negPoints/=nCall; badPoints/=nCall; integral.nCallTot+=nCall; scrcolor(FGmain,BGmain); printLn(iprt,&n_Line,"%4d %12.4E %10.2E %8d %s", ++integral.n_it, avgi,avgi? 100*sd/(double)fabs(avgi):0.,nCall,effInfo()); if(negPoints<0) sprintf(errtxt+strlen(errtxt)," Negative points %.1G%%;", -100*negPoints/(avgi-2*negPoints)); if(badPoints) sprintf(errtxt+strlen(errtxt), "Bad Precision %.1G%%;",100*badPoints/(avgi-2*negPoints)); if(errtxt[0]) { scrcolor(Red,BGmain); printLn(iprt,&n_Line,"%s",errtxt); } integral.s0+=sd*sd; integral.s1+=avgi; integral.s2+=avgi*avgi; } integral.In=integral.s1/integral.n_it; integral.dI=sqrt(integral.s0)/integral.n_it; if(integral.n_it<=1 || integral.s0==0 ) integral.khi2=0; else integral.khi2=(integral.s2-integral.s1*integral.s1/integral.n_it)*integral.n_it/(integral.n_it-1)/fabs(integral.s0); scrcolor(FGmain,BGmain); printLn(iprt,&n_Line," < > %12.4E %10.2E %8d %7.7s %-7.1G" , integral.In, fabs(integral.In)? 100*integral.dI/(double)fabs(integral.In):0., integral.nCallTot, effInfo(), integral.khi2); if(histTab.strings) { char fname[20]; FILE * d; sprintf(fname,"distr_%d",nSess); d=fopen(fname,"w"); wrt_hist2(d,Process); fclose(d); } messanykey(54,11,"Integration is over"); /* integral.freeze=0; */ break; case 5: showHist(54,10,Process); break; case 6: clearStatistics(-1); messanykey(54,13,"Old results for integral\n" "and distributions\nare deleted."); break; case 7: integral.freeze=!integral.freeze; break; case 8: if(!integral.freeze || mess_y_n(15,15,"The information for Event Generator will be lost\n OK?")) { int ndim=veg_Ptr->ndim; vegas_finish(veg_Ptr); veg_Ptr=vegas_init(ndim,50); messanykey(57,11,"OK"); } break; case 9: if(correctLong(50,12,"Enter new value ",&EventGrid,1)) { if(veg_Ptr->fMax) {free(veg_Ptr->fMax); veg_Ptr->fMax=NULL;veg_Ptr->nCubes=0;}} break; case 10: if( !veg_Ptr || !veg_Ptr->fMax) { char * mess="Before event generation one has to launch Vegas session with freezed grid\n" "to prepare generator"; if(blind) { printf("%s\n",mess); sortie(200);} else messanykey(4,13,mess); } else runEvents(); } } }
static void generateEvents( vegasGrid * vegPtr, double (*func)(double *,double,double*), char *fname, FILE * iprt) { int mode=1; void * pscr=NULL; static int regen=1; // if(!vegPtr->fMax)improveEvents(vegPtr,func); for(mode=1;;) { char strmen[]="\032" " Number of events=N1 " " Launch generator " " Allow weighted events OFF"; if(!regen) improveStr(strmen,"OFF","%s","ON"); improveStr(strmen,"N1","%d",nEvents); menu1(53,10,"",strmen,"n_gen_*",&pscr,&mode); switch(mode) { case 0: return; case 1: correctLong(50,15,"",&nEvents,1); break; case 2: { long nGenerated=0; double eff; int nmax,mult,neg; char mess[200]; long cEvent; long fileEnd; if(!vegPtr->fMax) { messanykey(15,15,"Generator is not ready."); break;} events_= fopen(fname,"a"); if(ftell(events_)==0) write_event_cap(); fileEnd=ftell(events_); build_cb_int(Nsub); cEvent= vegas_events(vegPtr,nEvents,max,func,writeEvent,regen,&eff,&nmax,&mult,&neg); fclose(events_); if(cEvent>0) { int l; sprintf(mess,"Statistic\n Events generated: %ld\n efficiency: %.1E\nMax event multiplicity: %d\n" "Multiple events(total): %d \nNegative weight events: %d \n", cEvent, eff,nmax,mult, neg); l=strlen(mess); strcat(mess,"---------------\n Accept events? "); if(mess_y_n(25,15,mess)) { long nEvPos=0; integral.old=1; mess[l]=0; events_=fopen(fname,"r+"); while(nEvPos==0) { char ch; char word[100]; do fscanf(events_,"%c",&ch); while(ch !='#'); fscanf(events_,"%s",word); if(strcmp(word,"Number_of_events")==0) nEvPos=ftell(events_); } fscanf(events_,"%ld",&nGenerated); nGenerated+=cEvent; fseek(events_,nEvPos,SEEK_SET); fprintf(events_," %10ld",nGenerated); fclose(events_); fprintf(iprt," %ld events are stored in '%s'\n",nGenerated,fname); fprintf(iprt,"%s\n",mess); fflush(iprt); } else truncate(fname,fileEnd); } destroy_cb_int(); } break; // case 3: improveEvents(vegPtr,func); put_text(&pscr); break; case 3: regen=!regen; // goto ret; } } }
int runVegas(void) { int i; double sd; double avgi; char mess[25]; FILE * iprt = NULL; int mode=1; void * pscr=NULL; static int n_Line=0; if(blind) vegas_control=NULL; else vegas_control=infoLine; i=imkmom(inP1,inP2); if(veg_Ptr&&veg_Ptr->dim!=i)clearGrid(); if(!veg_Ptr) veg_Ptr=vegas_init(i,func_,50); if(nin_int == 2) strcpy(mess, "Cross section[pb]"); else strcpy(mess, " Width[Gev] "); /* ** save current session parameters */ w_sess__(NULL); /* ** open protocol and resulting files */ { char fname[50]; sprintf(fname,"%sprt_%d",outputDir,nSess); iprt=fopen(fname,"a"); } /* ** initkinematics */ while(correctHistList()) editHist(); if(integral.old) { if(!n_Line) n_Line=9;} else { w_sess__(iprt); n_Line=8; } /* *** Main cycle */ for(;;) { int fz; char strmen[]="\030" " nSess = N2_1 " " nCalls = N1_1 " " Set Distributions " "*Start integration " " Display Distributions " " Clear statistic " " Freeze grid OFF " " Clear grid " " Event Cubes NCUBE " " Num. of events=NE " " Generate Events "; if(integral.freeze) { fz=1; improveStr(strmen,"OFF","ON"); improveStr(strmen,"NCUBE","%d",EventGrid); } else { fz=0; strmen[ 030*8+2]=0; } improveStr(strmen,"N1_1","%d",integral.ncall[fz]); improveStr(strmen,"N2_1","%d",integral.itmx[fz]); improveStr(strmen,"NE","%d",nEvents); menu1(54,7,"",strmen,"n_veg_*",&pscr,&mode); switch(mode) { case 0: w_sess__(NULL); if(iprt) fclose(iprt); return 0; case 1: correctInt(50,12,"Enter new value ", integral.itmx+fz,1); break; case 2: correctLong(50,12,"Enter new value ",integral.ncall+fz,1);break; case 3: editHist(); break; case 4: if(veg_Ptr->fMax && !integral.freeze) { free(veg_Ptr->fMax); veg_Ptr->fMax=NULL; veg_Ptr->evnCubes=0; } if(!veg_Ptr->fMax && integral.freeze) { setEventCubes(veg_Ptr, EventGrid); EventGrid=veg_Ptr->evnCubes; } for (i = 1; i <= integral.itmx[fz]; ++i) { char errtxt[100]=""; long nCall; if(integral.ncall[0]==0) break; negPoints=0; badPoints=0; hFill=1; nCall=vegas_int(veg_Ptr, integral.ncall[fz],1.5*(!fz),nPROCSS,&avgi, &sd); if(nCall<0) { messanykey(10,10,"NaN in integrand"); break;} if(nCall==0) break; integral.old=1; negPoints/=nCall; badPoints/=nCall; integral.nCallTot+=nCall; scrcolor(FGmain,BGmain); printLn(iprt,&n_Line,"%4d %12.4E %10.2E %8d %s", ++integral.n_it, avgi,avgi? 100*sd/(double)fabs(avgi):0.,nCall,effInfo()); if(negPoints<0) sprintf(errtxt+strlen(errtxt)," Negative points %.1G%%;", -100*negPoints/(avgi-2*negPoints)); if(badPoints) sprintf(errtxt+strlen(errtxt), "Bad Precision %.1G%%;",100*badPoints/(avgi-2*negPoints)); if(errtxt[0]) { scrcolor(Red,BGmain); printLn(iprt,&n_Line,"%s",errtxt); } integral.s0+=sd*sd; integral.s1+=avgi; integral.s2+=avgi*avgi; } integral.In=integral.s1/integral.n_it; integral.dI=sqrt(integral.s0)/integral.n_it; if(integral.n_it<=1 || integral.s0==0 ) integral.khi2=0; else integral.khi2=(integral.s2-integral.s1*integral.s1/integral.n_it)*integral.n_it/(integral.n_it-1)/fabs(integral.s0); scrcolor(FGmain,BGmain); printLn(iprt,&n_Line," < > %12.4E %10.2E %8d %7.7s %-7.1G" , integral.In, fabs(integral.In)? 100*integral.dI/(double)fabs(integral.In):0., integral.nCallTot, effInfo(), integral.khi2); if(histTab.strings) { char fname[20]; FILE * d; sprintf(fname,"distr_%d",nSess); d=fopen(fname,"w"); wrt_hist2(d,Process); fclose(d); } messanykey(54,11,"Integration is over"); /* integral.freeze=0; */ break; case 5: showHist(54,10,Process); break; case 6: clearStatistics(-1); messanykey(54,13,"Old results for integral\n" "and distributions\nare deleted."); break; case 7: if(veg_Ptr->fMax && integral.freeze) { if(mess_y_n(15,15,"You have event generator prepared.\n" " Setting the flag \"OFF\" will destroy it." " Press 'Y' to confirm.")) integral.freeze=0; } else integral.freeze=!integral.freeze; break; case 8: if(!integral.freeze || mess_y_n(15,15,"The information for Event Generator will be lost\n OK?")) { int ndim=veg_Ptr->dim; vegas_finish(veg_Ptr); veg_Ptr=vegas_init(ndim,func_,50); messanykey(57,11,"OK"); } break; case 9: if(correctLong(50,12,"Enter new value ",&EventGrid,1)) { if(veg_Ptr->fMax) {free(veg_Ptr->fMax); veg_Ptr->fMax=NULL;} printf("EventGrid=%d\n",EventGrid); setEventCubes(veg_Ptr, EventGrid); EventGrid=veg_Ptr->evnCubes; } break; case 10: correctInt(50,15,"",&nEvents,1); break; case 11: if( !veg_Ptr || !veg_Ptr->fMax) { char * mess="Before event generation one has to launch Vegas session with freezed grid\n" "to prepare generator"; if(blind) { printf("%s\n",mess); sortie(200);} else messanykey(4,13,mess); } else runEvents(); } } }