/**
Simulated data of proton-proton collisions of the TPC,
one set of tracks is given,
with single measured events of electron clouds reaching one of the two read-out chambers after a certain time with a certain charge

KV, 02/2010 (Data from Matevz Tadel), prepared for SBEII
**/

(
q = q ?? ();
q.dataPath = Document.current.dir ++ "/TPC_m_data_96";
q.myfile = "m_data_96";
);

// geht all tracks including [tracknumber, # of data associated with track]:
b = ZArchive.read(q.dataPath++"/"++q.myfile++"_tracks"++".zar");
q.numTracks = b.readItem;

//tracks of TPC_m_data_96:
//[ [ 5, 32961 ], [ 8, 1810 ], [ 33, 2364 ], [ 34, 2261 ], [ 35, 2531 ], [ 36, 23 ], [ 39, 5467 ], [ 44, 7137 ], [ 50, 8793 ], [ 51, 2916 ], [ 57, 28699 ], [ 59, 6145 ], [ 67, 15309 ], [ 68, 9 ], [ 70, 15503 ] ]

// read and post data for ONE track:
(
q.readTPC = { |q, index = 0|
	var t = q.numTracks[index][0];
	var 	b = ZArchive.read(q.dataPath++"/"++q.myfile ++"_"++ t ++ ".zar");
	var data = b.readItem;
	b.close; 
	data.do{|track, i| 
		track[3] = (track[4].abs)/2.6 * 100 
	}; // correct time! 
 	data
};
);

// q.data = q.readTPC(5);

// q.data.flop;

/**
	// [units are in cm]
	q.track = q.data[0];
	i = q.track[0] 	// track id, 			
	r = q.track[0]		// radius 	r,		[0..250] cm 
	p = q.track.flop[2] 	// angle 		phi,		[-pi, pi]
	t = q.track.flop[3]		// time 		t,		[0, 100] microsec, = (2.6-(z.abs))/2.58 (2.58=drift velocity)
											BUT sometimes negative, therefore corrected!
	z = q.track.flop[4] 	// coordinate	z,		[-2.6, 2.6] // in meter: total size is 5m!
	x = q.track.flop[5] 	// coordinate	x,		[-250, 250] cm		
	y = q.track.flop[6] 	// coordinate	y, 		[-200, 225] cm
	c = q.track.flop[7] 	// charge 	q,  		in out.dat: [0, 272] nb. of electrons
	k = q.track.flop[8]		// kind of particle that produced the track, see TPCmontecarlorpp.pdf
**/

// read  data for all tracks:
(
q.data = Array.fill(q.numTracks.size);
Task({
	q.numTracks.size.do{ |i|
		q.data[i] = q.readTPC(i);
		"ok".postln;
//		q.data.postln;
	};
}).play
);

//q.data[14].flop