AculCalParsScint.cpp 9.17 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
/*
 * AculCalParsScint.cpp
 *
 *  Created on: Oct 21, 2016
 *      Author: vratik
 */

#include "AculCalParsScint.h"
#include "TFile.h"

AculCalParsScint::AculCalParsScint() {
	// TODO Auto-generated constructor stub
	Reset();

}

AculCalParsScint::AculCalParsScint(const char* parFile) {
	// TODO Auto-generated constructor stub
	SetParFile(parFile);
	Init();

}

AculCalParsScint::~AculCalParsScint() {
	// TODO Auto-generated destructor stub
}

void AculCalParsScint::Init() {
	SetPars();
30
//	LoadCuts();
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
}

void AculCalParsScint::SetPars() {

	std::ifstream infile(fParFileName.Data());
	if ( !infile.is_open() ) {
		printf("AculCalParsScint::ReadParFile: File %s was not open and parameters were not set.\n", fParFileName.Data());
		Reset();
		return;
	}

	fEnergyPoints = 0;

	TString line;
	TString word;

	Int_t i, min, max;
	Int_t lineLength = 400;
	Char_t	det[lineLength];
	Char_t	part[lineLength];
	Char_t	fname[lineLength];
	Char_t	cname[lineLength];

	double en;	//energy

	while (!infile.eof()) {
		line.ReadLine(infile);

		if ( line.BeginsWith("#") || line.BeginsWith("//") ) continue;

		if ( line.BeginsWith("energies") ) {
			sscanf(line.Data(), "%*s %d", &i);
			continue;
		}

		if ( line.BeginsWith("crystals") ) {
			sscanf(line.Data(), "%*s %d", &i);
			fNoCrystals = i;
			continue;
		}

		if ( line.BeginsWith("files") ) {
			sscanf(line.Data(), "%*s %d", &i);
			fNoFiles = i;
75
			fFilePars.resize(fNoFiles);
76 77
			for (Int_t j = 0; j < fNoFiles; j++) {
				line.ReadLine(infile);
78 79 80 81
				if ( line.BeginsWith("#") || line.BeginsWith("//") ) {
					j--;
					continue;
				}
82 83
				sscanf(line, "%s", fname);
				word = fname;
84 85 86
				fFilePars[j].SetRawFileName(fname);
				fFilePars[j].SetNoCrystals(fNoCrystals);
				fFilePars[j].Init();
87 88 89 90
			}
			continue;
		}

91
		/*if ( line.BeginsWith("cutFile") ) {
92 93 94 95
			sscanf(line.Data(), "%*s %s %d", fname, &fNoCuts);
			fCutsFileName = fname;
			for (Int_t j = 0; j < fNoCuts; j++) {
				line.ReadLine(infile);
96 97 98 99
				if ( line.BeginsWith("#") || line.BeginsWith("//") ) {
					j--;
					continue;
				}
100 101 102 103
				sscanf(line, "%s", cname);
				fCutName.push_back(cname);
			}
			continue;
104
		}*/
105 106 107 108 109 110 111 112 113 114

		if ( line.BeginsWith("detector") ) {
			sscanf(line.Data(), "%*s %s %s", det, part);
			fDetName = det;
			fPartName = part;
			continue;
		}

		if ( line.BeginsWith("energy") ) {
			sscanf(line.Data(), "%*s %lf", &en);
115
			fFilePars[fEnergyPoints].SetEnergy(en);
116 117 118 119 120 121 122 123 124 125

			do {line.ReadLine(infile);}
			while (line.BeginsWith("#") || line.BeginsWith("//"));

			if ( line.BeginsWith("cutFile") ) {
				Int_t noCuts;
//				sscanf(line.Data(), "%*s %s %d", fname, &fNoCuts);
				sscanf(line.Data(), "%*s %s %d", fname, &noCuts);
				fFilePars[fEnergyPoints].SetNoCuts(noCuts);
				fFilePars[fEnergyPoints].SetCutFileName(fname);
126 127
//				cout << line << endl;
//				cout << noCuts << endl;
128 129 130
//				fCutsFileName = fname;
				for (Int_t j = 0; j < noCuts; j++) {
					line.ReadLine(infile);
131
//					cout << line << endl;
132 133 134 135 136 137 138 139 140 141
					if ( line.BeginsWith("#") || line.BeginsWith("//") ) {
						j--;
						continue;
					}
					sscanf(line, "%s", cname);
					fFilePars[fEnergyPoints].SetCutName(j, cname);
//					fCutName.push_back(cname);
				}
				fFilePars[fEnergyPoints].LoadCuts();
				line.ReadLine(infile);
142
//				cout << line << endl;
143 144 145 146 147 148
//				continue;
			}

//			cout << fNoCrystals << endl;
			for (Int_t j = 0; j < fNoCrystals; j++) {
//				line.ReadLine(infile);
149
//				cout << j << endl;
150 151 152 153 154 155 156 157 158 159 160 161
				if ( line.BeginsWith("#") || line.BeginsWith("//") ) {
					j--;
					line.ReadLine(infile);
					continue;
				}
				sscanf(line.Data(), "%d %d %d", &j, &min, &max);
//				cout << j << endl;
				fFilePars.at(fEnergyPoints).SetPeakMin(j, min);
				fFilePars.at(fEnergyPoints).SetPeakMax(j, max);
				line.ReadLine(infile);
			}

162 163 164 165
			fEnergyPoints++;
			continue;
		}

166
//		cout << "aklsjdlaksjd" << endl;
167

168 169 170
//		sscanf(line.Data(), "%d %d %d", &i, &min, &max);
//		fFilePars.at(fEnergyPoints-1).SetPeakMin(i, min);
//		fFilePars.at(fEnergyPoints-1).SetPeakMax(i, max);
171

172 173 174 175 176 177 178 179 180 181 182 183 184 185
	}//while

	infile.close();
	return;
}

