]>
Commit | Line | Data |
---|---|---|
1 | /************************************************************************** | |
2 | * Copyright(c) 1998-1999, 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 | #include "AliMUONPedestalEventGenerator.h" | |
19 | ||
20 | #include "AliHeader.h" | |
21 | #include "AliLog.h" | |
22 | #include "AliMUONCalibrationData.h" | |
23 | #include "AliMUONDigitStoreV1.h" | |
24 | #include "AliMUONRawWriter.h" | |
25 | #include "AliMUONStopwatchGroup.h" | |
26 | #include "AliMUONStopwatchGroupElement.h" | |
27 | #include "AliMUONVCalibParam.h" | |
28 | #include "AliMUONVDigit.h" | |
29 | #include "AliMUONVStore.h" | |
30 | #include "AliMpCathodType.h" | |
31 | #include "AliMpConstants.h" | |
32 | #include "AliMpDEStore.h" | |
33 | #include "AliMpDetElement.h" | |
34 | #include "AliMpIntPair.h" | |
35 | #include "AliMpPlaneType.h" | |
36 | #include "AliRunLoader.h" | |
37 | #include <TClonesArray.h> | |
38 | #include <TMath.h> | |
39 | #include <TROOT.h> | |
40 | #include <TRandom.h> | |
41 | #include <TStopwatch.h> | |
42 | #include <TSystem.h> | |
43 | #include "AliDAQ.h" | |
44 | ||
45 | /// | |
46 | /// \class AliMUONPedestalEventGenerator | |
47 | /// | |
48 | /// Generate simulated pedestal events for MUON TRK, to be able to e.g. test | |
49 | /// online calibration routines. | |
50 | /// | |
51 | /// The pedestals themselves are taken from the CDB. What we get from the CDB | |
52 | /// is, per channel, the mean and the sigma of the pedestal. We then use | |
53 | /// those informations to randomly get the pedestals for each channel, for | |
54 | /// each event (picking in a gaus(mean,sigma)). | |
55 | /// | |
56 | /// Output can be just digits, or digits + raw (ddl), or digits + raw (ddl) | |
57 | /// + raw (date files, one per LDC), depending of ctor and MakeDDL() method. | |
58 | /// | |
59 | /// \author L. Aphecetche | |
60 | /// | |
61 | ||
62 | /// \cond CLASSIMP | |
63 | ClassImp(AliMUONPedestalEventGenerator) | |
64 | /// \endcond | |
65 | ||
66 | Int_t AliMUONPedestalEventGenerator::fgCounter(0); | |
67 | ||
68 | //std::streambuf* RedirectTo(std::ostream& what, std::ostream& to) | |
69 | //{ | |
70 | // std::streambuf* old = what.rdbuf(); | |
71 | // | |
72 | // std::streambuf* psbuf = to.rdbuf(); | |
73 | // what.rdbuf(psbuf); | |
74 | // | |
75 | // return old; | |
76 | //} | |
77 | ||
78 | //_____________________________________________________________________________ | |
79 | AliMUONPedestalEventGenerator::AliMUONPedestalEventGenerator(Int_t runNumber, | |
80 | Int_t nevents, | |
81 | const char* filename) | |
82 | : TTask("AliMUONPedestalEventGenerator","Generate fake pedestal events"), | |
83 | fCalibrationData(new AliMUONCalibrationData(runNumber)), | |
84 | fDateFileName(filename), | |
85 | fGAliceFileName("galice.root"), | |
86 | fMakeDDL(kTRUE), | |
87 | fLoader(0x0), | |
88 | fPedestals(fCalibrationData->Pedestals()), | |
89 | fTimers(new AliMUONStopwatchGroup), | |
90 | fDigitStore(0x0), | |
91 | fRawWriter(0x0) | |
92 | { | |
93 | /// Will generate pedestals according to (mean,sigma)s found in CDB | |
94 | /// for run runNumber. | |
95 | /// Will generate nevents events | |
96 | /// If filename is != "", it will be the basename of the output LDC files | |
97 | /// | |
98 | if (!gSystem->IsAbsoluteFileName(fGAliceFileName)) | |
99 | { | |
100 | char* absFileName = gSystem->ConcatFileName(gSystem->WorkingDirectory(), | |
101 | fGAliceFileName); | |
102 | fGAliceFileName = absFileName; | |
103 | delete[] absFileName; | |
104 | } | |
105 | ||
106 | AliRunLoader* runLoader = LoadRun("recreate"); | |
107 | ||
108 | runLoader->SetNumberOfEventsPerFile(nevents); | |
109 | ||
110 | if (!runLoader) | |
111 | { | |
112 | AliError("Could not create RunLoader"); | |
113 | return; | |
114 | } | |
115 | ||
116 | // Initialize event headers. | |
117 | runLoader->MakeTree("E"); | |
118 | ||
119 | for ( Int_t iEvent = 0; iEvent < nevents; ++iEvent ) | |
120 | { | |
121 | runLoader->SetEventNumber(iEvent); | |
122 | runLoader->GetHeader()->Reset(runNumber,iEvent); | |
123 | runLoader->TreeE()->Fill(); | |
124 | } | |
125 | runLoader->WriteHeader("OVERWRITE"); | |
126 | runLoader->CdGAFile(); | |
127 | runLoader->Write(0, TObject::kOverwrite); | |
128 | ||
129 | delete runLoader; | |
130 | fLoader = 0x0; | |
131 | } | |
132 | ||
133 | ||
134 | //_____________________________________________________________________________ | |
135 | AliMUONPedestalEventGenerator::~AliMUONPedestalEventGenerator() | |
136 | { | |
137 | /// dtor | |
138 | delete fCalibrationData; | |
139 | AliInfo("Timers:"); | |
140 | fTimers->Print(); | |
141 | delete fTimers; | |
142 | AliInfo(Form("make a digit counter %d",fgCounter)); | |
143 | delete fDigitStore; | |
144 | delete fRawWriter; | |
145 | } | |
146 | ||
147 | //_____________________________________________________________________________ | |
148 | Bool_t | |
149 | AliMUONPedestalEventGenerator::ConvertRawFilesToDate() | |
150 | { | |
151 | /// convert raw data DDL files to DATE files with the program "dateStream". | |
152 | /// we make one file per LDC | |
153 | ||
154 | AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::ConvertRawFilesToDate"); | |
155 | ||
156 | AliInfo("Converting raw to date"); | |
157 | ||
158 | const Int_t kIDet = AliDAQ::DetectorID("MUONTRK"); | |
159 | ||
160 | const Int_t kNLDCs = TMath::CeilNint(AliDAQ::NumberOfLdcs(kIDet)); | |
161 | ||
162 | char* path = gSystem->Which(gSystem->Getenv("PATH"), "dateStream"); | |
163 | if (!path) | |
164 | { | |
165 | AliError("the program dateStream was not found"); | |
166 | return kFALSE; | |
167 | } | |
168 | ||
169 | delete[] path; | |
170 | ||
171 | AliRunLoader* runLoader = LoadRun("read"); | |
172 | if (!runLoader) return kFALSE; | |
173 | ||
174 | AliInfo(Form("converting raw data DDL files to DATE files %s", fDateFileName.Data())); | |
175 | FILE** pipe = new FILE*[kNLDCs]; | |
176 | ||
177 | for ( Int_t iFile = 0; iFile < kNLDCs; ++iFile) | |
178 | { | |
179 | char command[256]; | |
180 | // Note the option -s. It is used in order to avoid | |
181 | // the generation of SOR/EOR events. | |
182 | sprintf(command, "dateStream -s -D -o %s.LDC%d -# %d -C", | |
183 | fDateFileName.Data(), iFile, runLoader->GetNumberOfEvents()); | |
184 | pipe[iFile] = gSystem->OpenPipe(command, "w"); | |
185 | } | |
186 | ||
187 | for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); ++iEvent) | |
188 | { | |
189 | Float_t ldc = 0; | |
190 | Int_t prevLDC = -1; | |
191 | ||
192 | for (Int_t iDDL = 0; iDDL < AliDAQ::NumberOfDdls(kIDet); ++iDDL) | |
193 | { | |
194 | Int_t ddlID = AliDAQ::DdlID(kIDet,iDDL); | |
195 | Int_t ldcID = Int_t(ldc + 0.0001); | |
196 | ldc += AliDAQ::NumberOfLdcs(kIDet) / AliDAQ::NumberOfDdls(kIDet); | |
197 | ||
198 | char rawFileName[256]; | |
199 | sprintf(rawFileName, "raw%d/%s", | |
200 | iEvent, AliDAQ::DdlFileName(kIDet,iDDL)); | |
201 | ||
202 | // check existence and size of raw data file | |
203 | FILE* file = fopen(rawFileName, "rb"); | |
204 | if (!file) continue; | |
205 | fseek(file, 0, SEEK_END); | |
206 | unsigned long size = ftell(file); | |
207 | fclose(file); | |
208 | if (!size) continue; | |
209 | ||
210 | if (ldcID != prevLDC) { | |
211 | fprintf(pipe[ldcID], " LDC Id %d\n", ldcID); | |
212 | prevLDC = ldcID; | |
213 | } | |
214 | fprintf(pipe[ldcID], " Equipment Id %d Payload %s\n", ddlID, rawFileName); | |
215 | } | |
216 | } | |
217 | ||
218 | Int_t result(0); | |
219 | ||
220 | for ( Int_t iFile = 0; iFile < kNLDCs; ++iFile) | |
221 | { | |
222 | result += gSystem->ClosePipe(pipe[iFile]); | |
223 | } | |
224 | ||
225 | delete [] pipe; | |
226 | delete runLoader; | |
227 | fLoader=0x0; | |
228 | return (result == 0); | |
229 | } | |
230 | ||
231 | //_____________________________________________________________________________ | |
232 | AliMUONVDigitStore* | |
233 | AliMUONPedestalEventGenerator::DigitStore() | |
234 | { | |
235 | /// Return digt container; create it if it does not exist | |
236 | ||
237 | if (!fDigitStore) fDigitStore = new AliMUONDigitStoreV1; | |
238 | return fDigitStore; | |
239 | } | |
240 | ||
241 | //_____________________________________________________________________________ | |
242 | void | |
243 | AliMUONPedestalEventGenerator::Exec(Option_t*) | |
244 | { | |
245 | /// Main steering method | |
246 | ||
247 | AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::Exec"); | |
248 | ||
249 | if (!fPedestals) | |
250 | { | |
251 | AliError("No pedestal store. Cannot proceed."); | |
252 | return; | |
253 | } | |
254 | ||
255 | AliRunLoader* runLoader = LoadRun("update"); | |
256 | ||
257 | Int_t nevents = runLoader->GetNumberOfEvents(); | |
258 | ||
259 | for ( Int_t i = 0; i < nevents ; ++i ) | |
260 | { | |
261 | runLoader->GetEvent(i); | |
262 | ||
263 | fLoader->MakeDigitsContainer(); | |
264 | TTree* treeD = fLoader->TreeD(); | |
265 | if (!treeD) | |
266 | { | |
267 | AliError(Form("Could not get TreeD for event %d",i)); | |
268 | continue; | |
269 | } | |
270 | ||
271 | DigitStore()->Connect(*treeD); | |
272 | ||
273 | GenerateDigits(*(DigitStore())); | |
274 | ||
275 | // Fill the output treeD | |
276 | treeD->Fill(); | |
277 | ||
278 | // Write to the output tree(D). | |
279 | // Please note that as GlobalTrigger, LocalTrigger and Digits are in the same | |
280 | // tree (=TreeD) in different branches, this WriteDigits in fact writes all of | |
281 | // the 3 branches. | |
282 | ||
283 | fTimers->Start("MUON","AliMUONPedestalEventGenerator::Exec WriteDigits"); | |
284 | fLoader->WriteDigits("OVERWRITE"); | |
285 | fTimers->Stop("MUON","AliMUONPedestalEventGenerator::Exec WriteDigits"); | |
286 | ||
287 | fLoader->UnloadDigits(); | |
288 | ||
289 | if ( fMakeDDL ) | |
290 | { | |
291 | Digits2Raw(i); | |
292 | } | |
293 | ||
294 | // gROOT->ProcessLine(Form("gObjectTable->Print(); > generate.txt.%d",i)); | |
295 | } | |
296 | ||
297 | runLoader->WriteRunLoader("OVERWRITE"); | |
298 | delete runLoader; | |
299 | fLoader = 0x0; | |
300 | ||
301 | // Finally, if instructed to do so, convert DDL files to DATE file(s) | |
302 | if ( fMakeDDL && fDateFileName.Length() > 0 ) | |
303 | { | |
304 | Bool_t dateOutput = ConvertRawFilesToDate(); | |
305 | if (!dateOutput) | |
306 | { | |
307 | AliError("DATE output failed. Aborting."); | |
308 | return; | |
309 | } | |
310 | } | |
311 | } | |
312 | ||
313 | //_____________________________________________________________________________ | |
314 | void | |
315 | AliMUONPedestalEventGenerator::Digits2Raw(Int_t event) | |
316 | { | |
317 | /// Converts digits (from MUON.Digits.root file) to Raw DDL ascii files. | |
318 | ||
319 | AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::Digits2Raw"); | |
320 | ||
321 | if (!fRawWriter) fRawWriter = new AliMUONRawWriter; | |
322 | ||
323 | // Generate RAW data from the digits | |
324 | // Be carefull to create&change to the correct directory first... | |
325 | ||
326 | TString baseDir = gSystem->WorkingDirectory(); | |
327 | ||
328 | char dirName[256]; | |
329 | sprintf(dirName, "raw%d", event); | |
330 | gSystem->MakeDirectory(dirName); | |
331 | if (!gSystem->ChangeDirectory(dirName)) | |
332 | { | |
333 | AliError(Form("couldn't change to directory %s", dirName)); | |
334 | return; | |
335 | } | |
336 | ||
337 | fRawWriter->Digits2Raw(DigitStore(),0); | |
338 | ||
339 | gSystem->ChangeDirectory(baseDir); | |
340 | } | |
341 | ||
342 | //_____________________________________________________________________________ | |
343 | void | |
344 | AliMUONPedestalEventGenerator::GenerateDigits(AliMUONVDigitStore& digitStore) | |
345 | { | |
346 | /// Generate digits (where ADC is set to pedestal value) for all MUON TRK | |
347 | /// and for 1 event. | |
348 | ||
349 | AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::GenerateDigits(AliMUONVDigitStore&)"); | |
350 | ||
351 | digitStore.Clear(); | |
352 | ||
353 | Int_t ngenerated(0); | |
354 | Int_t nmanus(0); | |
355 | TIter next(fPedestals->CreateIterator()); | |
356 | AliMUONVCalibParam* pedestals; | |
357 | ||
358 | while ( ( pedestals = static_cast<AliMUONVCalibParam*>(next())) ) | |
359 | { | |
360 | Int_t detElemId = pedestals->ID0(); | |
361 | Int_t manuId = pedestals->ID1(); | |
362 | ||
363 | AliMpDetElement* de = AliMpDEStore::Instance()->GetDetElement(detElemId); | |
364 | AliMp::PlaneType planeType = AliMp::kBendingPlane; | |
365 | if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) ) | |
366 | { | |
367 | planeType = AliMp::kNonBendingPlane; | |
368 | } | |
369 | AliMp::CathodType cathode = de->GetCathodType(planeType); | |
370 | ||
371 | ++nmanus; | |
372 | ||
373 | for ( Int_t manuChannel = 0; manuChannel < pedestals->Size(); ++manuChannel ) | |
374 | { | |
375 | Float_t mean = pedestals->ValueAsFloat(manuChannel,0); | |
376 | if (mean == AliMUONVCalibParam::InvalidFloatValue()) | |
377 | { | |
378 | // This is a poor's man way of knowing if that channel really exists. | |
379 | // Better and safer way (but much slower too) would be to check pad existence | |
380 | // using AliMpVSegmentation::PadByLocation(AliMpIntPair(manuId,manuChannel)) | |
381 | continue; | |
382 | } | |
383 | else | |
384 | { | |
385 | Float_t sigma = pedestals->ValueAsFloat(manuChannel,1); | |
386 | ||
387 | ||
388 | AliMUONVDigit* d = digitStore.Add(detElemId,manuId,manuChannel, | |
389 | cathode, | |
390 | AliMUONVDigitStore::kIgnore); | |
391 | ||
392 | Float_t ped = gRandom->Gaus(mean,sigma); | |
393 | Int_t pedADC = TMath::FloorNint(ped); | |
394 | ||
395 | d->SetADC(pedADC); | |
396 | d->SetCharge(ped); | |
397 | // we do not set the remaining parts of the digit, as in principle | |
398 | // this is all we need : manuId, manuChannel and ADC, as far as | |
399 | // real data is concerned. | |
400 | ++fgCounter; | |
401 | ++ngenerated; | |
402 | } | |
403 | } | |
404 | } | |
405 | AliDebug(1,Form("ngenerated=%d nmanus=%d",ngenerated,nmanus)); | |
406 | } | |
407 | ||
408 | //_____________________________________________________________________________ | |
409 | AliRunLoader* | |
410 | AliMUONPedestalEventGenerator::LoadRun(const char* mode) | |
411 | { | |
412 | /// Get access to AliRunLoader object | |
413 | while (AliRunLoader::GetRunLoader()) | |
414 | { | |
415 | AliDebug(1,Form("Deleting AliRunLoader %p",AliRunLoader::GetRunLoader())); | |
416 | delete AliRunLoader::GetRunLoader(); | |
417 | } | |
418 | ||
419 | AliRunLoader* runLoader = | |
420 | AliRunLoader::Open(fGAliceFileName,AliConfig::GetDefaultEventFolderName(), | |
421 | mode); | |
422 | ||
423 | AliDebug(1,Form("AliRunLoader(%s)=%p",mode,runLoader)); | |
424 | ||
425 | if (!runLoader) | |
426 | { | |
427 | AliError("No run loader found in file galice.root"); | |
428 | } | |
429 | ||
430 | TString smode(mode); | |
431 | smode.ToUpper(); | |
432 | ||
433 | if (smode.Contains("RECREATE")) | |
434 | { | |
435 | AliInfo("Creating folder structure"); | |
436 | AliConfig::Instance() | |
437 | ->CreateDetectorFolders(runLoader->GetEventFolder(), | |
438 | "MUON", "MUON"); | |
439 | fLoader = new AliLoader("MUON",runLoader->GetEventFolder()); | |
440 | runLoader->AddLoader(fLoader); | |
441 | } | |
442 | ||
443 | fLoader = static_cast<AliLoader*>(runLoader->GetDetectorLoader("MUON")); | |
444 | ||
445 | return runLoader; | |
446 | } |