1 | // $Id: PhPToRootFileDetector.cc 2604 2006-03-20 21:51:35Z thea $ |
---|
2 | // Author: Alessandro Thea 2005/02/13 |
---|
3 | |
---|
4 | /***************************************************************************** |
---|
5 | * ESAF: Euso Simulation and Analysis Framework * |
---|
6 | * * |
---|
7 | * Id: PhPToRootFileDetector * |
---|
8 | * Package: <packagename> * |
---|
9 | * Coordinator: <coordinator> * |
---|
10 | * * |
---|
11 | *****************************************************************************/ |
---|
12 | |
---|
13 | //_____________________________________________________________________________ |
---|
14 | // |
---|
15 | // PhPToRootFileDetector |
---|
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 "PhPToRootFileDetector.hh" |
---|
27 | #include "EEvent.hh" |
---|
28 | #include "EPhoton.hh" |
---|
29 | #include "ETruth.hh" |
---|
30 | #include "EGeometry.hh" |
---|
31 | #include "EEventGeoFiller.hh" |
---|
32 | #include "DetectorGeometry.hh" |
---|
33 | #include "Photon.hh" |
---|
34 | #include "ParentPhoton.hh" |
---|
35 | #include "PhotonsOnPupil.hh" |
---|
36 | |
---|
37 | #include <TFile.h> |
---|
38 | #include <TTree.h> |
---|
39 | #include <TClonesArray.h> |
---|
40 | #include <TSystem.h> |
---|
41 | |
---|
42 | // DEBUG |
---|
43 | // #include "stdio.h" |
---|
44 | // DEBUG |
---|
45 | |
---|
46 | const UInt_t PhPToRootFileDetector::fgStartBufferSize = 10000; |
---|
47 | |
---|
48 | ClassImp(PhPToRootFileDetector) |
---|
49 | |
---|
50 | //_____________________________________________________________________________ |
---|
51 | PhPToRootFileDetector::PhPToRootFileDetector() : fNumPhotons(0) { |
---|
52 | // |
---|
53 | // Constructor |
---|
54 | // |
---|
55 | |
---|
56 | fFileName = Conf()->GetStr("PhPToRootFileDetector.fFileName"); |
---|
57 | |
---|
58 | fRootTruth = new ETruth(); |
---|
59 | fRootGeometry = new EGeometry(); |
---|
60 | |
---|
61 | fPhotons = new TClonesArray("EPhoton",fgStartBufferSize); |
---|
62 | fGeometry = new DetectorGeometry(); |
---|
63 | |
---|
64 | Double_t radius = Conf()->GetNum("PhPToRootFileDetector.fRadius"); |
---|
65 | Double_t fov = Conf()->GetNum("PhPToRootFileDetector.fFoV"); |
---|
66 | |
---|
67 | fGeometry->SetRadius(radius); |
---|
68 | fGeometry->SetFoV(fov); |
---|
69 | |
---|
70 | |
---|
71 | Initialize(); |
---|
72 | } |
---|
73 | |
---|
74 | //_____________________________________________________________________________ |
---|
75 | PhPToRootFileDetector::~PhPToRootFileDetector() { |
---|
76 | // |
---|
77 | // Destructor |
---|
78 | // |
---|
79 | |
---|
80 | Finalize(); |
---|
81 | |
---|
82 | SafeDelete( fRootTruth ); |
---|
83 | SafeDelete( fRootGeometry ); |
---|
84 | SafeDelete( fPhotons ); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | //______________________________________________________________________________ |
---|
89 | Telemetry* PhPToRootFileDetector::Get( PhotonsOnPupil* pupil ) { |
---|
90 | // |
---|
91 | // Saves photons on fFileName |
---|
92 | // |
---|
93 | |
---|
94 | if ( !pupil ) { |
---|
95 | MsgForm(EsafMsg::Warning,"PhotonsOnPupil is NULL. Skipping."); |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | EEvent* ev = EEvent::GetCurrent(); |
---|
100 | |
---|
101 | // save the truth in the php file |
---|
102 | if ( ev && ev->GetTruth() ) |
---|
103 | ev->GetTruth()->Copy(*fRootTruth); |
---|
104 | |
---|
105 | |
---|
106 | EEventGeoFiller a( fGeometry ); |
---|
107 | a.Fill(fRootGeometry); |
---|
108 | |
---|
109 | // DEBUG |
---|
110 | // FILE* flog = fopen("php-in.log","w"); |
---|
111 | // DEBUG |
---|
112 | fNumPhotons = 0; |
---|
113 | while( Photon* p = pupil->GetPhoton() ) { |
---|
114 | EPhoton* eph = new ((*fPhotons)[fNumPhotons++]) EPhoton(); |
---|
115 | |
---|
116 | eph->SetType( p->parent->Type() ); |
---|
117 | eph->SetTheta( p->dir.Theta() ); |
---|
118 | eph->SetPhi( p->dir.Phi() ); |
---|
119 | eph->SetLambda( p->wl ); |
---|
120 | eph->SetTime( p->time ); |
---|
121 | eph->SetPos( p->pos ); |
---|
122 | // DEBUG |
---|
123 | // fprintf(flog,"%d %.3f %.3f %.3f %.3f %.3f %.3f %.3f\n", |
---|
124 | // p->parent->Type(), p->dir.Theta()/deg, p->dir.Phi()/deg, p->wl/nm, p->time/ns, |
---|
125 | // p->pos.X(), p->pos.Y(), p->pos.Z()); |
---|
126 | // DEBUG |
---|
127 | } |
---|
128 | // DEBUG |
---|
129 | // fclose(flog); |
---|
130 | // DEBUG |
---|
131 | |
---|
132 | MsgForm(EsafMsg::Info,"%d photons read from PhotonOnPuil, %d saved on phproot file", |
---|
133 | fNumPhotons, fPhotons->GetEntries()); |
---|
134 | fTree->Fill(); |
---|
135 | |
---|
136 | return 0; |
---|
137 | } |
---|
138 | |
---|
139 | //______________________________________________________________________________ |
---|
140 | void PhPToRootFileDetector::Initialize() { |
---|
141 | // |
---|
142 | // Open file, create the tree and set the branches |
---|
143 | // |
---|
144 | |
---|
145 | |
---|
146 | string ext = ".php.root"; |
---|
147 | if (fFileName.find(ext) != (fFileName.size()-ext.size())) |
---|
148 | fFileName += ext; |
---|
149 | |
---|
150 | if ( !gSystem->AccessPathName(fFileName.c_str()) ) { |
---|
151 | |
---|
152 | MsgForm(EsafMsg::Warning,"Error opening file"); |
---|
153 | MsgForm(EsafMsg::Warning,"Probably it already exists or there is no "); |
---|
154 | MsgForm(EsafMsg::Warning,"write access permission to this directory"); |
---|
155 | |
---|
156 | |
---|
157 | //if (fFileName.find(ext) != (fFileName.size()-8)) |
---|
158 | fFileName.resize(fFileName.size()-ext.size()); |
---|
159 | |
---|
160 | time_t t = time(NULL); |
---|
161 | char buffer[64]; |
---|
162 | strftime(buffer, 64,".%d-%b-%G-%Hh%Mm%Ss", localtime(&t)); |
---|
163 | fFileName += buffer; |
---|
164 | fFileName += ext; |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | TDirectory* olddir = gDirectory; |
---|
169 | |
---|
170 | fFile = new TFile(fFileName.c_str(),"CREATE"); |
---|
171 | fFile->cd(); |
---|
172 | if (fFile->IsZombie()) |
---|
173 | FatalError("Cannot open root file"); |
---|
174 | |
---|
175 | MsgForm(EsafMsg::Info,"PhPRoot file %s successfully opened",fFileName.c_str()); |
---|
176 | |
---|
177 | fTree = new TTree("PhPTree","Photons OnPupil"); |
---|
178 | fTree->Branch("ETruth","ETruth",&fRootTruth); |
---|
179 | fTree->Branch("EGeometry","EGeometry",&fRootGeometry); |
---|
180 | fTree->Branch("EPhotons","TClonesArray",&fPhotons); |
---|
181 | fTree->Branch("ENumPhotons",&fNumPhotons,"ENumPhotons/I"); |
---|
182 | |
---|
183 | if ( olddir ) olddir->cd(); |
---|
184 | } |
---|
185 | |
---|
186 | //______________________________________________________________________________ |
---|
187 | void PhPToRootFileDetector::Finalize() { |
---|
188 | // |
---|
189 | // Write the tree on file and close the file |
---|
190 | // |
---|
191 | |
---|
192 | fFile->Write(); |
---|
193 | fFile->Close(); |
---|
194 | MsgForm(EsafMsg::Info,"%s closed.",fFile->GetName()); |
---|
195 | |
---|
196 | SafeDelete(fFile); |
---|
197 | |
---|
198 | } |
---|
199 | |
---|
200 | //______________________________________________________________________________ |
---|
201 | void PhPToRootFileDetector::Reset() { |
---|
202 | // |
---|
203 | // |
---|
204 | // |
---|
205 | |
---|
206 | fPhotons->Clear(); |
---|
207 | fRootTruth->Clear(); |
---|
208 | fRootGeometry->Clear(); |
---|
209 | fNumPhotons = 0; |
---|
210 | } |
---|
211 | |
---|
212 | //______________________________________________________________________________ |
---|
213 | Bool_t PhPToRootFileDetector::ClearMemory() { |
---|
214 | // |
---|
215 | // Shrink the vectors to the standard size |
---|
216 | // |
---|
217 | |
---|
218 | Reset(); |
---|
219 | fPhotons->Expand(fgStartBufferSize); |
---|
220 | |
---|
221 | return kTRUE; |
---|
222 | } |
---|