1 | // $Id: EOpticsResponse.cc 2918 2011-06-10 22:22:31Z mabl $ |
---|
2 | // Author: ale 2005/01/19 |
---|
3 | |
---|
4 | /***************************************************************************** |
---|
5 | * ESAF: Euso Simulation and Analysis Framework * |
---|
6 | * * |
---|
7 | * Id: EOpticsResponse * |
---|
8 | * Package: <packagename> * |
---|
9 | * Coordinator: <coordinator> * |
---|
10 | * * |
---|
11 | *****************************************************************************/ |
---|
12 | |
---|
13 | //_____________________________________________________________________________ |
---|
14 | // |
---|
15 | // OpticsResponse |
---|
16 | // |
---|
17 | // <extensive class description> |
---|
18 | // |
---|
19 | // Config file parameters |
---|
20 | // ====================== |
---|
21 | // |
---|
22 | // <parameter name>: <parameter description> |
---|
23 | // -Valid options: <available options> |
---|
24 | // |
---|
25 | |
---|
26 | #include "EOpticsResponse.hh" |
---|
27 | #include "EORSample.hh" |
---|
28 | #include "EORHeader.hh" |
---|
29 | |
---|
30 | #include <TFile.h> |
---|
31 | #include <TTree.h> |
---|
32 | #include <TH2F.h> |
---|
33 | #include <TClonesArray.h> |
---|
34 | #include <TSystem.h> |
---|
35 | #include <stdexcept> |
---|
36 | #include <iostream> |
---|
37 | #include <TMath.h> |
---|
38 | using namespace std; |
---|
39 | |
---|
40 | ClassImp(EOpticsResponse) |
---|
41 | |
---|
42 | //_____________________________________________________________________________ |
---|
43 | EOpticsResponse::EOpticsResponse( const char *dbname ) : |
---|
44 | fFile(0), fTree(0), fSample(0), fHeader(0), fPsf(0), fPoints(0) { |
---|
45 | // |
---|
46 | // Constructor |
---|
47 | // |
---|
48 | |
---|
49 | |
---|
50 | FileStat_t buf; |
---|
51 | if ( gSystem->GetPathInfo(dbname, buf) != 0 ) MakeZombie(); |
---|
52 | else { |
---|
53 | fFile = new TFile(dbname); |
---|
54 | if( !(fTree = (TTree*)fFile->Get("ORDatabase")) ) MakeZombie(); |
---|
55 | } |
---|
56 | |
---|
57 | if( IsZombie() ) { |
---|
58 | Error("EOpticsResponse","Can not open or find the Optics DataBase."); |
---|
59 | |
---|
60 | Error("EOpticsResponse","Please run macros/tools/opticsresponse.C click Build button"); |
---|
61 | |
---|
62 | return; |
---|
63 | } |
---|
64 | fHeader = (EORHeader*)fTree->GetUserInfo()->FindObject("EORHeader"); |
---|
65 | |
---|
66 | fTree->SetBranchAddress("EORSample",&fSample); |
---|
67 | |
---|
68 | fTree->BuildIndex("EORSample.fThetaIndex","EORSample.fLambdaIndex"); |
---|
69 | |
---|
70 | fPoints = new TClonesArray("TVector3",1000000); |
---|
71 | |
---|
72 | fPoints->BypassStreamer(kFALSE); |
---|
73 | |
---|
74 | Info("EOpticsResponse","Enabled. Reading from DB file: %s",fFile->GetName()); |
---|
75 | |
---|
76 | } |
---|
77 | |
---|
78 | //_____________________________________________________________________________ |
---|
79 | EOpticsResponse::~EOpticsResponse() { |
---|
80 | // |
---|
81 | // Destructor |
---|
82 | // |
---|
83 | |
---|
84 | SafeDelete(fFile); |
---|
85 | SafeDelete(fTree); |
---|
86 | SafeDelete(fSample); |
---|
87 | SafeDelete(fPoints); |
---|
88 | } |
---|
89 | |
---|
90 | //_____________________________________________________________________________ |
---|
91 | Bool_t EOpticsResponse::GetEntry(Float_t theta, Float_t lambda) { |
---|
92 | // |
---|
93 | // Reads the entry from the OR DB file for given theta (in rad) and lambda (in nm) |
---|
94 | // |
---|
95 | |
---|
96 | if ( IsZombie() ) return kFALSE; |
---|
97 | |
---|
98 | Float_t ThetaStep = ( fHeader->GetThetaMax() - fHeader->GetThetaMin() ) / fHeader->GetBinsTheta(); |
---|
99 | |
---|
100 | Float_t LambdaStep = ( fHeader->GetLambdaMax() - fHeader->GetLambdaMin() ) / fHeader->GetBinsLambda(); |
---|
101 | |
---|
102 | |
---|
103 | Int_t IndexTheta = Int_t(theta/ThetaStep); |
---|
104 | |
---|
105 | Int_t IndexLambda = Int_t((lambda-fHeader->GetLambdaMin())/LambdaStep); |
---|
106 | |
---|
107 | if ( theta <= fHeader->GetThetaMin() ) IndexTheta = fHeader->GetThetaIndexMin(); |
---|
108 | if ( theta >= fHeader->GetThetaMax() ) IndexTheta = fHeader->GetThetaIndexMax(); |
---|
109 | if ( lambda <= fHeader->GetLambdaMin() ) IndexLambda = fHeader->GetLambdaIndexMin(); |
---|
110 | if ( lambda >= fHeader->GetLambdaMax() ) IndexLambda = fHeader->GetLambdaIndexMax(); |
---|
111 | |
---|
112 | fTree->GetEntryWithIndex(IndexTheta,IndexLambda); |
---|
113 | |
---|
114 | return kTRUE; |
---|
115 | } |
---|
116 | |
---|
117 | //_____________________________________________________________________________ |
---|
118 | TH2F* EOpticsResponse::MakePsfHist(Float_t theta, Float_t phi, TH1F* spectrum){ |
---|
119 | // |
---|
120 | // Returns a histo with weighted sum of Psf distributions for the entry spectrum |
---|
121 | // |
---|
122 | if ( IsZombie() ) return NULL; |
---|
123 | if( !spectrum ) return (TH2F*) NULL; |
---|
124 | if( !spectrum->GetEntries() ) return (TH2F*) NULL; |
---|
125 | |
---|
126 | Float_t lambda, weigth, weigthtot(0); |
---|
127 | Int_t entry(0); |
---|
128 | |
---|
129 | for (Int_t i = 1; i <= spectrum->GetNbinsX(); i++) { |
---|
130 | lambda = spectrum->GetBinCenter(i); |
---|
131 | weigth = spectrum->GetBinContent(i); |
---|
132 | if (!weigth) continue; |
---|
133 | weigthtot += weigth; |
---|
134 | GetEntry(theta,lambda); |
---|
135 | |
---|
136 | TH2F* htemp = GetSample()->GetPsfHist(); // TODO: CHECK if should be deleted in the end!!! |
---|
137 | |
---|
138 | if (!htemp) { |
---|
139 | Warning("MakePsfHist(Float_t theta, Float_t phi, TH1F* spectrum)","can not find the histo"); |
---|
140 | return (TH2F*) NULL; |
---|
141 | } |
---|
142 | for (Int_t j=1; j<=htemp->GetNbinsX(); j++) |
---|
143 | for (Int_t k=1; k<=htemp->GetNbinsY(); k++) { |
---|
144 | if (entry > fPoints->GetEntriesFast()) break; |
---|
145 | |
---|
146 | Float_t x = htemp->GetXaxis()->GetBinCenter(j); |
---|
147 | Float_t y = htemp->GetYaxis()->GetBinCenter(k); |
---|
148 | Float_t z = htemp->GetBinContent(j,k); |
---|
149 | |
---|
150 | x = x*TMath::Cos(phi) - y*TMath::Sin(phi); |
---|
151 | y = x*TMath::Sin(phi) + y*TMath::Cos(phi); |
---|
152 | z = z*weigth; |
---|
153 | |
---|
154 | TClonesArray &ref = *fPoints; |
---|
155 | |
---|
156 | new(ref[entry++]) TVector3(x,y,z); |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | // define corners of the histo |
---|
161 | |
---|
162 | Float_t xmin(100000), xmax(-xmin), ymin(xmin), ymax(-ymin); |
---|
163 | for (Int_t i=0; i<entry; i++) { |
---|
164 | TVector3 *pos = (TVector3*)fPoints->At(i); |
---|
165 | if (xmin > pos->X()) xmin = pos->X(); |
---|
166 | if (xmax < pos->X()) xmax = pos->X(); |
---|
167 | if (ymin > pos->Y()) ymin = pos->Y(); |
---|
168 | if (ymax < pos->Y()) ymax = pos->Y(); |
---|
169 | } |
---|
170 | |
---|
171 | Int_t nx,ny; |
---|
172 | |
---|
173 | nx = ny = fHeader->GetPsfBins(); |
---|
174 | |
---|
175 | if ( fPsf ) { |
---|
176 | //TOCHECK: is it fast? |
---|
177 | |
---|
178 | fPsf->SetBins(nx, xmin, xmax, ny, ymin, ymax); |
---|
179 | fPsf->Reset(); |
---|
180 | } |
---|
181 | |
---|
182 | else { |
---|
183 | |
---|
184 | fPsf = new TH2F("psf", "Psf for the spectrum", nx, xmin, xmax, ny, ymin, ymax); |
---|
185 | // fPsf->SetDirectory(0); |
---|
186 | } |
---|
187 | |
---|
188 | for (Int_t i=0; i < entry; i++) { |
---|
189 | |
---|
190 | TVector3 *pos = (TVector3*)fPoints->At(i); |
---|
191 | fPsf->Fill(pos->X(),pos->Y(),pos->Z()/weigthtot); |
---|
192 | } |
---|
193 | return fPsf; |
---|
194 | } |
---|
195 | |
---|
196 | //______________________________________________________________________________ |
---|
197 | void EOpticsResponse::Clear( Option_t* ) { |
---|
198 | // |
---|
199 | // Clears all temporary histograms |
---|
200 | // |
---|
201 | |
---|
202 | SafeDelete( fPsf ); |
---|
203 | fPoints->Clear(); |
---|
204 | |
---|
205 | } |
---|
206 | |
---|
207 | //_____________________________________________________________________________ |
---|
208 | TH2F* EOpticsResponse::MakePsfHist(Float_t theta, Float_t phi, Float_t lambda){ |
---|
209 | // |
---|
210 | // Returns Psf distribution for the given theta and phi |
---|
211 | // |
---|
212 | if ( !GetEntry(theta,lambda) ) return NULL; |
---|
213 | TH2F* fPsf = fSample->MakePsfHistRotated(phi); |
---|
214 | // fPsf->SetDirectory(0); |
---|
215 | |
---|
216 | return fPsf; |
---|
217 | |
---|
218 | } |
---|