]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/MUON/macros/ExtractDDLs.C
Adding missing checks to macro.
[u/mrichter/AliRoot.git] / HLT / MUON / macros / ExtractDDLs.C
CommitLineData
4e155244 1/**************************************************************************
2 * This file is property of and copyright by the ALICE HLT Project *
3 * All rights reserved. *
4 * *
5 * Primary Authors: *
6 * Artur Szostak <artursz@iafrica.com> *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
16
17/// \ingroup macros
18/// \file ExtractDDLs.C
19/// \brief Macro for extracting DDL raw data from different AliRawReaders.
20///
21/// \author Artur Szostak <artursz@iafrica.com>
22///
23/// This macro is used to extract DDL raw data files for the muon spectrometer
24/// from different data sources using an AliRawReader. This macro will generate
25/// directories in the form rawX where X is an integer. This is the typical
26/// directory layout generated by AliRoot simulations.
27///
28
29#if !defined(__CINT__) || defined(__MAKECINT__)
30#include "TSystem.h"
31#include "TString.h"
32#include "AliRawReader.h"
33#include "AliRawDataHeader.h"
34#include "Riostream.h"
35#include <fstream>
36using std::fstream;
37#else
38#error This macro must be compiled.
39#endif
40
41/**
42 * Writes all DDL files for the current event from the AliRawReader.
43 */
44void WriteDDLs(AliRawReader* rawReader, const TString& dir, const char* detector)
45{
46 while (rawReader->ReadHeader())
47 {
48 // Allocate memory for the DDL.
49 UInt_t bufferSize = rawReader->GetDataSize() + sizeof(AliRawDataHeader);
50 UChar_t* buffer = new UChar_t[bufferSize];
51 if (buffer == NULL)
52 {
53 cerr << "ERROR: Out of memory!" << endl;
54 continue;
55 }
56
57 // Copy the CDH header.
58 memcpy(buffer, rawReader->GetDataHeader(), sizeof(AliRawDataHeader));
59
60 UChar_t* payload = buffer + sizeof(AliRawDataHeader);
61 UInt_t payloadSize = bufferSize - sizeof(AliRawDataHeader);
62
63 // Now copy the DDL payload.
64 if (! rawReader->ReadNext(payload, payloadSize))
65 {
66 cerr << "ERROR: Failed to read from AliRawReader." << endl;
67 delete [] buffer;
68 continue;
69 }
70
71 // Write the file.
72 TString filename = dir;
73 filename += "/";
74 filename += detector;
75 filename += "_";
76 char num[32];
77 sprintf(num, "%d", rawReader->GetEquipmentId());
78 filename += num;
79 filename += ".ddl";
80
81 fstream file(filename.Data(), fstream::out | fstream::binary);
e3f23a42 82 if (! file)
83 {
84 cerr << "ERROR: Could not create file: " << filename.Data() << endl;
85 delete [] buffer;
86 continue;
87 }
4e155244 88 file.write((char*)buffer, bufferSize);
e3f23a42 89 if (! file)
90 {
91 cerr << "ERROR: Failed to write to file: " << filename.Data() << endl;
92 file.close();
93 delete [] buffer;
94 continue;
95 }
4e155244 96 file.close();
e3f23a42 97
98 delete [] buffer;
4e155244 99 }
100}
101
102/**
103 * Extracts muon spectrometer raw DDL data files from a data source.
104 * \param dataSource This is the raw data to use. It can either be a directory name
105 * containing rawX directories with raw DDL files, a .root file containing raw
106 * data or a DATE raw data file.
107 * \param firstEvent The first event number to extract.
108 * \param lastEvent The last event number to extract. If set to a negative number then
109 * all events from <i>firstEvent</i> to the end of the data stream is extracted.
110 * \param outputDir This is the output directory to write the raw data to. The default
111 * is to use the current directory.
112 */
113void ExtractDDLs(
114 const char* dataSource = "raw.root",
115 Int_t firstEvent = 0, Int_t lastEvent = -1,
116 const char* outputDir = "."
117 )
118{
119 AliRawReader* rawReader = AliRawReader::Create(dataSource);
120 if (rawReader == NULL)
121 {
122 cerr << "ERROR: Could not create AliRawReader." << endl;
123 return;
124 }
125
126 Int_t count = 0;
127 if (lastEvent < 0) lastEvent = rawReader->GetNumberOfEvents() - 1;
128 Int_t event = firstEvent;
129 rawReader->GotoEvent(firstEvent);
130 while (event <= lastEvent)
131 {
132 char num[32];
133 sprintf(num, "%d", count);
134 TString dir = outputDir;
135 dir += "/raw";
136 dir += num;
e3f23a42 137 if (gSystem->MakeDirectory(dir.Data()) != 0)
138 {
139 cerr << "ERROR: Could not create directory: " << dir.Data() << endl;
140 }
141 else
142 {
143 rawReader->Select("MUONTRK", 0, 19);
144 WriteDDLs(rawReader, dir, "MUONTRK");
145 rawReader->Select("MUONTRG", 0, 1);
146 WriteDDLs(rawReader, dir, "MUONTRG");
147 }
4e155244 148 ++count;
149 ++event;
150 rawReader->NextEvent();
151 }
152
153 delete rawReader;
154}
155