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