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