AculConvert.cpp 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
#include "AculConvert.h"

ClassImp(AculConvert);

AculConvert::AculConvert()
{
	fRawEvent = new AculRaw();
}

AculConvert::~AculConvert()
{
	delete fRawEvent;
}

Int_t AculConvert::ConvertRun(const char* experimentalrun, const Int_t nofirstfolder, const Int_t nolastfolder/*, const Char_t* runaddress*/)
{
	TString fullWayToRun = experimentalrun;
//	fullWayToRun.ReplaceAll("/", "\\");		//for win

	fOutFaultFile.open("convertFault.log");

	Char_t	nameOfFolder[260];
//	Int_t	i = 0;

	for (Int_t i = nofirstfolder; i <= nolastfolder; i++) {
		sprintf(nameOfFolder, "%s.%03d", fullWayToRun.Data(), i);
		ConvertFolder(nameOfFolder);
	}

	fOutFaultFile.close();

	return 0;


}

Int_t AculConvert::ConvertFolder(const char* rawdatafolder)
{

	TString	fullWayToFolder = rawdatafolder;
//	fullWayToFolder.ReplaceAll("/", "\\");		//for win

	//necessary names preparing
	Char_t	nameOfFile[260];
	Char_t	nameOfRun[40];
	Char_t	nameOfFileProtocol[260];
//	strcpy(nameOfRun, fullWayToFolder(fullWayToFolder.Last('\\') + 1, fullWayToFolder.Length()).Data()); 	//for win
	strcpy(nameOfRun, fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data());	//for linux
	strcpy(strstr(nameOfRun, "."), "\0");
//	sprintf(nameOfFileProtocol, "%s\\%s.PRO", fullWayToFolder.Data(), nameOfRun);	//for win
	sprintf(nameOfFileProtocol, "%s/%s.PRO", fullWayToFolder.Data(), nameOfRun);	//for linux
//	printf("%s\\%s.PRO\n", fullWayToFolder.Data(), nameOfRun);		//for windows
	printf("%s/%s.PRO\n", fullWayToFolder.Data(), nameOfRun);		//for linux
	//file protocol reading
	if (ReadFileProtocol(nameOfFileProtocol) != 0) {
		return -1;
	}

	//root file creating
	Char_t	rootFileName[260];
//	sprintf(rootFileName, "%s\.root", fullWayToFolder(fullWayToFolder.Last('\\') + 1, fullWayToFolder.Length()).Data());	//for win
//	sprintf(rootFileName, "%s\.root", fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data());	//for linux
	sprintf(rootFileName, "%s.root", fullWayToFolder(fullWayToFolder.Last('/') + 1, fullWayToFolder.Length()).Data());	//for linux
	cout << endl << endl
		<< "===========================================================" << endl
		<< "== Raw data converting to file " << rootFileName << endl
		<< "===========================================================" << endl << endl;
	TFile *fw = new TFile(rootFileName, "RECREATE");
	TTree *tw = new TTree("RAW", "Transcription");
	tw->Bronch("channels", "AculRaw", &fRawEvent);

	//folder processing
	Int_t	i = 0;
	do {
//		sprintf(nameOfFile, "%s\\%s.%03d", rawdatafolder, nameOfRun, i);	//for win
		sprintf(nameOfFile, "%s/%s.%03d", rawdatafolder, nameOfRun, i);		//for linux
		i++;
		cout << endl << "=================================================" << endl;
	} while (	ConvertFile(nameOfFile, /*fullWayToFolder,*/ fw) == 0 );

	fw->Close();

	return 0;

}

