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 "AliLoader.h"
34 #include "AliTreeLoader.h"
36 #include "AliTRDrawData.h"
37 #include "AliTRDdigitsManager.h"
38 #include "AliTRDgeometry.h"
39 #include "AliTRDarrayDictionary.h"
40 #include "AliTRDarrayADC.h"
41 #include "AliTRDrawStream.h"
42 #include "AliTRDcalibDB.h"
43 #include "AliTRDSignalIndex.h"
44 #include "AliTRDfeeParam.h"
45 #include "AliTRDmcmSim.h"
46 #include "AliTRDtrackletWord.h"
47 #include "AliTRDdigitsParam.h"
49 ClassImp(AliTRDrawData)
51 Int_t AliTRDrawData::fgDataSuppressionLevel = 0;
53 //_____________________________________________________________________________
54 AliTRDrawData::AliTRDrawData()
67 ,fMcmSim(new AliTRDmcmSim)
71 // Default constructor
74 fFee = AliTRDfeeParam::Instance();
75 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
79 //_____________________________________________________________________________
80 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
93 ,fMcmSim(new AliTRDmcmSim)
100 fFee = AliTRDfeeParam::Instance();
101 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
105 //_____________________________________________________________________________
106 AliTRDrawData::~AliTRDrawData()
113 fTracklets->Delete();
126 //_____________________________________________________________________________
127 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, const TTree *tracks )
130 // Initialize necessary parameters and call one
131 // of the raw data simulator selected by SetRawVersion.
133 // Currently tracklet output is not spported yet and it
134 // will be supported in higher version simulator.
137 AliTRDdigitsManager* const digitsManager = new AliTRDdigitsManager();
139 if (!digitsManager->ReadDigits(digitsTree)) {
140 delete digitsManager;
144 if (tracks != NULL) {
145 delete digitsManager;
146 AliError("Tracklet input is not supported yet.");
150 fGeo = new AliTRDgeometry();
152 if (!AliTRDcalibDB::Instance()) {
153 AliError("Could not get calibration object");
155 delete digitsManager;
159 Int_t retval = kTRUE;
160 Int_t rv = fFee->GetRAWversion();
162 // Call appropriate Raw Simulator
163 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
166 AliWarning(Form("Unsupported raw version (%d).", rv));
171 delete digitsManager;
177 //_____________________________________________________________________________
178 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
181 // Raw data simulator for all versions > 0. This is prepared for real data.
182 // This version simulate only raw data with ADC data and not with tracklet.
185 const Int_t kMaxHcWords = (fGeo->TBmax()/3)
191 // Buffer to temporary store half chamber data
192 UInt_t *hcBuffer = new UInt_t[kMaxHcWords];
194 Bool_t newEvent = kFALSE; // only for correct readout tree
195 Bool_t newSM = kFALSE; // new SM flag, for writing SM index words
196 Bool_t newStack = kFALSE; // new stack flag, for writing stack index words
198 // Get digits parameter
199 fDigitsParam = digitsManager->GetDigitsParam();
201 // sect is same as iDDL, so I use only sect here.
202 for (Int_t sect = 0; sect < fGeo->Nsector(); sect++) {
205 snprintf(name,1024,"TRD_%d.ddl",sect + AliTRDrawStream::kDDLOffset);
207 AliFstream* of = new AliFstream(name);
209 // Write a dummy data header
210 AliRawDataHeaderSim header; // the event header
211 UInt_t hpos = of->Tellp();
212 of->WriteBuffer((char *) (& header), sizeof(header));
214 // Reset payload byte size (payload does not include header).
215 Int_t npayloadbyte = 0;
217 // check the existance of the data
218 // SM index word and Stack index word
219 UInt_t *iwbuffer = new UInt_t[109]; // index word buffer; max 109 = 2 SM headers + 67 dummy headers + 5*8 stack headers
221 UInt_t bStackMask = 0x0;
222 Bool_t bStackHasData = kFALSE;
223 Bool_t bSMHasData = kFALSE;
225 //iwbuffer[nheader++] = 0x0001a020; // SM index words
226 iwbuffer[nheader++] = 0x0044b020; // SM index words | additional SM header:48 = 1 SM header + 47 dummy words(for future use)
227 iwbuffer[nheader++] = 0x10404071; // SM header
228 for ( Int_t i=0; i<66; i++ ) iwbuffer[nheader++] = 0x00000000; // dummy words
229 iwbuffer[nheader++] = 0x10000000; // end of dummy words
231 for ( Int_t stack= 0; stack < fGeo->Nstack(); stack++) {
232 UInt_t linkMask = 0x0;
233 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
234 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
235 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
236 if ( fgDataSuppressionLevel==0 || digits->HasData() ) {
237 bStackMask = bStackMask | ( 1 << stack ); // active stack mask for new stack
238 linkMask = linkMask | ( 3 << (2*layer) ); // 3 = 0011
239 bStackHasData = kTRUE;
244 if ( fgDataSuppressionLevel==0 || bStackHasData ){
245 iwbuffer[nheader++] = 0x0007a000 | linkMask; // stack index word + link masks
246 if (fgDataSuppressionLevel==0) iwbuffer[nheader-1] = 0x0007afff; // no suppression
247 iwbuffer[nheader++] = 0x04045b01; // stack header
248 for (Int_t i=0;i<6;i++) iwbuffer[nheader++] = 0x00000000; // 6 dummy words
249 bStackHasData = kFALSE;
253 if ( fgDataSuppressionLevel==0 || bSMHasData ){
254 iwbuffer[0] = iwbuffer[0] | bStackMask; // add stack masks to SM index word
255 if (fgDataSuppressionLevel==0) iwbuffer[0] = 0x0044b03f; // no suppression : all stacks are active
256 of->WriteBuffer((char *) iwbuffer, nheader*4);
257 AliDebug(11, Form("SM %d index word: %08x", sect, iwbuffer[0]));
258 AliDebug(11, Form("SM %d header: %08x", sect, iwbuffer[1]));
260 // end of SM & stack header ------------------------------------------------------------------------
261 // -------------------------------------------------------------------------------------------------
263 // Prepare chamber data
264 for( Int_t stack = 0; stack < fGeo->Nstack(); stack++) {
265 for( Int_t layer = 0; layer < fGeo->Nlayer(); layer++) {
267 Int_t iDet = fGeo->GetDetector(layer,stack,sect);
269 newEvent = kTRUE; // it is expected that each event has at least one tracklet;
270 // this is only needed for correct readout tree
272 AliDebug(11, Form("New event!! Event counter: %d",fEventCounter));
275 if ( stack==0 && layer==0 ) newSM = kTRUE; // new SM flag
276 if ( layer==0 ) newStack = kTRUE; // new stack flag
277 AliDebug(15, Form("stack : %d, layer : %d, iDec : %d\n",stack,layer,iDet));
278 // Get the digits array
279 AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(iDet);
280 if (fgDataSuppressionLevel==0 || digits->HasData() ) { // second part is new!! and is for indicating a new event
282 if (digits->HasData()) digits->Expand();
286 // Process A side of the chamber
287 hcwords = ProduceHcData(digits,0,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
288 if ( newEvent ) newEvent = kFALSE;
289 //AssignLinkMask(hcBuffer, layer); // active link mask for this layer(2*HC)
290 of->WriteBuffer((char *) hcBuffer, hcwords*4);
291 npayloadbyte += hcwords*4;
293 // Process B side of the chamber
294 hcwords = ProduceHcData(digits,1,iDet,hcBuffer,kMaxHcWords,newEvent,newSM);
295 of->WriteBuffer((char *) hcBuffer, hcwords*4);
296 npayloadbyte += hcwords*4;
303 header.fSize = UInt_t(of->Tellp()) - hpos;
304 header.SetAttribute(0); // Valid data
305 of->Seekp(hpos); // Rewind to header position
306 of->WriteBuffer((char *) (& header), sizeof(header));
311 } // loop over sector(SM)
318 //_____________________________________________________________________________
319 void AliTRDrawData::ProduceSMIndexData(UInt_t *buf, Int_t& nw){
321 // This function generates
322 // 1) SM index words : ssssssss ssssssss vvvv rrrr r d t mmmmm
323 // - s : size of SM header (number of header, default = 0x0001)
324 // - v : SM header version (default = 0xa)
325 // - r : reserved for future use (default = 00000)
326 // - d : track data enabled bit (default = 0)
327 // - t : tracklet data enabled bit (default = 1)
328 // - m : stack mask (each bit corresponds a stack, default = 11111)
330 // 2) SM header : rrr c vvvv vvvvvvvv vvvv rrrr bbbbbbbb
331 // - r : reserved for future use (default = 000)
332 // - c : clean check out flag (default = 1)
333 // - v : hardware design revision (default = 0x0404)
334 // - r : reserved for future use (default = 0x0)
335 // - b : physical board ID (default = 0x71)
337 // 3) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
338 // - s : size of stack header (number of header, (default = 0x0007)
339 // - v : header version (default = 0xa)
340 // - m : link mask (default = 0xfff)
342 // 4) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
343 // - v : hardware design revision (default = 0x0404)
344 // - b : physical board ID (default = 0x5b)
345 // - r : reserved for future use (default = 0000 000)
346 // - c : clean checkout flag (default = 1)
348 // and 6 dummy words(0x00000000)
351 //buf[nw++] = 0x0001a03f; // SM index words
352 fSMindexPos = nw; // memorize position of the SM index word for re-allocating stack mask
353 buf[nw++] = 0x0001b020; // SM index words
354 buf[nw++] = 0x10404071; // SM header
356 fStackindexPos = nw; // memorize position of the stack index word for future adding
358 for (Int_t istack=0; istack<5; istack++){
359 buf[nw++] = 0x0007afff; // stack index words
360 buf[nw++] = 0x04045b01; // stack header
361 for (Int_t i=0;i<6;i++) buf[nw++] = 0x00000000; // 6 dummy words
362 } // loop over 5 stacks
366 //_____________________________________________________________________________
367 void AliTRDrawData::AssignStackMask(UInt_t *buf, Int_t nStack){
369 // This function re-assign stack mask active(from 0 to 1) in the SM index word
371 buf[fSMindexPos] = buf[fSMindexPos] | ( 1 << nStack );
374 //_____________________________________________________________________________
375 Int_t AliTRDrawData::AddStackIndexWords(UInt_t *buf, Int_t /*nStack*/, Int_t nMax){
377 // This function add stack index words and stack header when there is data for the stack
379 // 1) stack index words : ssssssss ssssssss vvvv mmmm mmmmmmmm
380 // - s : size of stack header (number of header, (default = 0x0007)
381 // - v : header version (default = 0xa)
382 // - m : link mask (default = 0xfff)
383 // - m : link mask (starting value = 0x000)
385 // 2) stack header : vvvvvvvv vvvvvvvv bbbbbbbb rrrr rrr c
386 // - v : hardware design revision (default = 0x0404)
387 // - b : physical board ID (default = 0x5b)
388 // - r : reserved for future use (default = 0000 000)
389 // - c : clean checkout flag (default = 1)
391 // and 6 dummy words(0x00000000)
394 Int_t nAddedWords = 0; // Number of added words
395 if ( ShiftWords(buf, fStackindexPos, 8, nMax)== kFALSE ){
396 AliError("Adding stack header failed.");
400 buf[fStackindexPos++] = 0x0007a000; // stack index words
401 buf[fStackindexPos++] = 0x04045b01; // stack header
402 for (Int_t i=0;i<6;i++) buf[fStackindexPos++] = 0x00000000; // 6 dummy words
408 //_____________________________________________________________________________
409 void AliTRDrawData::AssignLinkMask(UInt_t *buf, Int_t nLayer){
411 // This function re-assign link mask active(from 0 to 1) in the stack index word
413 buf[fStackindexPos-8] = buf[fStackindexPos-8] | ( 3 << (2*nLayer) ); // 3 = 0011
416 //_____________________________________________________________________________
417 Bool_t AliTRDrawData::ShiftWords(UInt_t *buf, Int_t nStart, Int_t nWords, Int_t nMax){
419 // This function shifts n words
421 //if ( nStart+nWords > sizeof(buf)/sizeof(UInt_t) ){
422 // AliError("Words shift failed. No more buffer space.");
426 for ( Int_t iw=nMax; iw>nStart-1; iw--){
427 buf[iw+nWords] = buf[iw];
432 //_____________________________________________________________________________
433 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*/){
435 // This function produces the raw data for one HC, i.e. tracklets, tracklet endmarkers,
436 // raw data, raw data endmarkers.
437 // This function can be used for both ZS and NZS data
440 Int_t nw = 0; // Number of written words
441 Int_t of = 0; // Number of overflowed words
442 Int_t *tempnw = &nw; // Number of written words for temp. buffer
443 Int_t *tempof = &of; // Number of overflowed words for temp. buffer
444 Int_t layer = fGeo->GetLayer( det ); // Layer
445 Int_t stack = fGeo->GetStack( det ); // Stack
446 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
447 const Int_t kCtype = fGeo->GetStack(det) == 2 ? 0 : 1; // Chamber type (0:C0, 1:C1)
449 Bool_t trackletOn = fFee->GetTracklet(); // tracklet simulation active?
451 AliDebug(1,Form("Producing raw data for sect=%d layer=%d stack=%d side=%d",sect,layer,stack,side));
453 UInt_t *tempBuffer = buf; // tempBuffer used to write ADC data
454 // different in case of tracklet writing
457 tempBuffer = new UInt_t[maxSize];
458 tempnw = new Int_t(0);
459 tempof = new Int_t(0);
462 WriteIntermediateWords(tempBuffer,*tempnw,*tempof,maxSize,det,side);
464 if (digits->HasData()) {
465 // scanning direction such, that tracklet-words are sorted in ascending z and then in ascending y order
466 // ROB numbering on chamber and MCM numbering on ROB increase with decreasing z and increasing y
467 for (Int_t iRobRow = 0; iRobRow <= (kCtype + 3)-1; iRobRow++ ) {
468 // ROB number should be increasing
469 Int_t iRob = iRobRow * 2 + side;
471 for (Int_t iMcmRB = 0; iMcmRB < fGeo->MCMmax(); iMcmRB++ ) {
472 Int_t iMcm = 16 - 4*(iMcmRB/4 + 1) + (iMcmRB%4);
474 fMcmSim->Init(det, iRob, iMcm);
475 fMcmSim->SetData(digits); // no filtering done here (already done in digitizer)
478 Int_t tempNw = fMcmSim->ProduceTrackletStream(&buf[nw], maxSize - nw);
482 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
487 fMcmSim->ZSMapping(); // Calculate zero suppression mapping
488 // at the moment it has to be rerun here
489 // Write MCM data to temp. buffer
490 Int_t tempNw = fMcmSim->ProduceRawStream( &tempBuffer[*tempnw], maxSize - *tempnw, fEventCounter );
493 *tempnw += maxSize - nw;
494 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
501 // in case of tracklet writing copy temp data to final buffer
503 if (nw + *tempnw < maxSize) {
504 memcpy(&buf[nw], tempBuffer, *tempnw * sizeof(UInt_t));
508 AliError("Buffer overflow detected");
514 delete [] tempBuffer;
519 // Write end of raw data marker
520 if (nw+3 < maxSize) {
521 buf[nw++] = fgkEndOfDataMarker;
522 buf[nw++] = fgkEndOfDataMarker;
523 buf[nw++] = fgkEndOfDataMarker;
524 buf[nw++] = fgkEndOfDataMarker;
530 AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
536 //_____________________________________________________________________________
537 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
540 // Vx of the raw data reading
543 rawReader->Select("TRD"); //[mj]
545 AliTRDarrayADC *digits = 0;
546 AliTRDarrayDictionary *track0 = 0;
547 AliTRDarrayDictionary *track1 = 0;
548 AliTRDarrayDictionary *track2 = 0;
550 //AliTRDSignalIndex *indexes = 0;
551 // Create the digits manager
552 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
553 digitsManager->CreateArrays();
555 AliTRDrawStream input(rawReader);
557 // ----- preparing tracklet output -----
558 AliDataLoader *trklLoader = AliRunLoader::Instance()->GetLoader("TRDLoader")->GetDataLoader("tracklets");
560 trklLoader = new AliDataLoader("TRD.Tracklets.root","tracklets", "tracklets");
561 AliRunLoader::Instance()->GetLoader("TRDLoader")->AddDataLoader(trklLoader);
566 AliTreeLoader *trklTreeLoader = dynamic_cast<AliTreeLoader*> (trklLoader->GetBaseLoader("tracklets-raw"));
567 if (!trklTreeLoader) {
568 trklTreeLoader = new AliTreeLoader("tracklets-raw", trklLoader);
569 trklLoader->AddBaseLoader(trklTreeLoader);
571 if (!trklTreeLoader) {
575 if (!trklTreeLoader->Tree())
576 trklTreeLoader->MakeTree();
578 input.SetTrackletArray(TrackletsArray());
579 input.SetTrackArray(TracksArray());
581 // Loop through the digits
586 det = input.NextChamber(digitsManager);
591 digits = (AliTRDarrayADC *) digitsManager->GetDigits(det);
592 track0 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,0);
593 track1 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,1);
594 track2 = (AliTRDarrayDictionary *) digitsManager->GetDictionary(det,2);
596 if (digits) digits->Compress();
597 if (track0) track0->Compress();
598 if (track1) track1->Compress();
599 if (track2) track2->Compress();
603 for (Int_t iSector = 0; iSector < fGeo->Nsector(); iSector++) {
604 fTrgFlags[iSector] = input.GetTriggerFlags(iSector);
607 trklTreeLoader->WriteData("OVERWRITE");
608 trklLoader->UnloadAll();
610 return digitsManager;
613 //_____________________________________________________________________________
614 void AliTRDrawData::WriteIntermediateWords(UInt_t* buf, Int_t& nw, Int_t& of, const Int_t& maxSize, const Int_t& det, const Int_t& side) {
616 // write tracklet end marker(0x10001000)
617 // and half chamber headers(H[0] and H[1])
620 Int_t layer = fGeo->GetLayer( det ); // Layer
621 Int_t stack = fGeo->GetStack( det ); // Stack
622 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
623 Int_t rv = fFee->GetRAWversion();
624 const Int_t kNTBin = fDigitsParam->GetNTimeBins(det);
625 Bool_t trackletOn = fFee->GetTracklet();
628 // Write end of tracklet marker
630 buf[nw++] = fgkEndOfTrackletMarker;
631 buf[nw++] = fgkEndOfTrackletMarker; // the number of tracklet end marker should be more than 2
637 // Half Chamber header
638 // h[0] (there are 2 HC headers) xmmm mmmm nnnn nnnq qqss sssp ppcc ci01
639 // , where x : Raw version speacial number (=1)
640 // m : Raw version major number (test pattern, ZS, disable tracklet, 0, options)
641 // n : Raw version minor number
642 // q : number of addtional header words (default = 1)
643 // s : SM sector number (ALICE numbering)
644 // p : plane(layer) number
645 // c : chamber(stack) number
646 // i : side number (0:A, 1:B)
647 Int_t majorv = 0; // The major version number
648 Int_t minorv = 0; // The minor version number
649 Int_t add = 1; // The number of additional header words to follow : now 1, previous 2
650 Int_t tp = 0; // test pattern (default=0)
651 Int_t zs = (rv==3) ? 1 : 0; // zero suppression
652 Int_t dt = (trackletOn) ? 0 : 1; // disable tracklet
654 majorv = (tp<<6) | (zs<<5) | (dt<<4) | 1; // major version
656 x = (1<<31) | (majorv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (layer<<6) | (stack<<3) | (side<<2) | 1;
657 if (nw < maxSize) buf[nw++] = x; else of++;
659 // h[1] tttt ttbb bbbb bbbb bbbb bbpp pphh hh01
660 // , where t : number of time bins
661 // b : bunch crossing number
662 // p : pretrigger counter
663 // h : pretrigger phase
664 Int_t bcCtr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
665 Int_t ptCtr = 15; // pretrigger counter. Here it is set to 15 always for no reason
666 Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
667 //x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1; // old format
668 x = ((kNTBin)<<26) | (bcCtr<<10) | (ptCtr<<6) | (ptPhase<<2) | 1;
669 if (nw < maxSize) buf[nw++] = x; else of++;
672 TClonesArray *AliTRDrawData::TrackletsArray()
674 // Returns the array of on-line tracklets
677 fTracklets = new TClonesArray("AliTRDtrackletWord", 200);
682 TClonesArray* AliTRDrawData::TracksArray()
684 // return array of GTU tracks (create TClonesArray if necessary)
687 fTracks = new TClonesArray("AliESDTrdTrack",100);