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"
20 #include "AliMpMotifReader.h"
21 #include "AliMpFiles.h"
22 #include "AliMpMotifType.h"
24 #include "AliMpSlat.h"
25 #include "AliMpSlatMotifMap.h"
26 #include "AliMpMotifSpecial.h"
27 #include "AliMpMotifPosition.h"
28 #include "AliMpMotif.h"
29 #include "AliMpHelper.h"
30 #include "AliMpSt345Reader.h"
31 #include "AliMpTrigger.h"
32 #include "AliMpConstants.h"
36 #include "Riostream.h"
38 #include "TObjString.h"
44 //-----------------------------------------------------------------------------
45 /// \class AliMpTriggerReader
46 /// Read trigger slat ASCII files
47 /// Basically provides two methods:
48 /// - AliMpTrigger* ReadSlat()
49 /// - AliMpPCB* ReadPCB()
51 /// \author Laurent Aphecetche
52 //-----------------------------------------------------------------------------
55 ClassImp(AliMpTriggerReader)
58 const TString AliMpTriggerReader::fgkKeywordLayer("LAYER");
59 const TString AliMpTriggerReader::fgkKeywordScale("SCALE");
60 const TString AliMpTriggerReader::fgkKeywordPcb("PCB");
61 const TString AliMpTriggerReader::fgkKeywordFlipX("FLIP_X");
62 const TString AliMpTriggerReader::fgkKeywordFlipY("FLIP_Y");
64 //_____________________________________________________________________________
65 AliMpTriggerReader::AliMpTriggerReader()
67 fMotifMap(AliMpSlatMotifMap::Instance()),
73 fLocalBoardMap.SetOwner(kTRUE);
76 //_____________________________________________________________________________
77 AliMpTriggerReader::~AliMpTriggerReader()
82 fLocalBoardMap.DeleteAll();
85 //_____________________________________________________________________________
87 AliMpTriggerReader::BuildSlat(const char* slatName,
88 AliMp::PlaneType planeType,
92 /// Construct a slat from the list of lines, taking into account
93 /// the scale factor. The returned pointer must be deleted by the client
95 AliDebug(1,Form("slat %s %s scale %e",
96 slatName,PlaneTypeName(planeType).Data(),scale))
99 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
103 // StdoutToAliDebug(3,lines.Print(););
106 while ( ( osline = (TObjString*)it.Next() ) )
108 // note that at this stage lines should not be empty.
109 TString sline(osline->String());
111 TObjArray* tokens = sline.Tokenize(' ');
113 TString& keyword = ((TObjString*)tokens->At(0))->String();
115 if ( keyword == fgkKeywordPcb )
117 if ( tokens->GetEntriesFast() != 3 )
119 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
120 " in following line:\n%s",sline.Data()));
125 TString pcbName = ((TObjString*)tokens->At(1))->String();
127 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
131 std::ostringstream s;
132 s << pcbName.Data() << "x" << scale;
133 pcbName = s.str().c_str();
136 AliMpPCB* pcbType = ReadPCB(pcbName.Data());
139 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
144 TArrayI allLocalBoards;
146 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
148 TArrayI localBoardNumbers;
149 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
150 Ssiz_t pos = localBoards.First('-');
153 pos = localBoards.Length();
155 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
156 ';',localBoardNumbers);
157 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
159 std::ostringstream name;
160 name << localBoards(0,pos-1) << localBoardNumbers[i];
161 AliDebugClass(3,name.str().c_str());
162 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
163 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
164 allLocalBoards.Set(allLocalBoards.GetSize()+1);
165 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
166 if (localBoardNumbers[i] < 0 )
168 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
169 " to be correct... : %s\n",slatName,name.str().c_str()));
173 AliDebug(3,"Deleting tokens");
175 AliDebug(3,"Deleting localBoardList");
176 delete localBoardList;
177 AliDebug(3,"Adding pcb to slat");
178 slat->Add(*pcbType,allLocalBoards);
179 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
184 if ( slat->DX()== 0 || slat->DY() == 0 )
186 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
191 //_____________________________________________________________________________
193 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
195 /// Decode the string to get the board name
198 TObjArray* tokens = s.Tokenize(' ');
200 TString& keyword = ((TObjString*)tokens->At(0))->String();
202 if ( keyword == fgkKeywordPcb &&
203 tokens->GetEntriesFast() == 3 )
205 boardName = ((TObjString*)tokens->At(2))->String();
213 //_____________________________________________________________________________
215 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
216 Int_t srcLine, Int_t destLine)
219 /// Change the local board names contained in lines,
220 /// to go from right to left, and/or
221 /// from top to bottom
227 // Simply swaps R(ight) and L(eft) in the first character of
232 while ( ( oline = (TObjString*)it.Next() ) )
234 TString& s = oline->String();
235 if ( s.Contains("RC") )
237 // Change right to left
238 s.ReplaceAll("RC","LC");
240 else if ( s.Contains("LC") )
242 // Change left to right
243 s.ReplaceAll("LC","RC");
250 // Change line number, according to parameters srcLine and destLine
251 // Note that because of road opening (for planes 3 and 4 at least),
252 // we loop for srcLine +-1
254 for ( Int_t line = -1; line <=1; ++line )
256 std::ostringstream src,dest;
257 src << "L" << srcLine+line;
258 dest << "L" << destLine-line;
259 if ( src.str() == dest.str() ) continue;
261 for ( Int_t i = 0; i < lines.GetSize(); ++i )
263 TObjString* oline = (TObjString*)lines.At(i);
265 TString& s = oline->String();
267 if ( !s.Contains(fgkKeywordPcb) )
269 // Only consider PCB lines.
273 if ( s.Contains(src.str().c_str()) )
275 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
276 src.str().c_str(),dest.str().c_str(),s.Data()));
278 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
280 AliDebugClass(4,s.Data());
282 TString boardName(GetBoardNameFromPCBLine(s));
286 // We must also change board numbers, with the tricky
287 // thing that up and down must be swapped...
288 // Up can only be 1 card so it must be B1
289 // Down must be the uppper card of the line before, so
290 // the biggest possible board number for this Line,Column
295 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
296 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
297 AliDebugClass(4,s.Data());
301 // find the largest valid board number
302 for ( int b = 4; b>=1; --b )
304 std::ostringstream bs;
305 bs << boardName(0,boardName.Length()-1) << b;
306 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
308 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
309 boardName(boardName.Length()-2,2).Data(),
312 s.ReplaceAll(boardName(boardName.Length()-2,2),
319 // Check that the replacement we did is ok. If not,
321 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
324 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
335 //___________________________________________________________________________
337 AliMpTriggerReader::IsLayerLine(const TString& sline)
339 /// Whether sline contains LAYER keyword
341 if ( sline.BeginsWith(fgkKeywordLayer) )
351 //___________________________________________________________________________
353 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
355 Bool_t& flipX, Bool_t& flipY)
357 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
359 Ssiz_t blankPos = sline.First(' ');
360 if ( blankPos < 0 ) return 0;
362 TString keyword(sline(0,blankPos));
364 if ( keyword == fgkKeywordFlipX )
367 } else if ( keyword == fgkKeywordFlipY )
376 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
380 //___________________________________________________________________________
382 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
383 Double_t& scale, TString& slatType)
385 /// Decode sline containing SCALE keyword
387 if ( sline(0,fgkKeywordScale.Length()) == fgkKeywordScale )
389 TString tmp(sline(fgkKeywordScale.Length()+1,
390 sline.Length()-fgkKeywordScale.Length()-1));
391 Ssiz_t blankPos = tmp.First(' ');
394 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
395 " SCALE keyword : %s\n",tmp.Data()));
400 slatType = tmp(0,blankPos);
401 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
409 //_____________________________________________________________________________
411 AliMpTriggerReader::GetLine(const TString& slatType)
414 /// Assuming slatType is a 4 character string of the form XSLN
415 /// where X=1,2,3 or 4
417 /// N is the line number
420 if ( isdigit(slatType[0]) &&
421 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
424 return atoi(slatType(3,1).Data());
429 //_____________________________________________________________________________
431 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
433 /// From local board name to local board number
435 if ( !fLocalBoardMap.GetSize() )
437 ReadLocalBoardMapping();
440 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
444 return atoi(((TObjString*)pair->Value())->String().Data());
449 //_____________________________________________________________________________
451 AliMpTriggerReader::ReadLines(const char* slatType,
452 AliMp::PlaneType planeType,
455 Bool_t& flipX, Bool_t& flipY,
456 Int_t& srcLine, Int_t& destLine)
459 /// Reads in lines from file for a given slat
460 /// Returns the list of lines (lines), together with some global
461 /// information as the scale, whether to flip the lines, etc...
463 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
464 " destLine %d\n",slatType,scale,flipX,flipY,
467 TString filename(AliMpFiles::SlatFilePath(AliMp::kStationTrigger,slatType,
469 std::ifstream in(filename.Data());
472 AliErrorClass(Form("Cannot read slat from %s",filename.Data()));
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 TString filename(AliMpFiles::LocalTriggerBoardMapping());
546 AliDebugClass(2,Form("Reading from %s\n",filename.Data()));
548 fLocalBoardMap.DeleteAll();
552 ifstream in(filename.Data());
555 AliErrorClass(Form("Cannot read file %s\n",filename.Data()));
560 Char_t localBoardName[20];
561 Int_t j,localBoardId;
566 for (Int_t i = 0; i < 4; ++i)
567 if (!in.getline(line,80)) continue; //skip 4 first lines
570 if (!in.getline(line,80)) break;
571 sscanf(line,"%hx",&mask);
573 for ( Int_t i = 0; i < 16; ++i )
575 if ( (mask >> i ) & 0x1 )
577 if (!in.getline(line,80)) break;
578 sscanf(line,"%02d %s %03d %03x", &j, localBoardName, &localBoardId, &switches);
579 if (localBoardId <= AliMpConstants::NofLocalBoards())
581 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
582 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
584 // skip 2 following lines
585 if (!in.getline(line,80)) break;
586 if (!in.getline(line,80)) break;
595 //_____________________________________________________________________________
597 AliMpTriggerReader::ReadPCB(const char* pcbType)
600 /// Create a new AliMpPCB object, by reading it from file.
601 /// Returned pointer must be deleted by client.
603 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
605 TString pcbName(pcbType);
607 Ssiz_t pos = pcbName.First('x');
609 Double_t scale = 1.0;
613 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
614 pcbName = pcbName(0,pos);
617 std::ifstream in(AliMpFiles::SlatPCBFilePath(AliMp::kStationTrigger,pcbName).Data());
620 AliErrorClass(Form("Cannot open file for PCB %s",pcbName.Data()));
624 AliMpMotifReader reader(AliMp::kStationTrigger,AliMp::kNonBendingPlane);
625 // note that the nonbending
626 // parameter is of no use for trigger, as far as reading motif is
627 // concerned, as all motifs are supposed to be in the same directory
628 // (as they are shared by bending/non-bending planes).
632 const TString kSizeKeyword("SIZES");
633 const TString kMotifKeyword("MOTIF");
634 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
638 while ( in.getline(line,80) )
640 if ( line[0] == '#' ) continue;
644 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
646 std::istringstream sin(sline(kSizeKeyword.Length(),
647 sline.Length()-kSizeKeyword.Length()-1).Data());
648 float padSizeX = 0.0;
649 float padSizeY = 0.0;
650 float pcbSizeX = 0.0;
651 float pcbSizeY = 0.0;
652 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
655 AliError("pcb not null as expected");
657 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
658 pcbSizeX*scale,pcbSizeY*scale);
661 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
663 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
664 sline.Length()-kMotifSpecialKeyword.Length()).Data());
665 TString sMotifSpecial;
667 sin >> sMotifSpecial >> sMotifType;
669 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
671 AliMpMotifSpecial* specialMotif =
672 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
675 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
676 sMotifSpecial.Data(),id.Data()));
677 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
680 AliDebug(1,Form("Reading motifType %s (%s) from file",
681 sMotifType.Data(),id.Data()));
682 motifType = reader.BuildMotifType(sMotifType.Data());
683 fMotifMap->AddMotifType(motifType);
687 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
688 sMotifType.Data(),id.Data()));
690 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
691 fMotifMap->AddMotif(specialMotif);
695 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
699 AliError("pcb not null as expected");
701 pcb = new AliMpPCB(pcbType,specialMotif);
704 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
706 std::istringstream sin(sline(kMotifKeyword.Length(),
707 sline.Length()-kMotifKeyword.Length()).Data());
711 sin >> sMotifType >> ix >> iy;
713 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
716 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
717 motifType = reader.BuildMotifType(sMotifType.Data());
718 fMotifMap->AddMotifType(motifType);
722 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
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 ( Int_t ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
799 TList& lines = *((TList*)layers.At(ilayer));
800 std::ostringstream slatName;
801 slatName << slatType << "-LAYER" << ilayer;
802 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines,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* slat = triggerSlat->GetLayer(j);
825 AliError(Form("Layer %d",j));
826 for ( Int_t i = 0; i < slat->GetSize(); ++i )
828 AliMpPCB* pcb = slat->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()));