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 $
20 #include "AliMpTriggerReader.h"
21 #include "AliMpMotifReader.h"
22 #include "AliMpFiles.h"
23 #include "AliMpMotifType.h"
25 #include "AliMpSlat.h"
26 #include "AliMpSlatMotifMap.h"
27 #include "AliMpMotifSpecial.h"
28 #include "AliMpMotifPosition.h"
29 #include "AliMpMotif.h"
30 #include "AliMpHelper.h"
31 #include "AliMpSt345Reader.h"
32 #include "AliMpTrigger.h"
33 #include "AliMpConstants.h"
37 #include "Riostream.h"
39 #include "TObjString.h"
45 //-----------------------------------------------------------------------------
46 /// \class AliMpTriggerReader
47 /// Read trigger slat ASCII files
48 /// Basically provides two methods:
49 /// - AliMpTrigger* ReadSlat()
50 /// - AliMpPCB* ReadPCB()
52 /// \author Laurent Aphecetche
53 //-----------------------------------------------------------------------------
56 ClassImp(AliMpTriggerReader)
59 const TString AliMpTriggerReader::fgkKeywordLayer("LAYER");
60 const TString AliMpTriggerReader::fgkKeywordScale("SCALE");
61 const TString AliMpTriggerReader::fgkKeywordPcb("PCB");
62 const TString AliMpTriggerReader::fgkKeywordFlipX("FLIP_X");
63 const TString AliMpTriggerReader::fgkKeywordFlipY("FLIP_Y");
65 //_____________________________________________________________________________
66 AliMpTriggerReader::AliMpTriggerReader()
68 fMotifMap(AliMpSlatMotifMap::Instance()),
74 fLocalBoardMap.SetOwner(kTRUE);
77 //_____________________________________________________________________________
78 AliMpTriggerReader::~AliMpTriggerReader()
83 fLocalBoardMap.DeleteAll();
86 //_____________________________________________________________________________
88 AliMpTriggerReader::BuildSlat(const char* slatName,
89 AliMp::PlaneType planeType,
93 /// Construct a slat from the list of lines, taking into account
94 /// the scale factor. The returned pointer must be deleted by the client
96 AliDebug(1,Form("slat %s %s scale %e",
97 slatName,PlaneTypeName(planeType).Data(),scale))
100 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
104 // StdoutToAliDebug(3,lines.Print(););
107 while ( ( osline = (TObjString*)it.Next() ) )
109 // note that at this stage lines should not be empty.
110 TString sline(osline->String());
112 TObjArray* tokens = sline.Tokenize(' ');
114 TString& keyword = ((TObjString*)tokens->At(0))->String();
116 if ( keyword == fgkKeywordPcb )
118 if ( tokens->GetEntriesFast() != 3 )
120 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
121 " in following line:\n%s",sline.Data()));
126 TString pcbName = ((TObjString*)tokens->At(1))->String();
128 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
132 std::ostringstream s;
133 s << pcbName.Data() << "x" << scale;
134 pcbName = s.str().c_str();
137 AliMpPCB* pcbType = ReadPCB(pcbName.Data());
140 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
145 TArrayI allLocalBoards;
147 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
149 TArrayI localBoardNumbers;
150 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
151 Ssiz_t pos = localBoards.First('-');
154 pos = localBoards.Length();
156 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
157 ';',localBoardNumbers);
158 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
160 std::ostringstream name;
161 name << localBoards(0,pos-1) << localBoardNumbers[i];
162 AliDebugClass(3,name.str().c_str());
163 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
164 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
165 allLocalBoards.Set(allLocalBoards.GetSize()+1);
166 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
167 if (localBoardNumbers[i] < 0 )
169 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
170 " to be correct... : %s\n",slatName,name.str().c_str()));
174 AliDebug(3,"Deleting tokens");
176 AliDebug(3,"Deleting localBoardList");
177 delete localBoardList;
178 AliDebug(3,"Adding pcb to slat");
179 slat->Add(*pcbType,allLocalBoards);
180 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
185 if ( slat->DX()== 0 || slat->DY() == 0 )
187 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
192 //_____________________________________________________________________________
194 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
196 /// Decode the string to get the board name
199 TObjArray* tokens = s.Tokenize(' ');
201 TString& keyword = ((TObjString*)tokens->At(0))->String();
203 if ( keyword == fgkKeywordPcb &&
204 tokens->GetEntriesFast() == 3 )
206 boardName = ((TObjString*)tokens->At(2))->String();
214 //_____________________________________________________________________________
216 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
217 Int_t srcLine, Int_t destLine)
220 /// Change the local board names contained in lines,
221 /// to go from right to left, and/or
222 /// from top to bottom
228 // Simply swaps R(ight) and L(eft) in the first character of
233 while ( ( oline = (TObjString*)it.Next() ) )
235 TString& s = oline->String();
236 if ( s.Contains("RC") )
238 // Change right to left
239 s.ReplaceAll("RC","LC");
241 else if ( s.Contains("LC") )
243 // Change left to right
244 s.ReplaceAll("LC","RC");
251 // Change line number, according to parameters srcLine and destLine
252 // Note that because of road opening (for planes 3 and 4 at least),
253 // we loop for srcLine +-1
255 for ( Int_t line = -1; line <=1; ++line )
257 std::ostringstream src,dest;
258 src << "L" << srcLine+line;
259 dest << "L" << destLine-line;
260 if ( src.str() == dest.str() ) continue;
262 for ( Int_t i = 0; i < lines.GetSize(); ++i )
264 TObjString* oline = (TObjString*)lines.At(i);
266 TString& s = oline->String();
268 if ( !s.Contains(fgkKeywordPcb) )
270 // Only consider PCB lines.
274 if ( s.Contains(src.str().c_str()) )
276 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
277 src.str().c_str(),dest.str().c_str(),s.Data()));
279 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
281 AliDebugClass(4,s.Data());
283 TString boardName(GetBoardNameFromPCBLine(s));
287 // We must also change board numbers, with the tricky
288 // thing that up and down must be swapped...
289 // Up can only be 1 card so it must be B1
290 // Down must be the uppper card of the line before, so
291 // the biggest possible board number for this Line,Column
296 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
297 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
298 AliDebugClass(4,s.Data());
302 // find the largest valid board number
303 for ( int b = 4; b>=1; --b )
305 std::ostringstream bs;
306 bs << boardName(0,boardName.Length()-1) << b;
307 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
309 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
310 boardName(boardName.Length()-2,2).Data(),
313 s.ReplaceAll(boardName(boardName.Length()-2,2),
320 // Check that the replacement we did is ok. If not,
322 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
325 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
336 //___________________________________________________________________________
338 AliMpTriggerReader::IsLayerLine(const TString& sline)
340 /// Whether sline contains LAYER keyword
342 if ( sline.BeginsWith(fgkKeywordLayer) )
352 //___________________________________________________________________________
354 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
356 Bool_t& flipX, Bool_t& flipY)
358 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
360 Ssiz_t blankPos = sline.First(' ');
361 if ( blankPos < 0 ) return 0;
363 TString keyword(sline(0,blankPos));
365 if ( keyword == fgkKeywordFlipX )
368 } else if ( keyword == fgkKeywordFlipY )
377 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
381 //___________________________________________________________________________
383 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
384 Double_t& scale, TString& slatType)
386 /// Decode sline containing SCALE keyword
388 if ( sline(0,fgkKeywordScale.Length()) == fgkKeywordScale )
390 TString tmp(sline(fgkKeywordScale.Length()+1,
391 sline.Length()-fgkKeywordScale.Length()-1));
392 Ssiz_t blankPos = tmp.First(' ');
395 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
396 " SCALE keyword : %s\n",tmp.Data()));
401 slatType = tmp(0,blankPos);
402 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
410 //_____________________________________________________________________________
412 AliMpTriggerReader::GetLine(const TString& slatType)
415 /// Assuming slatType is a 4 character string of the form XSLN
416 /// where X=1,2,3 or 4
418 /// N is the line number
421 if ( isdigit(slatType[0]) &&
422 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
425 return atoi(slatType(3,1).Data());
430 //_____________________________________________________________________________
432 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
434 /// From local board name to local board number
436 if ( !fLocalBoardMap.GetSize() )
438 ReadLocalBoardMapping();
441 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
445 return atoi(((TObjString*)pair->Value())->String().Data());
450 //_____________________________________________________________________________
452 AliMpTriggerReader::ReadLines(const char* slatType,
453 AliMp::PlaneType planeType,
456 Bool_t& flipX, Bool_t& flipY,
457 Int_t& srcLine, Int_t& destLine)
460 /// Reads in lines from file for a given slat
461 /// Returns the list of lines (lines), together with some global
462 /// information as the scale, whether to flip the lines, etc...
464 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
465 " destLine %d\n",slatType,scale,flipX,flipY,
468 TString filename(AliMpFiles::SlatFilePath(AliMp::kStationTrigger,slatType,
470 std::ifstream in(filename.Data());
473 AliErrorClass(Form("Cannot read slat from %s",filename.Data()));
478 while ( in.getline(line,80) )
480 TString sline(AliMpHelper::Normalize(line));
482 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
484 Bool_t isKeywordThere =
485 sline.Contains(fgkKeywordPcb) ||
486 sline.Contains(fgkKeywordLayer) ||
487 sline.Contains(fgkKeywordScale) ||
488 sline.Contains(fgkKeywordFlipX) ||
489 sline.Contains(fgkKeywordFlipY);
491 if ( !isKeywordThere )
493 AliErrorClass(Form("Got a line with no keyword : %s."
494 "That's not valid\n",line));
501 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
505 if ( isScaleLine < 0 )
507 AliFatalClass(Form("Syntax error near %s keyword\n",fgkKeywordScale.Data()));
509 else if ( isScaleLine > 0 && slatType2 != slatType )
511 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
517 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
522 srcLine = GetLine(slatType2);
523 destLine = GetLine(slatType);
527 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
531 lines.Add(new TObjString(sline.Data()));
539 //_____________________________________________________________________________
541 AliMpTriggerReader::ReadLocalBoardMapping()
543 /// Reads the file that contains the mapping local board name <-> number
545 TString filename(AliMpFiles::LocalTriggerBoardMapping());
547 AliDebugClass(2,Form("Reading from %s\n",filename.Data()));
549 fLocalBoardMap.DeleteAll();
553 ifstream in(filename.Data());
556 AliErrorClass(Form("Cannot read file %s\n",filename.Data()));
561 Char_t localBoardName[20];
562 Int_t j,localBoardId;
567 for (Int_t i = 0; i < 4; ++i)
568 if (!in.getline(line,80)) continue; //skip 4 first lines
571 if (!in.getline(line,80)) break;
572 sscanf(line,"%hx",&mask);
574 for ( Int_t i = 0; i < 16; ++i )
576 if ( (mask >> i ) & 0x1 )
578 if (!in.getline(line,80)) break;
579 sscanf(line,"%02d %s %03d %03x", &j, localBoardName, &localBoardId, &switches);
580 if (localBoardId <= AliMpConstants::NofLocalBoards())
582 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
583 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
585 // skip 2 following lines
586 if (!in.getline(line,80)) break;
587 if (!in.getline(line,80)) break;
596 //_____________________________________________________________________________
598 AliMpTriggerReader::ReadPCB(const char* pcbType)
601 /// Create a new AliMpPCB object, by reading it from file.
602 /// Returned pointer must be deleted by client.
604 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
606 TString pcbName(pcbType);
608 Ssiz_t pos = pcbName.First('x');
610 Double_t scale = 1.0;
614 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
615 pcbName = pcbName(0,pos);
618 std::ifstream in(AliMpFiles::SlatPCBFilePath(AliMp::kStationTrigger,pcbName).Data());
621 AliErrorClass(Form("Cannot open file for PCB %s",pcbName.Data()));
625 AliMpMotifReader reader(AliMp::kStationTrigger,AliMp::kNonBendingPlane);
626 // note that the nonbending
627 // parameter is of no use for trigger, as far as reading motif is
628 // concerned, as all motifs are supposed to be in the same directory
629 // (as they are shared by bending/non-bending planes).
633 const TString kSizeKeyword("SIZES");
634 const TString kMotifKeyword("MOTIF");
635 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
639 while ( in.getline(line,80) )
641 if ( line[0] == '#' ) continue;
645 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
647 std::istringstream sin(sline(kSizeKeyword.Length(),
648 sline.Length()-kSizeKeyword.Length()-1).Data());
649 float padSizeX = 0.0;
650 float padSizeY = 0.0;
651 float pcbSizeX = 0.0;
652 float pcbSizeY = 0.0;
653 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
656 AliError("pcb not null as expected");
658 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
659 pcbSizeX*scale,pcbSizeY*scale);
662 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
664 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
665 sline.Length()-kMotifSpecialKeyword.Length()).Data());
666 TString sMotifSpecial;
668 sin >> sMotifSpecial >> sMotifType;
670 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
672 AliMpMotifSpecial* specialMotif =
673 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
676 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
677 sMotifSpecial.Data(),id.Data()));
678 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
681 AliDebug(1,Form("Reading motifType %s (%s) from file",
682 sMotifType.Data(),id.Data()));
683 motifType = reader.BuildMotifType(sMotifType.Data());
684 fMotifMap->AddMotifType(motifType);
688 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
689 sMotifType.Data(),id.Data()));
691 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
692 fMotifMap->AddMotif(specialMotif);
696 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
700 AliError("pcb not null as expected");
702 pcb = new AliMpPCB(pcbType,specialMotif);
705 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
707 std::istringstream sin(sline(kMotifKeyword.Length(),
708 sline.Length()-kMotifKeyword.Length()).Data());
712 sin >> sMotifType >> ix >> iy;
714 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
717 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
718 motifType = reader.BuildMotifType(sMotifType.Data());
719 fMotifMap->AddMotifType(motifType);
723 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
726 pcb->Add(motifType,ix,iy);
735 //_____________________________________________________________________________
737 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
740 /// Create a new AliMpTrigger object, by reading it from file.
741 /// Returned object must be deleted by client.
743 Double_t scale = 1.0;
744 Bool_t flipX = kFALSE;
745 Bool_t flipY = kFALSE;
747 lines.SetOwner(kTRUE);
751 // Read the file and its include (if any) and store the result
752 // in a TObjArray of TObjStrings.
753 ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
755 // Here some more sanity checks could be done.
756 // For the moment we only insure that the first line contains
758 TString& firstLine = ((TObjString*)lines.First())->String();
759 if ( !IsLayerLine(firstLine) )
761 std::ostringstream s;
762 s << fgkKeywordLayer;
763 lines.AddFirst(new TObjString(s.str().c_str()));
766 AliDebugClass(2,Form("Scale=%g\n",scale));
768 FlipLines(lines,flipX,flipY,srcLine,destLine);
770 // Now splits the lines in packets corresponding to different layers
771 // (if any), and create sub-slats.
773 layers.SetOwner(kTRUE);
778 while ( ( osline = (TObjString*)it.Next() ) )
780 TString& s = osline->String();
781 if ( IsLayerLine(s) )
783 TList* list = new TList;
784 list->SetOwner(kTRUE);
790 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
794 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
796 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
798 for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
800 TList& lines1 = *((TList*)layers.At(ilayer));
801 std::ostringstream slatName;
802 slatName << slatType << "-LAYER" << ilayer;
803 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines1,scale);
806 Bool_t ok = triggerSlat->AdoptLayer(slat);
809 StdoutToAliError(cout << "could not add slat=" << endl;
811 cout << "to the triggerSlat=" << endl;
812 triggerSlat->Print();
814 AliError("Slat is=");
815 for ( Int_t i = 0; i < slat->GetSize(); ++i )
817 AliMpPCB* pcb = slat->GetPCB(i);
818 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
819 i,pcb->DX()*2,pcb->DY()*2,
820 pcb->DX()*2/scale,pcb->DY()*2/scale));
822 AliError("TriggerSlat is=");
823 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
825 AliMpSlat* slat1 = triggerSlat->GetLayer(j);
826 AliError(Form("Layer %d",j));
827 for ( Int_t i = 0; i < slat1->GetSize(); ++i )
829 AliMpPCB* pcb = slat1->GetPCB(i);
830 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
831 i,pcb->DX()*2,pcb->DY()*2,
832 pcb->DX()*2/scale,pcb->DY()*2/scale));
835 StdoutToAliError(fMotifMap->Print(););
840 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));