AculCalibScint.cpp 11.8 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
#include "AculCalibScint.h"
#include "AculCalParsScint.h"

ClassImp(AculCalibScint);

AculCalibScint::AculCalibScint() {

	printf("AculCalibScint::Default constructor called.\n");
	fInFiles = 0;
	fTrees = 0;
	fCutFile = 0;


}

AculCalibScint::AculCalibScint(const char* parfile) {
	printf("AculCalibScint::Constructor called.\n");

	SetParFile(parfile);
	Init();
//	cout << "nofiles: " << nofiles << endl;
//	OpenTrees();
//	LoadCuts();

//	fr.Print();
//	fr.At(0);
	

}

AculCalibScint::~AculCalibScint() {

	printf("AculCalibScint::Destructor called.\n");

}

void AculCalibScint::Init() {
	fPars = new AculCalParsScint(fParFileName.Data());
	OpenFiles();
	LoadTrees();
41 42 43 44 45 46

	fHistFull.resize(fPars->GetNoRawFiles());
	fHistCut.resize(fPars->GetNoRawFiles());

	fMeanPeakPos.resize(fPars->GetNoRawFiles());
	fMeanPeakRMS.resize(fPars->GetNoRawFiles());
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
//	SetPars();
}

void AculCalibScint::PrintTrees() {

	if (!fTrees) {
		cerr << "\"AculCalibScint::PrintTrees\" Probably no tree was open." << endl;
		return;
	}

//	TTree *curTree = 0;
	cout << " AculCalibScint::PrintTrees:" << endl;
	for (Int_t i = 0; i < fPars->GetNoRawFiles(); i++) {
//		curTree = fTrees[i];
		if (fTrees[i]) {
			printf("\tTree No. %d; File: %s; Name: %s\n", i, fTrees[i]->GetDirectory()->GetName(), fTrees[i]->GetName());
		} else {
64
			printf("\tTree No. %d was not loaded. Maximal number of trees is %d\n", i, fPars->GetNoRawFiles());
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
		}
	}

	return;
}

void AculCalibScint::OpenFiles() {

	if (!fPars) {
		cerr << "\"AculCalibScint::OpenFiles\" parameters were not initialized." << endl;
		return;
	}

	fPars->GetNoRawFiles();
	fInFiles = new TFile* [fPars->GetNoRawFiles()];
	for (Int_t i = 0; i < fPars->GetNoRawFiles(); i++) {
		fInFiles[i] = new TFile(fPars->GetFileName(i), "READ");
//		cout << "\"AculCalibScint::OpenFiles\" File \"" << fInFiles[i]->GetName() << "\" was opened." << endl;
	}

}

void AculCalibScint::LoadTrees() {

	if (!fInFiles) {
		cerr << "\"AculCalibScint::LoadTrees\" Input files were not open." << endl;
		return;
	}

	fTrees = new TTree* [fPars->GetNoRawFiles()];
	for (Int_t i = 0; i < fPars->GetNoRawFiles(); i++) {
		fTrees[i] = (TTree*)fInFiles[i]->Get("AnalysisxTree");
//		cout << "\"AculCalibScint::LoadTrees\" Tree \"" << fTrees[i]->GetName()
//				<< "\" from file \"" << fInFiles[i]->GetName() << "\" was loaded." << endl;
	}

}

103 104 105 106 107 108
TTree* AculCalibScint::GetTree(Int_t treeID) {
	if (treeID >= fPars->GetNoRawFiles()) {
		cerr << "\"AculCalibScint::DrawVariable\" Tree number " << treeID
				<< " cannot exist. Maximal treeID is " << fPars->GetNoRawFiles() - 1 << "." << endl;
		return 0;
	}
109

110 111 112 113
	return fTrees[treeID];
}

