C tricks
Updated on Sun, 2013-11-24 17:03. Originally created by balewski on 2009-01-23 22:33.
---------- pass function name as argument to another function -------
typedef float (*func)(float);
float ff1(float x) { return x*x;}
float ff2(float x) { return x+x;}
void usef(float (*fp)(float ), float y) {
float val=(fp)(y);
printf(" yy=%f xx=%f\n",y,val);
}
int main(int argc, char *argv[]) {
usef(ff1,10);
usef(ff1,7);
return 0;
}
Read ascii file, method 1 - read individual values
// constructing vectors #include <iostream> //#include <vector> //using namespace std; vector<int> runL; plPattern() { readRunList(); printf("main: runL len=%d\n", runL.size()); } Lost this code....
Read ascii - method 2 - read lines and read values form a line
//-------------------------------------------------- void readRunList(){ int k=1; FILE *fd=fopen("sort1/runListBoil.txt","r"); assert(fd); while(1) { int runNo; int x; int ret= fscanf(fd,"%d %d",&runNo,&x); if(ret!=2) break; printf("%d %d \n",k, runNo); runL.push_back(runNo); k++; } }
Stand alone code using Minuit + Makefile --> Att. A)
% nm.pl _ZTV17St_dst_bfc_status
this symbol is nowhere to be found other than in StTreeMaker
(undefined). This class has a reference to St_bfc_status as
Unpack CVS array read from a text file:
const int mx=30000; char buf[mx]; while(true) { char * ret=fgets(buf,mx,fd); if(ret==0) return 0; if(buf[0]=='#') continue; char name0[mx]; int ret1=sscanf(buf,"%s,",name0); printf("k=%d name0=%s=\n",k,name0); char *item=strtok(buf,","); // init 'strtok' int i=1; do { printf("i=%d, item=%s=\n",i++,item); } while (item=strtok(0,",")); // advance by one name
convert unix time --> string --> yyymmdd, hhmmss
int a(){ time_t rawtime=1302361369-3600*0; struct tm * timeinfo; char buffer [80]; timeinfo = gmtime ( &rawtime ); strftime (buffer,80,"%k%M%S",timeinfo); puts (buffer); int hhmmss=atoi(buffer); strftime (buffer,80,"%G%m%d",timeinfo); puts (buffer); int yyyymmdd=atoi(buffer); printf("day=%d, hour=%d\n",yyyymmdd,hhmmss); }Read text to histo
$ cat macros/plDose.C TH1F *hFl=0; TH1F *hFly=0; plDose() { readFlukaHisto(); hFly->Draw(); gPad->SetLogx(); // gPad->SetLogy(); gPad->SetGrid(); // tt(); } void tt() { enum {mxFB=99 }; Float_t xBB[mxFB]={ 1.0000e-05, 1.0970e-05, 1.2020e-05, 1.3180e-05, 1.4460e-05, 1.5850e-05, 1.7380e-05, 1.9060e-05, 2.0900e-05, 2.2920e-05, 2.5130e-05, 2.7560e-05, 3.0220e-05, 3.3130e-05, 3.6330e-05, 3.9840e-05, 4.3690e-05, 4.7900e-05, 5.2530e-05, 5.7600e-05, 6.3160e-05, 6.9260e-05, 7.5940e-05, 8.3270e-05, 9.1310e-05, 1.0010e-04, 1.0980e-04, 1.2040e-04, 1.3200e-04, 1.4480e-04, 1.5870e-04, 1.7410e-04, 1.9090e-04, 2.0930e-04, 2.2950e-04, 2.5160e-04, 2.7590e-04, 3.0260e-04, 3.3180e-04, 3.6380e-04, 3.9890e-04, 4.3740e-04, 4.7970e-04, 5.2600e-04, 5.7670e-04, 6.3240e-04, 6.9340e-04, 7.6040e-04, 8.3380e-04, 9.1430e-04, 1.0030e-03, 1.0990e-03, 1.2050e-03, 1.3220e-03, 1.4490e-03, 1.5890e-03, 1.7430e-03, 1.9110e-03, 2.0950e-03, 2.2980e-03, 2.5200e-03, 2.7630e-03, 3.0290e-03, 3.3220e-03, 3.6430e-03, 3.9940e-03, 4.3800e-03, 4.8030e-03, 5.2660e-03, 5.7750e-03, 6.3320e-03, 6.9430e-03, 7.6140e-03, 8.3490e-03, 9.1540e-03, 1.0040e-02, 1.1010e-02, 1.2070e-02, 1.3230e-02, 1.4510e-02, 1.5910e-02, 1.7450e-02, 1.9130e-02, 2.0980e-02, 2.3010e-02, 2.5230e-02, 2.7660e-02, 3.0330e-02, 3.3260e-02, 3.6470e-02, 3.9990e-02, 4.3850e-02, 4.8090e-02, 5.2730e-02, 5.7820e-02, 6.3400e-02, 6.9520e-02, 7.6230e-02, 8.3590e-02 }; hFxx= new TH1F("hFxx","xx in Fluka bins; Photon energy (GeV)",mxFB-1,xBB); hFxx->Fill(2e-3,30); hFxx->Draw(); } //-------------------------------------------------- void readFlukaHisto(){ const int mx=30000; double xLow[mx],yA[mx], eyA[mx]; int k=0; FILE *fd=fopen("../flukaDose.dat","r"); assert(fd); char buf[mx]; float x1,x2,y,e; while(true) { char * ret=fgets(buf,mx,fd); if(ret==0) break; //return 0; if(buf[0]=='#') continue; int ret1=sscanf(buf,"%f %f %f %f ",&x1, &x2, &y, &e); //printf("k=%d x1=%f, y=%f\n",k,x1,y); xLow[k]=x1; yA[k]=y; eyA[k]=y*e/100.; k++; // return; } xLow[k]=x2; int nb=k; hFl= new TH1F("hFlr","rate from Fluka; Photon energy (GeV)",nb,xLow); hFly= new TH1F("hFly","yield from errors from Fluka; Photon energy (GeV)",nb,xLow); //TH1(const char* name, const char* title, Int_t nbinsx, const Float_t* xbins) // populate the histo for(int i=0;iSetBinContent(i+1,yA[i]); hFl->SetBinError(i+1,eyA[i]); if( yA[i]>0 ) { double rerr=eyA[i]/yA[i]; //double yield=1./sqrt(rerr);// * (xLow[k double yield=(xLow[i+1] - xLow[i]) * yA[i] *2.5e+6; //printf("k=%d rerr=%f\n",k,rerr); hFly->SetBinContent(i+1,yield); } } // dump array with x-axis // double AA[3]={1,2,3}; printf("enum {mxFB=%d };\n",nb); printf("Float_t xBB[mxFB]={ "); for(int i=0;i
»
- balewski's blog
- Login or register to post comments