Macro for checking the serial & bin number of the mounted manus
[u/mrichter/AliRoot.git] / MUON / mapping / macros / MUONCheckManu.C
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
18 // By Ch. Finck, Subatech
19
20 #if !defined(__CINT__) || defined(__MAKECINT__)
21
22 #include <Riostream.h>
23
24 #include "TExMap.h"
25 #include "TList.h"
26 #include "TH1F.h"
27 #include "TH2F.h"
28 #include "TFile.h"
29 #include "TObjArray.h"
30 #include "TObjString.h"
31
32 #include "AliCDBManager.h"
33 #include "AliMpHelper.h"
34 #include "AliMpConstants.h"
35 #include "AliMpDEIterator.h"
36 #include "AliMpIntPair.h"
37 #include "AliMpFiles.h"
38 #include "AliMpSegmentation.h"
39 #include "AliMpVSegmentation.h"
40 #include "AliMpDEManager.h"
41 #include "AliMpDetElement.h"
42 #include "AliMpDDLStore.h"
43 #include "AliMpCDB.h"
44
45 #endif
46
47 const Int_t kStrangeBin = 26;
48 const Int_t kUndefBin   = 36;
49 const Int_t kBadCalBin  = 46;
50 const Int_t kBadBin       = 5;
51
52 const Int_t kNotScanSerial  = 40000;
53
54 void MUONCheckManu(Int_t iCh = 10, Bool_t rootFile = kFALSE)
55 {
56
57     // Macro to check the bin number for manu in the CDB for each chamber
58     // chamber = 0 means all chambers 
59     // The normal bin number ranges from 0-4.
60     // the abnormal bin number are as follow:
61     // 25 -> strange, 
62     // 35 ->undefined to be re-tested, 
63     // 45 -> bad calib0,
64     // 5-10 -> reading error on manu.
65     // cath: 0 -> bending, 1 -> non-bending
66     // author: Ch. Finck
67
68     static Int_t nBad    = 0;
69     static Int_t nBadS  = 0;
70     static Int_t nBadU  = 0;
71     static Int_t nDB     = 0;
72     static Int_t nManu = 0;
73     static Int_t badDE = 0;
74
75
76     // Load mapping
77     if ( !AliMpCDB::LoadMpSegmentation() ) {
78        printf("Could not access mapping from OCDB !\n");
79     }
80     
81     // Load DDL store
82     if ( !AliMpCDB::LoadDDLStore() ) {
83       printf("Could not access DDL Store from OCDB !\n");
84       exit(0);
85     }
86     AliMpDDLStore* ddlStore = AliMpDDLStore::Instance();
87                           
88     TString fileNameIn;
89     TExMap serialNbtoBinId;
90     TExMap serialNbtoGain;
91     TExMap serial;
92
93     char fileNameOut[255];
94     sprintf(fileNameOut,"serial_number_%d.dat",iCh);
95
96     char fileNameOut1[255];
97     sprintf(fileNameOut1,"serial_number_bad_%d.dat",iCh);
98
99
100     char histoFile[255];
101     TFile* fileOut = 0x0;
102
103     if (rootFile) {
104       sprintf(histoFile,"GainDE_%d.root",iCh);
105       fileOut = new TFile(histoFile,"RECREATE","DE manu gain");
106     }
107
108
109     fileNameIn  = AliMpFiles::SerialToBinFilePath();
110   
111     ifstream in(fileNameIn, ios::in);
112     if (!in) printf("file %s not found\n", fileNameIn.Data());
113
114     ofstream out(fileNameOut, ios::out);
115   
116     ofstream out1(fileNameOut1, ios::out);
117
118     char line[255];
119     Int_t binId          = 0;
120     Int_t manuSerial = 0;
121     Float_t gain        = 0;
122
123     // reading file
124     while ( in.getline(line,255) ) {
125
126       if ( line[0] == '#' ) continue;
127       if ( line[0] == '\n' ) continue;
128       if ( line[0] == '\0' ) continue;
129
130       TString tmp(AliMpHelper::Normalize(line));
131
132       TObjArray* stringList = tmp.Tokenize(TString(" "));
133
134       TString sSM   = ((TObjString*)stringList->At(2))->GetString();
135
136       TString sBin  = ((TObjString*)stringList->At(4))->GetString();
137
138       TString sGain  = ((TObjString*)stringList->At(6))->GetString();
139
140       manuSerial =  atoi(sSM.Data());
141       binId  =  atoi(sBin.Data()) + 1;
142
143       if (sBin.CompareTo("S") == 0) {
144         binId = kStrangeBin;
145       }
146
147       if (sBin.CompareTo("I") == 0) {
148         binId = kUndefBin;
149       }
150
151       if (sBin.CompareTo("C") == 0) {
152         binId = kBadCalBin;
153       }
154
155       gain  =  atof(sGain.Data());
156       //printf("%s %d   %s %d  %s %f\n", sSM.Data(), manuSerial, sBin.Data(),  binId, sGain.Data(), gain);
157
158       fflush(stdout);
159
160   
161       // to avoid that a manu that is at the same time in a normal bin and in a bin S, stays in bin S
162       if (serialNbtoBinId.GetValue(manuSerial)  != 0 && binId == kStrangeBin)  
163           continue;
164      
165
166       if (serialNbtoBinId.GetValue(manuSerial) != 0) {
167         serialNbtoBinId.Remove((Long_t)manuSerial);
168         serialNbtoGain.Remove((Long_t)manuSerial);
169       }
170
171
172       serialNbtoBinId.Add((Long_t)manuSerial, (Long_t)binId); 
173       serialNbtoGain.Add((Long_t)manuSerial, (Long_t)(gain*100000)); 
174       delete stringList;
175
176     }
177     in.close();
178
179 //  return;
180
181 // ///////////
182 // // iterates over DE
183   
184     char title[256];
185     char name[256];
186
187     AliMpDEIterator it;
188
189     out1 << "Bin:" << endl;
190     out1 << "  25 -> Strange, 35 -> re-tested, 45 -> bad calib0" << endl;
191     out1 << "  >= 5 error id, 10 -> 1" << endl;
192     out1 << "cath: 0 -> bending, 1 -> non-bending" << endl;
193
194
195     TH1F* hslat_manu_bp[1100];
196     TH1F* hslat_manu_nbp[1100];
197
198     TH1F* hCh_manu_bp[11];
199     TH1F* hCh_manu_nbp[11];
200
201     static TH1F* hAll_manu_bp;
202     static TH1F* hAll_manu_nbp;
203     static TH1F* hAll_manu;
204     static TH1F* hBinS;
205
206     TH2F* hBin_DE = 0x0;
207
208     if (rootFile) {
209
210         sprintf(name,"hAll_manu_bp");
211         sprintf(title,"MANU gain total for bending");
212         hAll_manu_bp = new TH1F(name,title,300,1.5,5.0);
213         hAll_manu_bp->SetDirectory(fileOut);
214
215         sprintf(name,"hAll_manu_npb");
216         sprintf(title,"MANU gain total for non-bending");
217         hAll_manu_nbp = new TH1F(name,title,300,1.5,5.0);
218         hAll_manu_nbp->SetDirectory(fileOut);
219
220         sprintf(name,"hAll_manu");
221         sprintf(title,"MANU gain total");
222         hAll_manu = new TH1F(name,title,300,1.5,5.0);
223         hAll_manu->SetDirectory(fileOut);
224
225         sprintf(name,"hBin_DE");
226         sprintf(title,"DE versus Bin");
227         hBin_DE = new TH2F(name,title,5,0,5, 1100, 1, 1100);
228         hBin_DE->SetDirectory(fileOut);
229
230         sprintf(name,"hBin_S");
231         sprintf(title,"Bin_S");
232         hBinS = new TH1F(name,title, 300, 600, 1100);
233         hBinS->SetDirectory(fileOut);
234     }
235
236     Int_t begCh;
237     Int_t endCh;
238
239     if (iCh == 0) {
240         begCh = 1;
241         endCh = 11;
242     } else {
243         begCh = iCh;
244         endCh = iCh+1;
245     }
246
247     for (iCh = begCh; iCh < endCh; ++iCh) {
248
249       if (rootFile) {
250
251         sprintf(name,"hCh_manu_bp_CH%d", iCh);
252         sprintf(title,"MANU gain total for CH%d bending", iCh);
253         hCh_manu_bp[iCh] = new TH1F(name,title,300,1.5,5.0);
254         hCh_manu_bp[iCh]->SetDirectory(fileOut);
255
256         sprintf(name,"hCh_manu_nbp_CH%d", iCh);
257         sprintf(title,"MANU gain total for CH%d non-bending", iCh);
258         hCh_manu_nbp[iCh] = new TH1F(name,title,300,1.5,5.0);
259         hCh_manu_nbp[iCh]->SetDirectory(fileOut);
260       }
261
262       for ( it.First(iCh-1); ! it.IsDone(); it.Next() ) {
263
264         Int_t flag = 0;
265         Int_t iDE = it.CurrentDE()->GetId();
266
267         AliMpDetElement* detElem =  AliMpDEManager::GetDetElement(iDE);
268         TString nameDE = detElem->GetDEName();
269
270         out << endl;
271         out << " DE: " << iDE << endl;
272         out << "manuId  serial  binId" << endl;
273
274         out1 << endl;
275         out1 << " DE: " << iDE << " name: " << nameDE.Data() << endl;
276         out1 << "manuId  serial  binId cath" << endl;
277
278         printf("\nDE:%d\n", iDE);
279
280         if (rootFile) {
281
282           sprintf(name,"hslat_manu_bp%d",iDE);
283           sprintf(title,"MANU gain for DE %d bending",iDE);
284           hslat_manu_bp[iDE] = new TH1F(name,title,300,1.5,4.5);
285           hslat_manu_bp[iDE]->SetDirectory(fileOut);
286
287           sprintf(name,"hslat_manu_nbp%d",iDE);
288           sprintf(title,"MANU gain for DE %d non-bending",iDE);
289           hslat_manu_nbp[iDE] = new TH1F(name,title,300,1.5,4.5);
290           hslat_manu_nbp[iDE]->SetDirectory(fileOut);
291         }
292
293         TList manuList;
294
295         for ( Int_t cath = 0; cath <=1 ; ++cath ) {
296           const AliMpVSegmentation* seg 
297               = AliMpSegmentation::Instance()->GetMpSegmentation(iDE,AliMp::GetCathodType(cath));
298         
299           TArrayI manus;
300
301           seg->GetAllElectronicCardIDs(manus);
302           
303           // filling
304           for ( Int_t im = 0; im < manus.GetSize(); ++im ) {
305             AliMpIntPair* manu = 0x0;
306             if (manus[im] > AliMpConstants::ManuMask(AliMp::kNonBendingPlane))
307                 manu = new AliMpIntPair(manus[im], 1, kTRUE);
308             else
309                 manu = new AliMpIntPair(manus[im], 0, kTRUE);
310
311             manuList.Add(manu);
312           }
313
314         }
315         manuList.Sort();
316    
317
318         // check manu serial
319         for (Int_t iEntry = 0; iEntry < manuList.GetEntries(); ++iEntry) {
320           AliMpIntPair* manuPtr = (AliMpIntPair*)manuList.At(iEntry);
321
322           AliMpDetElement* detElem =  ddlStore->GetDetElement(iDE);
323           manuSerial = detElem->GetManuSerialFromId(manuPtr->GetFirst());
324
325           binId =  (Int_t)serialNbtoBinId.GetValue(manuSerial);
326           if (manuSerial == 0) {
327             printf("manu %d not found in mapping\n",manuPtr->GetFirst()); 
328             continue;
329           }
330           if (manuSerial != 0)
331               nManu++;
332
333           if (!binId) {
334             printf("Bin for manu %d with serial %d not available\n",manuPtr->GetFirst(), manuSerial); 
335           }
336           if (binId > kBadBin && binId < kStrangeBin)
337               printf("Bin for manu %d with serial %d is bad\n",manuPtr->GetFirst(), manuSerial); 
338
339           if (binId == kStrangeBin)
340               printf("Bin for manu %d with serial %d is strange\n",manuPtr->GetFirst(), manuSerial); 
341
342           if (binId == kUndefBin)
343               printf("Bin for manu %d with serial %d is unidentified\n",manuPtr->GetFirst(), manuSerial); 
344
345           gain  =  (Float_t)(serialNbtoGain.GetValue(manuSerial)/100000.);
346
347           out  << setw(4) << ( manuPtr->GetFirst() & 0x3FF) << " " << setw(5) << manuSerial 
348                << " " << setw(2) << binId-1 << " " <<   manuPtr->GetSecond() 
349                << " " << setw(7) << gain << endl;
350
351           // bad manu
352           if ((binId == 0 || binId == kUndefBin) && manuSerial < kNotScanSerial) nBadU++;
353           if (binId == 0 && manuSerial >= kNotScanSerial) nDB++;
354
355           // check whether serial number appears twice
356           if (serial.GetValue(manuSerial) == 0) 
357             serial.Add((Long_t)manuSerial, (Long_t)binId);
358           else {
359             out1 << setw(4) << ( manuPtr->GetFirst() & 0x3FF) << " " << setw(5) << manuSerial 
360                    << " " << setw(2) << binId-1 << " " <<   manuPtr->GetSecond() 
361                    << " " << setw(7) << gain << " Serial number Twice" << endl;  
362           }
363
364           // count bad/strange manus
365           if (!binId || binId > kBadBin) {
366             if (binId == kStrangeBin) nBadS++;
367             if (binId > kBadBin) {
368               nBad++;
369               flag = 1;
370             }
371
372             out1 << setw(4) << ( manuPtr->GetFirst() & 0x3FF) << " " << setw(5) << manuSerial 
373                  << " " << setw(2) << binId-1 << " " <<   manuPtr->GetSecond() 
374                  << " " << setw(7) << gain << endl;
375           }
376
377           if (rootFile && binId == kStrangeBin) 
378               hBinS->Fill(iDE);
379
380           if (rootFile && binId != kStrangeBin) {
381             hAll_manu->Fill(gain);
382             hBin_DE->Fill(binId-1, iDE);
383
384             if(manuPtr->GetSecond()) {
385                 hslat_manu_nbp[iDE]->Fill(gain);
386                 hAll_manu_nbp->Fill(gain);
387                 hCh_manu_nbp[iCh]->Fill(gain);
388             } else {
389                 hslat_manu_bp[iDE]->Fill(gain);
390                 hAll_manu_bp->Fill(gain);
391                 hCh_manu_bp[iCh]->Fill(gain);
392             }
393         
394           }
395         } // manu
396
397         if (flag) badDE++;
398
399         manuList.Delete();
400       }// DE
401
402     } // ich
403
404     serialNbtoBinId.Delete();
405     serialNbtoGain.Delete();
406
407     if (rootFile) {
408       fileOut->Write();
409       fileOut->Close();
410     }
411     
412     out.close();
413     out1.close();
414
415     printf("\n");
416     printf("Number of bad manus %d and strange manus %d total %d\n", nBad, nBadS, nManu);
417     printf("Number of unidentified manus %d\n", nBadU);
418     printf("Number of manus not scanned %d\n", nDB);
419     printf("Number of bad DE %d\n", badDE);
420 }
421