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(const AliMpDataStreams& dataStreams, AliMpSlatMotifMap* motifMap)
111 fkDataStreams(dataStreams),
118 fLocalBoardMap.SetOwner(kTRUE);
121 //_____________________________________________________________________________
122 AliMpTriggerReader::~AliMpTriggerReader()
127 fLocalBoardMap.DeleteAll();
130 //_____________________________________________________________________________
132 AliMpTriggerReader::BuildSlat(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(pcbName.Data());
184 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
189 TArrayI allLocalBoards;
191 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
193 TArrayI localBoardNumbers;
194 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
195 Ssiz_t pos = localBoards.First('-');
198 pos = localBoards.Length();
200 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
201 ';',localBoardNumbers);
202 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
204 std::ostringstream name;
205 name << localBoards(0,pos-1) << localBoardNumbers[i];
206 AliDebugClass(3,name.str().c_str());
207 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
208 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
209 allLocalBoards.Set(allLocalBoards.GetSize()+1);
210 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
211 if (localBoardNumbers[i] < 0 )
213 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
214 " to be correct... : %s\n",slatName,name.str().c_str()));
218 AliDebug(3,"Deleting tokens");
220 AliDebug(3,"Deleting localBoardList");
221 delete localBoardList;
222 AliDebug(3,"Adding pcb to slat");
223 slat->Add(*pcbType,allLocalBoards);
224 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
229 if ( slat->DX()== 0 || slat->DY() == 0 )
231 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
236 //_____________________________________________________________________________
238 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
240 /// Decode the string to get the board name
243 TObjArray* tokens = s.Tokenize(' ');
245 TString& keyword = ((TObjString*)tokens->At(0))->String();
247 if ( keyword == GetKeywordPcb() &&
248 tokens->GetEntriesFast() == 3 )
250 boardName = ((TObjString*)tokens->At(2))->String();
258 //_____________________________________________________________________________
260 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
261 Int_t srcLine, Int_t destLine)
264 /// Change the local board names contained in lines,
265 /// to go from right to left, and/or
266 /// from top to bottom
272 // Simply swaps R(ight) and L(eft) in the first character of
277 while ( ( oline = (TObjString*)it.Next() ) )
279 TString& s = oline->String();
280 if ( s.Contains("RC") )
282 // Change right to left
283 s.ReplaceAll("RC","LC");
285 else if ( s.Contains("LC") )
287 // Change left to right
288 s.ReplaceAll("LC","RC");
295 // Change line number, according to parameters srcLine and destLine
296 // Note that because of road opening (for planes 3 and 4 at least),
297 // we loop for srcLine +-1
299 for ( Int_t line = -1; line <=1; ++line )
301 std::ostringstream src,dest;
302 src << "L" << srcLine+line;
303 dest << "L" << destLine-line;
304 if ( src.str() == dest.str() ) continue;
306 for ( Int_t i = 0; i < lines.GetSize(); ++i )
308 TObjString* oline = (TObjString*)lines.At(i);
310 TString& s = oline->String();
312 if ( !s.Contains(GetKeywordPcb()) )
314 // Only consider PCB lines.
318 if ( s.Contains(src.str().c_str()) )
320 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
321 src.str().c_str(),dest.str().c_str(),s.Data()));
323 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
325 AliDebugClass(4,s.Data());
327 TString boardName(GetBoardNameFromPCBLine(s));
331 // We must also change board numbers, with the tricky
332 // thing that up and down must be swapped...
333 // Up can only be 1 card so it must be B1
334 // Down must be the uppper card of the line before, so
335 // the biggest possible board number for this Line,Column
340 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
341 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
342 AliDebugClass(4,s.Data());
346 // find the largest valid board number
347 for ( int b = 4; b>=1; --b )
349 std::ostringstream bs;
350 bs << boardName(0,boardName.Length()-1) << b;
351 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
353 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
354 boardName(boardName.Length()-2,2).Data(),
357 s.ReplaceAll(boardName(boardName.Length()-2,2),
364 // Check that the replacement we did is ok. If not,
366 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
369 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
380 //___________________________________________________________________________
382 AliMpTriggerReader::IsLayerLine(const TString& sline) const
384 /// Whether sline contains LAYER keyword
386 if ( sline.BeginsWith(GetKeywordLayer()) )
396 //___________________________________________________________________________
398 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
400 Bool_t& flipX, Bool_t& flipY)
402 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
404 Ssiz_t blankPos = sline.First(' ');
405 if ( blankPos < 0 ) return 0;
407 TString keyword(sline(0,blankPos));
409 if ( keyword == GetKeywordFlipX() )
412 } else if ( keyword == GetKeywordFlipY() )
421 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
425 //___________________________________________________________________________
427 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
428 Double_t& scale, TString& slatType)
430 /// Decode sline containing SCALE keyword
432 if ( sline(0,GetKeywordScale().Length()) == GetKeywordScale() )
434 TString tmp(sline(GetKeywordScale().Length()+1,
435 sline.Length()-GetKeywordScale().Length()-1));
436 Ssiz_t blankPos = tmp.First(' ');
439 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
440 " SCALE keyword : %s\n",tmp.Data()));
445 slatType = tmp(0,blankPos);
446 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
454 //_____________________________________________________________________________
456 AliMpTriggerReader::GetLine(const TString& slatType)
459 /// Assuming slatType is a 4 character string of the form XSLN
460 /// where X=1,2,3 or 4
462 /// N is the line number
465 if ( isdigit(slatType[0]) &&
466 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
469 return atoi(slatType(3,1).Data());
474 //_____________________________________________________________________________
476 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
478 /// From local board name to local board number
480 if ( !fLocalBoardMap.GetSize() )
482 ReadLocalBoardMapping();
485 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
489 return atoi(((TObjString*)pair->Value())->String().Data());
494 //_____________________________________________________________________________
496 AliMpTriggerReader::ReadLines(const char* slatType,
497 AliMp::PlaneType planeType,
500 Bool_t& flipX, Bool_t& flipY,
501 Int_t& srcLine, Int_t& destLine)
504 /// Reads in lines from file for a given slat
505 /// Returns the list of lines (lines), together with some global
506 /// information as the scale, whether to flip the lines, etc...
508 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
509 " destLine %d\n",slatType,scale,flipX,flipY,
514 CreateDataStream(AliMpFiles::SlatFilePath(
515 AliMp::kStationTrigger,slatType, planeType));
519 while ( in.getline(line,80) )
521 TString sline(AliMpHelper::Normalize(line));
523 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
525 Bool_t isKeywordThere =
526 sline.Contains(GetKeywordPcb()) ||
527 sline.Contains(GetKeywordLayer()) ||
528 sline.Contains(GetKeywordScale()) ||
529 sline.Contains(GetKeywordFlipX()) ||
530 sline.Contains(GetKeywordFlipY());
532 if ( !isKeywordThere )
534 AliErrorClass(Form("Got a line with no keyword : %s."
535 "That's not valid\n",line));
542 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
546 if ( isScaleLine < 0 )
548 AliFatalClass(Form("Syntax error near %s keyword\n",GetKeywordScale().Data()));
550 else if ( isScaleLine > 0 && slatType2 != slatType )
552 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
558 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
563 srcLine = GetLine(slatType2);
564 destLine = GetLine(slatType);
568 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
572 lines.Add(new TObjString(sline.Data()));
580 //_____________________________________________________________________________
582 AliMpTriggerReader::ReadLocalBoardMapping()
584 /// Reads the file that contains the mapping local board name <-> number
586 fLocalBoardMap.DeleteAll();
592 CreateDataStream(AliMpFiles::LocalTriggerBoardMapping());
595 Char_t localBoardName[20];
596 Int_t j,localBoardId;
602 for (Int_t i = 0; i < 4; ++i)
603 if (!in.getline(line,80)) continue; //skip 4 first lines
606 if (!in.getline(line,80)) break;
607 sscanf(line,"%hx",&mask);
610 if (!in.getline(line,80)) break;
611 sscanf(line,"%d",&nofBoards);
613 for ( Int_t i = 0; i < nofBoards; ++i )
616 if (!in.getline(line,80)) break;
617 sscanf(line,"%02d %19s %03d %03x", &j, localBoardName, &localBoardId, &switches);
618 if (localBoardId <= AliMpConstants::NofLocalBoards())
620 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
621 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
623 // skip 2 following lines
624 if (!in.getline(line,80)) break;
625 if (!in.getline(line,80)) break;
633 //_____________________________________________________________________________
635 AliMpTriggerReader::ReadPCB(const char* pcbType)
638 /// Create a new AliMpPCB object, by reading it from file.
639 /// Returned pointer must be deleted by client.
641 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
643 TString pcbName(pcbType);
645 Ssiz_t pos = pcbName.First('x');
647 Double_t scale = 1.0;
651 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
652 pcbName = pcbName(0,pos);
657 CreateDataStream(AliMpFiles::SlatPCBFilePath(
658 AliMp::kStationTrigger,pcbName));
660 AliMpMotifReader reader(fkDataStreams,
661 AliMp::kStationTrigger, AliMq::kNotSt12, AliMp::kNonBendingPlane);
662 // note that the nonbending
663 // parameter is of no use for trigger, as far as reading motif is
664 // concerned, as all motifs are supposed to be in the same directory
665 // (as they are shared by bending/non-bending planes).
669 const TString kSizeKeyword("SIZES");
670 const TString kMotifKeyword("MOTIF");
671 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
675 while ( in.getline(line,80) )
677 if ( line[0] == '#' ) continue;
681 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
683 std::istringstream sin(sline(kSizeKeyword.Length(),
684 sline.Length()-kSizeKeyword.Length()-1).Data());
685 float padSizeX = 0.0;
686 float padSizeY = 0.0;
687 float pcbSizeX = 0.0;
688 float pcbSizeY = 0.0;
689 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
692 AliError("pcb not null as expected");
694 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
695 pcbSizeX*scale,pcbSizeY*scale);
698 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
700 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
701 sline.Length()-kMotifSpecialKeyword.Length()).Data());
702 TString sMotifSpecial;
704 sin >> sMotifSpecial >> sMotifType;
706 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
708 AliMpMotifSpecial* specialMotif =
709 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
712 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
713 sMotifSpecial.Data(),id.Data()));
714 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
717 AliDebug(1,Form("Reading motifType %s (%s) from file",
718 sMotifType.Data(),id.Data()));
719 motifType = reader.BuildMotifType(sMotifType.Data());
720 fMotifMap->AddMotifType(motifType);
724 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
725 sMotifType.Data(),id.Data()));
727 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
728 fMotifMap->AddMotif(specialMotif);
732 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
736 AliError("pcb not null as expected");
738 pcb = new AliMpPCB(pcbType,specialMotif);
741 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
743 std::istringstream sin(sline(kMotifKeyword.Length(),
744 sline.Length()-kMotifKeyword.Length()).Data());
748 sin >> sMotifType >> ix >> iy;
750 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
753 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
754 motifType = reader.BuildMotifType(sMotifType.Data());
755 fMotifMap->AddMotifType(motifType);
759 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
764 AliError("pcb null");
767 pcb->Add(motifType,ix,iy);
776 //_____________________________________________________________________________
778 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
781 /// Create a new AliMpTrigger object, by reading it from file.
782 /// Returned object must be deleted by client.
784 Double_t scale = 1.0;
785 Bool_t flipX = kFALSE;
786 Bool_t flipY = kFALSE;
788 lines.SetOwner(kTRUE);
792 // Read the file and its include (if any) and store the result
793 // in a TObjArray of TObjStrings.
794 ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
796 // Here some more sanity checks could be done.
797 // For the moment we only insure that the first line contains
799 TString& firstLine = ((TObjString*)lines.First())->String();
800 if ( !IsLayerLine(firstLine) )
802 std::ostringstream s;
803 s << GetKeywordLayer();
804 lines.AddFirst(new TObjString(s.str().c_str()));
807 AliDebugClass(2,Form("Scale=%g\n",scale));
809 FlipLines(lines,flipX,flipY,srcLine,destLine);
811 // Now splits the lines in packets corresponding to different layers
812 // (if any), and create sub-slats.
814 layers.SetOwner(kTRUE);
819 while ( ( osline = (TObjString*)it.Next() ) )
821 TString& s = osline->String();
822 if ( IsLayerLine(s) )
824 TList* list = new TList;
825 list->SetOwner(kTRUE);
831 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
835 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
837 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
839 for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
841 TList& lines1 = *((TList*)layers.At(ilayer));
842 std::ostringstream slatName;
843 slatName << slatType << "-LAYER" << ilayer;
844 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines1,scale);
847 Bool_t ok = triggerSlat->AdoptLayer(slat);
850 StdoutToAliError(cout << "could not add slat=" << endl;
852 cout << "to the triggerSlat=" << endl;
853 triggerSlat->Print();
855 AliError("Slat is=");
856 for ( Int_t i = 0; i < slat->GetSize(); ++i )
858 AliMpPCB* pcb = slat->GetPCB(i);
859 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
860 i,pcb->DX()*2,pcb->DY()*2,
861 pcb->DX()*2/scale,pcb->DY()*2/scale));
863 AliError("TriggerSlat is=");
864 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
866 AliMpSlat* slat1 = triggerSlat->GetLayer(j);
867 AliError(Form("Layer %d",j));
868 for ( Int_t i = 0; i < slat1->GetSize(); ++i )
870 AliMpPCB* pcb = slat1->GetPCB(i);
871 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
872 i,pcb->DX()*2,pcb->DY()*2,
873 pcb->DX()*2/scale,pcb->DY()*2/scale));
876 StdoutToAliError(fMotifMap->Print(););
881 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));