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