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 purpeateose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 // $MpId: AliMpTriggerReader.cxx,v 1.4 2006/05/24 13:58:52 ivana Exp $
19 #include "AliMpTriggerReader.h"
22 #include "AliMpConstants.h"
23 #include "AliMpDataStreams.h"
24 #include "AliMpFiles.h"
25 #include "AliMpHelper.h"
26 #include "AliMpMotif.h"
27 #include "AliMpMotifPosition.h"
28 #include "AliMpMotifReader.h"
29 #include "AliMpMotifSpecial.h"
30 #include "AliMpMotifType.h"
32 #include "AliMpSlat.h"
33 #include "AliMpSlatMotifMap.h"
34 #include "AliMpSlatMotifMap.h"
35 #include "AliMpSt345Reader.h"
36 #include "AliMpTrigger.h"
37 #include "Riostream.h"
40 #include "TObjString.h"
46 //-----------------------------------------------------------------------------
47 /// \class AliMpTriggerReader
48 /// Read trigger slat ASCII files
49 /// Basically provides two methods:
50 /// - AliMpTrigger* ReadSlat()
51 /// - AliMpPCB* ReadPCB()
53 /// \author Laurent Aphecetche
54 //-----------------------------------------------------------------------------
57 ClassImp(AliMpTriggerReader)
61 // static private methods
64 //_____________________________________________________________________________
65 const TString& AliMpTriggerReader::GetKeywordLayer()
68 static const TString kKeywordLayer("LAYER");
72 //_____________________________________________________________________________
73 const TString& AliMpTriggerReader::GetKeywordScale()
76 static const TString kKeywordScale("SCALE");
80 //_____________________________________________________________________________
81 const TString& AliMpTriggerReader::GetKeywordPcb()
84 static const TString kKeywordPcb("PCB");
88 //_____________________________________________________________________________
89 const TString& AliMpTriggerReader::GetKeywordFlipX()
92 static const TString kKeywordFlipX("FLIP_X");
96 //_____________________________________________________________________________
97 const TString& AliMpTriggerReader::GetKeywordFlipY()
100 static const TString kKeywordFlipY("FLIP_Y");
101 return kKeywordFlipY;
108 //_____________________________________________________________________________
109 AliMpTriggerReader::AliMpTriggerReader(AliMpSlatMotifMap* motifMap)
117 fLocalBoardMap.SetOwner(kTRUE);
120 //_____________________________________________________________________________
121 AliMpTriggerReader::~AliMpTriggerReader()
126 fLocalBoardMap.DeleteAll();
129 //_____________________________________________________________________________
131 AliMpTriggerReader::BuildSlat(const AliMpDataStreams& dataStreams,
132 const char* slatName,
133 AliMp::PlaneType planeType,
137 /// Construct a slat from the list of lines, taking into account
138 /// the scale factor. The returned pointer must be deleted by the client
140 AliDebug(1,Form("slat %s %s scale %e",
141 slatName,PlaneTypeName(planeType).Data(),scale))
144 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
148 // StdoutToAliDebug(3,lines.Print(););
151 while ( ( osline = (TObjString*)it.Next() ) )
153 // note that at this stage lines should not be empty.
154 TString sline(osline->String());
156 TObjArray* tokens = sline.Tokenize(' ');
158 TString& keyword = ((TObjString*)tokens->At(0))->String();
160 if ( keyword == GetKeywordPcb() )
162 if ( tokens->GetEntriesFast() != 3 )
164 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
165 " in following line:\n%s",sline.Data()));
170 TString pcbName = ((TObjString*)tokens->At(1))->String();
172 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
176 std::ostringstream s;
177 s << pcbName.Data() << "x" << scale;
178 pcbName = s.str().c_str();
181 AliMpPCB* pcbType = ReadPCB(dataStreams, pcbName.Data());
184 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
190 TArrayI allLocalBoards;
192 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
194 TArrayI localBoardNumbers;
195 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
196 Ssiz_t pos = localBoards.First('-');
199 pos = localBoards.Length();
201 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
202 ';',localBoardNumbers);
203 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
205 std::ostringstream name;
206 name << localBoards(0,pos-1) << localBoardNumbers[i];
207 AliDebugClass(3,name.str().c_str());
208 localBoardNumbers[i] = LocalBoardNumber(dataStreams,name.str().c_str());
209 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
210 allLocalBoards.Set(allLocalBoards.GetSize()+1);
211 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
212 if (localBoardNumbers[i] < 0 )
214 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
215 " to be correct... : %s\n",slatName,name.str().c_str()));
219 AliDebug(3,"Deleting tokens");
221 AliDebug(3,"Deleting localBoardList");
222 delete localBoardList;
223 AliDebug(3,"Adding pcb to slat");
224 slat->Add(*pcbType,allLocalBoards);
225 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
230 if ( slat->DX()== 0 || slat->DY() == 0 )
232 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
237 //_____________________________________________________________________________
239 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
241 /// Decode the string to get the board name
244 TObjArray* tokens = s.Tokenize(' ');
246 TString& keyword = ((TObjString*)tokens->At(0))->String();
248 if ( keyword == GetKeywordPcb() &&
249 tokens->GetEntriesFast() == 3 )
251 boardName = ((TObjString*)tokens->At(2))->String();
259 //_____________________________________________________________________________
261 AliMpTriggerReader::FlipLines(const AliMpDataStreams& dataStreams,
262 TList& lines, Bool_t flipX, Bool_t flipY,
263 Int_t srcLine, Int_t destLine)
266 /// Change the local board names contained in lines,
267 /// to go from right to left, and/or
268 /// from top to bottom
274 // Simply swaps R(ight) and L(eft) in the first character of
279 while ( ( oline = (TObjString*)it.Next() ) )
281 TString& s = oline->String();
282 if ( s.Contains("RC") )
284 // Change right to left
285 s.ReplaceAll("RC","LC");
287 else if ( s.Contains("LC") )
289 // Change left to right
290 s.ReplaceAll("LC","RC");
297 // Change line number, according to parameters srcLine and destLine
298 // Note that because of road opening (for planes 3 and 4 at least),
299 // we loop for srcLine +-1
301 for ( Int_t line = -1; line <=1; ++line )
303 std::ostringstream src,dest;
304 src << "L" << srcLine+line;
305 dest << "L" << destLine-line;
306 if ( src.str() == dest.str() ) continue;
308 for ( Int_t i = 0; i < lines.GetSize(); ++i )
310 TObjString* oline = (TObjString*)lines.At(i);
312 TString& s = oline->String();
314 if ( !s.Contains(GetKeywordPcb()) )
316 // Only consider PCB lines.
320 if ( s.Contains(src.str().c_str()) )
322 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
323 src.str().c_str(),dest.str().c_str(),s.Data()));
325 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
327 AliDebugClass(4,s.Data());
329 TString boardName(GetBoardNameFromPCBLine(s));
333 // We must also change board numbers, with the tricky
334 // thing that up and down must be swapped...
335 // Up can only be 1 card so it must be B1
336 // Down must be the uppper card of the line before, so
337 // the biggest possible board number for this Line,Column
342 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
343 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
344 AliDebugClass(4,s.Data());
348 // find the largest valid board number
349 for ( int b = 4; b>=1; --b )
351 std::ostringstream bs;
352 bs << boardName(0,boardName.Length()-1) << b;
353 if ( LocalBoardNumber(dataStreams,bs.str().c_str()) >= 0 )
355 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
356 boardName(boardName.Length()-2,2).Data(),
359 s.ReplaceAll(boardName(boardName.Length()-2,2),
366 // Check that the replacement we did is ok. If not,
368 Int_t lbn = LocalBoardNumber(dataStreams,GetBoardNameFromPCBLine(s));
371 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
382 //___________________________________________________________________________
384 AliMpTriggerReader::IsLayerLine(const TString& sline) const
386 /// Whether sline contains LAYER keyword
388 if ( sline.BeginsWith(GetKeywordLayer()) )
398 //___________________________________________________________________________
400 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
402 Bool_t& flipX, Bool_t& flipY)
404 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
406 Ssiz_t blankPos = sline.First(' ');
407 if ( blankPos < 0 ) return 0;
409 TString keyword(sline(0,blankPos));
411 if ( keyword == GetKeywordFlipX() )
414 } else if ( keyword == GetKeywordFlipY() )
423 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
427 //___________________________________________________________________________
429 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
430 Double_t& scale, TString& slatType)
432 /// Decode sline containing SCALE keyword
434 if ( sline(0,GetKeywordScale().Length()) == GetKeywordScale() )
436 TString tmp(sline(GetKeywordScale().Length()+1,
437 sline.Length()-GetKeywordScale().Length()-1));
438 Ssiz_t blankPos = tmp.First(' ');
441 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
442 " SCALE keyword : %s\n",tmp.Data()));
447 slatType = tmp(0,blankPos);
448 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
456 //_____________________________________________________________________________
458 AliMpTriggerReader::GetLine(const TString& slatType)
461 /// Assuming slatType is a 4 character string of the form XSLN
462 /// where X=1,2,3 or 4
464 /// N is the line number
467 if ( isdigit(slatType[0]) &&
468 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
471 return atoi(slatType(3,1).Data());
476 //_____________________________________________________________________________
478 AliMpTriggerReader::LocalBoardNumber(const AliMpDataStreams& dataStreams,
479 const char* localBoardName)
481 /// From local board name to local board number
483 if ( !fLocalBoardMap.GetSize() )
485 ReadLocalBoardMapping(dataStreams);
488 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
492 return atoi(((TObjString*)pair->Value())->String().Data());
497 //_____________________________________________________________________________
499 AliMpTriggerReader::ReadLines(const AliMpDataStreams& dataStreams,
500 const char* slatType,
501 AliMp::PlaneType planeType,
504 Bool_t& flipX, Bool_t& flipY,
505 Int_t& srcLine, Int_t& destLine)
508 /// Reads in lines from file for a given slat
509 /// Returns the list of lines (lines), together with some global
510 /// information as the scale, whether to flip the lines, etc...
512 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
513 " destLine %d\n",slatType,scale,flipX,flipY,
518 CreateDataStream(AliMpFiles::SlatFilePath(
519 AliMp::kStationTrigger,slatType, planeType));
523 while ( in.getline(line,80) )
525 TString sline(AliMpHelper::Normalize(line));
527 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
529 Bool_t isKeywordThere =
530 sline.Contains(GetKeywordPcb()) ||
531 sline.Contains(GetKeywordLayer()) ||
532 sline.Contains(GetKeywordScale()) ||
533 sline.Contains(GetKeywordFlipX()) ||
534 sline.Contains(GetKeywordFlipY());
536 if ( !isKeywordThere )
538 AliErrorClass(Form("Got a line with no keyword : %s."
539 "That's not valid\n",line));
546 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
550 if ( isScaleLine < 0 )
552 AliFatalClass(Form("Syntax error near %s keyword\n",GetKeywordScale().Data()));
554 else if ( isScaleLine > 0 && slatType2 != slatType )
556 ReadLines(dataStreams,
557 slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
563 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
568 srcLine = GetLine(slatType2);
569 destLine = GetLine(slatType);
573 ReadLines(dataStreams,
574 slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
578 lines.Add(new TObjString(sline.Data()));
586 //_____________________________________________________________________________
588 AliMpTriggerReader::ReadLocalBoardMapping(const AliMpDataStreams& dataStreams)
590 /// Reads the file that contains the mapping local board name <-> number
592 fLocalBoardMap.DeleteAll();
598 CreateDataStream(AliMpFiles::LocalTriggerBoardMapping());
601 Char_t localBoardName[20];
602 Int_t j,localBoardId;
608 for (Int_t i = 0; i < 4; ++i)
609 if (!in.getline(line,80)) continue; //skip 4 first lines
612 if (!in.getline(line,80)) break;
613 sscanf(line,"%hx",&mask);
616 if (!in.getline(line,80)) break;
617 sscanf(line,"%d",&nofBoards);
619 for ( Int_t i = 0; i < nofBoards; ++i )
622 if (!in.getline(line,80)) break;
623 sscanf(line,"%02d %19s %03d %03x", &j, localBoardName, &localBoardId, &switches);
624 if (localBoardId <= AliMpConstants::NofLocalBoards())
626 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
627 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
629 // skip 2 following lines
630 if (!in.getline(line,80)) break;
631 if (!in.getline(line,80)) break;
639 //_____________________________________________________________________________
641 AliMpTriggerReader::ReadPCB(const AliMpDataStreams& dataStreams,
645 /// Create a new AliMpPCB object, by reading it from file.
646 /// Returned pointer must be deleted by client.
648 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
650 TString pcbName(pcbType);
652 Ssiz_t pos = pcbName.First('x');
654 Double_t scale = 1.0;
658 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
659 pcbName = pcbName(0,pos);
664 CreateDataStream(AliMpFiles::SlatPCBFilePath(
665 AliMp::kStationTrigger,pcbName));
667 AliMpMotifReader reader(AliMp::kStationTrigger, AliMq::kNotSt12, AliMp::kNonBendingPlane);
668 // note that the nonbending
669 // parameter is of no use for trigger, as far as reading motif is
670 // concerned, as all motifs are supposed to be in the same directory
671 // (as they are shared by bending/non-bending planes).
675 const TString kSizeKeyword("SIZES");
676 const TString kMotifKeyword("MOTIF");
677 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
681 while ( in.getline(line,80) )
683 if ( line[0] == '#' ) continue;
687 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
689 std::istringstream sin(sline(kSizeKeyword.Length(),
690 sline.Length()-kSizeKeyword.Length()-1).Data());
691 float padSizeX = 0.0;
692 float padSizeY = 0.0;
693 float pcbSizeX = 0.0;
694 float pcbSizeY = 0.0;
695 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
698 AliError("pcb not null as expected");
700 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
701 pcbSizeX*scale,pcbSizeY*scale);
704 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
706 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
707 sline.Length()-kMotifSpecialKeyword.Length()).Data());
708 TString sMotifSpecial;
710 sin >> sMotifSpecial >> sMotifType;
712 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
714 AliMpMotifSpecial* specialMotif =
715 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
718 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
719 sMotifSpecial.Data(),id.Data()));
720 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
723 AliDebug(1,Form("Reading motifType %s (%s) from file",
724 sMotifType.Data(),id.Data()));
725 motifType = reader.BuildMotifType(dataStreams,sMotifType.Data());
726 fMotifMap->AddMotifType(motifType);
730 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
731 sMotifType.Data(),id.Data()));
733 specialMotif = reader.BuildMotifSpecial(dataStreams,sMotifSpecial,motifType,scale);
734 fMotifMap->AddMotif(specialMotif);
738 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
742 AliError("pcb not null as expected");
744 pcb = new AliMpPCB(pcbType,specialMotif);
747 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
749 std::istringstream sin(sline(kMotifKeyword.Length(),
750 sline.Length()-kMotifKeyword.Length()).Data());
754 sin >> sMotifType >> ix >> iy;
756 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
759 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
760 motifType = reader.BuildMotifType(dataStreams,sMotifType.Data());
761 fMotifMap->AddMotifType(motifType);
765 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
770 AliError("pcb null");
773 pcb->Add(motifType,ix,iy);
782 //_____________________________________________________________________________
784 AliMpTriggerReader::ReadSlat(const AliMpDataStreams& dataStreams,
785 const char* slatType, AliMp::PlaneType planeType)
788 /// Create a new AliMpTrigger object, by reading it from file.
789 /// Returned object must be deleted by client.
791 Double_t scale = 1.0;
792 Bool_t flipX = kFALSE;
793 Bool_t flipY = kFALSE;
795 lines.SetOwner(kTRUE);
799 // Read the file and its include (if any) and store the result
800 // in a TObjArray of TObjStrings.
801 ReadLines(dataStreams,
802 slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
804 // Here some more sanity checks could be done.
805 // For the moment we only insure that the first line contains
807 TString& firstLine = ((TObjString*)lines.First())->String();
808 if ( !IsLayerLine(firstLine) )
810 std::ostringstream s;
811 s << GetKeywordLayer();
812 lines.AddFirst(new TObjString(s.str().c_str()));
815 AliDebugClass(2,Form("Scale=%g\n",scale));
817 FlipLines(dataStreams,lines,flipX,flipY,srcLine,destLine);
819 // Now splits the lines in packets corresponding to different layers
820 // (if any), and create sub-slats.
822 layers.SetOwner(kTRUE);
827 while ( ( osline = (TObjString*)it.Next() ) )
829 TString& s = osline->String();
830 if ( IsLayerLine(s) )
832 TList* list = new TList;
833 list->SetOwner(kTRUE);
839 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
843 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
845 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
847 for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
849 TList& lines1 = *((TList*)layers.At(ilayer));
850 std::ostringstream slatName;
851 slatName << slatType << "-LAYER" << ilayer;
852 AliMpSlat* slat = BuildSlat(dataStreams,
853 slatName.str().c_str(),planeType,lines1,scale);
856 Bool_t ok = triggerSlat->AdoptLayer(slat);
859 StdoutToAliError(cout << "could not add slat=" << endl;
861 cout << "to the triggerSlat=" << endl;
862 triggerSlat->Print();
864 AliError("Slat is=");
865 for ( Int_t i = 0; i < slat->GetSize(); ++i )
867 AliMpPCB* pcb = slat->GetPCB(i);
868 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
869 i,pcb->DX()*2,pcb->DY()*2,
870 pcb->DX()*2/scale,pcb->DY()*2/scale));
872 AliError("TriggerSlat is=");
873 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
875 AliMpSlat* slat1 = triggerSlat->GetLayer(j);
876 AliError(Form("Layer %d",j));
877 for ( Int_t i = 0; i < slat1->GetSize(); ++i )
879 AliMpPCB* pcb = slat1->GetPCB(i);
880 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
881 i,pcb->DX()*2,pcb->DY()*2,
882 pcb->DX()*2/scale,pcb->DY()*2/scale));
885 StdoutToAliError(fMotifMap->Print(););
890 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));