FairRoot/PandaRoot
PndRestGas.cxx
Go to the documentation of this file.
1 // -------------------------------------------------------------------------
2 // ----- PndRestGas source file -----
3 // ----- -----
4 // -------------------------------------------------------------------------
5 
6 #include <stdlib.h>
7 #include "PndRestGas.h"
8 
9 using namespace std;
10 
11 // ----- Default constructor ------------------------------------------
13 {
14 
15 }
16 
17 // ----- Standard constructor -----------------------------------------
18 PndRestGas::PndRestGas(char DDfname[80])
19 {
20 
21  // create DD function from data in DDfname
22  LoadDD(DDfname);
23 
24 }
25 
26 // ----- Standard constructor -----------------------------------------
27 PndRestGas::PndRestGas(char DDfname[80], Double_t zmin, Double_t zmax)
28 {
29 
30  // create DD function from data in DDfname
31  LoadDD(DDfname);
32 
33  // set min/max considered extension of beam tube along z-axis
34  SetzRange(zmin,zmax);
35 
36 }
37 
38 // ----- Destructor ---------------------------------------------------
40 
41  free(fDDvals);
42  fDDvals=NULL;
43 
44 }
45 // ------------------------------------------------------------------------
46 // return rest gas density at zval
47 TGraph* fDensityGraph;
48 Double_t GetDDValue(Double_t *zval, Double_t *) { // p //[R.K.03/2017] unused variable(s)
49 
50  Double_t y = fDensityGraph->Eval(zval[0]);
51  return y;
52 
53 }
54 
55 // .........................................................................
56 void PndRestGas::LoadDD(char DDfname[80])
57 {
58 
59  // read gas density values from the file
60  Int_t npoints;
61  Double_t zmin, zmax;
62  fDDvals = ReadDD(DDfname, &npoints, &zmin, &zmax);
63  SetzRange(zmin,zmax);
64 
65  // create function fDensityFun with DD
66  fDensityGraph = new TGraph(npoints,fDDvals[0],fDDvals[1]);
67  fDensityFun = new TF1("Rest Gas DD",GetDDValue,fzmin,fzmax,0);
68 
69 }
70 
71 // ------------------------------------------------------------------------
72 // read DD from file DDfname
73 // get number of points npoints, zmin, and zmax
74 Float_t** PndRestGas::ReadDD(char DDfname[80], Int_t* npoints,
75  Double_t* zmin, Double_t* zmax)
76 {
77 
78  Int_t i;
79  Float_t *x, *y;
80  Float_t a, b;
81  Float_t **values;
82 
83  /* BEGIN Ugly piece of code to count the lines ... */
84  Int_t lines=0;
85 
86  FILE *file = fopen(DDfname, "r"); /* Get filename */
87  int ch, prev = '\n' /* so empty files have no lines */;
88  while ( (ch = fgetc(file)) != EOF ) /* Read all chars in the file. */ {
89  if ( ch == '\n' ) {
90  ++lines; /* Bump the counter for every newline. */
91  }
92  prev = ch; /* Keep a copy to later test whether... */
93  }
94  fclose(file);
95  if ( prev != '\n' ) /* ...the last line did not end in a newline. */ {
96  ++lines; /* If so, add one more to the total. */
97  }
98 
99  *npoints = lines;
100 
101  /* END Ugly piece of code to count the lines ... */
102 
103  // allocate memory for density data ...
104  x = (Float_t*) malloc(lines*sizeof(Float_t));
105  y = (Float_t*) malloc(lines*sizeof(Float_t));
106 
107  // and fill with data from file
108  FILE * fp = fopen(DDfname,"r");
109  for(i=0;i<lines;i++) {
110  fscanf(fp,"%f %f",&a,&b);
111  x[i]=a;
112  y[i]=b;
113  }
114  fclose(fp);
115 
116  // copy values to output
117  values = (Float_t**) malloc(sizeof(Float_t*) * 2);
118  values[0] = x;
119  values[1] = y;
120 
121  // set zmin/zmax
122  *zmin = x[0];
123  *zmax = x[*npoints-1];
124 
125  return values;
126 
127 }
128 
129 // ------------------------------------------------------------------------
TGraph * fDensityGraph
Definition: PndRestGas.cxx:47
virtual ~PndRestGas()
Definition: PndRestGas.cxx:39
Int_t i
Definition: run_full.C:25
TTree * b
TFile * file
Int_t a
Definition: anaLmdDigi.C:126
Double_t
void LoadDD(char DDfname[80])
Definition: PndRestGas.cxx:56
Float_t ** ReadDD(char DDFname[80], Int_t *npoints, Double_t *zmin, Double_t *zmax)
Definition: PndRestGas.cxx:74
Double_t x
Double_t y
Double_t GetDDValue(Double_t *zval, Double_t *)
Definition: PndRestGas.cxx:48