]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDDLStore.cxx
Functionality of defunct AliMpManuList is now in AliMpDetElement, filled from DDLStor...
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpDDLStore.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 purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // $Id$
17 // $MpId: AliMpDDLStore.cxx,v 1.4 2006/05/24 13:58:34 ivana Exp $
18 // Category: management
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpDDLStore
22 // --------------------
23 // The top container class for DDLs, det elements and bus patched
24 // It provides acces to DDL, det element and bus patches objects
25 // via various characteristics.
26 // Authors: Ivana Hrivnacova, IPN Orsay
27 //          Christian Finck, SUBATECH Nantes
28 //-----------------------------------------------------------------------------
29
30 #include "AliMpDDLStore.h"
31 #include "AliMpConstants.h"
32 #include "AliMpDEStore.h"
33 #include "AliMpDDL.h"
34 #include "AliMpFiles.h"
35 #include "AliMpHelper.h"
36 #include "AliMpDEManager.h"
37 #include "AliMpDetElement.h"
38 #include "AliMpBusPatch.h"
39 #include "AliMpTriggerCrate.h"
40 #include "AliMpLocalBoard.h"
41 #include "AliMpSegmentation.h"
42 #include "AliMpVSegmentation.h"
43 #include "AliMpStringObjMap.h"
44 #include "AliLog.h"
45
46 #include <Riostream.h>
47 #include <TList.h>
48 #include <TObjArray.h>
49 #include <TString.h>
50 #include <TObjString.h>
51 #include <TClass.h>
52
53 /// \cond CLASSIMP
54 ClassImp(AliMpDDLStore)
55 /// \endcond
56
57 AliMpDDLStore* AliMpDDLStore::fgInstance = 0;
58 const Int_t    AliMpDDLStore::fgkNofDDLs = 20;
59 const Int_t    AliMpDDLStore::fgkNofTriggerDDLs = 2;
60
61 //
62 // static methods
63 //
64
65 //______________________________________________________________________________
66 AliMpDDLStore* AliMpDDLStore::Instance(Bool_t warn)
67 {
68 /// Create the DDL store if it does not yet exist
69 /// and return its instance
70
71   if ( ! fgInstance && warn  ) {
72     AliWarningClass("DDL Store has not been loaded");
73   }  
74      
75   return fgInstance;
76 }    
77
78 //______________________________________________________________________________
79 AliMpDDLStore* AliMpDDLStore::ReadData(Bool_t warn)
80 {
81 /// Load the DDL store from ASCII data files
82 /// and return its instance
83
84   if ( fgInstance ) {
85     if ( warn )
86       AliWarningClass("DDL Store has been already loaded");
87     return fgInstance;
88   }  
89   
90   AliInfoClass("Reading DDL Store from ASCII files.");
91
92   fgInstance = new AliMpDDLStore();
93   return fgInstance;
94 }    
95
96 //
97 // ctors, dtor
98 //
99
100 //______________________________________________________________________________
101 AliMpDDLStore::AliMpDDLStore()
102 : TObject(),
103   fDDLs(fgkNofDDLs+fgkNofTriggerDDLs), // FIXEME
104   fBusPatches(true),
105   fTriggerCrates(true),
106   fLocalBoards(true),
107   fManuList12(),
108   fManuBridge2()
109 {  
110 /// Standard constructor
111
112   AliDebug(1,"");
113   fDDLs.SetOwner(true);
114   fBusPatches.SetOwner(true);
115   fBusPatches.SetSize(900);
116
117   fTriggerCrates.SetOwner(true);
118   fTriggerCrates.SetSize(16);
119
120   fLocalBoards.SetOwner(true);
121   fLocalBoards.SetSize(242); // included non-identied board
122
123
124   // Load segmentation & DE store data
125   if ( ! AliMpSegmentation::Instance(false) )
126     AliMpSegmentation::ReadData(true);
127
128   // Create all detection elements
129   ReadDDLs();
130   ReadTriggerDDLs();
131   SetManus();
132   SetPatchModules();
133   SetBusPatchLength();
134 }
135
136 //______________________________________________________________________________
137 AliMpDDLStore::AliMpDDLStore(TRootIOCtor* /*ioCtor*/)
138 : TObject(),
139   fDDLs(),
140   fBusPatches(),
141   fTriggerCrates(true),
142   fLocalBoards(true)
143 {  
144 /// Constructor for IO
145
146   AliDebug(1,"");
147
148   fgInstance = this;
149 }
150
151
152 //______________________________________________________________________________
153 AliMpDDLStore::~AliMpDDLStore()
154 {
155 /// Destructor
156
157   AliDebug(1,"");
158
159   // DDL objects are deleted with fDDLs 
160   // Bus patches objects are deleted with fBusPatches 
161   
162   fgInstance = 0;
163 }
164
165 //
166 // private methods
167 //
168
169 //______________________________________________________________________________
170 Int_t  AliMpDDLStore::GetManuListIndex(Int_t detElemId) const
171 {
172 /// Return the index of the manu list for given detElemId
173
174   return AliMpDEManager::GetChamberId(detElemId)*4 + (detElemId % 100);
175 }  
176   
177
178 //______________________________________________________________________________
179 Int_t AliMpDDLStore::GetBusPatchIndex(Int_t detElemId, Int_t manuId) const
180 {
181 /// Calculate the index of the buspatch from manuId
182
183     Int_t pos = 0;
184     AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
185     static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
186
187     if( stationType == AliMp::kStation345) {
188       pos = (manuId & manuMask)/100;
189     } else {
190       Int_t idx = GetManuListIndex(detElemId);
191
192       // using array defined from DetElemIdToBusPatch.dat file
193       for (pos = fManuList12[idx].GetSize()-1; pos >= 0; --pos)
194           if ( manuId >= fManuList12[idx].At(pos)) break;
195     }
196
197     return pos;
198 }
199
200
201 //______________________________________________________________________________
202 Bool_t AliMpDDLStore::ReadDDLs()
203 {
204 /// Read ddl <-> bus patch file
205  
206     
207   TString infile = AliMpFiles::BusPatchFilePath();
208
209   ifstream in(infile, ios::in);
210   if (!in) {
211     AliErrorStream() << "Data file " << infile << " not found.";
212     return false;
213   }  
214       
215   char line[255];
216
217   while ( in.getline(line,255) ) {
218
219      if ( line[0] == '#' ) continue;
220
221      TString tmp(AliMpHelper::Normalize(line));
222
223      TObjArray* stringList = tmp.Tokenize(TString(" "));
224
225      TString sDE = ((TObjString*)stringList->At(0))->GetString();
226      Int_t idDE  = atoi(sDE.Data());
227
228      if ( ! AliMpDEManager::IsValidDetElemId(idDE, false) ) {
229        AliErrorStream() << "DetElemId "<< idDE << " not valid." << endl;
230        return false;
231      }  
232
233      TString busPatch = ((TObjString*)stringList->At(1))->GetString();
234
235
236      TString sDDL = ((TObjString*)stringList->At(2))->GetString();
237      Int_t  iDDL  = atoi(sDDL.Data());
238
239      if ( iDDL < 0 || iDDL >= fgkNofDDLs ) {
240        AliErrorStream() << "DDL id "<< iDDL << " outside limits." << endl;
241        return false;
242      }
243   
244      AliDebugStream(3)
245          << "idDE " << idDE << " buspatch " << busPatch.Data() << " iDDL " << iDDL 
246          << endl;
247
248      // reading 1st manu Id for each bus patch (station 1 & 2)
249      if(AliMpDEManager::GetStationType(idDE) != AliMp::kStation345) {
250      
251        TString sManu = ((TObjString*)stringList->At(3))->GetString();
252        AliMpHelper::DecodeName(sManu,',',fManuList12[GetManuListIndex(idDE)]);
253   
254        if(AliMpDEManager::GetStationType(idDE) == AliMp::kStation2) {
255          TString sManuBridge = ((TObjString*)stringList->At(4))->GetString();
256          AliMpHelper::DecodeName(sManuBridge,',',fManuBridge2[GetManuListIndex(idDE)]);
257        }
258
259      }
260
261      delete stringList;
262
263      AliMpDDL* ddl = GetDDL(iDDL, false);
264      if ( !ddl) {
265        ddl = new AliMpDDL(iDDL);
266        fDDLs.AddAt(ddl, iDDL);
267      }  
268      ddl->AddDE(idDE);
269      
270      TArrayI busPatchList;
271      // decoding range of buspatch
272      AliMpHelper::DecodeName(busPatch,';',busPatchList);
273      
274      // Update DE
275      AliMpDetElement* de = AliMpDEManager::GetDetElement(idDE);
276      de->SetDdlId(iDDL);
277      // filling buspatch -> idDE
278      for (Int_t i = 0; i < busPatchList.GetSize(); i++) {
279        fBusPatches.Add(busPatchList[i], 
280                        new AliMpBusPatch(busPatchList[i], idDE, iDDL));
281        de->AddBusPatch(busPatchList[i]);
282      }  
283    }
284    
285    // Fill bus patch Ids array in DDLs now
286    for ( Int_t i=0; i<fDDLs.GetEntriesFast(); i++ ) {
287      AliMpDDL* ddl = (AliMpDDL*) fDDLs.At(i);
288      ddl->FillBusPatchIds();
289    }  
290    
291    in.close();
292    return true;
293 }
294
295 //______________________________________________________________________________
296 Bool_t  AliMpDDLStore::ReadTriggerDDLs()
297 {
298 /// create trigger DDL object ddl<->Crate<->local board
299   
300    AliMpStringObjMap inputXfromMap;
301    AliMpStringObjMap inputXtoMap;
302    AliMpStringObjMap inputYfromMap;
303    AliMpStringObjMap inputYtoMap;
304
305     Int_t nonNotified = 0;
306
307     Int_t iDDL = -1;
308
309     TString infile = AliMpFiles::LocalTriggerBoardMapping();
310   
311     ifstream in(infile, ios::in);
312     
313     if (!in) {
314       AliError(Form("Local Trigger Board Mapping File %s not found", infile.Data()));
315       return kFALSE;
316     }
317   
318     AliMpLocalBoard* board = 0x0;
319     AliMpTriggerCrate* crate = 0x0;
320
321     TString localNumber;
322     Int_t localNumberId = 0;
323     TArrayI list;
324
325     char line[255];
326     while (in.getline(line, 255))
327     {
328
329       // files contains lines with some blank caracters ???
330       if (line[0] == ' ' && strlen(line) < 12) continue;
331
332       TString tmp(AliMpHelper::Normalize(line));
333       if (tmp.IsNull()) continue; // Ignore empty lines
334  
335       if (tmp.Contains("Board")) 
336       {
337         // extract id, name, slot & crate name
338         TObjArray* stringList = tmp.Tokenize(TString(" "));
339   
340         TString localNumber = ((TObjString*)stringList->At(1))->GetString();
341
342         if (localNumber.CompareTo("nn") != 0) // Notified cards
343             localNumberId = localNumber.Atoi();
344         else 
345             localNumberId = AliMpConstants::NofLocalBoards()  + (++nonNotified);
346
347         TString localName = ((TObjString*)stringList->At(4))->GetString();
348         TString slotName  = ((TObjString*)stringList->At(10))->GetString();
349
350         AliDebug(3, Form("%d %s %d\n", localNumberId, localName.Data(), slotName.Atoi()));
351
352         board = new AliMpLocalBoard(localNumberId, localName.Data(), slotName.Atoi());
353
354         // not notified board
355         if (localNumber.CompareTo("nn") == 0)
356             board->SetNotified(false);
357
358         // compose name with number and side (Right or Left)
359         TString crateNumber = ((TObjString*)stringList->At(6))->GetString();
360         TString crateSide = ((TObjString*)stringList->At(7))->GetString();
361         TString crateName = crateNumber + crateSide;
362
363         // set crate name
364         board->SetCrate(crateName);
365
366         // determine ddl number vs crate side
367         if (crateName.Contains("R")) 
368             iDDL = fgkNofDDLs; // starts where tracker ends
369         else 
370             iDDL = fgkNofDDLs + 1;
371         
372         AliMpDDL* ddl = GetDDL(iDDL, false);
373         if ( !ddl) {
374           ddl = new AliMpDDL(iDDL);
375           fDDLs.AddAt(ddl, iDDL);
376         }  
377         
378         Int_t crateId; 
379         if (crateNumber.CompareTo("2-3") != 0)
380             crateId = crateNumber.Atoi();
381         else
382             crateId = 23;
383
384         // add trigger crate number for given ddl if not present
385         if ( !ddl->HasTriggerCrateId(crateId) )
386             ddl->AddTriggerCrate(crateId);
387
388         // if crate not existing then creating
389         if (!GetTriggerCrate(crateName, false)) {
390             crate = new AliMpTriggerCrate(crateName.Data(), iDDL);
391             fTriggerCrates.Add(crateName.Data(), crate);
392         }
393
394         // create list of local board in this crate
395         crate->AddLocalBoard(localNumberId);
396
397         delete stringList;
398       }
399
400       if (tmp.Contains("DetElemId")) 
401       {
402         list.Reset();
403         AliMpDDL* ddl = GetDDL(iDDL, true);
404
405         // add  DE for local board and DDL
406         TString detElem = &tmp[tmp.First(" ")+1];
407         AliMpHelper::DecodeName(detElem,' ',list);
408
409         for (Int_t i = 0; i < list.GetSize(); ++i) {
410
411           if (list[i]) { // skip copy card
412               AliMpDetElement* de = AliMpDEManager::GetDetElement(list[i]);
413               if (de->GetDdlId() == -1)
414                   de->SetDdlId(iDDL);
415           }
416             board->AddDE(list[i]);
417             if(!ddl->HasDEId(list[i]))
418                 ddl->AddDE(list[i]);
419         }
420       }
421
422       // map the different copies between cards in X and Y inputs
423       // when copying Xinput, Yinput are copied as well
424       if (tmp.Contains("X3input1")) 
425       {
426         TObjArray* stringList = tmp.Tokenize(TString(" "));
427   
428         TString copyX = ((TObjString*)stringList->At(3))->GetString();
429         TString input = ((TObjString*)stringList->At(2))->GetString();;
430
431         if (copyX.Contains("(1)")) { 
432           inputXfromMap.Add(input, board);
433           inputYfromMap.Add(input, board);
434
435         }
436         if (copyX.Contains("(2)")) {
437           inputXtoMap.Add(input, board);
438           inputYtoMap.Add(input, board);
439         }
440         delete stringList;
441       }
442
443       if (tmp.Contains("Y1input1")) 
444       {
445         TObjArray* stringList = tmp.Tokenize(TString(" "));
446
447         TString copyY = ((TObjString*)stringList->At(3))->GetString();
448         TString input = ((TObjString*)stringList->At(2))->GetString();;
449
450         if (copyY.Contains("(1)")) 
451             inputYfromMap.Add(input, board);
452         if (copyY.Contains("(2)")) 
453             inputYtoMap.Add(input, board);
454         delete stringList;
455       }
456
457
458       if (tmp.Contains("transv")) 
459       {
460         // set transverse connector
461         Int_t blankPos = tmp.Last(' ');
462
463         TString transv(tmp(blankPos+1, tmp.Length()-blankPos));
464         transv.ToUpper();
465         if (transv.CompareTo("NONE") == 0)
466             board->SetTC(false);
467       }
468
469       if (tmp.Contains("Switch")) 
470       {
471         list.Reset();
472         in.getline(line, 255);// skip one line
473
474         in.getline(line, 255);
475         TString tmp(AliMpHelper::Normalize(line));
476
477         // decode switches
478         AliMpHelper::DecodeName(tmp,' ',list);
479
480         // store switches
481         AliMpArrayI switches;
482
483         for (Int_t i = 0; i < list.GetSize(); ++i)
484             board->AddSwitch(list[i]);
485
486         // add local board into map
487         fLocalBoards.Add(board->GetId(), board);
488
489       }
490     }
491     
492     // set copy card number to where the X-Y inputs are copied and
493     // from where the X-Y inputs come.
494     // deleting the first item (TString) done by AliMpStringObjMap itself
495     // keep AliMpLocalBoard object undelete
496
497     TString value;
498
499     for (inputXfromMap.First(); !inputXfromMap.IsDone(); inputXfromMap.Next()) {
500       
501       value = inputXfromMap.CurrentKey();
502       AliMpLocalBoard* boardFrom = (AliMpLocalBoard*)inputXfromMap.CurrentItem();
503       AliMpLocalBoard* boardTo   = (AliMpLocalBoard*)inputXtoMap.Get(value);
504       boardFrom->SetInputXto(boardTo->GetId());
505       boardTo->SetInputXfrom(boardFrom->GetId());
506       AliDebug(3, Form("copy xInputs from local id %d_%s_%d to %d_%s_%d\n", 
507                        boardTo->GetInputXfrom(), boardFrom->GetCrate().Data(), boardFrom->GetSlot(),
508                        boardFrom->GetInputXto(), boardTo->GetCrate().Data(), boardTo->GetSlot()));
509     }
510
511     for (inputYfromMap.First(); !inputYfromMap.IsDone(); inputYfromMap.Next()) {
512
513       value = inputYfromMap.CurrentKey();
514       AliMpLocalBoard* boardFrom = (AliMpLocalBoard*)inputYfromMap.CurrentItem();
515       AliMpLocalBoard* boardTo   = (AliMpLocalBoard*)inputYtoMap.Get(value);
516       boardFrom->SetInputYto(boardTo->GetId());
517       boardTo->SetInputYfrom(boardFrom->GetId());
518       AliDebug(3, Form("copy yInputs from local id %d_%s_%d to %d_%s_%d\n", 
519                        boardTo->GetInputYfrom(), boardFrom->GetCrate().Data(), boardFrom->GetSlot(),
520                        boardFrom->GetInputYto(), boardTo->GetCrate().Data(), boardTo->GetSlot()));
521     }
522
523     return kTRUE;
524 }
525
526 //______________________________________________________________________________
527 Bool_t AliMpDDLStore::SetManus()
528 {
529 /// Set manus for each bus patch
530
531     Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
532
533     // loop over DDL 
534     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL) {
535
536       AliDebug(3, Form("DDL # %d\n", iDDL));
537
538       AliMpDDL* ddl = GetDDL(iDDL);
539
540       // loop over DE in the given DDL
541       for (Int_t detElemIdx = 0; detElemIdx < ddl->GetNofDEs(); ++detElemIdx) {
542
543         Int_t detElemId = ddl->GetDEId(detElemIdx);
544
545         AliMpDetElement* detElement = GetDetElement(detElemId);
546
547         AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
548
549
550         // list of manu per DE on both cathode
551         TList manuList;
552         for ( Int_t cath = 0; cath < 2 ; ++cath ) {
553           const AliMpVSegmentation* seg 
554               = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cath));
555         
556           AliMp::PlaneType planeType = detElement->GetPlaneType(AliMp::GetCathodType(cath));
557
558           TArrayI manus;
559           seg->GetAllElectronicCardIDs(manus);
560           
561           // filling TList manu
562           for ( Int_t im = 0; im < manus.GetSize(); ++im ) {
563       
564             AliMpIntPair* manu = 0x0;
565             if( stationType == AliMp::kStation345) 
566         manu = new AliMpIntPair((manus[im] & manuMask), planeType, kTRUE); //remove offset for NB
567             else 
568         manu = new AliMpIntPair(manus[im], planeType, kTRUE); //keep offset for NB
569             
570             manuList.Add(manu);
571       
572       detElement->AddManu(manus[im]);
573           }        
574         }// cathode
575   
576         manuList.Sort(); // sort regardless B or NB plane
577
578         // filling manu to the corresponding buspatch
579         for (Int_t iEntry = 0; iEntry < manuList.GetEntries(); ++iEntry) {
580
581           AliMpIntPair* manuPtr = (AliMpIntPair*)manuList.At(iEntry);
582
583           Int_t manuId = manuPtr->GetFirst();
584           Int_t pos    = GetBusPatchIndex(detElemId, manuId);
585
586           if (pos > detElement->GetNofBusPatches()) {
587             AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n", 
588                           pos, detElement->GetNofBusPatches(), manuId, detElemId));
589             return false;
590           }
591           
592           // get buspatch and fill manus
593           Int_t busPatchId = detElement->GetBusPatchId(pos);
594           AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
595
596           if( stationType == AliMp::kStation345) {
597       
598             if (manuPtr->GetSecond()) 
599         busPatch->AddManu(manuId+manuMask+1); // add offset again after sorted
600             else 
601         busPatch->AddManu(manuId); 
602       
603           } else {
604       
605             busPatch->AddManu(manuId); 
606       
607           }
608         }
609
610         manuList.Delete();
611
612         if (AliDebugLevel() == 3) {
613
614           // print out for checking
615           for(Int_t pos = 0; pos < detElement->GetNofBusPatches(); ++pos) {
616             Int_t busPatchId = detElement->GetBusPatchId(pos);
617             AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
618             printf("BusPatch: %d\n", busPatch->GetId());
619             for (Int_t iEntry = 0; iEntry < busPatch->GetNofManus(); ++iEntry) 
620                 printf("manu Id: %d\n", busPatch->GetManuId(iEntry));
621           }
622         }
623
624       } // detection element loop
625     }// DDL loop
626
627     return true;
628 }
629
630 //______________________________________________________________________________
631 Bool_t AliMpDDLStore::SetPatchModules()
632 {
633 /// Compute the number of manu per PCB for each buspatch 
634
635     AliMpDEIterator it;
636     Bool_t result = true;
637
638     for ( it.First(); !it.IsDone(); it.Next() ) {
639
640       AliMpDetElement* detElement = it.CurrentDE();
641       
642       for (Int_t i = 0; i < detElement->GetNofBusPatches(); ++i) {
643         AliMpBusPatch* busPatch = GetBusPatch(detElement->GetBusPatchId(i));
644         Bool_t newResult = false;
645         Int_t idDE = busPatch->GetDEId();
646
647         if (AliMpDEManager::GetStationType(idDE) == AliMp::kStation2 ) 
648             newResult = busPatch->SetNofManusPerModule(fManuBridge2[GetManuListIndex(idDE)].At(i));
649         else 
650             newResult = busPatch->SetNofManusPerModule();
651       }
652     }
653
654     return result;
655 }
656
657 //______________________________________________________________________________
658 Bool_t AliMpDDLStore::SetBusPatchLength()
659 {
660 /// read the buspatch length file and set buspatch length 
661
662     TString infile = AliMpFiles::BusPatchLengthFilePath();
663     ifstream in(infile, ios::in);
664     if (!in) {
665       AliErrorStream() << "Data file " << infile << " not found.";
666       return false;
667     }  
668     char line[255];
669
670     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL ) {
671       AliMpDDL* ddl = GetDDL(iDDL);
672
673       for (Int_t iBusPatch = 0; iBusPatch < ddl->GetNofBusPatches(); ++iBusPatch) {
674   
675         do {
676           if (!in.getline(line,255)) {
677             AliWarning(Form("Wrong size in bus patch length file; index %d DDL %d", 
678                             iBusPatch, iDDL));
679             return false;
680           }
681         } while(line[0] == '#');
682
683         TString tmp(AliMpHelper::Normalize(line));
684
685         TObjArray* stringList = tmp.Tokenize(TString(" "));
686         
687         TString sLocalBusId = ((TObjString*)stringList->At(0))->GetString();
688         Int_t   localBusId  = sLocalBusId.Atoi();
689
690         TString sLength = ((TObjString*)stringList->At(1))->GetString();
691         Float_t length  = sLength.Atof();
692
693         delete stringList;
694
695         if (localBusId != iBusPatch + 1)
696             AliWarning(Form("Wrong local buspatch id %d instead of %d", iBusPatch+1, localBusId));
697
698         Int_t busPatchId = ddl->GetBusPatchId(iBusPatch);
699         AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
700         busPatch->SetCableLength(length);
701       }
702     }
703
704     return true;
705 }
706
707
708 //________________________________________________________________
709 Int_t AliMpDDLStore::GetLocalBoardId(TString name) const
710 {
711 /// return the first board with a given side and line
712
713    TExMapIter i = fLocalBoards.GetIterator();
714     Long_t key, value;
715     while ( i.Next(key, value) ) {
716       AliMpLocalBoard* local = (AliMpLocalBoard*)value;
717
718       TString tmp(&local->GetName()[4], 2);
719       if (name.Contains(tmp))
720           if (name[0] == local->GetName()[0])
721               return local->GetId();
722     }
723
724     return 0;
725
726 }
727
728 //
729 // public methods
730 //
731
732
733 //______________________________________________________________________________
734 AliMpDDL* AliMpDDLStore::GetDDL(Int_t ddlId, Bool_t warn) const
735 {
736 /// Return DDL for given ddlId
737
738   AliMpDDL* ddl 
739     = (AliMpDDL*)fDDLs.At(ddlId);
740     
741   if ( ! ddl && warn ) {  
742     AliErrorStream() 
743         << "DDL with Id = " << ddlId << " not defined." << endl;
744   }     
745
746   return ddl;
747 }    
748
749 //______________________________________________________________________________
750 AliMpDetElement*  AliMpDDLStore::GetDetElement(Int_t detElemId, Bool_t warn) const
751 {
752 /// Return detection element with given detElemId
753
754   if ( ! AliMpDEStore::Instance() ) {
755     AliFatal("DE Store has not been loaded.");
756     return 0; 
757   }  
758
759   return AliMpDEStore::Instance()->GetDetElement(detElemId, warn);
760 }  
761
762 //______________________________________________________________________________
763 AliMpBusPatch* AliMpDDLStore::GetBusPatch(Int_t busPatchId, Bool_t warn) const
764 {
765 /// Return bus patch with given Id
766
767   AliMpBusPatch* busPatch
768     = (AliMpBusPatch*) fBusPatches.GetValue(busPatchId);
769     
770   if ( ! busPatch && warn ) {  
771     AliErrorStream() 
772         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
773   }     
774
775   return busPatch;
776 }    
777
778 //______________________________________________________________________________
779 AliMpLocalBoard* AliMpDDLStore::GetLocalBoard(Int_t localBoardId, Bool_t warn) const
780 {
781 /// Return bus patch with given Id
782
783   AliMpLocalBoard* localBoard
784     = (AliMpLocalBoard*) fLocalBoards.GetValue(localBoardId);
785     
786   if ( ! localBoard && warn ) {  
787     AliErrorStream() 
788         << "Local board with Id = " << localBoardId << " not defined." << endl;
789   }     
790
791   return localBoard;
792 }    
793
794 //______________________________________________________________________________
795 AliMpTriggerCrate* AliMpDDLStore::GetTriggerCrate(TString name, Bool_t warn) const
796 {
797 /// Return trigger crate with given name
798
799   AliMpTriggerCrate* crate
800      = (AliMpTriggerCrate*) fTriggerCrates.GetValue(name.Data());
801     
802   if ( ! crate && warn ) {  
803     AliErrorStream() 
804         << "Trigger crate with name = " << name.Data() << " not defined." << endl;
805   }     
806
807   return crate;
808 }  
809
810 //______________________________________________________________________________
811 AliMpTriggerCrate* AliMpDDLStore::GetTriggerCrate(Int_t ddlId, Int_t index, Bool_t warn) const
812 {
813 /// Return trigger crate with given ddl and index crate
814
815   if (ddlId == 0 || ddlId == 1)
816       ddlId += fgkNofDDLs;
817
818   AliMpDDL* ddl = GetDDL(ddlId, warn);
819   if ( ! ddl ) return 0; 
820   
821   if ( index >= ddl->GetNofTriggerCrates() ) {
822       AliError(Form("crate id %d greater than array[%d]", index, ddl->GetNofTriggerCrates()));
823       return 0;
824   }    
825    
826   Int_t crateId = ddl->GetTriggerCrateId(index);
827   TString name = AliMpTriggerCrate::GenerateName(crateId, ddlId, fgkNofDDLs);
828   
829   return GetTriggerCrate(name, warn);
830 }  
831
832 //______________________________________________________________________________
833 Int_t  AliMpDDLStore::GetDEfromBus(Int_t busPatchId) const
834 {
835 /// Return detection element Id for given busPatchId
836
837   AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
838   
839   if ( ! busPatch ) {
840     AliErrorStream() 
841         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
842     return 0;    
843   }     
844
845   return busPatch->GetDEId();
846 }  
847
848 //______________________________________________________________________________
849 Int_t  AliMpDDLStore::GetDEfromLocalBoard(Int_t localBoardId, Int_t chamberId) const
850 {
851 /// Return detElemId for local board Id and chamber id.
852
853   AliMpLocalBoard* localBoard = GetLocalBoard(localBoardId);
854   
855   if ( ! localBoard ) {
856     AliErrorStream() 
857         << "Loacl board with Id = " << localBoardId << " not defined." << endl;
858     return 0;    
859   }     
860
861    return localBoard->GetDEIdByChamber(chamberId);
862 }
863
864 //______________________________________________________________________________
865 Int_t  AliMpDDLStore::GetDDLfromBus(Int_t busPatchId) const
866 {
867 /// Return DDL Id for given busPatchId
868
869   AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
870   
871   if ( ! busPatch ) {
872     AliErrorStream() 
873         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
874     return 0;    
875   }     
876
877   return busPatch->GetDdlId();
878 }  
879
880 //______________________________________________________________________________
881 Int_t AliMpDDLStore::GetBusPatchId(Int_t detElemId, Int_t manuId) const
882 {
883 /// Return bus patch for a given manuId
884
885   AliMpDetElement* detElement = GetDetElement(detElemId);
886   Int_t pos = GetBusPatchIndex(detElemId, manuId);
887
888   if ( pos > detElement->GetNofBusPatches() ) {
889     AliErrorStream() 
890       << "Pos = " << pos 
891       << " greater than the size = " <<  detElement->GetNofBusPatches()
892       << " for detElemId = " << detElemId 
893       << " manuId = " << manuId << endl;
894     return -1;
895   }
896
897   return detElement->GetBusPatchId(pos);
898 }    
899
900 //______________________________________________________________________________
901 AliMpIntPair  AliMpDDLStore::GetDetElemIdManu(Int_t manuSerial) const
902 {
903 /// Return the detElemId and manuId for given serial manu number
904
905   if ( ! AliMpDEStore::Instance() ) {
906     AliFatal("DE Store has not been loaded.");
907     return AliMpIntPair::Invalid(); 
908   }  
909
910   return AliMpDEStore::Instance()->GetDetElemIdManu(manuSerial);
911 }  
912
913 //______________________________________________________________________________
914 void AliMpDDLStore::PrintAllManu() const
915 {
916 /// Print all manu Ids and their serial numbers sorted by detection element
917 /// and bus patch.                                                            \n
918 /// As serial manu numbers are filled in a different way than manu Ids this
919 /// printing allows to check that both ways are consistent
920
921   // Loop over DE
922   AliMpDEIterator it;
923   for ( it.First(); ! it.IsDone(); it.Next() ) {
924      AliMpDetElement* de = it.CurrentDE();
925      cout << "DE: " << de->GetId() << endl; 
926      
927      // Loop over bus patches in this DE
928      for ( Int_t i=0; i< de->GetNofBusPatches(); ++i ) {
929
930        AliMpBusPatch* busPatch = GetBusPatch(de->GetBusPatchId(i));    
931        cout << "  busPatch: " << busPatch->GetId() << endl; 
932
933        cout << "    Manu       : ";
934        for ( Int_t j=0; j<busPatch->GetNofManus(); ++j ) {
935          cout << std::setw(6) << busPatch->GetManuId(j) << " ";
936        }
937        cout << endl;
938        
939        cout << "    Manu serial: ";
940        for ( Int_t k=0; k<busPatch->GetNofManus(); ++k ) {
941          cout << std::setw(6) << de->GetManuSerialFromId(busPatch->GetManuId(k)) << " ";
942        }        
943        cout << endl;
944      }
945   }
946 }  
947
948 //________________________________________________________________
949 Int_t  AliMpDDLStore::GetNextDEfromLocalBoard(Int_t localBoardId, Int_t chamberId ) const
950 {
951 /// return the next detection element in line
952
953     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
954
955     TString name(localBoard->GetName());
956
957     Int_t line = localBoard->GetPosition().GetFirst();
958     ++line;
959     
960     name.Replace(4,1,Form("%d", line));
961
962    Int_t nextLocalId;
963     if ((nextLocalId = GetLocalBoardId(name)))
964         return GetDEfromLocalBoard(nextLocalId, chamberId);
965     else
966         return 0;
967
968     return 0;
969 }
970
971 //________________________________________________________________
972 Int_t  AliMpDDLStore::GetPreviousDEfromLocalBoard(Int_t localBoardId, Int_t chamberId) const
973 {
974 /// return the previous detection element in line
975
976     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
977
978     TString name(localBoard->GetName());
979
980     Int_t line = localBoard->GetPosition().GetFirst();
981     --line;
982     
983     name.Replace(4,1,Form("%d", line));
984
985     Int_t prevLocalId;
986     if ((prevLocalId = GetLocalBoardId(name)))
987         return GetDEfromLocalBoard(prevLocalId, chamberId);
988     else
989         return 0;
990
991 }
992