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