void AculCalibScint::DrawVariable(const char* variable, Int_t treeID, TCanvas *canvas, Int_t lowRange, Int_t upRange) {
114

115 116 117 118 119 120 121 122 123

	if (!canvas) {
		cerr << "\"AculCalibScint::DrawVariable\" Canvas pointer was NULL" << endl;
		return;
	}

	//todo change 16 to parameter
	const Int_t nPads = 16;
	CanDivider(canvas, nPads);
124 125 126 127 128 129

	TString canvasTitle;
	TString var;
	TString con;

	TTree *curTree = 0;
130
	curTree = GetTree(treeID);
131
	if (!curTree) {
132
		printf("AculCalibScint::DrawVariable: Tree No. %d was not found.\n", treeID);
133 134 135
		return;
	}

136
	canvasTitle.Form("variable: %s; tree: %d", variable, treeID);
137 138
	canvas->SetTitle(canvasTitle.Data());

139
	for (Int_t i = 0; i < nPads; i++) {
140 141 142 143 144 145 146 147
		var.Form("%s[%d]", variable, i);
		con.Form("%s[%d]>%d && %s[%d]<%d", variable, i, lowRange, variable, i, upRange);
		canvas->cd(i+1);
		curTree->Draw(var.Data(), con.Data());
		canvas->Update();
	}

}
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

