source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/packages/simulation/detector/G4Detector/G4fresnellens/src/FresnelSurfaceHelper.cc @ 117

Last change on this file since 117 was 117, checked in by moretto, 11 years ago

ESAF version compilable on mac OS

File size: 3.6 KB
Line 
1#include "FresnelSurfaceHelper.h"
2#include "ArraySurface.h"
3#include "SecondOrderSurface.h"
4#include "ParametricSurface.h"
5#include "FresnelSurface.h"
6
7#include <iostream>
8#include <fstream>
9#include <string.h>
10#include <float.h>
11
12using namespace std;
13using namespace G4FresnelLens;
14
15//______________________________________________________________________________
16FresnelSurfaceHelper::FresnelSurfaceHelper(const char* filename){
17    Init();
18    if (filename) Read(filename);
19}
20
21//______________________________________________________________________________
22FresnelSurfaceHelper::FresnelSurfaceHelper(int n, double *rho, double *z){
23    Init();
24    Read(n, rho, z);
25}
26
27//______________________________________________________________________________
28FresnelSurfaceHelper::~FresnelSurfaceHelper(){
29    Clear();
30}
31
32//______________________________________________________________________________
33void FresnelSurfaceHelper::Clear(){
34    if ( fZ ) delete [] fZ;
35    if ( fRho ) delete [] fRho;
36    fZ=fRho=0;
37    fN=0;
38
39    if (fOwnSurfaces) {
40        for (unsigned i(0); i<fSurfaces.size(); i++){
41            delete fSurfaces[i];
42        }
43    }
44    fSurfaces.clear();
45    fOwnSurfaces=true;
46}
47
48//______________________________________________________________________________
49void FresnelSurfaceHelper::Init(){
50    fN=0;
51    fZ=fRho=0;
52    fZfilter=DBL_MAX;
53    fShift=0.;
54    fScale=1.;
55    fOwnSurfaces=true;
56    fFitAccuracy=-1.;
57}
58
59//______________________________________________________________________________
60void FresnelSurfaceHelper::Read(const char* file){
61    Clear();
62    //printf("reading %s\n",file);
63
64    int nread=10000;
65    fRho=new double[nread];
66    fZ=new double[nread];
67
68    ifstream infile(file);
69    if( !infile.good() ) {
70        printf("Bad file %s\n", file);
71        exit(1);
72    }
73    string line;
74    fN=0;
75    while( getline(infile,line,'\n') ){
76        double rho,z;
77        if( 2!=sscanf(line.data(), "%lg %lg", &rho, &z) ) continue;
78        if ( fN>=nread ){
79            double *newRho=new double[nread*2];
80            double *newZ=new double[nread*2];
81            memcpy(newRho, fRho, nread*sizeof(double));
82            memcpy(newZ, fZ, nread*sizeof(double));
83            nread*=2;
84            delete [] fRho;
85            delete [] fZ;
86            fRho=newRho;
87            fZ=newZ;
88        }
89        AddPoint(rho,z);
90    }
91    infile.close();
92    Convert();
93}
94
95//______________________________________________________________________________
96void FresnelSurfaceHelper::Read(int n, double *rho, double *z){
97    Clear();
98    fRho=new double[n];
99    fZ=new double[n];
100    fN=0;
101    for(int i(0); i<n; i++) AddPoint(rho[i],z[i]);
102    Convert();
103}
104
105//______________________________________________________________________________
106void FresnelSurfaceHelper::Convert(){
107    int previ=0;
108    //printf("converting %i\n", fN);
109    for(int i(0); i<fN; i++){
110        if ( i<(fN-1) && fRho[i]!=fRho[i+1] ) continue;
111        SSurface* surf=0;
112        if ( fFitAccuracy>0. ){
113            SecondOrderSurface* fit=new SecondOrderSurface();
114            double dev=fit->GuessParameters(i-previ+1, fRho+previ, fZ+previ);
115            if ( dev<fFitAccuracy ) surf=(SSurface*)fit;
116        }
117        if (!surf) surf=new ArraySurface(i-previ+1, fRho+previ, fZ+previ);
118        //printf("%i -> %i\n", previ, i-previ+1);
119        fSurfaces.push_back(surf);
120        previ=i+1;
121    }
122    fOwnSurfaces=true;
123}
124
125//______________________________________________________________________________
126FresnelSurface* FresnelSurfaceHelper::CreateFresnelLens(ParametricSurface* downsurf, ParametricSurface* upsurf){
127    fOwnSurfaces=false;
128    return new FresnelSurface(fSurfaces, downsurf, upsurf);
129}
Note: See TracBrowser for help on using the repository browser.