AEvent.cpp 9.72 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * AEvent.cpp
 *
 *  Created on: Dec 28, 2016
 *      Author: daria
 */

#include "AEvent.h"

10
AEvent::AEvent() : fNPoints(1024) {	//fNPoints is number of points in one event, 1024 or 1000
11 12 13 14 15 16

	Init();
	Reset();

}

17 18 19 20 21 22
AEvent::AEvent(const Int_t npoints) : fNPoints(npoints) {

	Init();
	Reset();
}

23 24
AEvent::~AEvent() {
	// TODO Auto-generated destructor stub
25 26
	delete fGraphSignal;
	delete fGraphCFD;
27
	delete fInputEvent;
28 29 30
}


Vratislav Chudoba's avatar
Vratislav Chudoba committed
31
void AEvent::ProcessEvent(Bool_t bSmooth) {
32 33 34 35 36 37 38 39 40

	if (fInputEvent == NULL) {
		Warning("AEvent::ProcessEvent", "Input event wasn't set. Function won't be processed.");
		return;
	}

	const Double_t *amp = fInputEvent->GetAmp();
	const Double_t *time = fInputEvent->GetTime();

41
	for(Int_t j = 0; j < fNPoints; j++) {
42 43 44 45
		fAmpPos[j] = amp[j]*(-1.);
		fTime[j] = time[j];
	}

46
	fZeroLevel = FindZeroLevel();
47
	for(Int_t j = 0; j < fNPoints; j++) {
48
		fAmpPos[j] = fAmpPos[j] - fZeroLevel;
49 50
	}

51
	SetMaxAmplitudes();
52

Vratislav Chudoba's avatar
Vratislav Chudoba committed
53 54 55
	if (bSmooth == kTRUE) {
		SmoothGraphs();
	}
56 57 58
	else {
		SetGraphs();
	}
Vratislav Chudoba's avatar
Vratislav Chudoba committed
59

60
	FindFrontProperties();
61
	SetLED();
Vratislav Chudoba's avatar
Vratislav Chudoba committed
62
//	SetGraphs();
63

64
	SetCFD();
65
	SetChargeCFD();
66
	SetChargeLED();
67
	SetChargeTF();
68
	SetToT();
69

Vratislav Chudoba's avatar
Vratislav Chudoba committed
70
//	SmoothGraphs();
71

72 73 74 75 76 77
	return;

}

void AEvent::Reset() {

78
	for (Int_t i = 0; i < fNPoints; i++) {
79 80
		fAmpPos[i] = 0;
		fTime[i] = 0;
81
		fAmpCFD[i] = 0;
82
	}
83
	fTimeLED = -100.;
84
	fEdgeSlope=0.;
85
	fEdgeXi=0.;
86
	fEdgeSlope=-100.;
87 88
	fTime10=0.;
	fTime90=0.;
89
	fTimeMid=0.;
90 91
	fToT=-100.;
	fAmpMid=0.;
92 93
	fAmpMax = 0.;
	fTimeAmpMax = 0.;
94
	fTimeCFD = -100.;
95
	fZeroLevel = 0.;
96 97
	fChargeCFD = -10.;
	fChargeLED = -10.;
98
	fTimeFront = -100.;
99 100 101 102 103 104 105 106 107 108 109 110 111
}

void AEvent::SetInputEvent(RawEvent** event) {

	if (event == 0) {
		Warning("AEvent::SetInputEvent", "Input event was set as 0.");
	}
	fInputEvent = *event;

}

void AEvent::Init() {

112 113 114 115
	fAmpPos.Set(fNPoints);
	fTime.Set(fNPoints);
	fAmpCFD.Set(fNPoints);

116 117
	fGraphSignal = new TGraph();
	fGraphCFD = new TGraph();
118
//	fGraphSmooth = new TGraph();
119 120
	fInputEvent = 0;

121 122 123
	fCFratio = 0.;
	fCFtimeDelay = 0.;

124 125 126
	fNoiseRangeMin = 0.;
	fNoiseRangeMax = 1.;

127 128 129 130
}

void AEvent::SetGraphs() {

131
	fGraphSignal->Set(fNPoints);
132 133

	for (Int_t i=0; i<fNPoints; i++) {
134
		fGraphSignal->SetPoint(i, fTime[i], fAmpPos[i]);
135 136 137 138
	}

	return;
}
139