Int_t AculConvert::ConvertFile(const char* rawdatafile, TFile* rootRawFile)
{

	//file with ACCULINNA raw data opening
	FILE	*dataFile;

	TString	fullWayToFile = rawdatafile;
//	fullWayToFile.ReplaceAll("/", "\\");

//	cout << fullWayToFile.Data() << endl;
	//file for error output opening
	if ((dataFile = fopen(fullWayToFile.Data(), "rb")) == NULL) {
		cout << endl << "File " << fullWayToFile.Data() << " was not found\n";
		if ( fOutFaultFile.is_open() ) {
			fOutFaultFile << "File " << fullWayToFile.Data() << " was not found\n";
		}
        return -1;
    }
	cout << endl << endl
		<< "Data file " << fullWayToFile.Data() << " was opened" << endl;

	Char_t filename[50];
//	sprintf(filename, "%s", fullWayToFile(fullWayToFile.Last('\\') + 1, fullWayToFile.Length()).Data()); 	//for windows
	sprintf(filename, "%s", fullWayToFile(fullWayToFile.Last('/') + 1, fullWayToFile.Length()).Data());		//for linux

	//ROOT file for transcripted data creating
	TTree *treeRaw;
	if (rootRawFile == NULL) {
		Char_t rootFileName[260];
//		sprintf(rootFileName, "%s\.root", filename);
		sprintf(rootFileName, "%s.root", filename);
		rootRawFile = new TFile(rootFileName, "RECREATE");
		treeRaw = new TTree("RAW", "Transcription to .root file");
		treeRaw->Bronch("channels", "AculRaw", &fRawEvent);

		//file protocol reading
		TString	protocolName = filename;
		protocolName.Replace(protocolName.First('.'), 4, ".PRO");
		if (ReadFileProtocol(protocolName.Data()) != 0) {
			return -1;
		}
//		ReadFileProtocol( protocolName.Data() );
	}

	//TTree with class structure
//	AculRaw *rawEvent = new AculRaw();incorrEventsNumber
	else {
		treeRaw = (TTree*)(rootRawFile->Get("RAW"));
	}
//	treeRaw->Bronch("channels", "AculRaw", &fRawEvent);

	//file reading
	const int BUFFSIZE = 8500000;
	static unsigned char buffer[BUFFSIZE];

	for (Int_t i = 0; i < BUFFSIZE; i++) {
		buffer[i] = 0;
	}

//cout << "hovno4" << endl;

	fseek(dataFile, 0, SEEK_SET);

	if ( fread(buffer, sizeof(buffer), 1, dataFile) != 0 ) {
		cout << "!!!Data file is larger than expected!!!" << endl;
		if ( fOutFaultFile.is_open() ) {
			fOutFaultFile << "!!!Data file " << filename << " is larger than expected!!!" << endl;
		}
		return -2;
	}

	cout << endl
		<< "------------------------" << endl
		<< "	 Buffer analysis	" << endl
		<< "------------------------" << endl;

	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//	event structure:																				//
	//	n-times 2 bytes cells with parameters according to file protocol								//
	//	2 bytes cell with number of FLOAT parameters													//
	//	pairs of 2 bytes cells with identifier (crate, address, subaddress) and value					//
	//	2 bytes cell with end of the event sign (first byte, F0) and trigger identifier (second byte)	//
	//
	//	structure of 2 bytes cell: |lower|higher|
	//////////////////////////////////////////////////////////////////////////////////////////////////////

	Int_t bufferPosition = 0;
	Int_t corrEventsNumber = 0;
	Int_t incorrEventsNumber = 0;
	Int_t parametresNumber = 0;

	Int_t	identifier;				//2 bytes (0FFF mask)
									//1111(empty positions)111(crateMask)11111(blockPositionMask)1111(addresMask)
	const Int_t	addressMask = 15;
	const Int_t blockPositionMask = 31;
	const Int_t crateMask = 7;
	Int_t	address = 0;
	Int_t	blockPosition = 0;
	Int_t	crate = 0;
	UInt_t value = 0;



	while (bufferPosition < BUFFSIZE) {

		parametresNumber = static_cast<Int_t>(buffer[bufferPosition + 2*PERMPARNUMBER]);

		//verification of the correct EndOfEvent identifier position
		while ( static_cast<Int_t>(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber + 1/*cell with trigger ID*/]) != ENDOFEVENT && bufferPosition < BUFFSIZE) {
			bufferPosition++;	//search for the beginning of the correct event
			if ( static_cast<Int_t>(buffer[bufferPosition + 2*PERMPARNUMBER + 2 + 4*parametresNumber + 1]) == ENDOFEVENT ) {
				incorrEventsNumber++;		//increased after end of the event identifier
				cout << "!!!damaged event!!!" << endl;
				if ( fOutFaultFile.is_open() ) {
					fOutFaultFile << "!!!damaged event!!!" << endl;
				}
			}
		}//while

		//////////////////////////////////
		//// correct event processing ////
		//////////////////////////////////
		if ( static_cast<Int_t>(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber/*vysvetlit*/ + 1/*cell with trigger ID*/]) == ENDOFEVENT ) {

			//rawEvent reseting
			fRawEvent->Reset();

			//trigger
			fRawEvent->trigger = static_cast<Int_t>(buffer[bufferPosition + 2*PERMPARNUMBER + 2/*2 bytes with parametres Number*/ + 4*parametresNumber/*vysvetlit*/ /*+ 1*/ /*cell with trigger ID*/]);

			//dodelat MWPC`s

			//floating parametres
			for (Int_t i = 0; i < parametresNumber; i++) {
				identifier = static_cast<Int_t>( buffer[bufferPosition + 2*PERMPARNUMBER + 2 + 4*i] + (buffer[bufferPosition + 2*PERMPARNUMBER + 3 + 4*i] << 8) );
				address = 0;
				address = identifier & addressMask;
				blockPosition = 0;
				blockPosition = (identifier >> 4) & blockPositionMask;
				crate = 0;
				crate = (identifier >> 9) & crateMask;
				value = 0;
				value = static_cast<UInt_t>( buffer[bufferPosition + 2*PERMPARNUMBER + 4 + 4*i] + (buffer[bufferPosition + 2*PERMPARNUMBER + 5 + 4*i] << 8) );
				if (crate == 3) {
					fRawEvent->C3[blockPosition][address] = static_cast<Int_t>(value&fMaskC3[blockPosition]);

				}

			}

			bufferPosition = bufferPosition + 2*PERMPARNUMBER + 1 + 4*parametresNumber + 2 + 1;
			corrEventsNumber++;

			treeRaw->Fill();

		}//if

	}//while

	cout << endl
		<< "In file " << filename << " was found " << corrEventsNumber << " correct events" << endl
		<< "In file " << filename << " was found " << incorrEventsNumber << " incorrect events" << endl;

	treeRaw->AutoSave();

	if (rootRawFile->GetName() == "ConvertFileOutput.root") {			//promyslet, vhodny nazev
		rootRawFile->Close();
	}


	//close data file
	if (fclose(dataFile) == EOF) {												//parameter file closing
		cout << "File " << filename << " closing error\n";
		if ( fOutFaultFile.is_open() ) {
			fOutFaultFile << "File " << filename << " closing error\n";
		}
		return -1;
	}
	cout << endl
		<< "Data file " << filename << " was closed" << endl << endl;

	return 0;
}

Int_t AculConvert::ReadFileProtocol(const char* protocolname)
{

	for (Int_t i = 0; i < BLOCKSNUMBER; i++) {
		fMaskC3[i] = 0;
	}

	//file protocol opening
	ifstream protocolFile;
	protocolFile.open(protocolname);

	if (!protocolFile.is_open()) {
		cout << "File " << protocolname << " was not found\n";
		if ( fOutFaultFile.is_open() ) {
			fOutFaultFile << "File " << protocolname << " was not found\n";
		}
        return -1;
    }

	const Int_t lineLength = 200;
	Char_t line[lineLength];
	UInt_t	crate, address, subaddress;
	unsigned int mask;

	//reading parametres
	while (!protocolFile.eof()) {
		protocolFile.getline(line, lineLength);
		if ( line[0] != '#' ) {
			sscanf(line, "%*d %*s %*d %d %d %d %*c %x", &crate, &address, &subaddress, &mask);
			if (crate == 3) {
				fMaskC3[address] = static_cast<UInt_t>(mask);	//writing parameters
			}
		}
	}

	protocolFile.close();

	return 0;

}