void AculCalParsScint::PrintParameters(const char* option) {

	TString opt = option;

	cout << "Parameters read from file \"" << fParFileName.Data() << "\"." << endl;
	cout << "\tCalibration of detector \"" << fDetName << "\" with " << fNoCrystals << " crystals." << endl;
	cout << "\tParticle: " << fPartName << endl;
	cout << "\tInput files with raw data (" << fNoFiles << "):" << endl;
186 187
	for (Int_t i = 0; i < (Int_t)fFilePars.size(); i++) {
		cout << "\t " << GetFileName(i) << endl;
188 189 190
	}

	cout << "\tInput energies (" << fNoFiles << "):" << endl;
191
	for (Int_t i = 0; i < (Int_t)fFilePars.size(); i++) {
192
		cout << "\t " << fFilePars[i].GetEnergy() << " MeV" << endl;
193 194
	}

195 196 197 198 199 200 201 202
//	cout << "\tInput file with " << fNoCuts << " cuts: \"" << fCutsFileName << "\"" << endl;
//	if (opt.Contains("all")) {
//		for (Int_t i = 0; i < (Int_t)fCutName.size(); i++) {
//			cout << "\t cut: \"" << fCutName[i] << "\"" << endl;
//		}
//	}


203 204


205
	for (Int_t k = 0; k < (Int_t)fFilePars.size(); k++) {
206 207 208 209 210 211
		cout << "\t" << fFilePars[k].GetNoCuts() << " cuts from file \"" << fFilePars[k].GetCutFileName() << "\":" << endl;
		for (Int_t i = 0; i < fFilePars[k].GetNoCuts(); i++) {
			cout << "\t  " << fFilePars[k].GetCut(i)->GetName() << endl;
		}
		if (!opt.Contains("all")) continue;
		cout << "\tPeak limits for particular channels and energies:" << endl;
212 213 214
		cout << "\t  Set number: " << k << "; energy: " << fFilePars[k].GetEnergy() << " MeV" << endl;
		for (Int_t i = 0; i < (Int_t)fFilePars[k].GetNoCrystals(); i++) {
			cout << "\t\t " << fFilePars[k].GetPeakMin(i) << "\t" << fFilePars[k].GetPeakMax(i) << endl;
215 216 217 218 219 220 221 222
		}
	}

	return;
}

void AculCalParsScint::Reset() {

223 224
	fNoCrystals = 0;

225 226 227 228 229 230
	fParFileName = "";

	fDetName = "";
	fPartName = "";

	fNoFiles = 0;
231
	fFilePars.clear();
232 233
	fEnergyPoints = 0;

234 235
//	fCutsFileName = "";
//	fNoCuts = 0;				//number of cuts
236

237
//	fCutName.clear();
238 239 240 241 242 243

	return;
}

const char* AculCalParsScint::GetFileName(Int_t i) {

244 245
	if ( i >= (Int_t)fFilePars.size() ) {
		cerr << "\"AculCalParsScint::GetFileName\"  index i cannot be higher than " << fFilePars.size() - 1 << endl;
246 247
		return 0;
	}
248
	return fFilePars[i].GetRawFileName();
249 250
}

251
/*const char* AculCalParsScint::GetCutName(Int_t i) {
252 253 254 255 256 257

	if ( i > (Int_t)fCutName.size()-1 ) {
		cerr << "\"AculCalParsScint::GetCutName\"  index i cannot be higher than " << fCutName.size() - 1 << endl;
		return 0;
	}
	return fCutName[i].Data();
258
}*/
259