140 141 142
void AEvent::SmoothGraphs() {

	//smoothing graph
143
	fGraphSignal->Set(fNPoints - fWinSize/2);
144

145
	Int_t winMidSize = fWinSize / 2;	
146
	Double_t tmin, tmax, meanTime, meanAmp, sumAmp;	
147 148

	for(Int_t i = winMidSize; i < fNPoints - winMidSize; ++i) {
149
   		sumAmp = 0;
150 151 152 153 154 155 156
		tmin = 0;
		tmax = 0;
		meanTime = 0;
		meanAmp = 0;
    		for(Int_t j = i - winMidSize; j <= (i + winMidSize); ++j) {
			if (j == i - winMidSize) { tmin = fTime[j]; }
			if (j == i + winMidSize) { tmax = fTime[j]; }
157
      			sumAmp += fAmpPos[j];
158 159 160 161
    		}
		meanTime = (tmax - tmin)*0.5 + tmin;
		//cout<<"mean time "<<meant<<endl;

162
    		meanAmp = sumAmp / fWinSize;
163 164
		//cout<<"mean amp "<<fAmpPos[i]<<endl;

165
    	fGraphSignal->SetPoint(i, meanTime, meanAmp);
166 167 168
		
	}	

169
//	fGraphSignal->Clone
Vratislav Chudoba's avatar
Vratislav Chudoba committed
170

171 172 173
	return;
}

174
void AEvent::SetCFD() {
175

Muzalevsky I.A's avatar
Muzalevsky I.A committed
176
	Double_t time = 0;
177
	Double_t mytime = fCFtimeDelay;
178 179 180 181
	fGraphCFD->Set(fNPoints);

	//working variables
	Double_t maxCFD = 0., minCFD = 0.;
Muzalevsky I.A's avatar
Muzalevsky I.A committed
182
	Double_t TmaxCFD = 0., TminCFD = 0.;	
183 184
	Double_t ampCFD;
	const Double_t timeStep = 0.1;
185 186 187
	Int_t i = 0; //for graph 
 
	//while goes by the graph with the step of timeStep
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	while( mytime < (200. - fCFtimeDelay) ) {

		ampCFD = fGraphSignal->Eval(mytime)*fCFratio*(-1) + fGraphSignal->Eval(mytime - fCFtimeDelay);
		fGraphCFD->SetPoint(i, mytime, ampCFD);

		//point for max CFD amplitude
		if( ampCFD > maxCFD) {
			maxCFD = ampCFD;
			TmaxCFD = mytime;
		}

		//point for min CFD amplitude
		if( ampCFD < minCFD) {
			minCFD = ampCFD;
			TminCFD = mytime;
		}

		i++;
		mytime = mytime + timeStep;
	}
208

209
	//looking for the first point with the closest values to 0 and writing to fTimeCFD
Muzalevsky I.A's avatar
Muzalevsky I.A committed
210
	time = TminCFD;
211
	while( (fGraphCFD->Eval(time) <= 0) && (time <= TmaxCFD) /*&& (time >= TminCFD)*/ ) {
Muzalevsky I.A's avatar
Muzalevsky I.A committed
212 213 214 215
		fTimeCFD = time;
		time = time + timeStep;
	}

Muzalevsky I.A's avatar
Muzalevsky I.A committed
216
}
Muzalevsky I.A's avatar
Muzalevsky I.A committed
217

