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 "AliMpDataStreams.h"
24 #include "AliMpMotifType.h"
26 #include "AliMpSlat.h"
27 #include "AliMpSlatMotifMap.h"
28 #include "AliMpMotifSpecial.h"
29 #include "AliMpMotifPosition.h"
30 #include "AliMpMotif.h"
31 #include "AliMpHelper.h"
32 #include "AliMpSt345Reader.h"
33 #include "AliMpTrigger.h"
34 #include "AliMpConstants.h"
38 #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()
69 fMotifMap(AliMpSlatMotifMap::Instance()),
75 fLocalBoardMap.SetOwner(kTRUE);
78 //_____________________________________________________________________________
79 AliMpTriggerReader::~AliMpTriggerReader()
84 fLocalBoardMap.DeleteAll();
87 //_____________________________________________________________________________
89 AliMpTriggerReader::BuildSlat(const char* slatName,
90 AliMp::PlaneType planeType,
94 /// Construct a slat from the list of lines, taking into account
95 /// the scale factor. The returned pointer must be deleted by the client
97 AliDebug(1,Form("slat %s %s scale %e",
98 slatName,PlaneTypeName(planeType).Data(),scale))
101 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
105 // StdoutToAliDebug(3,lines.Print(););
108 while ( ( osline = (TObjString*)it.Next() ) )
110 // note that at this stage lines should not be empty.
111 TString sline(osline->String());
113 TObjArray* tokens = sline.Tokenize(' ');
115 TString& keyword = ((TObjString*)tokens->At(0))->String();
117 if ( keyword == fgkKeywordPcb )
119 if ( tokens->GetEntriesFast() != 3 )
121 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
122 " in following line:\n%s",sline.Data()));
127 TString pcbName = ((TObjString*)tokens->At(1))->String();
129 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
133 std::ostringstream s;
134 s << pcbName.Data() << "x" << scale;
135 pcbName = s.str().c_str();
138 AliMpPCB* pcbType = ReadPCB(pcbName.Data());
141 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
146 TArrayI allLocalBoards;
148 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
150 TArrayI localBoardNumbers;
151 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
152 Ssiz_t pos = localBoards.First('-');
155 pos = localBoards.Length();
157 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
158 ';',localBoardNumbers);
159 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
161 std::ostringstream name;
162 name << localBoards(0,pos-1) << localBoardNumbers[i];
163 AliDebugClass(3,name.str().c_str());
164 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
165 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
166 allLocalBoards.Set(allLocalBoards.GetSize()+1);
167 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
168 if (localBoardNumbers[i] < 0 )
170 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
171 " to be correct... : %s\n",slatName,name.str().c_str()));
175 AliDebug(3,"Deleting tokens");
177 AliDebug(3,"Deleting localBoardList");
178 delete localBoardList;
179 AliDebug(3,"Adding pcb to slat");
180 slat->Add(*pcbType,allLocalBoards);
181 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
186 if ( slat->DX()== 0 || slat->DY() == 0 )
188 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
193 //_____________________________________________________________________________
195 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
197 /// Decode the string to get the board name
200 TObjArray* tokens = s.Tokenize(' ');
202 TString& keyword = ((TObjString*)tokens->At(0))->String();
204 if ( keyword == fgkKeywordPcb &&
205 tokens->GetEntriesFast() == 3 )
207 boardName = ((TObjString*)tokens->At(2))->String();
215 //_____________________________________________________________________________
217 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
218 Int_t srcLine, Int_t destLine)
221 /// Change the local board names contained in lines,
222 /// to go from right to left, and/or
223 /// from top to bottom
229 // Simply swaps R(ight) and L(eft) in the first character of
234 while ( ( oline = (TObjString*)it.Next() ) )
236 TString& s = oline->String();
237 if ( s.Contains("RC") )
239 // Change right to left
240 s.ReplaceAll("RC","LC");
242 else if ( s.Contains("LC") )
244 // Change left to right
245 s.ReplaceAll("LC","RC");
252 // Change line number, according to parameters srcLine and destLine
253 // Note that because of road opening (for planes 3 and 4 at least),
254 // we loop for srcLine +-1
256 for ( Int_t line = -1; line <=1; ++line )
258 std::ostringstream src,dest;
259 src << "L" << srcLine+line;
260 dest << "L" << destLine-line;
261 if ( src.str() == dest.str() ) continue;
263 for ( Int_t i = 0; i < lines.GetSize(); ++i )
265 TObjString* oline = (TObjString*)lines.At(i);
267 TString& s = oline->String();
269 if ( !s.Contains(fgkKeywordPcb) )
271 // Only consider PCB lines.
275 if ( s.Contains(src.str().c_str()) )
277 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
278 src.str().c_str(),dest.str().c_str(),s.Data()));
280 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
282 AliDebugClass(4,s.Data());
284 TString boardName(GetBoardNameFromPCBLine(s));
288 // We must also change board numbers, with the tricky
289 // thing that up and down must be swapped...
290 // Up can only be 1 card so it must be B1
291 // Down must be the uppper card of the line before, so
292 // the biggest possible board number for this Line,Column
297 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
298 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
299 AliDebugClass(4,s.Data());
303 // find the largest valid board number
304 for ( int b = 4; b>=1; --b )
306 std::ostringstream bs;
307 bs << boardName(0,boardName.Length()-1) << b;
308 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
310 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
311 boardName(boardName.Length()-2,2).Data(),
314 s.ReplaceAll(boardName(boardName.Length()-2,2),
321 // Check that the replacement we did is ok. If not,
323 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
326 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
337 //___________________________________________________________________________
339 AliMpTriggerReader::IsLayerLine(const TString& sline)
341 /// Whether sline contains LAYER keyword
343 if ( sline.BeginsWith(fgkKeywordLayer) )
353 //___________________________________________________________________________
355 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
357 Bool_t& flipX, Bool_t& flipY)
359 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
361 Ssiz_t blankPos = sline.First(' ');
362 if ( blankPos < 0 ) return 0;
364 TString keyword(sline(0,blankPos));
366 if ( keyword == fgkKeywordFlipX )
369 } else if ( keyword == fgkKeywordFlipY )
378 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
382 //___________________________________________________________________________
384 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
385 Double_t& scale, TString& slatType)
387 /// Decode sline containing SCALE keyword
389 if ( sline(0,fgkKeywordScale.Length()) == fgkKeywordScale )
391 TString tmp(sline(fgkKeywordScale.Length()+1,
392 sline.Length()-fgkKeywordScale.Length()-1));
393 Ssiz_t blankPos = tmp.First(' ');
396 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
397 " SCALE keyword : %s\n",tmp.Data()));
402 slatType = tmp(0,blankPos);
403 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
411 //_____________________________________________________________________________
413 AliMpTriggerReader::GetLine(const TString& slatType)
416 /// Assuming slatType is a 4 character string of the form XSLN
417 /// where X=1,2,3 or 4
419 /// N is the line number
422 if ( isdigit(slatType[0]) &&
423 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
426 return atoi(slatType(3,1).Data());
431 //_____________________________________________________________________________
433 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
435 /// From local board name to local board number
437 if ( !fLocalBoardMap.GetSize() )
439 ReadLocalBoardMapping();
442 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
446 return atoi(((TObjString*)pair->Value())->String().Data());
451 //_____________________________________________________________________________
453 AliMpTriggerReader::ReadLines(const char* slatType,
454 AliMp::PlaneType planeType,
457 Bool_t& flipX, Bool_t& flipY,
458 Int_t& srcLine, Int_t& destLine)
461 /// Reads in lines from file for a given slat
462 /// Returns the list of lines (lines), together with some global
463 /// information as the scale, whether to flip the lines, etc...
465 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
466 " destLine %d\n",slatType,scale,flipX,flipY,
470 = AliMpDataStreams::Instance()
471 ->CreateDataStream(AliMpFiles::SlatFilePath(
472 AliMp::kStationTrigger,slatType, planeType));
476 while ( in.getline(line,80) )
478 TString sline(AliMpHelper::Normalize(line));
480 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
482 Bool_t isKeywordThere =
483 sline.Contains(fgkKeywordPcb) ||
484 sline.Contains(fgkKeywordLayer) ||
485 sline.Contains(fgkKeywordScale) ||
486 sline.Contains(fgkKeywordFlipX) ||
487 sline.Contains(fgkKeywordFlipY);
489 if ( !isKeywordThere )
491 AliErrorClass(Form("Got a line with no keyword : %s."
492 "That's not valid\n",line));
499 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
503 if ( isScaleLine < 0 )
505 AliFatalClass(Form("Syntax error near %s keyword\n",fgkKeywordScale.Data()));
507 else if ( isScaleLine > 0 && slatType2 != slatType )
509 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
515 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
520 srcLine = GetLine(slatType2);
521 destLine = GetLine(slatType);
525 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
529 lines.Add(new TObjString(sline.Data()));
537 //_____________________________________________________________________________
539 AliMpTriggerReader::ReadLocalBoardMapping()
541 /// Reads the file that contains the mapping local board name <-> number
543 fLocalBoardMap.DeleteAll();
548 = AliMpDataStreams::Instance()
549 ->CreateDataStream(AliMpFiles::LocalTriggerBoardMapping());
552 Char_t localBoardName[20];
553 Int_t j,localBoardId;
559 for (Int_t i = 0; i < 4; ++i)
560 if (!in.getline(line,80)) continue; //skip 4 first lines
563 if (!in.getline(line,80)) break;
564 sscanf(line,"%hx",&mask);
567 if (!in.getline(line,80)) break;
568 sscanf(line,"%d",&nofBoards);
570 for ( Int_t i = 0; i < nofBoards; ++i )
573 if (!in.getline(line,80)) break;
574 sscanf(line,"%02d %s %03d %03x", &j, localBoardName, &localBoardId, &switches);
575 if (localBoardId <= AliMpConstants::NofLocalBoards())
577 fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
578 AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
580 // skip 2 following lines
581 if (!in.getline(line,80)) break;
582 if (!in.getline(line,80)) break;
590 //_____________________________________________________________________________
592 AliMpTriggerReader::ReadPCB(const char* pcbType)
595 /// Create a new AliMpPCB object, by reading it from file.
596 /// Returned pointer must be deleted by client.
598 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
600 TString pcbName(pcbType);
602 Ssiz_t pos = pcbName.First('x');
604 Double_t scale = 1.0;
608 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
609 pcbName = pcbName(0,pos);
613 = AliMpDataStreams::Instance()
614 ->CreateDataStream(AliMpFiles::SlatPCBFilePath(
615 AliMp::kStationTrigger,pcbName));
617 AliMpMotifReader reader(AliMp::kStationTrigger,AliMp::kNonBendingPlane);
618 // note that the nonbending
619 // parameter is of no use for trigger, as far as reading motif is
620 // concerned, as all motifs are supposed to be in the same directory
621 // (as they are shared by bending/non-bending planes).
625 const TString kSizeKeyword("SIZES");
626 const TString kMotifKeyword("MOTIF");
627 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
631 while ( in.getline(line,80) )
633 if ( line[0] == '#' ) continue;
637 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
639 std::istringstream sin(sline(kSizeKeyword.Length(),
640 sline.Length()-kSizeKeyword.Length()-1).Data());
641 float padSizeX = 0.0;
642 float padSizeY = 0.0;
643 float pcbSizeX = 0.0;
644 float pcbSizeY = 0.0;
645 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
648 AliError("pcb not null as expected");
650 pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
651 pcbSizeX*scale,pcbSizeY*scale);
654 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
656 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
657 sline.Length()-kMotifSpecialKeyword.Length()).Data());
658 TString sMotifSpecial;
660 sin >> sMotifSpecial >> sMotifType;
662 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
664 AliMpMotifSpecial* specialMotif =
665 dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
668 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
669 sMotifSpecial.Data(),id.Data()));
670 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
673 AliDebug(1,Form("Reading motifType %s (%s) from file",
674 sMotifType.Data(),id.Data()));
675 motifType = reader.BuildMotifType(sMotifType.Data());
676 fMotifMap->AddMotifType(motifType);
680 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
681 sMotifType.Data(),id.Data()));
683 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
684 fMotifMap->AddMotif(specialMotif);
688 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
692 AliError("pcb not null as expected");
694 pcb = new AliMpPCB(pcbType,specialMotif);
697 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
699 std::istringstream sin(sline(kMotifKeyword.Length(),
700 sline.Length()-kMotifKeyword.Length()).Data());
704 sin >> sMotifType >> ix >> iy;
706 AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
709 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
710 motifType = reader.BuildMotifType(sMotifType.Data());
711 fMotifMap->AddMotifType(motifType);
715 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
718 pcb->Add(motifType,ix,iy);
727 //_____________________________________________________________________________
729 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
732 /// Create a new AliMpTrigger object, by reading it from file.
733 /// Returned object must be deleted by client.
735 Double_t scale = 1.0;
736 Bool_t flipX = kFALSE;
737 Bool_t flipY = kFALSE;
739 lines.SetOwner(kTRUE);
743 // Read the file and its include (if any) and store the result
744 // in a TObjArray of TObjStrings.
745 ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
747 // Here some more sanity checks could be done.
748 // For the moment we only insure that the first line contains
750 TString& firstLine = ((TObjString*)lines.First())->String();
751 if ( !IsLayerLine(firstLine) )
753 std::ostringstream s;
754 s << fgkKeywordLayer;
755 lines.AddFirst(new TObjString(s.str().c_str()));
758 AliDebugClass(2,Form("Scale=%g\n",scale));
760 FlipLines(lines,flipX,flipY,srcLine,destLine);
762 // Now splits the lines in packets corresponding to different layers
763 // (if any), and create sub-slats.
765 layers.SetOwner(kTRUE);
770 while ( ( osline = (TObjString*)it.Next() ) )
772 TString& s = osline->String();
773 if ( IsLayerLine(s) )
775 TList* list = new TList;
776 list->SetOwner(kTRUE);
782 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
786 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
788 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
790 for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
792 TList& lines1 = *((TList*)layers.At(ilayer));
793 std::ostringstream slatName;
794 slatName << slatType << "-LAYER" << ilayer;
795 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines1,scale);
798 Bool_t ok = triggerSlat->AdoptLayer(slat);
801 StdoutToAliError(cout << "could not add slat=" << endl;
803 cout << "to the triggerSlat=" << endl;
804 triggerSlat->Print();
806 AliError("Slat is=");
807 for ( Int_t i = 0; i < slat->GetSize(); ++i )
809 AliMpPCB* pcb = slat->GetPCB(i);
810 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
811 i,pcb->DX()*2,pcb->DY()*2,
812 pcb->DX()*2/scale,pcb->DY()*2/scale));
814 AliError("TriggerSlat is=");
815 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
817 AliMpSlat* slat1 = triggerSlat->GetLayer(j);
818 AliError(Form("Layer %d",j));
819 for ( Int_t i = 0; i < slat1->GetSize(); ++i )
821 AliMpPCB* pcb = slat1->GetPCB(i);
822 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
823 i,pcb->DX()*2,pcb->DY()*2,
824 pcb->DX()*2/scale,pcb->DY()*2/scale));
827 StdoutToAliError(fMotifMap->Print(););
832 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));