source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/packages/simulation/lightsources/src/ListPhotonsInAtmosphere.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: 9.3 KB
Line 
1// ESAF : Euso Simulation and Analysis Framework
2// $Id: ListPhotonsInAtmosphere.cc 2681 2006-05-03 10:58:33Z moreggia $
3// Anne Stutz created Dec,  1 2003
4
5#include "ListPhotonsInAtmosphere.hh"
6#include "SinglePhoton.hh"
7#include "BunchOfPhotons.hh"
8#include <stdexcept>
9
10ClassImp(ListPhotonsInAtmosphere)
11
12//______________________________________________________________________________
13ListPhotonsInAtmosphere::ListPhotonsInAtmosphere() : PhotonsInAtmosphere("list") {
14    //
15    // ctor
16    //
17    fNbNotSaved = 0;
18    fCountS = 0;
19    fCountB = 0;
20    fTrackNbSteps = 0;
21    fBeforeTrackstart = 0;
22    fTrack.clear();
23}
24
25//______________________________________________________________________________
26ListPhotonsInAtmosphere::ListPhotonsInAtmosphere(const ListPhotonsInAtmosphere& o) : PhotonsInAtmosphere(o) {
27    //
28    // cpy ctor
29    //
30    Copy(o);
31    fNbNotSaved = o.GetNbNotSaved();
32    fCountS = 0;
33    fCountB = 0;
34}
35
36//______________________________________________________________________________
37ListPhotonsInAtmosphere::~ListPhotonsInAtmosphere() {
38    //
39    // dtor
40    //
41    Reset();
42}
43
44//______________________________________________________________________________
45Bool_t ListPhotonsInAtmosphere::ClearMemory() {
46    //
47    //
48    //
49   
50    Reset(); 
51
52    // force the buffer shrinking
53    vector<SinglePhoton*> emptySingle;
54    emptySingle.swap(fListSingle);
55
56    vector<BunchOfPhotons*> emptyBunch;
57    emptyBunch.swap(fListBunch);
58
59    vector<EarthVector> emptyEV;
60    emptyEV.swap(fTrack);
61
62    return kTRUE;
63}
64
65//______________________________________________________________________________
66SinglePhoton* ListPhotonsInAtmosphere::GetSingle() const {
67    //
68    // Extract one photon from the list
69    //
70    if(fCountS == fListSingle.size()) 
71        return 0;
72   
73    return fListSingle[fCountS++];
74}
75
76//______________________________________________________________________________
77BunchOfPhotons* ListPhotonsInAtmosphere::GetBunch() const {
78    //
79    // Extract one Bunch of photons from the list
80    //
81    if(fCountB == fListBunch.size()) {
82       return 0;
83    }
84    return fListBunch[fCountB++]; 
85}
86
87//______________________________________________________________________________
88BunchOfPhotons* ListPhotonsInAtmosphere::GetBunch(size_t i) const {
89    //
90    // get a Bunch of photons from the list
91    //
92    if(i < fListBunch.size()) return fListBunch[i];
93    else return 0;
94}
95
96//______________________________________________________________________________
97void ListPhotonsInAtmosphere::Add(SinglePhoton* p, Bool_t saved) {
98    //
99    // add photon to the lists
100    // if saved true, means photon is already saved into root
101    //
102    if(p) {
103        fListSingle.push_back(p);
104        fNbNotSaved++;
105    }
106    else Msg(EsafMsg::Warning) <<"Trying to add NULL SinglePhoton in ListPhotonsInAtmosphere" << MsgDispatch;
107}
108
109//______________________________________________________________________________
110void ListPhotonsInAtmosphere::Add(BunchOfPhotons* p) {
111    //
112    // Add bunch of photons to the lists
113    //
114
115    if(p) fListBunch.push_back(p);
116    else Msg(EsafMsg::Warning) <<"Trying to add NULL BunchOfPhotons in ListPhotonsInAtmosphere" << MsgDispatch;
117}
118
119//______________________________________________________________________________
120void ListPhotonsInAtmosphere::Add(vector<SinglePhoton*>& list, Bool_t saved) {
121    //
122    // Add a list of SinglePhoton to the list of singles
123    // IMPORTANT : "list" is then destroyed to avoid wrong manipulation of pointers
124    // if saved true, means SinglePhotons are already saved into root
125    //
126    for(size_t i=0; i<list.size(); i++) {
127        if(!list[i]) continue;
128        fListSingle.push_back(list[i]);
129        if(!saved) fNbNotSaved++;
130    }
131    list.clear();
132}
133
134//______________________________________________________________________________
135void ListPhotonsInAtmosphere::Add(ListPhotonsInAtmosphere& list, Bool_t saved) {
136    //
137    // Add the elements of the given list
138    // IMPORTANT : "list" is then destroyed to avoid wrong manipulation of pointers
139    // if saved true, means SinglePhotons are already saved into root
140    //
141    for(size_t i=0; i<list.GetListOfBunch().size(); i++)
142        fListBunch.push_back(list.GetListOfBunch()[i]);
143
144    for(size_t i=0; i<list.GetListOfSingle().size(); i++) {
145        fListSingle.push_back(list.GetListOfSingle()[i]);
146        if(!saved) fNbNotSaved++;
147    }
148    list.Clear();
149}
150
151//______________________________________________________________________________
152void ListPhotonsInAtmosphere::Copy(const vector<SinglePhoton*>& list, Bool_t saved) {
153    //
154    // Same as the matching Add() method, BUT copies of pointers hold by list
155    // are created, instead of storing the same pointers
156    //
157    SinglePhoton* s = 0;
158    for(size_t i=0; i<list.size(); i++) {
159        s = new SinglePhoton(*list[i]);
160        fListSingle.push_back(s);
161        if(!saved) fNbNotSaved++;
162    }
163}
164
165
166
167//______________________________________________________________________________
168void ListPhotonsInAtmosphere::Copy(const ListPhotonsInAtmosphere& list, Bool_t saved) {
169    //
170    // Same as the matching Add() method, BUT copies of list's SinglePhoton and
171    // BunchOfPhotons are created, instead of storing the pointers hold by
172    // list, so that the internal vectors of list are not destroyed
173    //
174    SinglePhoton* s = 0;
175    BunchOfPhotons* b = 0;
176    for(size_t i=0; i<list.GetListOfBunch().size(); i++) {
177        b = new BunchOfPhotons(*(list.GetListOfBunch()[i]));
178        fListBunch.push_back(b);
179    }
180
181    for(size_t i=0; i<list.GetListOfSingle().size(); i++) {
182        s =  new SinglePhoton(*(list.GetListOfSingle()[i]));
183        fListSingle.push_back(s);
184        if(!saved) fNbNotSaved++;
185    }
186   
187    if(list.GetNbTrackSteps()) {
188        fTrack.reserve(list.GetNbTrackSteps());
189        for(UInt_t i = 0; i<list.GetNbTrackSteps(); i++) fTrack[i] = list.GetTrackStep(i);
190    }
191    else ClearTrack();
192}
193
194//______________________________________________________________________________
195void ListPhotonsInAtmosphere::Clear(Int_t t) {
196    //
197    // Clear the two lists
198    // - t = 0       both list
199    // - t = 1       singles not deleted
200    // - t = 2       bunches not deleted
201    //
202    if(t < 0 || t > 2) Msg(EsafMsg::Warning) <<"<Clear> wrong t value, list not cleared" << MsgDispatch;
203    if(t != 1) fListSingle.clear();
204    if(t != 2) fListBunch.clear();
205    fNbNotSaved = 0;
206    fCountS = 0;
207    fCountB = 0;
208    fBeforeTrackstart = 0;
209}
210
211//______________________________________________________________________________
212void ListPhotonsInAtmosphere::Reset(Int_t t) {
213    //
214    // Delete photons in "vector" lists
215    // - t = 0       both list, fTrack deleted
216    // - t = 1       singles not deleted
217    // - t = 2       bunches and fTrack not deleted
218    //
219    if(t < 0 || t > 2) Msg(EsafMsg::Warning) <<"<Reset> wrong t value, list not reset" << MsgDispatch;
220    SinglePhoton* p = 0;
221    if(t != 1) {
222        for(size_t i=0; i<fListSingle.size(); i++) {
223            p = fListSingle[i];
224            SafeDelete(p);
225        }
226        fListSingle.clear();
227        if(fNbNotSaved) cout<<"[Warning] <ListPhotonsInAtmosphere::Reset>  List contains not saved SinglePhotons\n";
228        fNbNotSaved = 0;
229        fCountS = 0;
230    }
231
232    BunchOfPhotons* bp = 0;
233    if(t != 2) {
234        for(size_t i=0; i<fListBunch.size(); i++) {
235            if(!i) fListBunch[0]->Reset();   // to reset static data members
236            bp = fListBunch[i];
237            SafeDelete(bp);
238        }
239        fListBunch.clear();
240        fCountB = 0;
241        ClearTrack();
242    }
243}
244
245//_____________________________________________________________________________
246UInt_t ListPhotonsInAtmosphere::TrackStepIndex(const EarthVector& pos) const {
247    //
248    // Return the fTrack step index that matches pos projection on the track
249    //
250    EarthVector first = fTrack[0];
251    EarthVector last = fTrack[fTrackNbSteps-1];
252   
253    Double_t mag = (pos-first).Dot(last-first)/(last-first).Mag();
254    if(mag < 0) {
255        fBeforeTrackstart++;
256        Msg(EsafMsg::Debug) <<"<TrackStepIndex> mag<0 = "<<mag<<" -> put to zero, ("<<fBeforeTrackstart<<" photons)" << MsgDispatch;
257        mag = 0;
258    }
259   
260    UInt_t nabove, nbelow, middle;
261    nabove = fTrackNbSteps+1;
262    nbelow = 0;
263    while(nabove-nbelow > 1) {
264       middle = (nabove+nbelow)/2;
265       if (mag == (fTrack[middle-1]-first).Mag()) return middle-1;
266       if (mag  < (fTrack[middle-1]-first).Mag()) nabove = middle;
267       else nbelow = middle;
268    }
269    return nbelow-1;
270}
271
272//_____________________________________________________________________________
273UInt_t ListPhotonsInAtmosphere::TrackStepIndex(const EarthVector& pos, EarthVector& rtn) const {
274    //
275    // Same as above + returns pos projection along the track
276    //
277    EarthVector first = fTrack[0];
278    EarthVector last = fTrack[fTrackNbSteps-1];
279    Double_t mag = (pos-first).Dot(last-first)/(last-first).Mag();
280    if(mag < 0) {
281        fBeforeTrackstart++;
282        Msg(EsafMsg::Debug) <<"<TrackStepIndex> mag<0 = "<<mag<<" -> put to zero, ("<<fBeforeTrackstart<<" photons)" << MsgDispatch;
283        mag = 0;
284    }
285    rtn = (mag/(last-first).Mag()) * (last-first) + first;
286   
287    UInt_t nabove, nbelow, middle;
288    nabove = fTrackNbSteps+1;
289    nbelow = 0;
290    while(nabove-nbelow > 1) {
291       middle = (nabove+nbelow)/2;
292       if (mag == (fTrack[middle-1]-first).Mag()) return middle-1;
293       if (mag  < (fTrack[middle-1]-first).Mag()) nabove = middle;
294       else nbelow = middle;
295    }
296    return nbelow-1;
297}
Note: See TracBrowser for help on using the repository browser.