3dd14e20 |
1 | /************************************************************************** |
2 | * Copyright(c) 1998-2007, ALICE Experiment at CERN, All rights reserved. * |
3 | * * |
4 | * Author: The ALICE Off-line Project. * |
5 | * Contributors are mentioned in the code where appropriate. * |
6 | * * |
7 | * Permission to use, copy, modify and distribute this software and its * |
8 | * documentation strictly for non-commercial purposes is hereby granted * |
9 | * without fee, provided that the above copyright notice appears in all * |
10 | * copies and that both the copyright notice and this permission notice * |
11 | * appear in the supporting documentation. The authors make no claims * |
12 | * about the suitability of this software for any purpose. It is * |
13 | * provided "as is" without express or implied warranty. * |
14 | **************************************************************************/ |
15 | |
16 | /* $Id$ */ |
17 | |
18 | /** |
19 | * @file AliHLTMUONRecHitsSource.cxx |
20 | * @author Artur Szostak <artursz@iafrica.com> |
21 | * @date |
22 | * @brief Implementation of the AliHLTMUONRecHitsSource component. |
23 | */ |
24 | |
25 | #include "AliHLTMUONRecHitsSource.h" |
26 | #include "AliHLTMUONConstants.h" |
27 | #include "AliHLTMUONDataBlockWriter.h" |
28 | #include "AliMUONSimData.h" |
29 | #include "AliMUONRecData.h" |
30 | #include "AliMUONHit.h" |
31 | #include "AliMUONRawCluster.h" |
32 | #include "AliMUONConstants.h" |
33 | #include "AliRunLoader.h" |
34 | #include "AliLoader.h" |
35 | #include "TClonesArray.h" |
36 | #include <cstdlib> |
37 | #include <cstdio> |
38 | #include <cerrno> |
39 | #include <cassert> |
40 | #include <new> |
41 | |
42 | #include <iostream> |
43 | using namespace std; |
44 | |
45 | |
46 | //namespace |
47 | //{ |
48 | // The global object used for automatic component registration. |
49 | // Note DO NOT use this component for calculation! |
50 | AliHLTMUONRecHitsSource gAliHLTMUONRecHitsSource; |
51 | //} |
52 | |
53 | |
54 | ClassImp(AliHLTMUONRecHitsSource); |
55 | |
56 | |
57 | AliHLTMUONRecHitsSource::AliHLTMUONRecHitsSource() : |
58 | AliHLTOfflineDataSource(), |
59 | fSimData(NULL), fRecData(NULL), |
60 | fRunLoader(NULL), fLoader(NULL) |
61 | { |
62 | } |
63 | |
64 | AliHLTMUONRecHitsSource::~AliHLTMUONRecHitsSource() |
65 | { |
66 | } |
67 | |
68 | |
69 | int AliHLTMUONRecHitsSource::DoInit(int argc, const char** argv) |
70 | { |
71 | // Parse the command line arguments: |
72 | bool simdata = false; |
73 | bool recdata = false; |
74 | int i = 0; |
75 | while (i < argc) |
76 | { |
77 | if (strcmp(argv[i], "-simdata") != 0) |
78 | simdata = true; |
79 | else if (strcmp(argv[i], "-recdata") != 0) |
80 | recdata = true; |
81 | else |
82 | { |
83 | Logging(kHLTLogError, |
84 | "AliHLTMUONRecHitsSource::DoInit", |
85 | "Unknown argument", |
86 | "The argument '%s' is invalid.", |
87 | argv[i] |
88 | ); |
89 | return EINVAL; |
90 | } |
91 | } |
92 | |
93 | // Check the parameters we have parsed. |
94 | if (simdata and recdata) |
95 | { |
96 | Logging(kHLTLogError, |
97 | "AliHLTMUONRecHitsSource::DoInit", |
98 | "Invalid arguments", |
99 | "Cannot have both -simdata and -recdata set." |
100 | ); |
101 | return EINVAL; |
102 | } |
103 | |
104 | if (not simdata and not recdata) |
105 | { |
106 | Logging(kHLTLogError, |
107 | "AliHLTMUONRecHitsSource::DoInit", |
108 | "Missing arguments", |
109 | "Must have either -simdata or -recdata specified." |
110 | ); |
111 | return EINVAL; |
112 | } |
113 | |
114 | // Now we can initialise the data interface objects and loaders. |
115 | if (simdata) |
116 | { |
117 | try |
118 | { |
119 | fSimData = new AliMUONSimData("galice.root"); |
120 | } |
121 | catch (const std::bad_alloc&) |
122 | { |
123 | Logging(kHLTLogError, |
124 | "AliHLTMUONRecHitsSource::DoInit", |
125 | "Out of memory", |
126 | "Not enough memory to allocate AliMUONSimData." |
127 | ); |
128 | return ENOMEM; |
129 | } |
130 | fLoader = fSimData->GetLoader(); |
131 | fLoader->LoadHits("READ"); |
132 | } |
133 | else if (recdata) |
134 | { |
135 | try |
136 | { |
137 | fRecData = new AliMUONRecData("galice.root"); |
138 | } |
139 | catch (const std::bad_alloc&) |
140 | { |
141 | Logging(kHLTLogError, |
142 | "AliHLTMUONRecHitsSource::DoInit", |
143 | "Out of memory", |
144 | "Not enough memory to allocate AliMUONRecData." |
145 | ); |
146 | return ENOMEM; |
147 | } |
148 | fLoader = fRecData->GetLoader(); |
149 | fLoader->LoadRecPoints("READ"); |
150 | } |
151 | |
152 | fRunLoader = AliRunLoader::GetRunLoader(); |
153 | |
154 | return 0; |
155 | } |
156 | |
157 | |
158 | int AliHLTMUONRecHitsSource::DoDeinit() |
159 | { |
160 | if (fSimData != NULL) |
161 | { |
162 | fLoader->UnloadHits(); |
163 | delete fSimData; |
164 | fSimData = NULL; |
165 | } |
166 | if (fRecData != NULL) |
167 | { |
168 | fLoader->UnloadRecPoints(); |
169 | delete fRecData; |
170 | fRecData = NULL; |
171 | } |
172 | fRunLoader = NULL; |
173 | fLoader = NULL; |
174 | return 0; |
175 | } |
176 | |
177 | |
178 | const char* AliHLTMUONRecHitsSource::GetComponentID() |
179 | { |
180 | return AliHLTMUONConstants::RecHitsSourceId(); |
181 | } |
182 | |
183 | AliHLTComponentDataType AliHLTMUONRecHitsSource::GetOutputDataType() |
184 | { |
185 | return AliHLTMUONConstants::RecHitsBlockDataType(); |
186 | } |
187 | |
188 | void AliHLTMUONRecHitsSource::GetOutputDataSize( |
189 | unsigned long& constBase, double& inputMultiplier |
190 | ) |
191 | { |
192 | constBase = sizeof(AliHLTMUONRecHitsBlockStruct); |
193 | inputMultiplier = 1; |
194 | } |
195 | |
196 | AliHLTComponent* AliHLTMUONRecHitsSource::Spawn() |
197 | { |
198 | return new AliHLTMUONRecHitsSource(); |
199 | } |
200 | |
201 | |
202 | int AliHLTMUONRecHitsSource::GetEvent( |
203 | const AliHLTComponentEventData& evtData, |
204 | AliHLTComponentTriggerData& trigData, |
205 | AliHLTUInt8_t* outputPtr, |
206 | AliHLTUInt32_t& size, |
207 | vector<AliHLTComponentBlockData>& outputBlocks |
208 | ) |
209 | { |
210 | assert( fSimData != NULL or fRecData != NULL ); |
211 | assert( fRunLoader != NULL ); |
212 | assert( fLoader != NULL ); |
213 | |
214 | // Check the size of the event descriptor structure. |
215 | if (evtData.fStructSize < sizeof(AliHLTComponentEventData)) |
216 | { |
217 | Logging(kHLTLogError, |
218 | "AliHLTMUONRecHitsSource::GetEvent", |
219 | "Invalid event descriptor", |
220 | "The event descriptor (AliHLTComponentEventData) size is" |
221 | " smaller than expected. It claims to be %d bytes, but" |
222 | " we expect it to be %d bytes.", |
223 | evtData.fStructSize, |
224 | sizeof(AliHLTComponentEventData) |
225 | ); |
226 | size = 0; // Important to tell framework that nothing was generated. |
227 | return EINVAL; |
228 | } |
229 | |
230 | // Use the fEventID as the event number to load, check it and load that |
231 | // event with the runloader. |
232 | Int_t eventnumber = Int_t(evtData.fEventID); |
233 | if (eventnumber >= fRunLoader->GetNumberOfEvents()) |
234 | { |
235 | Logging(kHLTLogError, |
236 | "AliHLTMUONRecHitsSource::GetEvent", |
237 | "Bad event ID", |
238 | "The event number (%d) is larger than the available number" |
239 | " of events on file (%d).", |
240 | eventnumber, |
241 | fRunLoader->GetNumberOfEvents() |
242 | ); |
243 | size = 0; // Important to tell framework that nothing was generated. |
244 | return EINVAL; |
245 | } |
246 | fRunLoader->GetEvent(eventnumber); |
247 | |
248 | // Create and initialise a new data block. |
249 | AliHLTMUONRecHitsBlockWriter block(outputPtr, size); |
250 | if (not block.InitCommonHeader()) |
251 | { |
252 | Logging(kHLTLogError, |
253 | "AliHLTMUONRecHitsSource::GetEvent", |
254 | "Buffer too small", |
255 | "There is not enough buffer space to create a new data block." |
256 | " We require at least %d bytes but the buffer is only %d bytes.", |
257 | sizeof(AliHLTMUONRecHitsBlockWriter::HeaderType), |
258 | block.BufferSize() |
259 | ); |
260 | size = 0; // Important to tell framework that nothing was generated. |
261 | return ENOBUFS; |
262 | } |
263 | |
264 | if (fSimData != NULL) |
265 | { |
266 | // Loop over all tracks, extract the hits and write them to the |
267 | // data block. |
268 | fSimData->SetTreeAddress("H"); |
269 | for (Int_t i = 0; i < fSimData->GetNtracks(); i++) |
270 | { |
271 | fSimData->GetTrack(i); |
272 | assert( fSimData->Hits() != NULL ); |
273 | Int_t nhits = fSimData->Hits()->GetEntriesFast(); |
274 | for (Int_t j = 0; j < nhits; j++) |
275 | { |
276 | AliMUONHit* hit = static_cast<AliMUONHit*>( |
277 | fSimData->Hits()->At(j) |
278 | ); |
279 | hit->Print(); |
280 | |
281 | // Select only hits on a certain chamber. |
282 | if (hit->Chamber() != 7) continue; |
283 | |
284 | AliHLTMUONRecHitStruct* rechit = block.AddEntry(); |
285 | if (rechit == NULL) |
286 | { |
287 | Logging(kHLTLogError, |
288 | "AliHLTMUONRecHitsSource::GetEvent", |
289 | "Buffer overflow", |
290 | "There is not enough buffer space to add more hits." |
291 | " We overflowed the buffer which is only %d bytes.", |
292 | block.BufferSize() |
293 | ); |
294 | fSimData->ResetHits(); |
295 | size = 0; // Important to tell framework that nothing was generated. |
296 | return ENOBUFS; |
297 | } |
298 | |
299 | rechit->fX = hit->Xref(); |
300 | rechit->fY = hit->Yref(); |
301 | rechit->fZ = hit->Zref(); |
302 | } |
303 | fSimData->ResetHits(); |
304 | } |
305 | } |
306 | else if (fRecData != NULL) |
307 | { |
308 | //Int_t nchambers = AliMUONConstants::NTrackingCh(); |
309 | fRecData->SetTreeAddress("RC,TC"); |
310 | fRecData->GetRawClusters(); |
311 | |
312 | // Select a specific chamber. |
313 | Int_t chamber = 7; |
314 | char branchname[32]; |
315 | sprintf(branchname, "MUONRawClusters%d", chamber); |
316 | |
317 | TClonesArray* clusterarray = fRecData->RawClusters(chamber); |
318 | Int_t nrecpoints = clusterarray->GetEntriesFast(); |
319 | for (Int_t i = 0; i < nrecpoints; i++) |
320 | { |
321 | AliMUONRawCluster* cluster = static_cast<AliMUONRawCluster*>(clusterarray->At(i)); |
322 | cluster->GetX(); |
323 | |
324 | AliHLTMUONRecHitStruct* rechit = block.AddEntry(); |
325 | if (rechit == NULL) |
326 | { |
327 | Logging(kHLTLogError, |
328 | "AliHLTMUONRecHitsSource::GetEvent", |
329 | "Buffer overflow", |
330 | "There is not enough buffer space to add more hits." |
331 | " We overflowed the buffer which is only %d bytes.", |
332 | block.BufferSize() |
333 | ); |
334 | fRecData->ResetRawClusters(); |
335 | size = 0; // Important to tell framework that nothing was generated. |
336 | return ENOBUFS; |
337 | } |
338 | |
339 | rechit->fX = cluster->GetX(); |
340 | rechit->fY = cluster->GetY(); |
341 | rechit->fZ = cluster->GetZ(); |
342 | } |
343 | |
344 | fRecData->ResetRawClusters(); |
345 | } |
346 | else |
347 | { |
348 | Logging(kHLTLogError, |
349 | "AliHLTMUONRecHitsSource::GetEvent", |
350 | "Missing data interface", |
351 | "Neither AliMUONSimData or AliMUONRecData were created." |
352 | ); |
353 | size = 0; // Important to tell framework that nothing was generated. |
354 | return EFAULT; |
355 | } |
356 | |
357 | return 0; |
358 | } |