260
/*TCutG* AculCalParsScint::GetCut(Int_t i) {
261 262 263 264 265
	if ( i >= (Int_t)fCuts.size() ) {
		cerr << "\"AculCalParsScint::GetCut\"  index i cannot be higher than " << fCuts.size() - 1 << endl;
		return 0;
	}
	return &fCuts[i];
266
}*/
267

268
TCutG* AculCalParsScint::GetCut(const char* cutName, Int_t treeID) {
269 270 271

	const TString cName = cutName;

272 273 274 275 276 277 278 279 280 281 282 283 284
	if (treeID >= (Int_t)fFilePars.size()) {
		cerr << "\"AculCalParsScint::GetCut\" treeID does not exist. It cannot be higher than "
				<< fFilePars.size() << "." << endl;
		return 0;
	}

//	TCutG *curCut = 0;
	for (Int_t i = 0; i < fFilePars[treeID].GetNoCuts(); i++) {
//		if (cName.EqualTo(fFilePars[treeID])) { return &fCuts[i]; }
		if ( cName.EqualTo( fFilePars[treeID].GetCutName(i) ) ) {
			return fFilePars[treeID].GetCut(i);
//			return curCut;
		}
285 286 287 288 289 290 291
	}


	cerr << "\"AculCalParsScint::GetCut\"  cut \"" << cutName << "\" was not found." << endl;
	return 0;
}

292 293
Double_t AculCalParsScint::GetCalEnergy(Int_t i) {

294 295
	if ( i >= (Int_t)fFilePars.size() ) {
		cerr << "\"AculCalParsScint::GetCalEnergy\"  index i cannot be higher than " << fFilePars.size() - 1 << endl;
296 297
		return 0;
	}
298
	return fFilePars[i].GetEnergy();
299 300 301 302
}

Int_t AculCalParsScint::GetMinChannel(Int_t energy, Int_t crystal) {

303 304 305
	if ( energy >= (Int_t)fFilePars.size() ) {
		cerr << "\"AculCalParsScint::GetMinChannel\"  index \"energy\" cannot be higher than "
				<< fFilePars.size() - 1 << endl;
306 307 308
		return 0;
	}

309 310 311
	if ( crystal >= fFilePars[energy].GetNoCrystals() ) {
		cerr << "\"AculCalParsScint::GetMinChannel\"  index \"crystal\" cannot be higher than "
				<< fFilePars[energy].GetNoCrystals() - 1 << endl;
312 313 314
		return 0;
	}

315
	return fFilePars[energy].GetPeakMin(crystal);
316 317 318 319
}

Int_t AculCalParsScint::GetMaxChannel(Int_t energy, Int_t crystal) {

320 321 322
	if ( energy >= (Int_t)fFilePars.size() ) {
		cerr << "\"AculCalParsScint::GetMinChannel\"  index \"energy\" cannot be higher than "
				<< fFilePars.size() - 1 << endl;
323 324 325
		return 0;
	}

326 327 328
	if ( crystal >= fFilePars[energy].GetNoCrystals() ) {
		cerr << "\"AculCalParsScint::GetMinChannel\"  index \"crystal\" cannot be higher than "
				<< fFilePars[energy].GetNoCrystals() - 1 << endl;
329 330 331
		return 0;
	}

332
	return fFilePars[energy].GetPeakMax(crystal);
333 334
}

335
/*void AculCalParsScint::LoadCuts() {
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

	if (fCutsFileName.Length() == 0) {
		printf("AculCalParsScint::LoadCuts: Name of file (*.root) with cuts was not provided.\n");
		printf("AculCalParsScint::LoadCuts: No cuts has been loaded.\n");
		return;
	}

	TFile cutFile(fCutsFileName.Data(), "READ");
	if(!cutFile.IsOpen()) {
		cerr << "\"AculCalParsScint::LoadCuts\" File " << fCutsFileName.Data()
				<< " was not open and no cuts were loaded." << endl;
		return;
	}

	for (Int_t i = 0; i < (Int_t)fCutName.size(); i++) {
		TCutG *currentCut = (TCutG*)cutFile.Get(fCutName[i]);
352 353 354 355
		if (!currentCut) {
			cout << "\"AculCalParsScint::LoadCuts\" Cut \"" << fCutName[i]
				<< "\" was not found in file " << fCutsFileName << "." << endl;
			continue;
356
		}
357
		fCuts.push_back(*currentCut);
358 359
	}

360
}*/