ROOT logo
/**************************************************************************
 * This file is property of and copyright by the ALICE HLT Project        *
 * All rights reserved.                                                   *
 *                                                                        *
 * Primary Authors:                                                       *
 *   Artur Szostak <artursz@iafrica.com>                                  *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/// \ingroup macros
/// \file ExtractDDLs.C
/// \brief Macro for extracting DDL raw data from different AliRawReaders.
///
/// \author Artur Szostak <artursz@iafrica.com>
///
/// This macro is used to extract DDL raw data files for the muon spectrometer
/// from different data sources using an AliRawReader. This macro will generate
/// directories in the form rawX where X is an integer. This is the typical
/// directory layout generated by AliRoot simulations.
///

#if !defined(__CINT__) || defined(__MAKECINT__)
#include "TSystem.h"
#include "TString.h"
#include "AliRawReader.h"
#include "AliRawDataHeader.h"
#include "Riostream.h"
#include <fstream>
using std::fstream;
#else
#error This macro must be compiled.
#endif

/**
 * Writes all DDL files for the current event from the AliRawReader.
 */
void WriteDDLs(AliRawReader* rawReader, const TString& dir, const char* detector)
{
	while (rawReader->ReadHeader())
	{
		// Allocate memory for the DDL.
		UInt_t bufferSize = rawReader->GetDataSize() + sizeof(AliRawDataHeader);
		UChar_t* buffer = new UChar_t[bufferSize];
		if (buffer == NULL)
		{
			cerr << "ERROR: Out of memory!" << endl;
			continue;
		}
		
		// Copy the CDH header.
		memcpy(buffer, rawReader->GetDataHeader(), sizeof(AliRawDataHeader));
		
		UChar_t* payload = buffer + sizeof(AliRawDataHeader);
		UInt_t payloadSize = bufferSize - sizeof(AliRawDataHeader);
		
		// Now copy the DDL payload.
		if (! rawReader->ReadNext(payload, payloadSize))
		{
			cerr << "ERROR: Failed to read from AliRawReader." << endl;
			delete [] buffer;
			continue;
		}
		
		// Write the file.
		TString filename = dir;
		filename += "/";
		filename += detector;
		filename += "_";
		char num[32];
		sprintf(num, "%d", rawReader->GetEquipmentId());
		filename += num;
		filename += ".ddl";
		
		fstream file(filename.Data(), fstream::out | fstream::binary);
		if (! file)
		{
			cerr << "ERROR: Could not create file: " << filename.Data() << endl;
			delete [] buffer;
			continue;
		}
		file.write((char*)buffer, bufferSize);
		if (! file)
		{
			cerr << "ERROR: Failed to write to file: " << filename.Data() << endl;
			file.close();
			delete [] buffer;
			continue;
		}
		file.close();
		
		delete [] buffer;
	}
}

/**
 * Extracts muon spectrometer raw DDL data files from a data source.
 * \param dataSource  This is the raw data to use. It can either be a directory name
 *      containing rawX directories with raw DDL files, a .root file containing raw
 *      data or a DATE raw data file.
 * \param firstEvent The first event number to extract.
 * \param lastEvent The last event number to extract. If set to a negative number then
 *      all events from <i>firstEvent</i> to the end of the data stream is extracted.
 * \param outputDir  This is the output directory to write the raw data to. The default
 *      is to use the current directory.
 */
