fillChain.cxx 24.3 KB
Newer Older
1 2
#if !defined(__CLING__)

3 4 5
#include "TSystem.h"
#include "TFile.h"
#include "TTree.h"
6
#include "TVector3.h"
7 8 9 10 11 12
#include "TH2F.h"
#include "../src/TNeEvent.h"
#include "../src/TNeDet16.h"
#include "TStopwatch.h"

#endif
13 14 15 16

using std::cout;
using std::endl;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Int_t GetClustersMWPC(unsigned short n, unsigned short *x)
{
	Int_t clusters = 1;

	if(n == 0) clusters = 0.;
	else if(n > 1 && n<=32)
	{
		for(Int_t i = 1; i < n; ++i)
		{
//			if (x[i]>50 || x[i-1]>50) continue;
			if( (abs(x[i] - x[i-1]) > 1) && (abs(x[i] - x[i-1]) < 32) ) clusters += 1.;
		}
	}
	return clusters;
} //--------------------------------------------------------------------

33
Double_t GetClusterPositionMWPC(unsigned short n, unsigned short *x, Float_t wireStep, Float_t planeOffset=0.)
34 35 36 37 38 39 40 41 42
{
	Double_t position = -100.;




//	if(n == 0) clusters = 0.;
	if(n > 0 && n<=32)
	{
43 44 45

//		x2p = (revent->x2[0] + 0.5 - 16)*kMWPCwireStepX2 + MWPC2_X_offset;
		position = (x[0]+n/2. + 0.5 -16)*wireStep + planeOffset;
46 47 48 49 50 51
//		cout << n << endl;
	}
	return position;
} //--------------------------------------------------------------------

