]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpTriggerReader.cxx
Always delete TObjArrays created by TString::Tokenize (Ruben)
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpTriggerReader.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
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 **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpTriggerReader.cxx,v 1.4 2006/05/24 13:58:52 ivana Exp $
18
19 #include "AliMpTriggerReader.h"
20
21 #include "AliLog.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"
31 #include "AliMpPCB.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"
38 #include "TClass.h"
39 #include "TList.h"
40 #include "TObjString.h"
41 #include "TString.h"
42 #include <TArrayI.h>
43 #include <cstdlib>
44 #include <sstream>
45
46 //-----------------------------------------------------------------------------
47 /// \class AliMpTriggerReader
48 /// Read trigger slat ASCII files
49 /// Basically provides two methods:
50 /// - AliMpTrigger* ReadSlat()
51 /// - AliMpPCB* ReadPCB()
52 ///
53 /// \author Laurent Aphecetche
54 //-----------------------------------------------------------------------------
55
56 /// \cond CLASSIMP
57 ClassImp(AliMpTriggerReader)
58 /// \endcond
59
60 //
61 // static private methods
62 //
63
64 //_____________________________________________________________________________
65 const TString& AliMpTriggerReader::GetKeywordLayer()
66 {
67   /// Keyword: LAYER
68   static const TString kKeywordLayer("LAYER");
69   return kKeywordLayer;
70 }  
71
72 //_____________________________________________________________________________
73 const TString& AliMpTriggerReader::GetKeywordScale()
74 {
75   /// Keyword: SCALE
76   static const TString kKeywordScale("SCALE");
77   return kKeywordScale;
78 }
79
80 //_____________________________________________________________________________
81 const TString& AliMpTriggerReader::GetKeywordPcb()
82 {
83   /// Keyword : PCB
84   static const TString kKeywordPcb("PCB");  
85   return kKeywordPcb;
86 }    
87   
88 //_____________________________________________________________________________
89 const TString& AliMpTriggerReader::GetKeywordFlipX()
90 {
91   /// Keyword : FLIPX
92   static const TString kKeywordFlipX("FLIP_X");
93   return kKeywordFlipX;
94 }  
95   
96 //_____________________________________________________________________________
97 const TString& AliMpTriggerReader::GetKeywordFlipY()
98 {
99   /// Keyword : FLIPY
100   static const TString kKeywordFlipY("FLIP_Y");
101   return kKeywordFlipY;
102 }  
103
104 //
105 // ctors, dtor
106 //
107
108 //_____________________________________________________________________________
109 AliMpTriggerReader::AliMpTriggerReader(const AliMpDataStreams& dataStreams, AliMpSlatMotifMap* motifMap) 
110 : TObject(),
111   fkDataStreams(dataStreams),
112   fMotifMap(motifMap),
113   fLocalBoardMap()
114 {
115   ///
116   /// Default ctor.
117   ///
118     fLocalBoardMap.SetOwner(kTRUE);
119
120
121 //_____________________________________________________________________________
122 AliMpTriggerReader::~AliMpTriggerReader()
123 {
124   ///
125   /// Dtor.
126   ///
127   fLocalBoardMap.DeleteAll();
128 }
129
130 //_____________________________________________________________________________
131 AliMpSlat*
132 AliMpTriggerReader::BuildSlat(const char* slatName,
133                               AliMp::PlaneType planeType,
134                               const TList& lines,
135                               Double_t scale)
136 {
137   /// Construct a slat from the list of lines, taking into account
138   /// the scale factor. The returned pointer must be deleted by the client
139
140   AliDebug(1,Form("slat %s %s scale %e",
141                   slatName,PlaneTypeName(planeType).Data(),scale))
142   ;
143   
144   AliMpSlat* slat = new AliMpSlat(slatName, planeType);
145     
146   TIter it(&lines);
147   
148 //  StdoutToAliDebug(3,lines.Print(););
149   
150   TObjString* osline;
151   while ( ( osline = (TObjString*)it.Next() ) )
152   {
153     // note that at this stage lines should not be empty.
154     TString sline(osline->String());
155     
156     TObjArray* tokens = sline.Tokenize(' ');
157     
158     TString& keyword = ((TObjString*)tokens->At(0))->String();
159     
160     if ( keyword == GetKeywordPcb() )
161     {
162       if ( tokens->GetEntriesFast() != 3 )
163       {
164         AliErrorClass(Form("Syntax error : expecting PCB type localboard-list"
165                            " in following line:\n%s",sline.Data()));
166         delete slat;
167         delete tokens;
168         return 0;
169       }
170       TString pcbName = ((TObjString*)tokens->At(1))->String();
171       
172       TObjArray* localBoardList = ((TObjString*)tokens->At(2))->String().Tokenize(',');
173       
174       if ( scale != 1.0 )
175       {
176         std::ostringstream s;
177         s << pcbName.Data() << "x" << scale;
178         pcbName = s.str().c_str();
179       }
180       
181       AliMpPCB* pcbType = ReadPCB(pcbName.Data());        
182       if (!pcbType)
183       {
184         AliErrorClass(Form("Cannot read pcbType=%s",pcbName.Data()));
185         delete slat;
186         delete tokens;
187         return 0;
188       }      
189
190       TArrayI allLocalBoards;
191       
192       for ( Int_t ilb = 0; ilb < localBoardList->GetEntriesFast(); ++ilb)
193       {
194         TArrayI localBoardNumbers;
195         TString& localBoards = ((TObjString*)localBoardList->At(ilb))->String();
196         Ssiz_t pos = localBoards.First('-');
197         if ( pos < 0 ) 
198         {
199           pos = localBoards.Length();
200         }
201         AliMpHelper::DecodeName(localBoards(pos-1,localBoards.Length()-pos+1).Data(),
202                                 ';',localBoardNumbers);      
203         for ( int i = 0; i < localBoardNumbers.GetSize(); ++i )
204         {
205           std::ostringstream name;
206           name << localBoards(0,pos-1) << localBoardNumbers[i];
207           AliDebugClass(3,name.str().c_str());
208           localBoardNumbers[i] = LocalBoardNumber(name.str().c_str());
209           AliDebugClass(3,Form("LOCALBOARDNUMBER %d\n",localBoardNumbers[i]));
210           allLocalBoards.Set(allLocalBoards.GetSize()+1);
211           allLocalBoards[allLocalBoards.GetSize()-1] = localBoardNumbers[i];
212           if (localBoardNumbers[i] < 0 )
213           {
214             AliErrorClass(Form("Got a negative local board number in %s ? Unlikely"
215                                " to be correct... : %s\n",slatName,name.str().c_str()));
216           }
217         }
218       }
219       AliDebug(3,"Deleting tokens");
220       delete tokens;
221       AliDebug(3,"Deleting localBoardList");
222       delete localBoardList;
223       AliDebug(3,"Adding pcb to slat");
224       slat->Add(*pcbType,allLocalBoards);
225       AliDebug(3,Form("Deleting pcbType=%p %s",pcbType,pcbName.Data()));
226       delete pcbType;
227     }
228   }
229   
230   if ( slat->DX()== 0 || slat->DY() == 0 )
231   {
232     AliFatalClass(Form("Slat %s has invalid null size\n",slat->GetID()));
233   }
234   return slat;
235 }
236
237 //_____________________________________________________________________________
238 TString
239 AliMpTriggerReader::GetBoardNameFromPCBLine(const TString& s)
240 {
241   /// Decode the string to get the board name
242   TString boardName;
243   
244   TObjArray* tokens = s.Tokenize(' ');
245   
246   TString& keyword = ((TObjString*)tokens->At(0))->String();
247
248   if ( keyword == GetKeywordPcb() &&
249        tokens->GetEntriesFast() == 3 )
250   {
251     boardName = ((TObjString*)tokens->At(2))->String();
252   }
253   
254   delete tokens;
255   
256   return boardName;
257 }
258   
259 //_____________________________________________________________________________
260 void
261 AliMpTriggerReader::FlipLines(TList& lines, Bool_t flipX, Bool_t flipY,
262                               Int_t srcLine, Int_t destLine)
263 {
264   ///
265   /// Change the local board names contained in lines, 
266   /// to go from right to left, and/or
267   /// from top to bottom
268   ///
269  
270
271   if ( flipX )
272   {
273     // Simply swaps R(ight) and L(eft) in the first character of 
274     // local board names
275
276     TObjString* oline;
277     TIter it(&lines);
278     while ( ( oline = (TObjString*)it.Next() ) )
279     {
280       TString& s = oline->String();
281       if ( s.Contains("RC") ) 
282       {
283         // Change right to left
284         s.ReplaceAll("RC","LC");
285       }
286       else if ( s.Contains("LC") )
287       {
288         // Change left to right
289         s.ReplaceAll("LC","RC");
290       }
291     }
292   }
293   
294   if ( flipY )
295   {
296     // Change line number, according to parameters srcLine and destLine
297     // Note that because of road opening (for planes 3 and 4 at least),
298     // we loop for srcLine +-1
299     //
300     for ( Int_t line = -1; line <=1; ++line )
301     {
302       std::ostringstream src,dest;
303       src << "L" << srcLine+line;
304       dest << "L" << destLine-line;
305       if ( src.str() == dest.str() ) continue;
306       
307       for ( Int_t i = 0; i < lines.GetSize(); ++i )
308       {
309         TObjString* oline = (TObjString*)lines.At(i);
310         
311         TString& s = oline->String();
312         
313         if ( !s.Contains(GetKeywordPcb()) )
314         {
315           // Only consider PCB lines.
316           continue;
317         }
318         
319         if ( s.Contains(src.str().c_str()) )
320         {
321           AliDebugClass(4,Form("Replacing %s by %s in %s\n",
322                                src.str().c_str(),dest.str().c_str(),s.Data()));
323           
324           s.ReplaceAll(src.str().c_str(),dest.str().c_str());
325           
326           AliDebugClass(4,s.Data());
327           
328           TString boardName(GetBoardNameFromPCBLine(s));
329           
330           if ( line )
331           {
332             // We must also change board numbers, with the tricky
333             // thing that up and down must be swapped...
334             // Up can only be 1 card so it must be B1
335             // Down must be the uppper card of the line before, so
336             // the biggest possible board number for this Line,Column
337             
338             if (line>0)
339             {
340                 // force to B1
341               AliDebugClass(4,Form("Forcing B1 in %s\n",s.Data()));
342               s.ReplaceAll(boardName(boardName.Length()-2,2),"B1");
343               AliDebugClass(4,s.Data());
344             }
345             else
346             {
347               // find the largest valid board number
348               for ( int b = 4; b>=1; --b )
349               {
350                 std::ostringstream bs;
351                 bs << boardName(0,boardName.Length()-1) << b;
352                 if ( LocalBoardNumber(bs.str().c_str()) >= 0 )
353                 {
354                   AliDebugClass(4,Form("Replacing %s by %s in %s\n",
355                                   boardName(boardName.Length()-2,2).Data(),
356                                   Form("B%d",b),
357                                   s.Data()));
358                   s.ReplaceAll(boardName(boardName.Length()-2,2),
359                                Form("B%d",b));
360                   AliDebugClass(4,s);
361                   break;
362                 }
363               }
364             }  
365             // Check that the replacement we did is ok. If not,
366             // skip the line.
367             Int_t lbn = LocalBoardNumber(GetBoardNameFromPCBLine(s));
368             if ( lbn < 0 )
369             {
370               AliDebugClass(4,Form("Removing line %s\n",s.Data()));
371               lines.Remove(oline);
372             }
373             
374           } // if (line)          
375         }
376       }    
377     }
378   }
379 }
380
381 //___________________________________________________________________________
382 Int_t
383 AliMpTriggerReader::IsLayerLine(const TString& sline) const
384 {
385   /// Whether sline contains LAYER keyword
386
387   if ( sline.BeginsWith(GetKeywordLayer()) )
388   {
389     return 1;
390   }
391   else
392   {
393     return 0;
394   }
395 }
396
397 //___________________________________________________________________________
398 Int_t
399 AliMpTriggerReader::DecodeFlipLine(const TString& sline,
400                                    TString& slatType2,
401                                    Bool_t& flipX, Bool_t& flipY)
402 {
403   /// Decode a line containing FLIP_X and/or FLIP_Y keywords
404
405   Ssiz_t blankPos = sline.First(' ');
406   if ( blankPos < 0 ) return 0;
407   
408   TString keyword(sline(0,blankPos));
409   
410   if ( keyword == GetKeywordFlipX() )
411   {
412     flipX = kTRUE;
413   } else if ( keyword == GetKeywordFlipY() )
414   {
415     flipY = kTRUE;
416   }
417   else
418   {
419     return 0;
420   }
421   
422   slatType2 = sline(blankPos+1,sline.Length()-blankPos-1);
423   return 1;
424 }
425
426 //___________________________________________________________________________
427 Int_t
428 AliMpTriggerReader::DecodeScaleLine(const TString& sline, 
429                                     Double_t& scale, TString& slatType)
430 {
431   /// Decode sline containing SCALE keyword
432
433   if ( sline(0,GetKeywordScale().Length()) == GetKeywordScale() )
434   {
435     TString tmp(sline(GetKeywordScale().Length()+1,
436                       sline.Length()-GetKeywordScale().Length()-1));
437     Ssiz_t blankPos = tmp.First(' ');
438     if ( blankPos < 0 )
439     {
440       AliErrorClass(Form("Syntax error in slat file, should get a slatType after "
441                     " SCALE keyword : %s\n",tmp.Data()));
442       return -1;
443     }
444     else
445     {
446       slatType = tmp(0,blankPos);
447       scale = TString(tmp(blankPos+1,tmp.Length()-blankPos-1)).Atof();
448       return 1;
449     }
450   }
451   scale = 1.0;
452   return 0;
453 }
454
455 //_____________________________________________________________________________
456 Int_t
457 AliMpTriggerReader::GetLine(const TString& slatType)
458 {
459   ///
460   /// Assuming slatType is a 4 character string of the form XSLN
461   /// where X=1,2,3 or 4
462   /// S = R or L
463   /// N is the line number
464   /// returns N
465   
466   if ( isdigit(slatType[0]) && 
467        ( slatType[1] == 'R' || slatType[1] == 'L' ) &&
468        slatType[2] == 'L' )
469   {
470     return atoi(slatType(3,1).Data());
471   }
472   return -1;
473 }
474
475 //_____________________________________________________________________________
476 int
477 AliMpTriggerReader::LocalBoardNumber(const char* localBoardName)
478 {
479   /// From local board name to local board number
480
481   if ( !fLocalBoardMap.GetSize() ) 
482   {
483     ReadLocalBoardMapping();
484   }
485   
486   TPair* pair = (TPair*)fLocalBoardMap.FindObject(localBoardName);
487   
488   if (pair)
489   {
490     return atoi(((TObjString*)pair->Value())->String().Data());
491   }
492   return -1;
493 }
494
495 //_____________________________________________________________________________
496 void 
497 AliMpTriggerReader::ReadLines(const char* slatType,
498                               AliMp::PlaneType planeType,
499                               TList& lines,
500                               Double_t& scale,
501                               Bool_t& flipX, Bool_t& flipY,
502                               Int_t& srcLine, Int_t& destLine)
503 {
504   ///
505   /// Reads in lines from file for a given slat
506   /// Returns the list of lines (lines), together with some global
507   /// information as the scale, whether to flip the lines, etc...
508   ///
509   AliDebugClass(2,Form("SlatType %s Scale %e FlipX %d FlipY %d srcLine %d"
510                        " destLine %d\n",slatType,scale,flipX,flipY,
511                        srcLine,destLine));
512   
513   istream& in 
514     = fkDataStreams.
515         CreateDataStream(AliMpFiles::SlatFilePath(
516                              AliMp::kStationTrigger,slatType, planeType));
517   
518   char line[80];
519   
520   while ( in.getline(line,80) )
521   {
522     TString sline(AliMpHelper::Normalize(line));
523
524     if ( sline.Length() == 0 || sline[0] == '#' ) continue;
525     
526     Bool_t isKeywordThere = 
527       sline.Contains(GetKeywordPcb()) || 
528       sline.Contains(GetKeywordLayer()) ||
529       sline.Contains(GetKeywordScale()) || 
530       sline.Contains(GetKeywordFlipX()) || 
531       sline.Contains(GetKeywordFlipY());
532     
533     if ( !isKeywordThere ) 
534     {
535       AliErrorClass(Form("Got a line with no keyword : %s."
536                          "That's not valid\n",line));
537       continue; 
538     }
539     
540     Double_t scale2;
541     TString slatType2;
542     
543     Int_t isScaleLine = DecodeScaleLine(sline,scale2,slatType2);
544     
545     scale *= scale2;
546
547     if ( isScaleLine < 0 )
548     {
549       AliFatalClass(Form("Syntax error near %s keyword\n",GetKeywordScale().Data()));
550     }
551     else if ( isScaleLine > 0 && slatType2 != slatType )
552     {
553       ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
554     }
555     else    
556     {
557       Bool_t fx(kFALSE);
558       Bool_t fy(kFALSE);
559       Int_t isFlipLine = DecodeFlipLine(sline,slatType2,fx,fy);
560       if ( isFlipLine )
561       {
562         if (fy)
563         {
564           srcLine = GetLine(slatType2);
565           destLine = GetLine(slatType);
566         }
567         flipX |= fx;
568         flipY |= fy;
569         ReadLines(slatType2.Data(),planeType,lines,scale,flipX,flipY,srcLine,destLine);
570       }
571       else
572       {
573         lines.Add(new TObjString(sline.Data()));
574       }
575     }
576   }
577   
578   delete &in;
579 }
580                                         
581 //_____________________________________________________________________________
582 void
583 AliMpTriggerReader::ReadLocalBoardMapping()
584 {
585   /// Reads the file that contains the mapping local board name <-> number
586
587   fLocalBoardMap.DeleteAll();
588   
589   UShort_t mask;
590   
591   istream& in 
592     = fkDataStreams.
593         CreateDataStream(AliMpFiles::LocalTriggerBoardMapping());
594
595   char line[80];
596   Char_t localBoardName[20];
597   Int_t j,localBoardId;
598   UInt_t switches;
599   Int_t nofBoards;
600
601   while (!in.eof())
602   {
603     for (Int_t i = 0; i < 4; ++i)
604       if (!in.getline(line,80)) continue; //skip 4 first lines
605  
606     // read mask
607     if (!in.getline(line,80)) break;
608     sscanf(line,"%hx",&mask);
609  
610    // read # boards
611     if (!in.getline(line,80)) break;
612     sscanf(line,"%d",&nofBoards);
613    
614     for ( Int_t i = 0; i < nofBoards; ++i ) 
615     {      
616   
617       if (!in.getline(line,80)) break; 
618       sscanf(line,"%02d %19s %03d %03x", &j, localBoardName, &localBoardId, &switches);
619       if (localBoardId <= AliMpConstants::NofLocalBoards()) 
620       {
621         fLocalBoardMap.Add(new TObjString(localBoardName), new TObjString(Form("%d",localBoardId)));
622         AliDebugClass(10,Form("Board %s has number %d\n", localBoardName, localBoardId));
623       }
624       // skip 2 following lines
625       if (!in.getline(line,80)) break; 
626       if (!in.getline(line,80)) break; 
627        
628     }
629   }
630   
631   delete &in;      
632 }
633
634 //_____________________________________________________________________________
635 AliMpPCB*
636 AliMpTriggerReader::ReadPCB(const char* pcbType)
637
638   ///
639   /// Create a new AliMpPCB object, by reading it from file.
640   /// Returned pointer must be deleted by client.
641   
642   AliDebugClass(2,Form("pcbType=%s\n",pcbType));
643   
644   TString pcbName(pcbType);
645   
646   Ssiz_t pos = pcbName.First('x');
647
648   Double_t scale = 1.0;
649   
650   if ( pos > 0 )
651   {
652     scale = TString(pcbName(pos+1,pcbName.Length()-pos-1)).Atof();
653     pcbName = pcbName(0,pos);
654   }
655   
656   istream& in 
657     = fkDataStreams.
658         CreateDataStream(AliMpFiles::SlatPCBFilePath(
659                              AliMp::kStationTrigger,pcbName));
660  
661   AliMpMotifReader reader(fkDataStreams,
662                           AliMp::kStationTrigger, AliMq::kNotSt12, AliMp::kNonBendingPlane); 
663   // note that the nonbending
664   // parameter is of no use for trigger, as far as reading motif is 
665   // concerned, as all motifs are supposed to be in the same directory
666   // (as they are shared by bending/non-bending planes).
667      
668   char line[80];
669   
670   const TString kSizeKeyword("SIZES");
671   const TString kMotifKeyword("MOTIF");
672   const TString kMotifSpecialKeyword("SPECIAL_MOTIF");
673   
674   AliMpPCB* pcb(0x0);
675   
676   while ( in.getline(line,80) )
677   {
678     if ( line[0] == '#' ) continue;
679     
680     TString sline(line);
681     
682     if ( sline(0,kSizeKeyword.Length()) == kSizeKeyword )
683     {
684       std::istringstream sin(sline(kSizeKeyword.Length(),
685                                    sline.Length()-kSizeKeyword.Length()-1).Data());
686       float padSizeX = 0.0;
687       float padSizeY = 0.0;
688       float pcbSizeX = 0.0;
689       float pcbSizeY = 0.0;
690       sin >> padSizeX >> padSizeY >> pcbSizeX >> pcbSizeY;
691       if (pcb)
692       {
693         AliError("pcb not null as expected");
694       }
695       pcb = new AliMpPCB(fMotifMap,pcbType,padSizeX*scale,padSizeY*scale,
696                          pcbSizeX*scale,pcbSizeY*scale);
697     }
698     
699     if ( sline(0,kMotifSpecialKeyword.Length()) == kMotifSpecialKeyword )
700     {
701       std::istringstream sin(sline(kMotifSpecialKeyword.Length(),
702                                    sline.Length()-kMotifSpecialKeyword.Length()).Data());
703       TString sMotifSpecial;
704       TString sMotifType;
705       sin >> sMotifSpecial >> sMotifType;
706       
707       TString id = reader.MotifSpecialName(sMotifSpecial,scale);
708       
709       AliMpMotifSpecial* specialMotif =
710         dynamic_cast<AliMpMotifSpecial*>(fMotifMap->FindMotif(id));
711       if (!specialMotif)
712       {
713         AliDebug(1,Form("Reading motifSpecial %s (%s) from file",
714                         sMotifSpecial.Data(),id.Data()));
715         AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
716         if ( !motifType)
717         {
718           AliDebug(1,Form("Reading motifType %s (%s) from file",
719                           sMotifType.Data(),id.Data()));
720           motifType = reader.BuildMotifType(sMotifType.Data());
721           fMotifMap->AddMotifType(motifType);
722         }
723         else
724         {
725           AliDebug(1,Form("Got motifType %s (%s) from motifMap",
726                           sMotifType.Data(),id.Data()));        
727         }
728         specialMotif = reader.BuildMotifSpecial(sMotifSpecial,motifType,scale);
729         fMotifMap->AddMotif(specialMotif);
730       }
731       else
732       {
733         AliDebug(1,Form("Got motifSpecial %s from motifMap",sMotifSpecial.Data()));
734       }
735       if (pcb)
736       {
737         AliError("pcb not null as expected");
738       }
739       pcb = new AliMpPCB(pcbType,specialMotif);
740     }
741     
742     if ( sline(0,kMotifKeyword.Length()) == kMotifKeyword )
743     {
744       std::istringstream sin(sline(kMotifKeyword.Length(),
745                                    sline.Length()-kMotifKeyword.Length()).Data());
746       TString sMotifType;
747       int ix;
748       int iy;
749       sin >> sMotifType >> ix >> iy;
750       
751       AliMpMotifType* motifType = fMotifMap->FindMotifType(sMotifType.Data());
752       if ( !motifType)
753       {
754         AliDebug(1,Form("Reading motifType %s from file",sMotifType.Data()));
755         motifType = reader.BuildMotifType(sMotifType.Data());
756         fMotifMap->AddMotifType(motifType);
757       }
758       else
759       {
760         AliDebug(1,Form("Got motifType %s from motifMap",sMotifType.Data()));        
761       }
762       
763       if (! pcb)
764       {
765         AliError("pcb null");
766         continue;
767       }
768       pcb->Add(motifType,ix,iy);
769     }
770   }
771   
772   delete &in;
773   
774   return pcb;
775 }
776
777 //_____________________________________________________________________________
778 AliMpTrigger*
779 AliMpTriggerReader::ReadSlat(const char* slatType, AliMp::PlaneType planeType)
780 {
781   ///
782   /// Create a new AliMpTrigger object, by reading it from file.
783   /// Returned object must be deleted by client.
784
785   Double_t scale = 1.0;
786   Bool_t flipX = kFALSE;
787   Bool_t flipY = kFALSE;
788   TList lines;
789   lines.SetOwner(kTRUE);
790   Int_t srcLine(-1);
791   Int_t destLine(-1);
792   
793   // Read the file and its include (if any) and store the result
794   // in a TObjArray of TObjStrings.
795   ReadLines(slatType,planeType,lines,scale,flipX,flipY,srcLine,destLine);
796
797   // Here some more sanity checks could be done.
798   // For the moment we only insure that the first line contains 
799   // a layer keyword.
800   TString& firstLine = ((TObjString*)lines.First())->String();
801   if ( !IsLayerLine(firstLine) ) 
802   {
803     std::ostringstream s;
804     s << GetKeywordLayer();
805     lines.AddFirst(new TObjString(s.str().c_str()));
806   }
807   
808   AliDebugClass(2,Form("Scale=%g\n",scale));
809   
810   FlipLines(lines,flipX,flipY,srcLine,destLine);
811   
812   // Now splits the lines in packets corresponding to different layers 
813   // (if any), and create sub-slats.
814   TObjArray layers;
815   layers.SetOwner(kTRUE);
816   Int_t ilayer(-1);
817   TIter it(&lines);
818   TObjString* osline;
819   
820   while ( ( osline = (TObjString*)it.Next() ) )
821   {
822     TString& s = osline->String();
823     if ( IsLayerLine(s) )
824     {
825       TList* list = new TList;
826       list->SetOwner(kTRUE);
827       layers.Add(list);
828       ++ilayer;
829     }
830     else
831     {
832       ((TList*)layers.At(ilayer))->Add(new TObjString(s));
833     }
834   }
835
836   AliDebugClass(2,Form("nlayers=%d\n",layers.GetEntriesFast()));
837
838   AliMpTrigger* triggerSlat = new AliMpTrigger(slatType, planeType);
839     
840   for ( ilayer = 0; ilayer < layers.GetEntriesFast(); ++ilayer )
841   {
842     TList& lines1 = *((TList*)layers.At(ilayer));
843     std::ostringstream slatName;
844     slatName << slatType << "-LAYER" << ilayer;
845     AliMpSlat* slat = BuildSlat(slatName.str().c_str(),planeType,lines1,scale);
846     if ( slat )
847     {
848       Bool_t ok = triggerSlat->AdoptLayer(slat);
849       if (!ok)
850       {
851         StdoutToAliError(cout << "could not add slat=" << endl;
852                          slat->Print();
853                          cout << "to the triggerSlat=" << endl;
854                          triggerSlat->Print();
855                          );
856         AliError("Slat is=");
857         for ( Int_t i = 0; i < slat->GetSize(); ++i )
858         {
859           AliMpPCB* pcb = slat->GetPCB(i);
860           AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
861                                 i,pcb->DX()*2,pcb->DY()*2,
862                                 pcb->DX()*2/scale,pcb->DY()*2/scale));
863         }
864         AliError("TriggerSlat is=");
865         for ( Int_t j = 0; j < triggerSlat->GetSize(); ++j )
866         {
867           AliMpSlat* slat1 = triggerSlat->GetLayer(j);
868           AliError(Form("Layer %d",j));
869           for ( Int_t i = 0; i < slat1->GetSize(); ++i )
870           {
871             AliMpPCB* pcb = slat1->GetPCB(i);
872             AliError(Form("ERR pcb %d size %e,%e (unscaled is %e,%e)",
873                           i,pcb->DX()*2,pcb->DY()*2,
874                           pcb->DX()*2/scale,pcb->DY()*2/scale));
875           }
876         } 
877         StdoutToAliError(fMotifMap->Print(););
878       }
879     }
880     else
881     {
882       AliErrorClass(Form("Could not read %s\n",slatName.str().c_str()));
883       delete triggerSlat;
884       return 0;
885     }
886   }
887   
888   return triggerSlat;
889 }