void ExtractDDLs(
		const char* dataSource = "raw.root",
		Int_t firstEvent = 0, Int_t lastEvent = -1,
		const char* outputDir = "."
	)
{
	AliRawReader* rawReader = AliRawReader::Create(dataSource);
	if (rawReader == NULL)
	{
		cerr << "ERROR: Could not create AliRawReader." << endl;
		return;
	}
	
	Int_t count = 0;
	if (lastEvent < 0) lastEvent = rawReader->GetNumberOfEvents() - 1;
	Int_t event = firstEvent;
	rawReader->GotoEvent(firstEvent);
	while (event <= lastEvent)
	{
		char num[32];
		sprintf(num, "%d", count);
		TString dir = outputDir;
		dir += "/raw";
		dir += num;
		if (gSystem->MakeDirectory(dir.Data()) != 0)
		{
			cerr << "ERROR: Could not create directory: " << dir.Data() << endl;
		}
		else
		{
			rawReader->Select("MUONTRK", 0, 19);
			WriteDDLs(rawReader, dir, "MUONTRK");
			rawReader->Select("MUONTRG", 0, 1);
			WriteDDLs(rawReader, dir, "MUONTRG");
		}
		++count;
		++event;
		rawReader->NextEvent();
	}

	delete rawReader;
}

 ExtractDDLs.C:1
 ExtractDDLs.C:2
 ExtractDDLs.C:3
 ExtractDDLs.C:4
 ExtractDDLs.C:5
 ExtractDDLs.C:6
 ExtractDDLs.C:7
 ExtractDDLs.C:8
 ExtractDDLs.C:9
 ExtractDDLs.C:10
 ExtractDDLs.C:11
 ExtractDDLs.C:12
 ExtractDDLs.C:13
 ExtractDDLs.C:14
 ExtractDDLs.C:15
 ExtractDDLs.C:16
 ExtractDDLs.C:17
 ExtractDDLs.C:18
 ExtractDDLs.C:19
 ExtractDDLs.C:20
 ExtractDDLs.C:21
 ExtractDDLs.C:22
 ExtractDDLs.C:23
 ExtractDDLs.C:24
 ExtractDDLs.C:25
 ExtractDDLs.C:26
 ExtractDDLs.C:27
 ExtractDDLs.C:28
 ExtractDDLs.C:29
 ExtractDDLs.C:30
 ExtractDDLs.C:31
 ExtractDDLs.C:32
 ExtractDDLs.C:33
 ExtractDDLs.C:34
 ExtractDDLs.C:35
 ExtractDDLs.C:36
 ExtractDDLs.C:37
 ExtractDDLs.C:38
 ExtractDDLs.C:39
 ExtractDDLs.C:40
 ExtractDDLs.C:41
 ExtractDDLs.C:42
 ExtractDDLs.C:43
 ExtractDDLs.C:44
 ExtractDDLs.C:45
 ExtractDDLs.C:46
 ExtractDDLs.C:47
 ExtractDDLs.C:48
 ExtractDDLs.C:49
 ExtractDDLs.C:50
 ExtractDDLs.C:51
 ExtractDDLs.C:52
 ExtractDDLs.C:53
 ExtractDDLs.C:54
 ExtractDDLs.C:55
 ExtractDDLs.C:56
 ExtractDDLs.C:57
 ExtractDDLs.C:58
 ExtractDDLs.C:59
 ExtractDDLs.C:60
 ExtractDDLs.C:61
 ExtractDDLs.C:62
 ExtractDDLs.C:63
 ExtractDDLs.C:64
 ExtractDDLs.C:65
 ExtractDDLs.C:66
 ExtractDDLs.C:67
 ExtractDDLs.C:68
 ExtractDDLs.C:69
 ExtractDDLs.C:70
 ExtractDDLs.C:71
 ExtractDDLs.C:72
 ExtractDDLs.C:73
 ExtractDDLs.C:74
 ExtractDDLs.C:75
 ExtractDDLs.C:76
 ExtractDDLs.C:77
 ExtractDDLs.C:78
 ExtractDDLs.C:79
 ExtractDDLs.C:80
 ExtractDDLs.C:81
 ExtractDDLs.C:82
 ExtractDDLs.C:83
 ExtractDDLs.C:84
 ExtractDDLs.C:85
 ExtractDDLs.C:86
 ExtractDDLs.C:87
 ExtractDDLs.C:88
 ExtractDDLs.C:89
 ExtractDDLs.C:90
 ExtractDDLs.C:91
 ExtractDDLs.C:92
 ExtractDDLs.C:93
 ExtractDDLs.C:94
 ExtractDDLs.C:95
 ExtractDDLs.C:96
 ExtractDDLs.C:97
 ExtractDDLs.C:98
 ExtractDDLs.C:99
 ExtractDDLs.C:100
 ExtractDDLs.C:101
 ExtractDDLs.C:102
 ExtractDDLs.C:103
 ExtractDDLs.C:104
 ExtractDDLs.C:105
 ExtractDDLs.C:106
 ExtractDDLs.C:107
 ExtractDDLs.C:108
 ExtractDDLs.C:109
 ExtractDDLs.C:110
 ExtractDDLs.C:111
 ExtractDDLs.C:112
 ExtractDDLs.C:113
 ExtractDDLs.C:114
 ExtractDDLs.C:115
 ExtractDDLs.C:116
 ExtractDDLs.C:117
 ExtractDDLs.C:118
 ExtractDDLs.C:119
 ExtractDDLs.C:120
 ExtractDDLs.C:121
 ExtractDDLs.C:122
 ExtractDDLs.C:123
 ExtractDDLs.C:124
 ExtractDDLs.C:125
 ExtractDDLs.C:126
 ExtractDDLs.C:127
 ExtractDDLs.C:128
 ExtractDDLs.C:129
 ExtractDDLs.C:130
 ExtractDDLs.C:131
 ExtractDDLs.C:132
 ExtractDDLs.C:133
 ExtractDDLs.C:134
 ExtractDDLs.C:135
 ExtractDDLs.C:136
 ExtractDDLs.C:137
 ExtractDDLs.C:138
 ExtractDDLs.C:139
 ExtractDDLs.C:140
 ExtractDDLs.C:141
 ExtractDDLs.C:142
 ExtractDDLs.C:143
 ExtractDDLs.C:144
 ExtractDDLs.C:145
 ExtractDDLs.C:146
 ExtractDDLs.C:147
 ExtractDDLs.C:148
 ExtractDDLs.C:149
 ExtractDDLs.C:150
 ExtractDDLs.C:151
 ExtractDDLs.C:152
 ExtractDDLs.C:153
 ExtractDDLs.C:154
 ExtractDDLs.C:155
 ExtractDDLs.C:156