218 219
void AEvent::FindFrontProperties() {

220 221 222 223 224
	//in percents
	const Double_t minHeight = 0.1;
	const Double_t maxHeight = 0.9;

	const Double_t timeStep = 0.05;	//in ns
225
	Double_t time = 0;			//in ns
226
	Double_t mytime = 0.;
227

228
	if (!fGraphSignal) {
229
		Warning("AEvent::FindFrontProperties", "Graph was not set");
230 231 232
		return;
	}

233
	//TODO search of minimum should be done from the lower edge on the time axis
234

235
	while (fGraphSignal->Eval(time) <= maxHeight*fAmpMax && time<200.) {
236
		fTime90 = time;
237 238 239 240 241 242
		time = time + timeStep;
	};

	time = fTime90;
	while( fGraphSignal->Eval(time) >= minHeight*fAmpMax && time>0) {
		fTime10 = time;
243
		time = time - timeStep;
244
	}
245

246
	TF1 *fit1 = new TF1("fit1","[1]*x+[0]");	//function for one parameter fitting in the range of pmin-pmax
247
	fit1->SetRange(fTime10,fTime90);
248

249
	fGraphSignal->Fit(fit1,"RQN","goff");
250

251
	fEdgeSlope = fit1->GetParameter(1);
252
	fEdgeXi = fit1->GetChisquare();
253

254
	fTimeMid = (fTime90 -fTime10)*0.5 + fTime10; 	//time point between fTime90 and fTime10
255
	fAmpMid = fGraphSignal->Eval(fTimeMid);
256

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	//adding point where fit function crosses zero
	Double_t a = 0, b = 0;
	TF1 *line = new TF1("line","[1]*x + [0]");
	a = fit1->GetParameter(1);
	b = fit1->GetParameter(0);
	line->SetParameter(0,b);
	line->SetParameter(1,a);	
	//cout<<"par 0 "<<b<<endl;
	//cout<<"par 1 "<<a<<endl;

	if( a!= 0. && b!= 0. ) {	//in case of fit data is empty
		while(line->Eval(mytime) <= 0 && mytime <= 200.) {	
			//cout<< "mytime "<<mytime<<endl;
			fTimeFront = mytime;
			mytime = mytime + timeStep;
		}
	}

275 276
	delete fit1;
}
277

278
Double_t AEvent::FindZeroLevel() {
279 280 281

	SetGraphs();
	Double_t correction = 0;
282
	TF1 *fit1 = new TF1("fit1","[0]");	//function for one parameter fitting in the range of pmin-pmax
283
	fit1->SetRange(fNoiseRangeMin,fNoiseRangeMax);
284 285 286 287 288

	if (!fGraphSignal) {
		Warning("AEvent::FindZeroLevel", "Graph was not set");
		return 0;
	}
289

290 291
	fGraphSignal->Fit(fit1,"RQN","goff");
	correction = fit1->GetParameter(0);
292

293 294
	delete fit1;
	return correction;
295
}
296

297
void AEvent::SetChargeCFD(Int_t tmin, Int_t tmax) { // tmin = -3, tmax = 17
298
	
299 300 301 302
	Double_t integral = 0.;					//voltage
	Double_t time_sig = 0;					//approximate signal duration in seconds
	const Double_t res = 50.; 				//resistance 50 Om
	time_sig = (double)(-tmin + tmax)*(1e-9);
303

304
	/*for(Int_t i = 0; i < fNPoints; i++) {
305
		if( fTime[i] > (fTimeCFD + tmin) && fTime[i] < (fTimeCFD + tmax) ) {
306 307
			integral = integral + fAmpPos[i];
		}
308 309 310 311 312 313 314 315 316 317
	}*/

	Int_t imin = 0, imax = 0;

	Int_t i = 0;
	while ( fTime[i] < (fTimeCFD + tmin) && (i < (fGraphSignal->GetN()-1)) ) { imin = i; i++; }
	while ( fTime[i] < (fTimeCFD + tmax) && (i < (fGraphSignal->GetN()-1)) ) { imax = i; i++; }

	integral = fGraphSignal->Integral(imin, imax);

318
	fChargeCFD = integral*time_sig/res;
319 320 321 322 323

	return;

}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
void AEvent::SetChargeLED(Int_t tmin, Int_t tmax) { // tmin = -3, tmax = 17
	
	Double_t integral = 0.;					//voltage
	Double_t time_sig = 0;					//approximate signal duration in seconds
	const Double_t res = 50.; 				//resistance 50 Om
	time_sig = (double)(-tmin + tmax)*(1e-9);

/*	for(Int_t i = 0; i < fNPoints; i++) {
		if( fTime[i] > (fTimeLED + tmin) && fTime[i] < (fTimeLED + tmax) ) {
			integral = integral + fAmpPos[i];
		}
	}*/

	Int_t imin = 0, imax = 0;

	Int_t i = 0;
	while ( fTime[i] < (fTimeLED + tmin) && (i < (fGraphSignal->GetN()-1)) ) { imin = i; i++; }
341
//	i = 0;
342 343 344 345 346 347 348 349 350 351
	while ( fTime[i] < (fTimeLED + tmax) && (i < (fGraphSignal->GetN()-1)) ) { imax = i; i++; }

	integral = fGraphSignal->Integral(imin, imax);

	fChargeLED = integral*time_sig/res;

	return;

}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
void AEvent::SetChargeTF(Int_t tmin, Int_t tmax) { // tmin = -3, tmax = 17
	
	Double_t integral = 0.;					//voltage
	Double_t time_sig = 0;					//approximate signal duration in seconds
	const Double_t res = 50.; 				//resistance 50 Om
	time_sig = (double)(-tmin + tmax)*(1e-9);

	Int_t imin = 0, imax = 0;

	Int_t i = 0;
	while ( fTime[i] < (fTimeFront + tmin) && (i < (fGraphSignal->GetN()-1)) ) { imin = i; i++; }
//	i = 0;
	while ( fTime[i] < (fTimeFront + tmax) && (i < (fGraphSignal->GetN()-1)) ) { imax = i; i++; }

	integral = fGraphSignal->Integral(imin, imax);

	fChargeTF = integral*time_sig/res;

	return;

}

