1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // TRD raw data conversion class //
22 ///////////////////////////////////////////////////////////////////////////////
29 #include "AliRawDataHeaderSim.h"
30 #include "AliRawReader.h"
32 #include "AliFstream.h"
33 #include "AliTreeLoader.h"
35 #include "AliTRDrawData.h"
36 #include "AliTRDdigitsManager.h"
37 #include "AliTRDgeometry.h"
38 #include "AliTRDarrayDictionary.h"
39 #include "AliTRDarrayADC.h"
40 #include "AliTRDrawStream.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliTRDSignalIndex.h"
43 #include "AliTRDfeeParam.h"
44 #include "AliTRDmcmSim.h"
45 #include "AliTRDtrackletWord.h"
46 #include "AliTRDdigitsParam.h"
48 ClassImp(AliTRDrawData)
50 Int_t AliTRDrawData::fgDataSuppressionLevel = 0;
52 //_____________________________________________________________________________
53 AliTRDrawData::AliTRDrawData()
65 ,fMcmSim(new AliTRDmcmSim)
69 // Default constructor
72 fFee = AliTRDfeeParam::Instance();
73 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
77 //_____________________________________________________________________________
78 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
90 ,fMcmSim(new AliTRDmcmSim)
97 fFee = AliTRDfeeParam::Instance();
98 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
102 //_____________________________________________________________________________
103 AliTRDrawData::~AliTRDrawData()
110 fTracklets->Delete();
123 //_____________________________________________________________________________
124 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, const TTree *tracks )
127 // Initialize necessary parameters and call one
128 // of the raw data simulator selected by SetRawVersion.
130 // Currently tracklet output is not spported yet and it
131 // will be supported in higher version simulator.
134 AliTRDdigitsManager* const digitsManager = new AliTRDdigitsManager();
136 if (!digitsManager->ReadDigits(digitsTree)) {
137 delete digitsManager;
141 if (tracks != NULL) {
142 delete digitsManager;
143 AliError("Tracklet input is not supported yet.");
147 fGeo = new AliTRDgeometry();
149 if (!AliTRDcalibDB::Instance()) {
150 AliError("Could not get calibration object");
152 delete digitsManager;
156 Int_t retval = kTRUE;
157 Int_t rv = fFee->GetRAWversion();
159 // Call appropriate Raw Simulator
160 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
163 AliWarning(Form("Unsupported raw version (%d).", rv));
168 delete digitsManager;
174 //_____________________________________________________________________________
175 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
178 // Raw data simulator for all versions > 0. This is prepared for real data.
179 // This version simulate only raw data with ADC data and not with tracklet.
182 const Int_t kMaxHcWords = (fGeo->TBmax()/3)
188 // Buffer to temporary store half chamber data
189 UInt_t *hcBuffer = new UInt_t[kMaxHcWords];
191 Bool_t newEvent = kFALSE; // only for correct readout tree
192 Bool_t newSM = kFALSE; // new SM flag, for writing SM index words
193 Bool_t newStack = kFALSE; // new stack flag, for writing stack index words
195 // Get digits parameter
196 fDigitsParam = digitsManager->GetDigitsParam();
198 // sect is same as iDDL, so I use only sect here.
199 for (Int_t sect = 0; sect < fGeo->Nsector(); sect++) {
202 snprintf(name,1024,"TRD_%d.ddl",sect + AliTRDrawStream::kDDLOffset);
204 AliFstream* of = new AliFstream(name);
206 // Write a dummy data header
207 AliRawDataHeaderSim header; // the event header
208 UInt_t hpos = of->Tellp();
209 of->WriteBuffer((char *) (& header), sizeof(header));
211 // Reset payload byte size (payload does not include header).
212 Int_t npayloadbyte = 0;
214 // check the existance of the data
215 // SM index word and Stack index word
216 UInt_t *iwbuffer = new UInt_t[109]; // index word buffer; max 109 = 2 SM headers + 67 dummy headers + 5*8 stack headers
218 UInt_t bStackMask = 0x0;
219 Bool_t bStackHasData = kFALSE;
220 Bool_t bSMHasData = kFALSE;
222 //iwbuffer[nheader++] = 0x0001a020; // SM index words
223 iwbuffer[nheader++] = 0x0044a020; // SM index words | additional SM header:48 = 1 SM header + 47 dummy words(for future use)
224 iwbuffer[nheader++] = 0x10404071; // SM header
225 for ( Int_t i=0; i<66; i++ ) iwbuffer[nheader++] = 0x00000000; // dummy words
226 iwbuffer[nheader++] = 0x10000000; // end of dummy words
228 for ( Int_t stack= 0; stack < fGeo->Nstack(); stack++) {
229 UInt_t linkMask = 0x0;
230 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
231 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
232 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
233 if ( fgDataSuppressionLevel==0 || digits->HasData() ) {
234 bStackMask = bStackMask | ( 1 << stack ); // active stack mask for new stack
235 linkMask = linkMask | ( 3 << (2*layer) ); // 3 = 0011
236 bStackHasData = kTRUE;
241 if ( fgDataSuppressionLevel==0 || bStackHasData ){
242 iwbuffer[nheader++] = 0x0007a000 | linkMask; // stack index word + link masks
243 if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff; // no suppression
244 iwbuffer[nheader++] = 0x04045b01; // stack header
245 for (Int_t i=0;i<6;i++) iwbuffer[nheader++] = 0x00000000; // 6 dummy words
246 bStackHasData = kFALSE;
250 if ( fgDataSuppressionLevel==0 || bSMHasData ){
251 iwbuffer[0] = iwbuffer[0] | bStackMask; // add stack masks to SM index word
252 if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0044a03f; // no suppression : all stacks are active
253 of->WriteBuffer((char *) iwbuffer, nheader*4);
254 AliDebug(11, Form("SM %d index word: %08x", sect, iwbuffer[0]));
255 AliDebug(11, Form("SM %d header: %08x", sect, iwbuffer[1]));
257 // end of SM & stack header ------------------------------------------------------------------------
258 // -------------------------------------------------------------------------------------------------
260 // Prepare chamber data
261 for( Int_t stack = 0; stack < fGeo->Nstack(); stack++) {
262 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
264 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
266 newEvent = kTRUE; // it is expected that each event has at least one tracklet;
267 // this is only needed for correct readout tree
269 AliDebug(11, Form("New event!! Event counter: %d",fEventCounter));
272 if ( stack==0 && layer==0 ) newSM = kTRUE; // new SM flag
273 if ( layer==0 ) newStack = kTRUE; // new stack flag
274 AliDebug(15, Form("stack : %d, layer : %d, iDec : %d\n",stack,layer,iDet));
275 // Get the digits array
276 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
277 if (fgDataSuppressionLevel==0 || digits->HasData() ) { // second part is new!! and is for indicating a new event
279 if (digits->HasData()) digits->Expand();
283 // Process A side of the chamber
284 hcwords = ProduceHcData(digits,0,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
285 if ( newEvent ) newEvent = kFALSE;
286 //AssignLinkMask(hcBuffer, layer); // active link mask for this layer(2*HC)
287 of->WriteBuffer((char *) hcBuffer, hcwords*4);
288 npayloadbyte += hcwords*4;
290 // Process B side of the chamber
291 hcwords = ProduceHcData(digits,1,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
292 of->WriteBuffer((char *) hcBuffer, hcwords*4);
293 npayloadbyte += hcwords*4;
300 header.fSize = UInt_t(of->Tellp()) - hpos;
301 header.SetAttribute(0); // Valid data
302 of->Seekp(hpos); // Rewind to header position
303 of->WriteBuffer((char *) (& header), sizeof(header));
308 } // loop over sector(SM)
315 //_____________________________________________________________________________
316 void AliTRDrawData::ProduceSMIndexData(UInt_t *buf, Int_t& nw){
318 // This function generates
319 // 1) SM index words : ssssssss ssssssss vvvv rrrr r d t mmmmm
320 // - s : size of SM header (number of header, default = 0x0001)
321 // - v : SM header version (default = 0xa)
322 // - r : reserved for future use (default = 00000)
323 // - d : track data enabled bit (default = 0)
324 // - t : tracklet data enabled bit (default = 1)
325 // - m : stack mask (each bit corresponds a stack, default = 11111)
327 // 2) SM header : rrr c vvvv vvvvvvvv vvvv rrrr bbbbbbbb
328 // - r : reserved for future use (default = 000)
329 // - c : clean check out flag (default = 1)
330 // - v : hardware design revision (default = 0x0404)
331 // - r : reserved for future use (default = 0x0)
332 // - b : physical board ID (default = 0x71)
334 // 3) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
335 // - s : size of stack header (number of header, (default = 0x0007)
336 // - v : header version (default = 0xa)
337 // - m : link mask (default = 0xfff)
339 // 4) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
340 // - v : hardware design revision (default = 0x0404)
341 // - b : physical board ID (default = 0x5b)
342 // - r : reserved for future use (default = 0000 000)
343 // - c : clean checkout flag (default = 1)
345 // and 6 dummy words(0x00000000)
348 //buf[nw++] = 0x0001a03f; // SM index words
349 fSMindexPos = nw; // memorize position of the SM index word for re-allocating stack mask
350 buf[nw++] = 0x0001a020; // SM index words
351 buf[nw++] = 0x10404071; // SM header
353 fStackindexPos = nw; // memorize position of the stack index word for future adding
355 for (Int_t istack=0; istack<5; istack++){
356 buf[nw++] = 0x0007afff; // stack index words
357 buf[nw++] = 0x04045b01; // stack header
358 for (Int_t i=0;i<6;i++) buf[nw++] = 0x00000000; // 6 dummy words
359 } // loop over 5 stacks
363 //_____________________________________________________________________________
364 void AliTRDrawData::AssignStackMask(UInt_t *buf, Int_t nStack){
366 // This function re-assign stack mask active(from 0 to 1) in the SM index word
368 buf[fSMindexPos] = buf[fSMindexPos] | ( 1 << nStack );
371 //_____________________________________________________________________________
372 Int_t AliTRDrawData::AddStackIndexWords(UInt_t *buf, Int_t /*nStack*/, Int_t nMax){
374 // This function add stack index words and stack header when there is data for the stack
376 // 1) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
377 // - s : size of stack header (number of header, (default = 0x0007)
378 // - v : header version (default = 0xa)
379 // - m : link mask (default = 0xfff)
380 // - m : link mask (starting value = 0x000)
382 // 2) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
383 // - v : hardware design revision (default = 0x0404)
384 // - b : physical board ID (default = 0x5b)
385 // - r : reserved for future use (default = 0000 000)
386 // - c : clean checkout flag (default = 1)
388 // and 6 dummy words(0x00000000)
391 Int_t nAddedWords = 0; // Number of added words
392 if ( ShiftWords(buf, fStackindexPos, 8, nMax)== kFALSE ){
393 AliError("Adding stack header failed.");
397 buf[fStackindexPos++] = 0x0007a000; // stack index words
398 buf[fStackindexPos++] = 0x04045b01; // stack header
399 for (Int_t i=0;i<6;i++) buf[fStackindexPos++] = 0x00000000; // 6 dummy words
405 //_____________________________________________________________________________
406 void AliTRDrawData::AssignLinkMask(UInt_t *buf, Int_t nLayer){
408 // This function re-assign link mask active(from 0 to 1) in the stack index word
410 buf[fStackindexPos-8] = buf[fStackindexPos-8] | ( 3 << (2*nLayer) ); // 3 = 0011
413 //_____________________________________________________________________________
414 Bool_t AliTRDrawData::ShiftWords(UInt_t *buf, Int_t nStart, Int_t nWords, Int_t nMax){
416 // This function shifts n words
418 //if ( nStart+nWords > sizeof(buf)/sizeof(UInt_t) ){
419 // AliError("Words shift failed. No more buffer space.");
423 for ( Int_t iw=nMax; iw>nStart-1; iw--){
424 buf[iw+nWords] = buf[iw];
429 //_____________________________________________________________________________
430 Int_t AliTRDrawData::ProduceHcData(AliTRDarrayADC *digits, Int_t side, Int_t det, UInt_t *buf, Int_t maxSize, Bool_t /*newEvent = kFALSE*/, Bool_t /*newSM = kFALSE*/){
432 // This function produces the raw data for one HC, i.e. tracklets, tracklet endmarkers,
433 // raw data, raw data endmarkers.
434 // This function can be used for both ZS and NZS data
437 Int_t nw = 0; // Number of written words
438 Int_t of = 0; // Number of overflowed words
439 Int_t *tempnw = &nw; // Number of written words for temp. buffer
440 Int_t *tempof = &of; // Number of overflowed words for temp. buffer
441 Int_t layer = fGeo->GetLayer( det ); // Layer
442 Int_t stack = fGeo->GetStack( det ); // Stack
443 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
444 const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1; // Chamber type (0:C0, 1:C1)
446 Bool_t trackletOn = fFee->GetTracklet(); // tracklet simulation active?
448 AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d",sect,layer,stack,side));
450 UInt_t *tempBuffer = buf; // tempBuffer used to write ADC data
451 // different in case of tracklet writing
454 tempBuffer = new UInt_t[maxSize];
455 tempnw = new Int_t(0);
456 tempof = new Int_t(0);
459 WriteIntermediateWords(tempBuffer,*tempnw,*tempof,maxSize,det,side);
461 if (digits->HasData()) {
462 // scanning direction such, that tracklet-words are sorted in ascending z and then in ascending y order
463 // ROB numbering on chamber and MCM numbering on ROB increase with decreasing z and increasing y
464 for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {
465 // ROB number should be increasing
466 Int_t iRob = iRobRow * 2 + side;
468 for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
469 Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
471 fMcmSim->Init(det, iRob, iMcm);
472 fMcmSim->SetData(digits); // no filtering done here (already done in digitizer)
475 Int_t tempNw = fMcmSim->ProduceTrackletStream(&buf[nw], maxSize - nw);
479 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
484 fMcmSim->ZSMapping(); // Calculate zero suppression mapping
485 // at the moment it has to be rerun here
486 // Write MCM data to temp. buffer
487 Int_t tempNw = fMcmSim->ProduceRawStream( &tempBuffer[*tempnw], maxSize - *tempnw, fEventCounter );
490 *tempnw += maxSize - nw;
491 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
498 // in case of tracklet writing copy temp data to final buffer
500 if (nw + *tempnw < maxSize) {
501 memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
505 AliError("Buffer overflow detected");
511 delete [] tempBuffer;
516 // Write end of raw data marker
517 if (nw+3 < maxSize) {
518 buf[nw++] = fgkEndOfDataMarker;
519 buf[nw++] = fgkEndOfDataMarker;
520 buf[nw++] = fgkEndOfDataMarker;
521 buf[nw++] = fgkEndOfDataMarker;
527 AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
533 //_____________________________________________________________________________
534 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
537 // Vx of the raw data reading
540 rawReader->Select("TRD"); //[mj]
542 AliTRDarrayADC *digits = 0;
543 AliTRDarrayDictionary *track0 = 0;
544 AliTRDarrayDictionary *track1 = 0;
545 AliTRDarrayDictionary *track2 = 0;
547 //AliTRDSignalIndex *indexes = 0;
548 // Create the digits manager
549 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
550 digitsManager->CreateArrays();
552 AliTRDrawStream input(rawReader);
554 // ----- preparing tracklet output -----
555 AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets");
557 trklLoader = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
558 AliRunLoader::Instance()->GetLoader("TRDLoader")->AddDataLoader(trklLoader);
563 AliTreeLoader *trklTreeLoader = dynamic_cast<AliTreeLoader*> (trklLoader->GetBaseLoader("tracklets-raw"));
564 if (!trklTreeLoader) {
565 trklTreeLoader = new AliTreeLoader("tracklets-raw", trklLoader);
566 trklLoader->AddBaseLoader(trklTreeLoader);
568 if (!trklTreeLoader) {
572 if (!trklTreeLoader->Tree())
573 trklTreeLoader->MakeTree();
575 input.SetTrackletArray(TrackletsArray());
576 input.SetTrackArray(TracksArray());
578 // Loop through the digits
583 det = input.NextChamber(digitsManager);
588 digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
589 track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
590 track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
591 track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
593 if (digits) digits->Compress();
594 if (track0) track0->Compress();
595 if (track1) track1->Compress();
596 if (track2) track2->Compress();
600 trklTreeLoader->WriteData("OVERWRITE");
601 trklLoader->UnloadAll();
603 return digitsManager;
606 //_____________________________________________________________________________
607 void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
609 // write tracklet end marker(0x10001000)
610 // and half chamber headers(H[0] and H[1])
613 Int_t layer = fGeo->GetLayer( det ); // Layer
614 Int_t stack = fGeo->GetStack( det ); // Stack
615 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
616 Int_t rv = fFee->GetRAWversion();
617 const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
618 Bool_t trackletOn = fFee->GetTracklet();
621 // Write end of tracklet marker
623 buf[nw++] = fgkEndOfTrackletMarker;
624 buf[nw++] = fgkEndOfTrackletMarker; // the number of tracklet end marker should be more than 2
630 // Half Chamber header
631 // h[0] (there are 2 HC headers) xmmm mmmm nnnn nnnq qqss sssp ppcc ci01
632 // , where x : Raw version speacial number (=1)
633 // m : Raw version major number (test pattern, ZS, disable tracklet, 0, options)
634 // n : Raw version minor number
635 // q : number of addtional header words (default = 1)
636 // s : SM sector number (ALICE numbering)
637 // p : plane(layer) number
638 // c : chamber(stack) number
639 // i : side number (0:A, 1:B)
640 Int_t majorv = 0; // The major version number
641 Int_t minorv = 0; // The minor version number
642 Int_t add = 1; // The number of additional header words to follow : now 1, previous 2
643 Int_t tp = 0; // test pattern (default=0)
644 Int_t zs = (rv==3) ? 1 : 0; // zero suppression
645 Int_t dt = (trackletOn) ? 0 : 1; // disable tracklet
647 majorv = (tp<<6) | (zs<<5) | (dt<<4) | 1; // major version
649 x = (1<<31) | (majorv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
650 if (nw < maxSize) buf[nw++] = x; else of++;
652 // h[1] tttt ttbb bbbb bbbb bbbb bbpp pphh hh01
653 // , where t : number of time bins
654 // b : bunch crossing number
655 // p : pretrigger counter
656 // h : pretrigger phase
657 Int_t bcCtr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
658 Int_t ptCtr = 15; // pretrigger counter. Here it is set to 15 always for no reason
659 Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
660 //x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1; // old format
661 x = ((kNTBin)<<26) | (bcCtr<<10) | (ptCtr<<6) | (ptPhase<<2) | 1;
662 if (nw < maxSize) buf[nw++] = x; else of++;
665 TClonesArray *AliTRDrawData::TrackletsArray()
667 // Returns the array of on-line tracklets
670 fTracklets = new TClonesArray("AliTRDtrackletWord", 2*MAXTRACKLETSPERHC);
675 TClonesArray* AliTRDrawData::TracksArray()
677 // return array of GTU tracks (create TClonesArray if necessary)
680 fTracks = new TClonesArray("AliESDTrdTrack",100);