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"
35 #include "Riostream.h"
37 #include "TObjString.h"
44 /// \class AliMpTriggerReader
45 /// Read trigger slat ASCII files
46 /// Basically provides two methods:
47 /// - AliMpTrigger* ReadSlat()
48 /// - AliMpPCB* ReadPCB()
50 /// \author Laurent Aphecetche
53 ClassImp(AliMpTriggerReader)
56 const TString AliMpTriggerReader::fgkKeywordLayer("LAYER");
57 const TString AliMpTriggerReader::fgkKeywordScale("SCALE");
58 const TString AliMpTriggerReader::fgkKeywordPcb("PCB");
59 const TString AliMpTriggerReader::fgkKeywordFlipX("FLIP_X");
60 const TString AliMpTriggerReader::fgkKeywordFlipY("FLIP_Y");
62 //_____________________________________________________________________________
63 AliMpTriggerReader::AliMpTriggerReader(AliMpSlatMotifMap& motifMap)
71 fLocalBoardMap.SetOwner(kTRUE);
74 //_____________________________________________________________________________
75 AliMpTriggerReader::~AliMpTriggerReader()
80 fLocalBoardMap.DeleteAll();
83 //_____________________________________________________________________________
85 AliMpTriggerReader::BuildSlat(const char* slatName,
86 AliMp::PlaneType planeType,
90 /// Construct a slat from the list of lines, taking into account
91 /// the scale factor. The returned pointer must be deleted by the client
93 AliDebug(1,Form("slat %s %s scale %e",
94 slatName,PlaneTypeName(planeType).Data(),scale))
97 AliMpSlat* slat = new AliMpSlat(slatName, planeType);
101 // StdoutToAliDebug(3,lines.Print(););
104 while ( ( osline = (TObjString*)it.Next() ) )
106 // note that at this stage lines should not be empty.
107 TString sline(osline->String());
109 TObjArray* tokens = sline.Tokenize(' ');
111 TString& keyword = ((TObjString*)tokens->At(0))->String();
113 if ( keyword == fgkKeywordPcb )
115 if ( tokens->GetEntriesFast() != 3 )
117 AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
118 " in following line:\n%s",sline.Data()));
123 TString pcbName = ((TObjString*)tokens->At(1))->String();
125 TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
129 std::ostringstream s;
130 s << pcbName.Data() << "x" << scale;
131 pcbName = s.str().c_str();
134 AliMpPCB* pcbType = ReadPCB(pcbName.Data());
137 AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
142 TArrayI allLocalBoards;
144 for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
146 TArrayI localBoardNumbers;
147 TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
148 Ssiz_t pos = localBoards.First('-');
151 pos = localBoards.Length();
153 AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
154 ';',localBoardNumbers);
155 for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
157 std::ostringstream name;
158 name << localBoards(0,pos-1) << localBoardNumbers[i];
159 AliDebugClass(3,name.str().c_str());
160 localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
161 AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
162 allLocalBoards.Set(allLocalBoards.GetSize()+1);
163 allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
164 if (localBoardNumbers[i] < 0 )
166 AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
167 " to be correct... : %s\n",slatName,name.str().c_str()));
171 AliDebug(3,"Deleting tokens");
173 AliDebug(3,"Deleting localBoardList");
174 delete localBoardList;
175 AliDebug(3,"Adding pcb to slat");
176 slat->Add(*pcbType,allLocalBoards);
177 AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
182 if ( slat->DX()== 0 || slat->DY() == 0 )
184 AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
189 //_____________________________________________________________________________
191 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
193 // Decode the string to get the board name
196 TObjArray* tokens = s.Tokenize(' ');
198 TString& keyword = ((TObjString*)tokens->At(0))->String();
200 if ( keyword == fgkKeywordPcb &&
201 tokens->GetEntriesFast() == 3 )
203 boardName = ((TObjString*)tokens->At(2))->String();
211 //_____________________________________________________________________________
213 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
214 Int_t srcLine, Int_t destLine)
217 /// Change the local board names contained in lines,
218 /// to go from right to left, and/or
219 /// from top to bottom
225 // Simply swaps R(ight) and L(eft) in the first character of
230 while ( ( oline = (TObjString*)it.Next() ) )
232 TString& s = oline->String();
233 if ( s.Contains("RC") )
235 // Change right to left
236 s.ReplaceAll("RC","LC");
238 else if ( s.Contains("LC") )
240 // Change left to right
241 s.ReplaceAll("LC","RC");
248 // Change line number, according to parameters srcLine and destLine
249 // Note that because of road opening (for planes 3 and 4 at least),
250 // we loop for srcLine +-1
252 for ( Int_t line = -1; line <=1; ++line )
254 std::ostringstream src,dest;
255 src << "L" << srcLine+line;
256 dest << "L" << destLine-line;
257 if ( src.str() == dest.str() ) continue;
259 for ( Int_t i = 0; i < lines.GetSize(); ++i )
261 TObjString* oline = (TObjString*)lines.At(i);
263 TString& s = oline->String();
265 if ( !s.Contains(fgkKeywordPcb) )
267 // Only consider PCB lines.
271 if ( s.Contains(src.str().c_str()) )
273 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
274 src.str().c_str(),dest.str().c_str(),s.Data()));
276 s.ReplaceAll(src.str().c_str(),dest.str().c_str());
278 AliDebugClass(4,s.Data());
280 TString boardName(GetBoardNameFromPCBLine(s));
284 // We must also change board numbers, with the tricky
285 // thing that up and down must be swapped...
286 // Up can only be 1 card so it must be B1
287 // Down must be the uppper card of the line before, so
288 // the biggest possible board number for this Line,Column
293 AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
294 s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
295 AliDebugClass(4,s.Data());
299 // find the largest valid board number
300 for ( int b = 4; b>=1; --b )
302 std::ostringstream bs;
303 bs << boardName(0,boardName.Length()-1) << b;
304 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
306 AliDebugClass(4,Form("Replacing %s by %s in %s\n",
307 boardName(boardName.Length()-2,2).Data(),
310 s.ReplaceAll(boardName(boardName.Length()-2,2),
317 // Check that the replacement we did is ok. If not,
319 Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
322 AliDebugClass(4,Form("Removing line %s\n",s.Data()));
333 //___________________________________________________________________________
335 AliMpTriggerReader::IsLayerLine(const TString& sline)
337 /// Whether sline contains LAYER keyword
339 if ( sline.BeginsWith(fgkKeywordLayer) )
349 //___________________________________________________________________________
351 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
353 Bool_t& flipX, Bool_t& flipY)
355 /// Decode a line containing FLIP_X and/or FLIP_Y keywords
357 Ssiz_t blankPos = sline.First(' ');
358 if ( blankPos < 0 ) return 0;
360 TString keyword(sline(0,blankPos));
362 if ( keyword == fgkKeywordFlipX )
365 } else if ( keyword == fgkKeywordFlipY )
374 slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
378 //___________________________________________________________________________
380 AliMpTriggerReader::DecodeScaleLine(const TString& sline,
381 Double_t& scale, TString& slatType)
383 /// Decode sline containing SCALE keyword
385 if ( sline(0,fgkKeywordScale.Length()) == fgkKeywordScale )
387 TString tmp(sline(fgkKeywordScale.Length()+1,
388 sline.Length()-fgkKeywordScale.Length()-1));
389 Ssiz_t blankPos = tmp.First(' ');
392 AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
393 " SCALE keyword : %s\n",tmp.Data()));
398 slatType = tmp(0,blankPos);
399 scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
407 //_____________________________________________________________________________
409 AliMpTriggerReader::GetLine(const TString& slatType)
412 /// Assuming slatType is a 4 character string of the form XSLN
413 /// where X=1,2,3 or 4
415 /// N is the line number
418 if ( isdigit(slatType[0]) &&
419 ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
422 return atoi(slatType(3,1).Data());
427 //_____________________________________________________________________________
429 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
431 /// From local board name to local board number
433 if ( !fLocalBoardMap.GetSize() )
435 ReadLocalBoardMapping();
438 TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
442 return atoi(((TObjString*)pair->Value())->String().Data());
447 //_____________________________________________________________________________
449 AliMpTriggerReader::ReadLines(const char* slatType,
450 AliMp::PlaneType planeType,
453 Bool_t& flipX, Bool_t& flipY,
454 Int_t& srcLine, Int_t& destLine)
457 /// Reads in lines from file for a given slat
458 /// Returns the list of lines (lines), together with some global
459 /// information as the scale, whether to flip the lines, etc...
461 AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
462 " destLine %d\n",slatType,scale,flipX,flipY,
465 TString filename(AliMpFiles::SlatFilePath(AliMp::kStationTrigger,slatType,
467 std::ifstream in(filename.Data());
470 AliErrorClass(Form("Cannot read slat from %s",filename.Data()));
475 while ( in.getline(line,80) )
477 TString sline(AliMpHelper::Normalize(line));
479 if ( sline.Length() == 0 || sline[0] == '#' ) continue;
481 Bool_t isKeywordThere =
482 sline.Contains(fgkKeywordPcb) ||
483 sline.Contains(fgkKeywordLayer) ||
484 sline.Contains(fgkKeywordScale) ||
485 sline.Contains(fgkKeywordFlipX) ||
486 sline.Contains(fgkKeywordFlipY);
488 if ( !isKeywordThere )
490 AliErrorClass(Form("Got a line with no keyword : %s."
491 "That's not valid\n",line));
498 Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
502 if ( isScaleLine < 0 )
504 AliFatalClass(Form("Syntax error near %s keyword\n",fgkKeywordScale.Data()));
506 else if ( isScaleLine > 0 && slatType2 != slatType )
508 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
514 Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
519 srcLine = GetLine(slatType2);
520 destLine = GetLine(slatType);
524 ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
528 lines.Add(new TObjString(sline.Data()));
536 //_____________________________________________________________________________
538 AliMpTriggerReader::ReadLocalBoardMapping()
540 /// Reads the file that contains the mapping local board name <-> number
542 TString filename(AliMpFiles::LocalTriggerBoardMapping());
544 AliDebugClass(2,Form("Reading from %s\n",filename.Data()));
546 fLocalBoardMap.DeleteAll();
548 ifstream in(filename.Data());
551 AliErrorClass(Form("Cannot read file %s\n",filename.Data()));
557 while ( in.getline(line,80) )
559 if ( line[0] == '#' ) continue;
562 if ( sline.Contains("Board") )
564 TObjArray* tokens = sline.Tokenize(' ');
565 TString& number = ((TObjString*)(tokens->At(1)))->String();
566 Int_t n = atoi(number.Data());
567 if ( n == 0 ) continue;
568 TString& name = ((TObjString*)(tokens->At(4)))->String();
569 fLocalBoardMap.Add(new TObjString(name), new TObjString(number));
570 AliDebugClass(10,Form("Board %s has number %s\n",name.Data(),number.Data()));
578 //_____________________________________________________________________________
580 AliMpTriggerReader::ReadPCB(const char* pcbType)
583 /// Create a new AliMpPCB object, by reading it from file.
584 /// Returned pointer must be deleted by client.
586 AliDebugClass(2,Form("pcbType=%s\n",pcbType));
588 TString pcbName(pcbType);
590 Ssiz_t pos = pcbName.First('x');
592 Double_t scale = 1.0;
596 scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
597 pcbName = pcbName(0,pos);
600 std::ifstream in(AliMpFiles::SlatPCBFilePath(AliMp::kStationTrigger,pcbName).Data());
603 AliErrorClass(Form("Cannot open file for PCB %s",pcbName.Data()));
607 AliMpMotifReader reader(AliMp::kStationTrigger,AliMp::kNonBendingPlane);
608 // note that the nonbending
609 // parameter is of no use for trigger, as far as reading motif is
610 // concerned, as all motifs are supposed to be in the same directory
611 // (as they are shared by bending/non-bending planes).
615 const TString kSizeKeyword("SIZES");
616 const TString kMotifKeyword("MOTIF");
617 const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
621 while ( in.getline(line,80) )
623 if ( line[0] == '#' ) continue;
627 if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
629 std::istringstream sin(sline(kSizeKeyword.Length(),
630 sline.Length()-kSizeKeyword.Length()-1).Data());
631 float padSizeX = 0.0;
632 float padSizeY = 0.0;
633 float pcbSizeX = 0.0;
634 float pcbSizeY = 0.0;
635 sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
638 AliError("pcb not null as expected");
640 pcb = new AliMpPCB(&fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
641 pcbSizeX*scale,pcbSizeY*scale);
644 if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
646 std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
647 sline.Length()-kMotifSpecialKeyword.Length()).Data());
648 TString sMotifSpecial;
650 sin >> sMotifSpecial >> sMotifType;
652 TString id = reader.MotifSpecialName(sMotifSpecial,scale);
654 AliMpMotifSpecial* specialMotif =
655 dynamic_cast<AliMpMotifSpecial*>(fMotifMap.FindMotif(id));
658 AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
659 sMotifSpecial.Data(),id.Data()));
660 AliMpMotifType* motifType = fMotifMap.FindMotifType(sMotifType.Data());
663 AliDebug(1,Form("Reading motifType %s (%s) from file",
664 sMotifType.Data(),id.Data()));
665 motifType = reader.BuildMotifType(sMotifType.Data());
666 fMotifMap.AddMotifType(motifType);
670 AliDebug(1,Form("Got motifType %s (%s) from motifMap",
671 sMotifType.Data(),id.Data()));
673 specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
674 fMotifMap.AddMotif(specialMotif);
678 AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
682 AliError("pcb not null as expected");
684 pcb = new AliMpPCB(pcbType,specialMotif);
687 if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
689 std::istringstream sin(sline(kMotifKeyword.Length(),
690 sline.Length()-kMotifKeyword.Length()).Data());
694 sin >> sMotifType >> ix >> iy;
696 AliMpMotifType* motifType = fMotifMap.FindMotifType(sMotifType.Data());
699 AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
700 motifType = reader.BuildMotifType(sMotifType.Data());
701 fMotifMap.AddMotifType(motifType);
705 AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));
708 pcb->Add(motifType,ix,iy);
717 //_____________________________________________________________________________
719 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
722 /// Create a new AliMpTrigger object, by reading it from file.
723 /// Returned object must be deleted by client.
725 Double_t scale = 1.0;
726 Bool_t flipX = kFALSE;
727 Bool_t flipY = kFALSE;
729 lines.SetOwner(kTRUE);
733 // Read the file and its include (if any) and store the result
734 // in a TObjArray of TObjStrings.
735 ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
737 // Here some more sanity checks could be done.
738 // For the moment we only insure that the first line contains
740 TString& firstLine = ((TObjString*)lines.First())->String();
741 if ( !IsLayerLine(firstLine) )
743 std::ostringstream s;
744 s << fgkKeywordLayer;
745 lines.AddFirst(new TObjString(s.str().c_str()));
748 AliDebugClass(2,Form("Scale=%g\n",scale));
750 FlipLines(lines,flipX,flipY,srcLine,destLine);
752 // Now splits the lines in packets corresponding to different layers
753 // (if any), and create sub-slats.
755 layers.SetOwner(kTRUE);
760 while ( ( osline = (TObjString*)it.Next() ) )
762 TString& s = osline->String();
763 if ( IsLayerLine(s) )
765 TList* list(new TList);
766 list->SetOwner(kTRUE);
772 ((TList*)layers.At(ilayer))->Add(new TObjString(s));
776 AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
778 AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
780 for ( Int_t ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
782 TList& lines = *((TList*)layers.At(ilayer));
783 std::ostringstream slatName;
784 slatName << slatType << "-LAYER" << ilayer;
785 AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines,scale);
788 Bool_t ok = triggerSlat->AdoptLayer(slat);
791 StdoutToAliError(cout << "could not add slat=" << endl;
793 cout << "to the triggerSlat=" << endl;
794 triggerSlat->Print();
796 AliError("Slat is=");
797 for ( Int_t i = 0; i < slat->GetSize(); ++i )
799 AliMpPCB* pcb = slat->GetPCB(i);
800 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
801 i,pcb->DX()*2,pcb->DY()*2,
802 pcb->DX()*2/scale,pcb->DY()*2/scale));
804 AliError("TriggerSlat is=");
805 for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
807 AliMpSlat* slat = triggerSlat->GetLayer(j);
808 AliError(Form("Layer %d",j));
809 for ( Int_t i = 0; i < slat->GetSize(); ++i )
811 AliMpPCB* pcb = slat->GetPCB(i);
812 AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
813 i,pcb->DX()*2,pcb->DY()*2,
814 pcb->DX()*2/scale,pcb->DY()*2/scale));
817 StdoutToAliError(fMotifMap.Print(););
822 AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));