Muzalevsky I.A's avatar
Muzalevsky I.A committed
374
Double_t AEvent::GetfCFD() {
375
		return fTimeCFD;
Muzalevsky I.A's avatar
Muzalevsky I.A committed
376 377
}

Muzalevsky I.A's avatar
Muzalevsky I.A committed
378 379 380 381
Double_t AEvent::GetfLED() {
		return fTimeLED;
}

Muzalevsky I.A's avatar
Muzalevsky I.A committed
382 383 384
Double_t AEvent::GetOnefTime(Int_t i) {
		return fTime[i];
}
385

Muzalevsky I.A's avatar
Muzalevsky I.A committed
386 387
Double_t AEvent::GetOnefAmpPos(Int_t i) {
		return fAmpPos[i];
388
}
389 390

Double_t AEvent::GetT_10() {
391
		return fTime10;
392 393 394
}

Double_t AEvent::GetT_90() {
395
		return fTime90;
396 397 398
}

Double_t AEvent::GetEdgeSlope() {
399 400
		return fEdgeSlope;
}
401

402 403 404 405
Double_t AEvent::GetEdgeXi() {
		return fEdgeXi;
}

406 407 408 409 410 411 412 413 414 415 416 417 418
void AEvent::SetMaxAmplitudes() {
	Double_t maxAmp = 0.;
	Double_t maxAmpT = 0.;

	maxAmp = fAmpPos[0];
	for(Int_t j=0; j < fNPoints; j++) {
		if(fAmpPos[j] > maxAmp) {
			maxAmp = fAmpPos[j];
			maxAmpT = fTime[j];
		}
	}
	fAmpMax = maxAmp;
	fTimeAmpMax = maxAmpT;
419

420
}
421 422 423 424

void AEvent::SetLED(Double_t threshold) {
	
	Double_t time = 0;
425 426
	const Double_t timeStep = 0.05;
	while( time < fTimeAmpMax && fGraphSignal->Eval(time) <= threshold ) {
427 428 429
		fTimeLED = time;
		time = time + timeStep;
	}
430 431
	//fTimeLED = time; найти номера точек которые лежат на 3 нс раньше и на 20 нс позже чем точка с временем timeled (искать точки в пределах условных)
	// сделать через функцию getpoint и цикл по точкам от 
432 433

}
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

void AEvent::SetToT() {

	Double_t time = fTimeMid;
	Double_t timeBack = 0;
	const Double_t ns = 15.; //withing this interval signal should end for sure, in nanosec

	const Double_t timeStep = 0.05;

	//cout<<"fAmpMid "<<fAmpMid<<endl;
	while( fGraphSignal->Eval(time) >= fAmpMid && time < fTimeMid + ns) {
		//cout<<"timeback "<<timeBack<<endl;
		//cout<<"fGraphSignal->Eval(time) "<<fGraphSignal->Eval(time)<<endl;
		//cout<<endl;
		//if(timeBack>150.) {return;}
		timeBack = time;
		time = time + timeStep;
	}
	//cout<<"timeback "<<timeBack<<endl;

	fToT = timeBack - fTimeMid;
	//cout<<"ftot "<<fToT<<endl;



}