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)
60 const TString AliMpTriggerReader::fgkKeywordLayer("LAYER");
61 const TString AliMpTriggerReader::fgkKeywordScale("SCALE");
62 const TString AliMpTriggerReader::fgkKeywordPcb("PCB");
63 const TString AliMpTriggerReader::fgkKeywordFlipX("FLIP_X");
64 const TString AliMpTriggerReader::fgkKeywordFlipY("FLIP_Y");
66 //_____________________________________________________________________________
67 AliMpTriggerReader::AliMpTriggerReader(const AliMpDataStreams& dataStreams, AliMpSlatMotifMap* motifMap)
69 fkDataStreams(dataStreams),
76 fLocalBoardMap.SetOwner(kTRUE);
79 //_____________________________________________________________________________
80 AliMpTriggerReader::~AliMpTriggerReader()
85 fLocalBoardMap.DeleteAll();
88 //_____________________________________________________________________________
90 AliMpTriggerReader::BuildSlat(const char* slatName,
91 AliMp::PlaneType planeType,
95 /// Construct a slat from the list of lines, taking into account
96 /// the scale factor. The returned pointer must be deleted by the client
98 AliDebug(1,Form("slat %s %s scale %e",
99 slatName,PlaneTypeName(planeType).Data(),scale))
102 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
106 // StdoutToAliDebug(3,lines.Print(););
109 while ( ( osline = (TObjString*)it.Next() ) )
111 // note that at this stage lines should not be empty.
112 TString sline(osline->String());
114 TObjArray* tokens = sline.Tokenize(' ');
116 TString& keyword = ((TObjString*)tokens->At(0))->String();
118 if ( keyword == fgkKeywordPcb )
120 if ( tokens->GetEntriesFast() != 3 )
122 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
123 " in following line:\n%s",sline.Data()));
128 TString pcbName = ((TObjString*)tokens->At(1))->String();
130 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
134 std::ostringstream s;
135 s << pcbName.Data() << "x" << scale;
136 pcbName = s.str().c_str();
139 AliMpPCB* pcbType = ReadPCB(pcbName.Data());
142 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
147 TArrayI allLocalBoards;
149 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
151 TArrayI localBoardNumbers;
152 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
153 Ssiz_t pos = localBoards.First('-');
156 pos = localBoards.Length();
158 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
159 ';',localBoardNumbers);
160 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
162 std::ostringstream name;
163 name << localBoards(0,pos-1) << localBoardNumbers[i];
164 AliDebugClass(3,name.str().c_str());
165 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
166 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
167 allLocalBoards.Set(allLocalBoards.GetSize()+1);
168 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
169 if (localBoardNumbers[i] < 0 )
171 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
172 " to be correct... : %s\n",slatName,name.str().c_str()));
176 AliDebug(3,"Deleting tokens");
178 AliDebug(3,"Deleting localBoardList");
179 delete localBoardList;
180 AliDebug(3,"Adding pcb to slat");
181 slat->Add(*pcbType,allLocalBoards);
182 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
187 if ( slat->DX()== 0 || slat->DY() == 0 )
189 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
194 //_____________________________________________________________________________
196 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
198 /// Decode the string to get the board name
201 TObjArray* tokens = s.Tokenize(' ');
203 TString& keyword = ((TObjString*)tokens->At(0))->String();
205 if ( keyword == fgkKeywordPcb &&
206 tokens->GetEntriesFast() == 3 )
208 boardName = ((TObjString*)tokens->At(2))->String();
216 //_____________________________________________________________________________
218 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
219 Int_t srcLine, Int_t destLine)
222 /// Change the local board names contained in lines,
223 /// to go from right to left, and/or
224 /// from top to bottom
230 // Simply swaps R(ight) and L(eft) in the first character of
235 while ( ( oline = (TObjString*)it.Next() ) )
237 TString& s = oline->String();
238 if ( s.Contains("RC") )
240 // Change right to left
241 s.ReplaceAll("RC","LC");
243 else if ( s.Contains("LC") )
245 // Change left to right
246 s.ReplaceAll("LC","RC");
253 // Change line number, according to parameters srcLine and destLine
254 // Note that because of road opening (for planes 3 and 4 at least),
255 // we loop for srcLine +-1
257 for ( Int_t line = -1; line <=1; ++line )
259 std::ostringstream src,dest;
260 src << "L" << srcLine+line;
261 dest << "L" << destLine-line;
262 if ( src.str() == dest.str() ) continue;
264 for ( Int_t i = 0; i < lines.GetSize(); ++i )
266 TObjString* oline = (TObjString*)lines.At(i);
268 TString& s = oline->String();
270 if ( !s.Contains(fgkKeywordPcb) )
272 // Only consider PCB lines.
276 if ( s.Contains(src.str().c_str()) )
278 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
279 src.str().c_str(),dest.str().c_str(),s.Data()));
281 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
283 AliDebugClass(4,s.Data());
285 TString boardName(GetBoardNameFromPCBLine(s));
289 // We must also change board numbers, with the tricky
290 // thing that up and down must be swapped...
291 // Up can only be 1 card so it must be B1
292 // Down must be the uppper card of the line before, so
293 // the biggest possible board number for this Line,Column
298 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
299 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
300 AliDebugClass(4,s.Data());
304 // find the largest valid board number
305 for ( int b = 4; b>=1; --b )
307 std::ostringstream bs;
308 bs << boardName(0,boardName.Length()-1) << b;
309 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
311 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
312 boardName(boardName.Length()-2,2).Data(),
315 s.ReplaceAll(boardName(boardName.Length()-2,2),
322 // Check that the replacement we did is ok. If not,
324 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
327 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
338 //___________________________________________________________________________
340 AliMpTriggerReader::IsLayerLine(const TString& sline) const
342 /// Whether sline contains LAYER keyword
344 if ( sline.BeginsWith(fgkKeywordLayer) )
354 //___________________________________________________________________________
356 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
358 Bool_t& flipX, Bool_t& flipY)
360 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
362 Ssiz_t blankPos = sline.First(' ');
363 if ( blankPos < 0 ) return 0;
365 TString keyword(sline(0,blankPos));
367 if ( keyword == fgkKeywordFlipX )
370 } else if ( keyword == fgkKeywordFlipY )
379 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
383 //___________________________________________________________________________
385 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
386 Double_t& scale, TString& slatType)
388 /// Decode sline containing SCALE keyword
390 if ( sline(0,fgkKeywordScale.Length()) == fgkKeywordScale )
392 TString tmp(sline(fgkKeywordScale.Length()+1,
393 sline.Length()-fgkKeywordScale.Length()-1));
394 Ssiz_t blankPos = tmp.First(' ');
397 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
398 " SCALE keyword : %s\n",tmp.Data()));
403 slatType = tmp(0,blankPos);
404 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
412 //_____________________________________________________________________________
414 AliMpTriggerReader::GetLine(const TString& slatType)
417 /// Assuming slatType is a 4 character string of the form XSLN
418 /// where X=1,2,3 or 4
420 /// N is the line number
423 if ( isdigit(slatType[0]) &&
424 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
427 return atoi(slatType(3,1).Data());
432 //_____________________________________________________________________________
434 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
436 /// From local board name to local board number
438 if ( !fLocalBoardMap.GetSize() )
440 ReadLocalBoardMapping();
443 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
447 return atoi(((TObjString*)pair->Value())->String().Data());
452 //_____________________________________________________________________________
454 AliMpTriggerReader::ReadLines(const char* slatType,
455 AliMp::PlaneType planeType,
458 Bool_t& flipX, Bool_t& flipY,
459 Int_t& srcLine, Int_t& destLine)
462 /// Reads in lines from file for a given slat
463 /// Returns the list of lines (lines), together with some global
464 /// information as the scale, whether to flip the lines, etc...
466 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
467 " destLine %d\n",slatType,scale,flipX,flipY,
472 CreateDataStream(AliMpFiles::SlatFilePath(
473 AliMp::kStationTrigger,slatType, planeType));
477 while ( in.getline(line,80) )
479 TString sline(AliMpHelper::Normalize(line));
481 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
483 Bool_t isKeywordThere =
484 sline.Contains(fgkKeywordPcb) ||
485 sline.Contains(fgkKeywordLayer) ||
486 sline.Contains(fgkKeywordScale) ||
487 sline.Contains(fgkKeywordFlipX) ||
488 sline.Contains(fgkKeywordFlipY);
490 if ( !isKeywordThere )
492 AliErrorClass(Form("Got a line with no keyword : %s."
493 "That's not valid\n",line));
500 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
504 if ( isScaleLine < 0 )
506 AliFatalClass(Form("Syntax error near %s keyword\n",fgkKeywordScale.Data()));
508 else if ( isScaleLine > 0 && slatType2 != slatType )
510 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
516 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
521 srcLine = GetLine(slatType2);
522 destLine = GetLine(slatType);
526 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
530 lines.Add(new TObjString(sline.Data()));
538 //_____________________________________________________________________________
540 AliMpTriggerReader::ReadLocalBoardMapping()
542 /// Reads the file that contains the mapping local board name <-> number
544 fLocalBoardMap.DeleteAll();
550 CreateDataStream(AliMpFiles::LocalTriggerBoardMapping());
553 Char_t localBoardName[20];
554 Int_t j,localBoardId;
560 for (Int_t i = 0; i < 4; ++i)
561 if (!in.getline(line,80)) continue; //skip 4 first lines
564 if (!in.getline(line,80)) break;
565 sscanf(line,"%hx",&mask);
568 if (!in.getline(line,80)) break;
569 sscanf(line,"%d",&nofBoards);
571 for ( Int_t i = 0; i < nofBoards; ++i )
574 if (!in.getline(line,80)) break;
575 sscanf(line,"%02d %19s %03d %03x", &j, localBoardName, &localBoardId, &switches);
576 if (localBoardId <= AliMpConstants::NofLocalBoards())
578 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
579 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
581 // skip 2 following lines
582 if (!in.getline(line,80)) break;
583 if (!in.getline(line,80)) break;
591 //_____________________________________________________________________________
593 AliMpTriggerReader::ReadPCB(const char* pcbType)
596 /// Create a new AliMpPCB object, by reading it from file.
597 /// Returned pointer must be deleted by client.
599 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
601 TString pcbName(pcbType);
603 Ssiz_t pos = pcbName.First('x');
605 Double_t scale = 1.0;
609 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
610 pcbName = pcbName(0,pos);
615 CreateDataStream(AliMpFiles::SlatPCBFilePath(
616 AliMp::kStationTrigger,pcbName));
618 AliMpMotifReader reader(fkDataStreams,
619 AliMp::kStationTrigger, AliMq::kNotSt12, AliMp::kNonBendingPlane);
620 // note that the nonbending
621 // parameter is of no use for trigger, as far as reading motif is
622 // concerned, as all motifs are supposed to be in the same directory
623 // (as they are shared by bending/non-bending planes).
627 const TString kSizeKeyword("SIZES");
628 const TString kMotifKeyword("MOTIF");
629 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
633 while ( in.getline(line,80) )
635 if ( line[0] == '#' ) continue;
639 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
641 std::istringstream sin(sline(kSizeKeyword.Length(),
642 sline.Length()-kSizeKeyword.Length()-1).Data());
643 float padSizeX = 0.0;
644 float padSizeY = 0.0;
645 float pcbSizeX = 0.0;
646 float pcbSizeY = 0.0;
647 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
650 AliError("pcb not null as expected");
652 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
653 pcbSizeX*scale,pcbSizeY*scale);
656 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
658 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
659 sline.Length()-kMotifSpecialKeyword.Length()).Data());
660 TString sMotifSpecial;
662 sin >> sMotifSpecial >> sMotifType;
664 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
666 AliMpMotifSpecial* specialMotif =
667 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
670 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
671 sMotifSpecial.Data(),id.Data()));
672 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
675 AliDebug(1,Form("Reading motifType %s (%s) from file",
676 sMotifType.Data(),id.Data()));
677 motifType = reader.BuildMotifType(sMotifType.Data());
678 fMotifMap->AddMotifType(motifType);
682 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
683 sMotifType.Data(),id.Data()));
685 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
686 fMotifMap->AddMotif(specialMotif);
690 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
694 AliError("pcb not null as expected");
696 pcb = new AliMpPCB(pcbType,specialMotif);
699 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
701 std::istringstream sin(sline(kMotifKeyword.Length(),
702 sline.Length()-kMotifKeyword.Length()).Data());
706 sin >> sMotifType >> ix >> iy;
708 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
711 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
712 motifType = reader.BuildMotifType(sMotifType.Data());
713 fMotifMap->AddMotifType(motifType);
717 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
722 AliError("pcb null");
725 pcb->Add(motifType,ix,iy);
734 //_____________________________________________________________________________
736 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
739 /// Create a new AliMpTrigger object, by reading it from file.
740 /// Returned object must be deleted by client.
742 Double_t scale = 1.0;
743 Bool_t flipX = kFALSE;
744 Bool_t flipY = kFALSE;
746 lines.SetOwner(kTRUE);
750 // Read the file and its include (if any) and store the result
751 // in a TObjArray of TObjStrings.
752 ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
754 // Here some more sanity checks could be done.
755 // For the moment we only insure that the first line contains
757 TString& firstLine = ((TObjString*)lines.First())->String();
758 if ( !IsLayerLine(firstLine) )
760 std::ostringstream s;
761 s << fgkKeywordLayer;
762 lines.AddFirst(new TObjString(s.str().c_str()));
765 AliDebugClass(2,Form("Scale=%g\n",scale));
767 FlipLines(lines,flipX,flipY,srcLine,destLine);
769 // Now splits the lines in packets corresponding to different layers
770 // (if any), and create sub-slats.
772 layers.SetOwner(kTRUE);
777 while ( ( osline = (TObjString*)it.Next() ) )
779 TString& s = osline->String();
780 if ( IsLayerLine(s) )
782 TList* list = new TList;
783 list->SetOwner(kTRUE);
789 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
793 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
795 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
797 for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
799 TList& lines1 = *((TList*)layers.At(ilayer));
800 std::ostringstream slatName;
801 slatName << slatType << "-LAYER" << ilayer;
802 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines1,scale);
805 Bool_t ok = triggerSlat->AdoptLayer(slat);
808 StdoutToAliError(cout << "could not add slat=" << endl;
810 cout << "to the triggerSlat=" << endl;
811 triggerSlat->Print();
813 AliError("Slat is=");
814 for ( Int_t i = 0; i < slat->GetSize(); ++i )
816 AliMpPCB* pcb = slat->GetPCB(i);
817 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
818 i,pcb->DX()*2,pcb->DY()*2,
819 pcb->DX()*2/scale,pcb->DY()*2/scale));
821 AliError("TriggerSlat is=");
822 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
824 AliMpSlat* slat1 = triggerSlat->GetLayer(j);
825 AliError(Form("Layer %d",j));
826 for ( Int_t i = 0; i < slat1->GetSize(); ++i )
828 AliMpPCB* pcb = slat1->GetPCB(i);
829 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
830 i,pcb->DX()*2,pcb->DY()*2,
831 pcb->DX()*2/scale,pcb->DY()*2/scale));
834 StdoutToAliError(fMotifMap->Print(););
839 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));