]> git.uio.no Git - u/mrichter/AliRoot.git/blame - MUON/AliMUONPedestalEventGenerator.cxx
Removing class AliMUONTrackK
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestalEventGenerator.cxx
CommitLineData
ea199e33 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
ea199e33 20#include "AliHeader.h"
21#include "AliLog.h"
22#include "AliMUONCalibrationData.h"
42825ed9 23#include "AliMUONDigitStoreV1.h"
ea199e33 24#include "AliMUONRawWriter.h"
42825ed9 25#include "AliMUONStopwatchGroup.h"
26#include "AliMUONStopwatchGroupElement.h"
ea199e33 27#include "AliMUONVCalibParam.h"
42825ed9 28#include "AliMUONVDigit.h"
29#include "AliMUONVStore.h"
30#include "AliMpCathodType.h"
31#include "AliMpConstants.h"
32#include "AliMpDEStore.h"
33#include "AliMpDetElement.h"
ea199e33 34#include "AliMpIntPair.h"
42825ed9 35#include "AliMpPlaneType.h"
ea199e33 36#include "AliRunLoader.h"
42825ed9 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"
ea199e33 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
63ClassImp(AliMUONPedestalEventGenerator)
64/// \endcond
65
66Int_t AliMUONPedestalEventGenerator::fgCounter(0);
67
42825ed9 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
ea199e33 78//_____________________________________________________________________________
79AliMUONPedestalEventGenerator::AliMUONPedestalEventGenerator(Int_t runNumber,
80 Int_t nevents,
81 const char* filename)
82: TTask("AliMUONPedestalEventGenerator","Generate fake pedestal events"),
ea199e33 83fCalibrationData(new AliMUONCalibrationData(runNumber)),
84fDateFileName(filename),
85fGAliceFileName("galice.root"),
42825ed9 86fMakeDDL(kTRUE),
87fLoader(0x0),
88fPedestals(fCalibrationData->Pedestals()),
89fTimers(new AliMUONStopwatchGroup),
90fDigitStore(0x0),
91fRawWriter(0x0)
ea199e33 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
ea199e33 116 // Initialize event headers.
117 runLoader->MakeTree("E");
42825ed9 118
ea199e33 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
ea199e33 129 delete runLoader;
42825ed9 130 fLoader = 0x0;
ea199e33 131}
132
133
134//_____________________________________________________________________________
135AliMUONPedestalEventGenerator::~AliMUONPedestalEventGenerator()
136{
137 /// dtor
ea199e33 138 delete fCalibrationData;
42825ed9 139 AliInfo("Timers:");
140 fTimers->Print();
141 delete fTimers;
ea199e33 142 AliInfo(Form("make a digit counter %d",fgCounter));
42825ed9 143 delete fDigitStore;
144 delete fRawWriter;
ea199e33 145}
146
147//_____________________________________________________________________________
148Bool_t
149AliMUONPedestalEventGenerator::ConvertRawFilesToDate()
150{
151 /// convert raw data DDL files to DATE files with the program "dateStream".
152 /// we make one file per LDC
153
42825ed9 154 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::ConvertRawFilesToDate");
155
156 AliInfo("Converting raw to date");
157
ea199e33 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;
42825ed9 227 fLoader=0x0;
ea199e33 228 return (result == 0);
229}
230
42825ed9 231//_____________________________________________________________________________
232AliMUONVDigitStore*
233AliMUONPedestalEventGenerator::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
ea199e33 241//_____________________________________________________________________________
242void
243AliMUONPedestalEventGenerator::Exec(Option_t*)
244{
245 /// Main steering method
246
42825ed9 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");
ea199e33 256
42825ed9 257 Int_t nevents = runLoader->GetNumberOfEvents();
258
259 for ( Int_t i = 0; i < nevents ; ++i )
ea199e33 260 {
ea199e33 261 runLoader->GetEvent(i);
ea199e33 262
42825ed9 263 fLoader->MakeDigitsContainer();
264 TTree* treeD = fLoader->TreeD();
265 if (!treeD)
ea199e33 266 {
42825ed9 267 AliError(Form("Could not get TreeD for event %d",i));
268 continue;
ea199e33 269 }
ea199e33 270
42825ed9 271 DigitStore()->Connect(*treeD);
272
273 GenerateDigits(*(DigitStore()));
274
ea199e33 275 // Fill the output treeD
42825ed9 276 treeD->Fill();
ea199e33 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.
42825ed9 282
283 fTimers->Start("MUON","AliMUONPedestalEventGenerator::Exec WriteDigits");
284 fLoader->WriteDigits("OVERWRITE");
285 fTimers->Stop("MUON","AliMUONPedestalEventGenerator::Exec WriteDigits");
ea199e33 286
42825ed9 287 fLoader->UnloadDigits();
ea199e33 288
42825ed9 289 if ( fMakeDDL )
290 {
291 Digits2Raw(i);
292 }
293
294// gROOT->ProcessLine(Form("gObjectTable->Print(); > generate.txt.%d",i));
ea199e33 295 }
42825ed9 296
ea199e33 297 runLoader->WriteRunLoader("OVERWRITE");
ea199e33 298 delete runLoader;
42825ed9 299 fLoader = 0x0;
300
ea199e33 301 // Finally, if instructed to do so, convert DDL files to DATE file(s)
302 if ( fMakeDDL && fDateFileName.Length() > 0 )
303 {
ea199e33 304 Bool_t dateOutput = ConvertRawFilesToDate();
ea199e33 305 if (!dateOutput)
306 {
307 AliError("DATE output failed. Aborting.");
308 return;
309 }
310 }
ea199e33 311}
312
313//_____________________________________________________________________________
314void
42825ed9 315AliMUONPedestalEventGenerator::Digits2Raw(Int_t event)
ea199e33 316{
317 /// Converts digits (from MUON.Digits.root file) to Raw DDL ascii files.
318
42825ed9 319 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::Digits2Raw");
ea199e33 320
42825ed9 321 if (!fRawWriter) fRawWriter = new AliMUONRawWriter;
ea199e33 322
323 // Generate RAW data from the digits
324 // Be carefull to create&change to the correct directory first...
42825ed9 325
ea199e33 326 TString baseDir = gSystem->WorkingDirectory();
42825ed9 327
328 char dirName[256];
329 sprintf(dirName, "raw%d", event);
330 gSystem->MakeDirectory(dirName);
331 if (!gSystem->ChangeDirectory(dirName))
ea199e33 332 {
42825ed9 333 AliError(Form("couldn't change to directory %s", dirName));
334 return;
ea199e33 335 }
336
42825ed9 337 fRawWriter->Digits2Raw(DigitStore(),0);
ea199e33 338
42825ed9 339 gSystem->ChangeDirectory(baseDir);
ea199e33 340}
341
342//_____________________________________________________________________________
343void
42825ed9 344AliMUONPedestalEventGenerator::GenerateDigits(AliMUONVDigitStore& digitStore)
ea199e33 345{
346 /// Generate digits (where ADC is set to pedestal value) for all MUON TRK
347 /// and for 1 event.
348
42825ed9 349 AliMUONStopwatchGroupElement timer(fTimers,"MUON","AliMUONPedestalEventGenerator::GenerateDigits(AliMUONVDigitStore&)");
350
351 digitStore.Clear();
352
ea199e33 353 Int_t ngenerated(0);
42825ed9 354 Int_t nmanus(0);
355 TIter next(fPedestals->CreateIterator());
356 AliMUONVCalibParam* pedestals;
ea199e33 357
42825ed9 358 while ( ( pedestals = static_cast<AliMUONVCalibParam*>(next())) )
ea199e33 359 {
42825ed9 360 Int_t detElemId = pedestals->ID0();
361 Int_t manuId = pedestals->ID1();
ea199e33 362
42825ed9 363 AliMpDetElement* de = AliMpDEStore::Instance()->GetDetElement(detElemId);
364 AliMp::PlaneType planeType = AliMp::kBendingPlane;
365 if ( manuId & AliMpConstants::ManuMask(AliMp::kNonBendingPlane) )
ea199e33 366 {
42825ed9 367 planeType = AliMp::kNonBendingPlane;
ea199e33 368 }
42825ed9 369 AliMp::CathodType cathode = de->GetCathodType(planeType);
370
371 ++nmanus;
372
ea199e33 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);
42825ed9 386
387
388 AliMUONVDigit* d = digitStore.Add(detElemId,manuId,manuChannel,
389 cathode,
390 AliMUONVDigitStore::kIgnore);
391
ea199e33 392 Float_t ped = gRandom->Gaus(mean,sigma);
42825ed9 393 Int_t pedADC = TMath::FloorNint(ped);
394
395 d->SetADC(pedADC);
396 d->SetCharge(ped);
ea199e33 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.
ea199e33 400 ++fgCounter;
401 ++ngenerated;
402 }
403 }
404 }
42825ed9 405 AliDebug(1,Form("ngenerated=%d nmanus=%d",ngenerated,nmanus));
ea199e33 406}
407
408//_____________________________________________________________________________
409AliRunLoader*
410AliMUONPedestalEventGenerator::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);
42825ed9 422
ea199e33 423 AliDebug(1,Form("AliRunLoader(%s)=%p",mode,runLoader));
42825ed9 424
ea199e33 425 if (!runLoader)
426 {
427 AliError("No run loader found in file galice.root");
428 }
42825ed9 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
ea199e33 445 return runLoader;
446}