void fillTree(const TString beam = "he", Int_t nofile = 0, const Int_t noevents = 0) {
52 53

	TString inFile;
54 55
	if (beam.Contains("he")) inFile.Form("~/data/exp1804/h5_14_%02d.root", nofile);
	if (beam.Contains("be")) inFile.Form("~/data/exp1804/be10_03_%d0.root", nofile);		//files 00,10,...,90
56 57 58
																//where 70 is run 01
																//		80 is run 02
																//		90 is run 05
59
//	inFile.Form("~/data/exp1804/calib/si_20_03.root");
60 61

	TString outFile;
62 63
	if (beam.Contains("he")) outFile.Form("~/data/exp1804/h5_14_%02d_calib.root", nofile);
	if (beam.Contains("be")) outFile.Form("~/data/exp1804/be10_03_%d0_calib.root", nofile);	//files 00,10,...,60
64 65 66
																	//where 70 is run 01
																	//		80 is run 02
																	//		90 is run 05
67
//	outFile.Form("~/data/exp1804/calib/si_20_03_calib.root");
68 69 70 71

	cout << "Input file: " << inFile << endl;
	cout << "Output file: " << outFile << endl;

72 73 74 75
	///////////////////////////////////////////////////
	// Input file initialization
	///////////////////////////////////////////////////

76 77 78 79 80 81
	TFile *fr = new TFile(inFile);
	TTree *tr = (TTree*)fr->Get("AnalysisxTree");

	TNeEvent *revent = new TNeEvent();
	tr->SetBranchAddress("NeEvent.", &revent);

82 83 84 85 86

	///////////////////////////////////////////////////
	// Output file initialization
	///////////////////////////////////////////////////

87 88 89
	TFile *fw = new TFile(outFile, "RECREATE");
	TTree *tw = new TTree("cal", "Calibrated information");

90 91 92 93
	///////////////////////////////////////////////////
	// Output tree variables
	///////////////////////////////////////////////////

94 95
	Int_t trigger;

96 97
	Float_t SQ20E[16];
	Float_t SQ20Ecorr[16];
98
	Float_t SQ20EcorrHit[16];
99
	Float_t SQ20EcorrHitC[16];
100
	Float_t SQ20Esum;
101 102 103 104 105 106 107

	Float_t SQ20time[16];
	Int_t SQ20timeMult;
	Float_t SQLXtime[32];
	Int_t SQLXtimeMult;
//	Float_t SQLXtimeSum;

108 109
	Float_t SQLXE[32];
	Float_t SQLXEsum;
110 111 112 113

	Float_t SQLXEtimeFiltered[32];
	Float_t SQLXEtimeFilteredSum;

114 115
	Float_t SQLYE[16];
	Float_t SQLYEsum;
116 117 118 119 120
	Float_t SQLYtime[16];
	Int_t SQLYtimeMult;

	Float_t SQLYEtimeFiltered[16];
	Float_t SQLYEtimeFilteredSum;
121 122 123 124

	Int_t SQLXmult;
	Int_t SQLYmult;
	Int_t SQ20EcorrMult;
125
	Int_t SQ20EcorrHitMult;
126 127 128 129 130
	Int_t SQ20EcorrHitCMult;

	Bool_t CsI_L_veto;
	Int_t expectedThinStrip;

131 132 133 134 135

	Float_t SQRXE[32];
	Float_t SQRXEsum;
	Int_t SQRXmult;
	Float_t TOF, dEbeam;
136 137 138 139

	Float_t tF5;

	//MWPC, wire multiplicity
140 141
	Float_t x1p, y1p, x2p, y2p, xt, yt;

142 143 144 145
	//MWPC, cluster multiplicity
	Int_t x1MultC, x2MultC, y1MultC, y2MultC;
	Float_t x1c, y1c, x2c, y2c, xtc, ytc;

146 147 148 149 150 151 152 153 154 155 156 157

	//left 1 mm position
	Float_t x1mm, y1mm;

	Float_t xThin, yThin;

	Int_t mapXbin, mapYbin;

	TVector3 vHit1mm;
	TVector3 vNorm(0.,0.,1.);
	Double_t angleLeft;

158 159 160 161
	///////////////////////////////////////////////////
	// Output tree branches initialization
	///////////////////////////////////////////////////

162 163
	tw->Branch("trigger",&trigger,"trigger/I");

164 165 166 167 168 169 170 171 172
	tw->Branch("angleLeft",&angleLeft,"angleLeft/D");
	tw->Branch("x1mm",&x1mm,"x1mm/F");
	tw->Branch("y1mm",&y1mm,"y1mm/F");
	tw->Branch("xThin",&xThin,"xThin/F");
	tw->Branch("yThin",&yThin,"yThin/F");

	tw->Branch("mapXbin",&mapXbin,"mapXbin/I");
	tw->Branch("mapYbin",&mapYbin,"mapYbin/I");

173 174
	tw->Branch("SQ20E",SQ20E,"SQ20E[16]/F");
	tw->Branch("SQ20Ecorr",SQ20Ecorr,"SQ20Ecorr[16]/F");
175
	tw->Branch("SQ20EcorrHit",SQ20EcorrHit,"SQ20EcorrHit[16]/F");
176
	tw->Branch("SQ20EcorrHitC",SQ20EcorrHitC,"SQ20EcorrHitC[16]/F");
177
	tw->Branch("SQ20Esum",&SQ20Esum,"SQ20Esum/F");
178 179 180 181 182 183 184 185 186 187 188

	tw->Branch("SQ20time",SQ20time,"SQ20time[16]/F");
	tw->Branch("SQ20timeMult",&SQ20timeMult,"SQ20timeMult/I");
//	tw->Branch("SQLXtimeSum",&SQLXtimeSum,"SQLXtimeSum/F");

	tw->Branch("SQLXtime",SQLXtime,"SQLXtime[32]/F");
	tw->Branch("SQLXtimeMult",&SQLXtimeMult,"SQLXtimeMult/I");

	tw->Branch("SQLYtime",SQLYtime,"SQLYtime[16]/F");
	tw->Branch("SQLYtimeMult",&SQLYtimeMult,"SQLYtimeMult/I");

189 190
	tw->Branch("SQLXE",SQLXE,"SQLXE[32]/F");
	tw->Branch("SQLXEsum",&SQLXEsum,"SQLXE/F");
191 192 193 194 195 196 197

	tw->Branch("SQLXEtimeFiltered",SQLXEtimeFiltered,"SQLXEtimeFiltered[32]/F");
	tw->Branch("SQLXEsumtimeFilteredSum",&SQLXEtimeFilteredSum,"SQLXEtimeFilteredSum/F");

	tw->Branch("SQLYEtimeFiltered",SQLYEtimeFiltered,"SQLYEtimeFiltered[16]/F");
	tw->Branch("SQLYEsumtimeFilteredSum",&SQLYEtimeFilteredSum,"SQLYEtimeFilteredSum/F");

198 199 200 201 202 203
	tw->Branch("SQLYE",SQLYE,"SQLYE[16]/F");
	tw->Branch("SQLYEsum",&SQLYEsum,"SQLYEsum/F");

	tw->Branch("SQLXmult",&SQLXmult,"SQLXmult/I");
	tw->Branch("SQLYmult",&SQLYmult,"SQLYmult/I");
	tw->Branch("SQ20EcorrMult",&SQ20EcorrMult,"SQ20EcorrMult/I");
204 205 206 207 208
	tw->Branch("SQ20EcorrHitCMult",&SQ20EcorrHitCMult,"SQ20EcorrHitCMult/I");
//	tw->Branch("SQ20EcorrMult",&SQ20EcorrMult,"SQ20EcorrMult/I");

	tw->Branch("CsI_L_veto", &CsI_L_veto, "CsI_L_veto/O");
	tw->Branch("expectedThinStrip",&expectedThinStrip,"expectedThinStrip/I");
209 210 211 212 213 214 215

	tw->Branch("SQRXE",SQRXE,"SQRXE[32]/F");
	tw->Branch("SQRXEsum",&SQRXEsum,"SQRXE/F");
	tw->Branch("SQRXmult",&SQRXmult,"SQRXmult/I");

	tw->Branch("TOF", &TOF, "TOF/F");
	tw->Branch("dEbeam", &dEbeam, "dEbeam/F");
216
	tw->Branch("tF5", &tF5, "tF5/F");
217 218 219 220 221 222 223 224 225

	tw->Branch("x1p", &x1p, "x1p/F");
	tw->Branch("y1p", &y1p, "y1p/F");
	tw->Branch("x2p", &x2p, "x2p/F");
	tw->Branch("y2p", &y2p, "y2p/F");

	tw->Branch("xt", &xt, "xt/F");
	tw->Branch("yt", &yt, "yt/F");

226 227 228 229 230 231 232 233 234 235 236 237 238
	tw->Branch("x1MultC",&x1MultC,"x1MultC/I");
	tw->Branch("x2MultC",&x2MultC,"x2MultC/I");
	tw->Branch("y1MultC",&y1MultC,"y1MultC/I");
	tw->Branch("y2MultC",&y2MultC,"y2MultC/I");

	tw->Branch("x1c", &x1c, "x1c/F");
	tw->Branch("y1c", &y1c, "y1c/F");
	tw->Branch("x2c", &x2c, "x2c/F");
	tw->Branch("y2c", &y2c, "y2c/F");

	tw->Branch("xtc", &xtc, "xtc/F");
	tw->Branch("ytc", &ytc, "ytc/F");

239 240 241 242 243 244

	Long64_t nevents;
	if (noevents == 0) nevents = tr->GetEntries();
	else nevents = noevents;
	if (nevents > tr->GetEntries()) nevents = tr->GetEntries();

245 246 247 248 249
	///////////////////////////////////////////////////
	// calibration coefficients and thickness map
	///////////////////////////////////////////////////


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
//	TNeDet16 *pSQX_L_EC = new TNeDet16("SQX_L_EC");
//	TNeDet16 pSQX_L_EC("../SQX_L_EC");
	TNeDet16 pSQX_L_EC("./SQX_L");
	pSQX_L_EC.ReadData();

	TNeDet16 pSQX_L_TC("./SQX_L_time");
	pSQX_L_TC.ReadData();

	TNeDet16 pSQY_L_EC("./SQY_L");
	pSQY_L_EC.ReadData();

	TNeDet16 pSQX_R_EC("../SQX_R_EC");
	pSQX_R_EC.ReadData();

	TNeDet16 pSQ20_EC("./sq20_58");
	pSQ20_EC.ReadData();

	Float_t energy = 0;

	cout << nevents << " entries will be treated." << endl;

//	TFile fThickness("thicknessPreliminary.root", "OPEN");
	TFile fThickness("thicknessFinal.root", "OPEN");
	fr->cd();
	TH2F *hThickness = new TH2F(*(TH2F*)fThickness.Get("hTh"));
275
//	hThickness->Draw("col");
276

277 278 279 280 281

	///////////////////////////////////////////////////
	// Parameters related to geometry
	///////////////////////////////////////////////////

282 283 284 285 286
	//all distances in mm

	//MWPC
	const Float_t kMWPCz1 = -815.;	//z coordinate of the center of MWPC1
	const Float_t kMWPCz2 = -270.;	//z coordinate of the center of MWPC2
287

288 289 290 291 292 293
	//step between two wires;
	//sign mean onward (+) or rearward (-) to physical axis
	const Float_t kMWPCwireStepX1 = -1.25;
	const Float_t kMWPCwireStepY1 = 1.25;		//step between two wires
	const Float_t kMWPCwireStepX2 = -1.25;		//step between two wires
	const Float_t kMWPCwireStepY2 = 1.25;		//step between two wires
294 295

	//offsets taken from S. Krupko
296 297
	const Float_t MWPC1_X_offset = -1.19;
	const Float_t MWPC1_Y_offset = -2.12;
298
	const Float_t MWPC2_X_offset = 0.2;
299
	const Float_t MWPC2_Y_offset = -1.02;
300 301 302 303

	//left telescope
	const Float_t z1mm = 230.;
	const Float_t zThin = 230.-53.6;
304 305 306
	//TODO: check signs and values
//	const Float_t xThinOffset = -3.;
//	const Float_t yThinOffset = -1.8;
307 308


309 310
	//TODO: add sign taking into account direction of numbering
	const Int_t kSQL_X_strips = -32;
311 312 313
	const Int_t kSQL_Y_strips = 16;
	const Int_t kSQL_20_strips = 16;

314 315 316 317
	//1mm left size
	const Float_t kSQL_X_size = 60.;
	const Float_t kSQL_Y_size = 60.;

318
	//thin detector
319
	const Double_t kSQ20_norm_thickness = 20.;
320 321
	const Float_t kSQ20_X_size = 50.;
	const Float_t kSQ20_Y_size = 50.;
322

323 324
	const Double_t kThinXoffset = -1.;
	const Double_t kThinYoffset = -1.8;
325 326 327 328 329 330 331 332 333


	//left CsI detectors
	const UShort_t CsIleftThr = 180;

	///////////////////////////////////////////////////
	// Individual thresholds
	///////////////////////////////////////////////////

334

335 336 337 338 339
	Float_t timeCorr[16] = {0, -2, 0, 2,
							-2.5, 5, -6, 4,
							7, 3, 3, 4,
							6, 4, -4, 13
	};
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
	Int_t thinChThresholds[16] = {
		113, 121, 125, 125,
		125, 132, 130, 115,
		118, 128, 132, 130,
		130, 130, 130, 112
	};

	Int_t thickLYChThresholds[16] = {
		4000, 130, 82, 110,
		87, 112, 82, 108,
		82, 112, 82, 108,
		80, 110, 90, 105,
	};

	Int_t thickLXChThresholds[32] = {
		60, 45, 45, 45,
		45, 4000, 4000, 45,
		45, 45, 45, 45,
		45, 45, 45, 45,
		/////
		45, 45, 45, 45,
		45, 45, 45, 45,
		45, 45, 45, 45,
		45, 45, 45, 60
	};

	Int_t minTimeLthin[16] {
		410, 410, 420, 415,
		420, 410, 415, 415,
		420, 420, 420, 420,
		410, 425, 0, 0
	};

	Int_t maxTimeLthin[16] {
		520, 525, 520, 530,
		520, 530, 525, 525,
		530, 530, 530, 520,
		510, 525, 0, 0
	};

	Double_t minTimeLX[32] {
		0, 0, 325, 328,
		330, 0, 0, 330,
		326, 328, 326, 335,
		326, 328, 328, 330,
		//
		420, 425, 420, 420,
		420, 420, 420, 420,
		430, 425, 420, 420,
		415, 415, 425, 415
	};

	Double_t maxTimeLX[32] {
		0, 0, 345, 345,
		350, 0, 0, 350,
		345, 345, 345, 350,
		340, 342, 345, 345,
		//
		460, 460, 460, 460,
		460, 460, 460, 460,
		460, 460, 460, 460,
		460, 460, 460, 453
	};

405 406 407 408 409 410 411
	fw->cd();

	//////////////////////////////////
	//event processing
	//////////////////////////////////

	for (Int_t eventNo = 0; eventNo < nevents; eventNo++) {
412

413
		tr->GetEvent(eventNo);
414

415 416 417 418
		trigger = revent->trigger;

		if (trigger != 3) continue;

419 420
		//reset values to be written

421 422 423 424 425 426
		SQ20Esum = 0.;
		SQLXEsum = 0.;
		SQLYEsum = 0.;
		SQRXEsum = 0.;
		SQRXmult = 0;

427 428 429
		SQLXEtimeFilteredSum = 0.;
		SQLYEtimeFilteredSum = 0.;

430 431 432
		SQLXmult = 0;
		SQLYmult = 0;
		SQ20EcorrMult = 0;
433
		SQ20EcorrHitMult = 0;
434
		SQ20EcorrHitCMult = 0;
435 436 437 438

		SQ20timeMult = 0;
		SQLXtimeMult = 0;
		SQLYtimeMult = 0;
439

440 441 442
		CsI_L_veto = kFALSE;
		expectedThinStrip = -1;

443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
		x1p = -100.;
		y1p = -100.;
		x2p = -100.;
		y2p = -100.;
		xt = -100.;
		yt = -100.;

		x1c = -100.;
		y1c = -100.;
		x2c = -100.;
		y2c = -100.;
		xtc = -100.;
		ytc = -100.;

		x1MultC = 0;
		y1MultC = 0;
		x2MultC = 0;
		y2MultC = 0;

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

		///////////////////////////////////////////////
		//beam diagnostics
		///////////////////////////////////////////////

			///////////////////////////////////////////////
			//TOF
			///////////////////////////////////////////////

		dEbeam = 0.;
		TOF = 0.;
		if (revent->tF5[0]>0 && revent->tF5[1]>0 && revent->tF5[2]>0 && revent->tF5[3]>0
				&& revent->tF3[0]>0 && revent->tF3[1]>0 && revent->tF3[2]>0 && revent->tF3[3]>0
				&& revent->F5[0]>0 && revent->F5[1]>0 && revent->F5[2]>0 && revent->F5[3]>0
//				&& ( (revent->tF5[0]+revent->tF5[1]+revent->tF5[2]+revent->tF5[3]) - (revent->tF3[0]+revent->tF3[1]+revent->tF3[2]+revent->tF3[3]) )/4.*0.125+89.165<200
//				&& ( (tF5[0]+tF5[1]+tF5[2]+tF5[3]) - (tF3[0]+tF3[1]+tF3[2]+tF3[3]) )/4.*0.125+89.165>100
//				&& (F5[0]+F5[1]+F5[2]+F5[3])/4. < 2500
		) {
			dEbeam = (revent->F5[0]+revent->F5[1]+revent->F5[2]+revent->F5[3])/4.;
			TOF = ( (revent->tF5[0]+revent->tF5[1]+revent->tF5[2]+revent->tF5[3]) - (revent->tF3[0]+revent->tF3[1]+revent->tF3[2]+revent->tF3[3]) )/4.*0.125+89.165;
		}

484
		if (TOF<166. || TOF>181.) continue;
485 486 487 488 489 490 491 492 493 494 495

			///////////////////////////////////////////////
			//MWPC's
			///////////////////////////////////////////////


		Bool_t flagMWPC = 1;

		tF5 = 0.125*(revent->tF5[0]+revent->tF5[1]+revent->tF5[2]+revent->tF5[3])/4.;
//		tF5 = 0.125*revent->tF5[0];

496 497
		//check for MWPC timing
		//TODO: parametrization of time thresholds
498 499 500 501 502 503
		if(revent->nx1>10 || revent->ny1>10 || revent->nx2>10 || revent->ny2>10) flagMWPC=0;
		if((0.125*revent->tMWPC[0]-tF5)<60 || (0.125*revent->tMWPC[0]-tF5)>77) flagMWPC=0;
		if((0.125*revent->tMWPC[1]-tF5)<60 || (0.125*revent->tMWPC[1]-tF5)>80) flagMWPC=0;
		if((0.125*revent->tMWPC[2]-tF5)<70 || (0.125*revent->tMWPC[2]-tF5)>90) flagMWPC=0;
		if((0.125*revent->tMWPC[3]-tF5)<60 || (0.125*revent->tMWPC[3]-tF5)>80) flagMWPC=0;

504 505
		if (flagMWPC==0) continue;

506 507 508
		//MWPC: work with wire multiplicity equal to 1
		if (flagMWPC!=0) {
			if (revent->x1[0]<1000 && revent->y1[0]<1000 && revent->nx1==1 && revent->ny1==1) {
509 510
//				x1p = (revent->x1[0]+0.5)*1.25-20. + MWPC1_X_offset;
//				y1p = (revent->y1[0]+0.5)*1.25-20. + MWPC1_Y_offset;
511

512 513
				x1p = (revent->x1[0] + 0.5 - 16)*kMWPCwireStepX1 + MWPC1_X_offset;
				y1p = (revent->y1[0] + 0.5 - 16)*kMWPCwireStepY1 + MWPC1_Y_offset;
514 515 516
			}

			if (revent->x2[0]<1000 && revent->y2[0]<1000 && revent->nx2==1 && revent->ny2==1) {
517 518 519 520 521
//				x2p = (revent->x2[0]+0.5)*1.25-20. + MWPC2_X_offset;
//				y2p = (revent->y2[0]+0.5)*1.25-20. + MWPC2_Y_offset;

				x2p = (revent->x2[0] + 0.5 - 16)*kMWPCwireStepX2 + MWPC2_X_offset;
				y2p = (revent->y2[0] + 0.5 - 16)*kMWPCwireStepY2 + MWPC2_Y_offset;
522 523 524
			}

			if (x1p > -50. && y1p > -50. && x2p > -50. && y2p > -50.) {
525 526
//				xt = x1p - (x1p - x2p)*((l12+lt)/l12);
//				yt = y1p - (y1p - y2p)*((l12+lt)/l12);
527 528
//				xt = (l12*x1p + (l12+lt)*(x2p-x1p))/(l12 - (x2p-x1p)*TMath::Tan(TMath::DegToRad()*12.));
//				yt = (y2p-y1p)*(xt-x1p)/(x2p-x1p) + y1p;
529 530 531 532

				xt = x1p - (x2p -x1p)*kMWPCz1/(kMWPCz2-kMWPCz1);
				yt = y1p - (y2p -y1p)*kMWPCz1/(kMWPCz2-kMWPCz1);

533 534 535 536 537
			}
		}//if wire multiplicity == 1

//		cout << flagMWPC << endl;

538
		//MWPC: work with cluster multiplicity equal to 1
539 540 541 542 543 544
		if (flagMWPC!=0) {
			x1MultC = GetClustersMWPC(revent->nx1, revent->x1);
			x2MultC = GetClustersMWPC(revent->nx2, revent->x2);
			y1MultC = GetClustersMWPC(revent->ny1, revent->y1);
			y2MultC = GetClustersMWPC(revent->ny2, revent->y2);

545
			//set wire step as input parameter for the function
546
			if (x1MultC==1 && y1MultC==1) {
547 548
				x1c = GetClusterPositionMWPC(revent->nx1, revent->x1, kMWPCwireStepX1, MWPC1_X_offset);
				y1c = GetClusterPositionMWPC(revent->ny1, revent->y1, kMWPCwireStepY1, MWPC1_Y_offset);
549 550 551
			}

			if (x2MultC==1 && y2MultC==1) {
552 553
				x2c = GetClusterPositionMWPC(revent->nx2, revent->x2, kMWPCwireStepX2, MWPC2_X_offset);
				y2c = GetClusterPositionMWPC(revent->ny2, revent->y2, kMWPCwireStepY2, MWPC2_Y_offset);
554 555 556
			}

			if (x1c > -50. && y1c > -50. && x2c > -50. && y2c > -50.) {
557 558
//				xtc = x1c - (x1c - x2c)*((l12+lt)/l12);
//				ytc = y1c - (y1c - y2c)*((l12+lt)/l12);
559 560
//				xtc = (l12*x1c + (l12+lt)*(x2c-x1c))/(l12 - (x2c-x1c)*TMath::Tan(TMath::DegToRad()*12.));
//				ytc = (y2c-y1c)*(xtc-x1c)/(x2c-x1c) + y1c;
561 562 563

				xt = x1c - (x2c -x1c)*kMWPCz1/(kMWPCz2-kMWPCz1);
				yt = y1c - (y2c -y1c)*kMWPCz1/(kMWPCz2-kMWPCz1);
564 565 566
			}
		}

567

568 569 570 571 572 573 574
		if (x1MultC!=1 || y1MultC!=1 || x2MultC!=1 || y2MultC!=1) continue;



		///////////////////////////////////////////////
		//work with Si detectors
		///////////////////////////////////////////////
575 576 577

//cout << endl;

578 579 580 581 582 583
		//CdI left -veto:
		for(Int_t i = 0; i<16; i++) {
			if (revent->CsI_L[i]>CsIleftThr) CsI_L_veto = kTRUE;
		}


584 585 586 587
		for (Int_t i = 0; i < 32; i++) {
//			cout << pSQX_L_EC.Energy(1, i) << endl;
			SQLXE[i] = 0;
			SQRXE[i] = 0;
588 589
			SQLXtime[i] = 0;
			SQLXEtimeFiltered[i] = 0.;
590

591 592
			if (revent->SQX_L[i] > thickLXChThresholds[i]) {
				energy = pSQX_L_EC.Energy(revent->SQX_L[i], i);
593 594 595 596 597
				SQLXE[i] = energy;
				SQLXEsum += SQLXE[i];
				SQLXmult++;
			}

598 599 600
			if (i<16 && i!=0 && i!=1
					&& SQLXE[i]>0
					&& revent->tSQX_L[i]*0.3>minTimeLX[i] && revent->tSQX_L[i]*0.3<maxTimeLX[i]) {
601 602 603 604 605 606 607 608 609
				SQLXtime[i] = revent->tSQX_L[i]*0.3;
				SQLXtimeMult++;
//				SQLXtimeSum = SQLXtimeSum + SQLXtime[i];
				SQLXEtimeFiltered[i]=energy;
				SQLXEtimeFilteredSum = SQLXEtimeFilteredSum + SQLXEtimeFiltered[i];
//				cout << SQLXtime[i] << endl;
//				cout << i << endl;
			}
			if (i>15
610 611
					&& SQLXE[i]>0
					&& revent->tSQX_L[i]*0.3>minTimeLX[i] && revent->tSQX_L[i]*0.3<maxTimeLX[i]) {
612 613 614 615 616 617 618 619
				SQLXtime[i] = revent->tSQX_L[i]*0.3;
				SQLXtimeMult++;
//				SQLXtimeSum = SQLXtimeSum + SQLXtime[i];
				SQLXEtimeFiltered[i] = energy;
				SQLXEtimeFilteredSum = SQLXEtimeFilteredSum + SQLXEtimeFiltered[i];
////				cout << SQLXtime[i] << endl;
			}

620 621 622 623 624 625 626
			energy = pSQX_R_EC.Energy(revent->SQX_R[i], i);

			if (energy > 1.0) {
				SQRXE[i] = energy;
				SQRXEsum += SQRXE[i];
				SQRXmult++;
			}
627
		}// for 32
628 629 630 631 632 633

		for (Int_t i = 0; i < 16; i++) {
			//			cout << pSQX_L_EC.Energy(1, i) << endl;

			SQLYE[i] = 0.;
			SQ20E[i] = 0.;
634
			SQ20time[i] = 0.;
635
			SQ20Ecorr[i] = 0.;
636
			SQ20EcorrHit[i] = 0.;
637
			SQ20EcorrHitC[i] = 0.;
638 639
			//				SQRXE[i] = 0;

640 641 642
			SQLYtime[i] = 0.;
			SQLYEtimeFiltered[i] = 0.;

643 644
			if (revent->SQ20[i] > thinChThresholds[i]) {
				energy = pSQ20_EC.Energy(revent->SQ20[i], i);
645 646 647 648
				SQ20E[i] = energy;
				SQ20Esum += SQ20E[i];
//				cout << energy << endl;
//				cout << i << "\t" << energy << "\t" << pSQ20_EC.a[i][0] << "\t" << pSQ20_EC.a[i][1] << "\t" << pSQ20_EC.a[i][2] << endl;
649
			}
650

651
			SQ20time[i] = revent->tSQ20[i]*0.3;
652 653
			if (SQ20E[i]>0 && SQ20time[i]>minTimeLthin[i] && SQ20time[i]<maxTimeLthin[i]) SQ20timeMult++;

654

655
//			if (i==0) continue;
656 657 658 659 660 661



			//				cout << energy << "\t" << pSQY_L_EC.a[i][0] << "\t" << pSQY_L_EC.a[i][1] << "\t" << pSQY_L_EC.a[i][2] << endl;
			//			cout << energy << endl;

662 663
			if (revent->SQY_L[i] > thickLYChThresholds[i]) {
				energy = pSQY_L_EC.Energy(revent->SQY_L[i], i);
664 665 666 667 668
				SQLYE[i] = energy;
				SQLYEsum += SQLYE[i];
				SQLYmult++;
			}

669 670
			Double_t time = revent->tSQY_L[i]*0.3 + timeCorr[i];

671
			if (SQLYE[i]>0 && time>325 && time<333) {
672 673 674 675 676 677
				SQLYtime[i] = time;
				SQLYtimeMult++;
				SQLYEtimeFiltered[i]=energy;
				SQLYEtimeFilteredSum = SQLYEtimeFilteredSum + SQLYEtimeFiltered[i];
			}

678 679
		}//for 16

680 681
//		cout << SQLXtimeMult << endl;

682 683 684
//		cout << endl;

		///////////////////////////////////////////////
685 686 687
		// correction for thickness inhomogeneity
		// when not taking into account hitting
		// particle tracking
688 689 690
		//
		// note: correction for thickness is not affected
		// by used system of coordinates
691 692 693 694 695 696 697
		///////////////////////////////////////////////

		Double_t currThickness;

		if (SQLXmult == 1 && SQLYmult == 1) {

			for (Int_t yi = 0; yi < kSQL_Y_strips; yi++) {
698 699

				//if energy in 1mm Y strip is reasonable
700
				if (SQLYE[yi]>0) {
701

702
					for (Int_t xi = 0; xi < TMath::Abs(kSQL_X_strips); xi++) {
703

704
						//if energy in 1mm X strip is reasonable
705
						if (SQLXE[xi] > 0) {
706 707 708
							currThickness = hThickness->GetBinContent(xi+1, yi+1);

							for (Int_t x20 = 0; x20 < kSQL_20_strips; x20++) {
709
								if (SQ20E[x20] > 0/*SQ20timeMult==1*/ && currThickness<30.) {
710 711 712 713 714

									SQ20Ecorr[x20] = (kSQ20_norm_thickness/currThickness)*SQ20E[x20];
									SQ20EcorrMult++;

								}
715 716
							}//for thin detector X strips
						}//if 1 mm X
717

718 719 720
					}//for thick detector X strips
				}//if 1 mm Y
			}//for thick detector Y strips
721

722
		}//if for correction according energy multiplicity
723 724 725



726 727

		///////////////////////////////////////////////
728
		// position of hit in left telescope
729 730 731 732 733 734 735 736 737 738 739 740 741 742
		///////////////////////////////////////////////

		//reset
		mapXbin = -1;
		mapYbin = -1;
		x1mm = -100.;
		y1mm = -100.;
		xThin = -100.;
		yThin = -100.;
		vHit1mm.SetXYZ(x1mm, y1mm, -1.);
		angleLeft = TMath::Pi();

		if (SQLXtimeMult==1) {
			for (Int_t i = 0; i<32; i++) {
743
				if (SQLXEtimeFiltered[i]>0) {
744 745
//					x1mm = -30. + (i+1./2.)*60./32.;
					x1mm = (i+0.5)*kSQL_X_size/kSQL_X_strips + kSQL_X_size/2.;	//ok
746
				}
747 748 749 750 751 752
			}
		}
//		cout << SQLYtimeMult << endl;

		if (SQLYtimeMult==1) {
			for (Int_t i = 0; i<16; i++) {
753
				if (SQLYEtimeFiltered[i]>0) {
754 755
//					y1mm = -30. + (i+1./2.)*60./16.;
					y1mm = -kSQL_Y_size/2. + (i+0.5)*kSQL_Y_size/kSQL_Y_strips;	//ok
756
				}
757 758 759
			}
		}

760
		//for MWPC wire multiplicity == 1
761

762
		//vHit1mm conversion to plane of thin detector
763 764 765 766 767 768 769 770
		if (SQLXtimeMult==1 && SQLYtimeMult==1 && xt>-50. && yt>-50.) {
			vHit1mm.SetXYZ(x1mm-xt, y1mm-yt, z1mm);
			vHit1mm *= zThin/z1mm;
			angleLeft = vHit1mm.Angle(vNorm);
		}


		if (SQLXtimeMult==1 && SQLYtimeMult==1
771 772 773 774 775 776
				//check if the particle passed through sensitive area of thin detector
				&& vHit1mm.X()+xt < (kSQ20_X_size/2. + kThinXoffset)
				&& vHit1mm.X()+xt > (-kSQ20_X_size/2. + kThinXoffset)
				&& vHit1mm.Y()+yt < (kSQ20_Y_size/2. + kThinYoffset)
				&& vHit1mm.Y()+yt > (-kSQ20_Y_size/2. + kThinYoffset)
		) {
777 778 779


			//coordinate in the thin detector plane
780 781 782
			xThin = vHit1mm.X()+xt;
			yThin = vHit1mm.Y()+yt;

783 784 785 786
			mapYbin = yThin*kSQL_Y_strips/kSQL_Y_size + kSQL_Y_strips/2.;	//ok
			mapXbin = xThin*kSQL_X_strips/kSQL_X_size - kSQL_X_strips/2.;	//ok


787
			//calculation of corrected energy
788
			//TODO: check the mapping here at second
789
			currThickness = hThickness->GetBinContent(mapXbin+1, mapYbin+1);
790

791
			for (Int_t x20 = 0; x20 < kSQL_20_strips; x20++) {
792
				if (SQ20E[x20] > 0/*SQ20timeMult==1*/ && currThickness<30.) {
793
					SQ20EcorrHit[x20] = (kSQ20_norm_thickness/currThickness)*SQ20E[x20];
794
					//when multiplicity larger than 1, we obtain incorrect information
795 796 797 798 799 800 801 802 803 804
					SQ20EcorrHitMult++;
//					cout << mapXbin << "\t" << mapYbin << "\t" << x20
//						<< "\t" << currThickness
//						<< "\t" << SQ20E[x20]  << "\t" << SQ20EcorrHit[x20] << "\t" << endl;

				}
			}//for*/

		}//if inside the map

805
		//end of MWPC wire multiplicity == 1
806

807
		//for MWPC cluster multiplicity == 1
808

809 810 811 812 813
		if (SQLXtimeMult==1 && SQLYtimeMult==1 && xtc>-50. && ytc>-50.) {
			vHit1mm.SetXYZ(x1mm-xtc, y1mm-ytc, z1mm);
			vHit1mm *= zThin/z1mm;
			angleLeft = vHit1mm.Angle(vNorm);
		}
814

815 816

		if (SQLXtimeMult==1 && SQLYtimeMult==1
817 818 819 820 821 822 823 824
				//check if the particle passed through sensitive area of thin detector
				&& vHit1mm.X()+xtc < (kSQ20_X_size/2. + kThinXoffset)
				&& vHit1mm.X()+xtc > (-kSQ20_X_size/2. + kThinXoffset)
				&& vHit1mm.Y()+ytc < (kSQ20_Y_size/2. + kThinYoffset)
				&& vHit1mm.Y()+ytc > (-kSQ20_Y_size/2. + kThinYoffset)
//				&& vHit1mm.X()+xtc>-30. && vHit1mm.X()+xtc<30.
//				&& vHit1mm.Y()+ytc>-30. && vHit1mm.Y()+ytc<30.
		) {
825 826 827 828 829


			xThin = vHit1mm.X()+xtc;
			yThin = vHit1mm.Y()+ytc;

830 831 832 833 834 835 836
			mapYbin = yThin*kSQL_Y_strips/kSQL_Y_size + kSQL_Y_strips/2.;	//ok
			mapXbin = xThin*kSQL_X_strips/kSQL_X_size - kSQL_X_strips/2.;	//ok
//			mapYbin = (vHit1mm.Y()+ytc+kThinYoffset+30.-0.5*60./16.)*16./60.;
//			mapXbin = (vHit1mm.X()+xtc+kThinXoffset+30.-0.5*60./32.)*32./60.;
			//coordinate in the thin detector plane


837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
			//calculation of corrected energy
			currThickness = hThickness->GetBinContent(mapXbin+1, mapYbin+1);

			for (Int_t x20 = 0; x20 < kSQL_20_strips; x20++) {
				if (SQ20E[x20] > 0/*SQ20timeMult==1*/ && currThickness<30.) {
					SQ20EcorrHitC[x20] = (kSQ20_norm_thickness/currThickness)*SQ20E[x20];
					SQ20EcorrHitCMult++;
					//					cout << mapXbin << "\t" << mapYbin << "\t" << x20
					//						<< "\t" << currThickness
					//						<< "\t" << SQ20E[x20]  << "\t" << SQ20EcorrHit[x20] << "\t" << endl;

				}
			}//for*/

			if (SQ20EcorrHitCMult==1) {
852 853
				//TODO: check this statement
				expectedThinStrip = (xThin + 25. - kThinXoffset)*(16./50.)-0.5;
854 855 856 857 858 859 860 861 862 863 864 865 866 867
			}

		}//if inside the map

		//end of MWPC wire multiplicity == 1

		if (
				SQLXtimeMult>0 && SQLXEtimeFilteredSum>0
				&& trigger==3
				&& x1MultC==1 && y1MultC==1 && x2MultC==1 && y2MultC==1
		) {

			tw->Fill();
		}//if TTree::Fill
868 869 870 871

		if (eventNo%100000 == 0 && eventNo !=0) {
			cout << eventNo << " events processed." << endl;
		}
872
	}//for events
873 874 875 876 877 878 879 880 881 882

	cout << nevents << " entries processed." << endl;

	fw->cd();
	tw->Write();
	fw->Close();

}


883 884 885 886 887
void fillChain(const TString beam = "he", const Int_t from = 0, const Int_t to = 9, const Int_t noevents = 0) {

	TStopwatch stopwatch;
	stopwatch.Start();

888 889
//	cout << "aksjdlkajbdlkjasd" << endl;

890
	for (Int_t i = from; i <= to; i++) {
891
		fillTree(beam, i, noevents);
892
	}
893 894 895

	cout << "Finished in "<< stopwatch.RealTime() << " seconds" << endl;
	cout << "Finished in "<< stopwatch.RealTime()/60. << " minutes" << endl;
896
}