void AculCalibScint::DrawBeam(TCanvas *canvas, Int_t file, const char* variable) {

	canvas->SetTitle("Beam");

	const Int_t noFiles = file;

	//todo delet parameter "files"
	CanDivider(canvas, 0, noFiles, 3);

	TTree *curTree = 0;
	TCutG *curCutG = 0;
	TString var;
	TString con;

163 164 165
//	cout << "cuts " << fPars->GetNoCuts() << endl;
	cout << "this function has to be checked" << endl;
	return;
166 167 168 169 170 171 172 173 174 175 176 177

	for (Int_t i = 0; i < noFiles; i++) {
		canvas->cd(i+1);
		curTree = GetTree(i);
		if (!curTree) {
			printf("AculCalibCsI::DrawBeam: Tree No. %d was not found.\n", i);
			continue;
		}
		curTree->Draw("QDC[0]:TDC[0]", "TDC[0]<1000 && QDC[0]<2000", "cont");
//		curTree->Draw("QDC[0]+QDC[1]:(TDC[2]+TDC[3])/2. - (TDC[0]+TDC[1])/2.", "", "cont");
//		cout << "aksjda\t" << energyPoints << endl;
		for (Int_t j = 0; j < fPars->GetNoRawFiles(); j++) {
178 179 180
//			if ( fPars->GetCut(j) ) {
	//			TCutG *cutTemp = fPars->GetCut(j);
	//			curCutG = new TCutG(*cutTemp);
181 182
				curCutG->Draw("same");
//				printf("AculCalibCsI::DrawBeam: cTOF cut No. %d cannot be drawn, need to repair this function.\n", j);
183
//			}
184 185 186 187 188
		}

		canvas->cd(noFiles+1+i);
		curTree->Draw("QDC[0]:QDC[1]", "", "cont");
		for (Int_t j = 0; j < fPars->GetNoRawFiles(); j++) {
189 190 191
//			if ( fPars->GetCut(noFiles + j) ) {
//				TCutG *cutTemp = fPars->GetCut(noFiles + j);
//				curCutG = new TCutG(*cutTemp);
192 193
				curCutG->Draw("same");
//				printf("AculCalibCsI::DrawBeam: cQCD cut No. %d cannot be drawn, need to repair this function.\n", j);
194
//			}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		}

		canvas->cd(2*noFiles+1+i);
		var.Form("%s[5]:TDC[0]", variable);
		con.Form("%s[5]>200", variable);
		curTree->Draw(var.Data(), con.Data(), "cont");
		for (Int_t j = 0; j < fPars->GetNoRawFiles(); j++) {
//			if ( cutsCol.At(files + j) ) {
//				curCutG = (TCutG*)cutsCol.At(files + j);
//				curCutG->Draw("same");
//				printf("AculCalibCsI::DrawBeam: cQCD cut No. %d cannot be drawn, need to repair this function.\n", j);
//			}
		}
		canvas->Update();
	}
}

212
void AculCalibScint::GetPeakMean(const char* variable, Int_t treeID, /*Int_t energy,*/ TCanvas *canvas, const char* beamcut, const Int_t nbins, Int_t lowRange) {
213 214 215 216 217 218 219 220 221

//	todo: change this parameter as nonconstant
	const Int_t noCrystals = 16;
	CanDivider(canvas, noCrystals);

	TString var;
	TString con;
	TString hname;
	TString canvasTitle;
222 223 224 225 226 227 228
	TCutG *currCut = fPars->GetCut(beamcut, treeID);

//	if (!fPars->GetCut(beamcut, treeID)) {
//		cout << "\"AculCalibScint::GetPeakMean\" cut with name \"" << beamcut << "\" was not found." << endl;
//	} else {
//		currCut =
//	}
229 230

//	todo manage the canvas name using cut name
231 232 233 234 235 236 237
	if (currCut) {
		canvasTitle.Form("variable: %s; tree: %d; cut: %s;", variable, treeID, currCut->GetName());
		canvas->SetTitle(canvasTitle.Data());
	} else {
		canvasTitle.Form("variable: %s; tree: %d; no cut;", variable, treeID);
		canvas->SetTitle(canvasTitle.Data());
	}
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

	TTree *curTree = 0;
	curTree = GetTree(treeID);
	if (!curTree) {
		printf("AculCalibCsI::GetPeakMean: Tree No. %d was not found.\n", treeID);
		return;
	}

	TString sVariable = variable;
	sVariable.ToLower();
	Int_t channel = 0;

	for (Int_t i = 0; i < noCrystals; i++) {

		if (sVariable.Contains("anc")) {
			channel = i+1;
		} else {
			channel = i;
		}

		var.Form("%s[%d]>>hfull[%d][%d]", variable, channel, treeID, i);
259 260 261 262 263 264 265
		if (currCut) {
			con.Form("%s[%d]>%d && %s", variable, channel, lowRange, currCut->GetName());
		}
		else {
//			cout << "\"AculCalibScint::GetPeakMean\" cut with name \"" << beamcut << "\" was not found." << endl;
			con.Form("%s[%d]>%d", variable, channel, lowRange);
		}
266 267 268 269 270 271 272
		canvas->cd(i+1);
		hname.Form("hfull[%d][%d]", treeID, i);
		fHistFull[treeID].push_back( new TH1I(hname.Data(), "title", nbins, 0, 4096) );
		curTree->SetLineColor(1);
		curTree->Draw(var.Data(), con.Data());

		var.Form("%s[%d]>>hcut[%d][%d]", variable, channel, treeID, i);
273 274 275 276 277 278 279 280 281 282 283 284
		if (currCut) {
			con.Form("%s[%d]>%d && %s[%d]<%d && %s",
					variable, channel, fPars->GetMinChannel(treeID, i),
					variable, channel, fPars->GetMaxChannel(treeID, i),
					fPars->GetCut(beamcut, treeID)->GetName());
		}
		else {
			con.Form("%s[%d]>%d && %s[%d]<%d",
					variable, channel, fPars->GetMinChannel(treeID, i),
					variable, channel, fPars->GetMaxChannel(treeID, i));
		}

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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
		hname.Form("hcut[%d][%d]", treeID, i);
		fHistCut[treeID].push_back( new TH1I(hname.Data(), "title", nbins, 0, 4096) );
		fHistCut[treeID][i]->SetLineColor(3);
		curTree->Draw(var.Data(), con.Data(), "same");

		gPad->Update();
		fMeanPeakPos[treeID].push_back(fHistCut[treeID][i]->GetMean());
//		cout << fMeanPeakPos[treeID][i] << endl;
		fMeanPeakRMS[treeID].push_back(fHistCut[treeID][i]->GetRMS());
	}


//	cout << fMeanPeakPos.size() << "\t" << fMeanPeakPos[treeID].size() << endl;
//	cout << fMeanPeakPos.size() << "\t" << fMeanPeakPos[0].size() << endl;
//	cout << fMeanPeakPos.size() << "\t" << fMeanPeakPos[1].size() << endl;
//	cout << fMeanPeakPos.size() << "\t" << fMeanPeakPos[2].size() << endl;
//	cout << fMeanPeakPos.size() << "\t" << fMeanPeakPos[3].size() << endl;

	canvas->Update();

}

void AculCalibScint::Calibrate(TCanvas *canvas, Bool_t savefile, const char* filename, const char* option) {

	//todo change this parameter as nonconstant
	//e.g. the number of found fPeakMeans
	const Int_t noChannels = 16;

	//todo
	if (!canvas) return;
	CanDivider(canvas, noChannels);

	for (Int_t i = 0; i < fPars->GetNoRawFiles(); i++) {
		cout << i << "\t" << fMeanPeakPos[i].size() << endl;
		if (fMeanPeakPos[i].size() < noChannels || fMeanPeakRMS[i].size() < noChannels) {
//			cout << i << "\t" << fMeanPeakPos[i].size() << endl;
			cout << "\"AculCalibScint::Calibrate\" something bad here" << endl;
			return;
		}
	}

//	cout << alphas2.GetSize()+1 << endl;
//	cout << energyPoints+1 << endl;
	cout << fPars->GetNoEPoints() << endl;
	const Int_t energyPoints = fPars->GetNoEPoints();

	TString gName;
	TString gTitle;

//	if (savefile) fGraphs->Open(filename, "RECREATE");

	TF1 *fnc;

	for (Int_t i = 0; i < noChannels; i++) {
		canvas->cd(i+1);
		gCal[i] = new TGraphErrors(energyPoints+1);
//		FillGraph(gCal[i], energies.GetSize()+1, energies.GetArray(), i);
		FillGraph(gCal[i], energyPoints+1, i);
//		if (savefile) gCal[i]->Write();
//		gCal[i]->Draw("Al*");
		gCal[i]->Draw("A*");
		gName.Form("g%s%s%d\n", fPars->GetDetName(), fPars->GetParticleName(), i);
		gTitle.Form("%s %s\n", fPars->GetDetName(), fPars->GetParticleName());
//		gCal[i]->SetTitle(gTitle.Data());
		gCal[i]->SetName(gName.Data());
		gCal[i]->Fit("pol1");
		fnc = gCal[i]->GetFunction("pol1");
		fnc->SetLineColor(kRed);
		fA.push_back(fnc->GetParameter(1));
		fB.push_back(fnc->GetParameter(0));
		canvas->Update();
	}
	if (savefile) SaveClbGraphs(filename, option);

}

void AculCalibScint::FillGraph(TGraphErrors *g, Int_t npoints, /*Double_t *energies,*/ Int_t graphNumber, const char* option) {

	TString opt = option;

	//todo check if fMeanPeakPos is full

	//all available energy points and (0,0)
	g->SetPoint(0, 0., 0.);
	for (Int_t i = 0; i < npoints-1; i++) {
//		g->SetPoint(i+1, energies[i], mean[i][graphNumber]);
//		g->SetPointError(i+1, 0, meanRMS[i][graphNumber]);
//		cout << "Graph number: " << graphNumber << endl;
//		cout << graphNumber << "\t" << fMeanPeakPos[i][graphNumber] << endl;
		g->SetPoint(i+1, fMeanPeakPos[i][graphNumber], fPars->GetCalEnergy(i));
		g->SetPointError(i+1, fMeanPeakRMS[i][graphNumber], 0);
	}

//	for (Int_t j = 1; j < 4; j++) {
//			g->SetPoint(j, 0., 0.);
//	}


}

void AculCalibScint::SaveClbGraphs(const char* filename, const char* option) {

//	cout << "asdasd" << endl;
//	cout << fGraphs << endl;
	TFile fGraphs(filename, option);

//	if (fGraphs) fGraphs->Close();

//	cout << "asdasd" << endl;
//	fGraphs = new TFile(filename, option);
	cout << fGraphs.IsOpen() << endl;
		cout << fGraphs.GetName() << endl;
	fGraphs.Print();
//	if (!fGraphs->IsOpen()) {
//		printf("AculCalibCsI::SaveClbGraphs: file %s was not open.\n", filename);
//		return;
//	}

	for (Int_t i = 0; i<16; i++) {
		fGraphs.cd();
		gCal[i]->Write();
	}
	fGraphs.Close();
	return;
}

void AculCalibScint::WriteClbParameters(const char* filename) {

	std::ofstream outfile(filename);
	if ( !outfile.is_open() ) {
		printf("AculCalibCsI::WriteClbParameters: File %s was not open.\n", filename);
		return;
	}

	outfile << "#detector:\t" << fPars->GetDetName() << ",\tparticle:\t" << fPars->GetParticleName() << endl;
	outfile << "#channel\tfA\tfB" << endl;
	for (Int_t i = 0; i < (Int_t)fA.size(); i++) {
		outfile << i << "\t" << fA[i] << "\t" << fB[i] << endl;
	}

	outfile.close();
}