79fcc7d07adebffe04c263e42e1fdc0145d463a6
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestal.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
18 #include "AliMUONPedestal.h"
19 #include "AliMUONErrorCounter.h"
20 #include "AliMUONVStore.h"
21 #include "AliMUON2DMap.h"
22 #include "AliMUONCalibParamND.h"
23 #include "AliMpConstants.h"
24 #include <TString.h>
25 #include <TTimeStamp.h>
26 #include <TMath.h>
27 #include <TTree.h>
28 #include <TFile.h>
29 #include <TH1F.h>
30 #include <Riostream.h>
31
32 #include <sstream>
33
34 //-----------------------------------------------------------------------------
35 /// \class AliMUONPedestal
36 ///
37 /// Implementation of the pedestal computing
38 ///
39 /// add
40 /// 
41 ///
42 /// \author Alberto Baldisseri, JL Charvet (05/05/2009)
43 //-----------------------------------------------------------------------------
44
45 /// \cond CLASSIMP
46 ClassImp(AliMUONPedestal)
47 /// \endcond
48
49 //______________________________________________________________________________
50 AliMUONPedestal::AliMUONPedestal()
51 : TObject(),
52 //fN(0),
53 fNEvents(0),
54 fRunNumber(0),
55 fNChannel(0),
56 fNManu(0),
57 fNManuConfig(0),
58 fConfig(1),
59 fErrorBuspatchTable(new AliMUON2DMap(kFALSE)),
60 fManuBuspatchTable(new AliMUON2DMap(kFALSE)),
61 fManuBPoutofconfigTable(new AliMUON2DMap(kFALSE)),
62 fDate(new TTimeStamp()),
63 fFilcout(0),
64 fHistoFileName(),
65 fPedestalStore(new AliMUON2DMap(kTRUE)),
66 fIndex(-1),
67 fPrefixDA()
68 {
69 /// Default constructor
70 }
71
72 //______________________________________________________________________________
73 AliMUONPedestal::~AliMUONPedestal()
74 {
75 /// Destructor
76   delete fErrorBuspatchTable;
77   delete fManuBuspatchTable;
78   delete fPedestalStore;
79   delete fManuBPoutofconfigTable;
80 }
81
82 //______________________________________________________________________________
83 const char* 
84 AliMUONPedestal::GetHistoFileName() const
85 {
86   /// Return the name of file we use to store histograms
87   return fHistoFileName.Data();
88 }
89
90 //______________________________________________________________________________
91 void AliMUONPedestal::LoadConfig(const char* dbfile)
92 {
93   /// Load MUONTRK configuration from ascii file "dbfile" (in DetDB)
94
95   Int_t manuId;
96   Int_t busPatchId;
97
98   ifstream filein(dbfile,ios::in);
99
100   // check if the 1st caracter of the 1st line is # (Config read from the OCDB => OffLine)
101   string line; 
102   getline(filein, line, '\n');
103   cout << " line 1: " << line ;
104   if ( int(line[0]) == 35 )  // ascii code of # character
105     {
106       cout << " ==>  1st caracter = " << line[0] << " (ascii code =" << int(line[0]) << ")" << endl;    
107     }
108   else  
109     { filein.clear();  filein.seekg(0);  // rewind
110       cout << " ==> rewind configuration file: "<< dbfile << endl;           
111     } 
112   
113   while (!filein.eof())
114     { 
115       filein >> busPatchId >> manuId;
116
117       AliMUONErrorCounter* manuCounter;
118       AliMUONVCalibParam* ped = 
119         static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
120
121       if (!ped) {
122         fNManuConfig++;
123         fNChannel+=64;
124   ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.); // put default wise -1, not connected channel
125         fPedestalStore ->Add(ped);  
126
127         if (!(manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId))))
128           {
129             // New (buspatch,manu)
130             manuCounter = new AliMUONErrorCounter(busPatchId,manuId);
131             fManuBuspatchTable->Add(manuCounter);
132           }
133       }
134     }
135
136 //______________________________________________________________________________
137 void AliMUONPedestal::MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, Int_t charge)
138 {
139   /// Compute pedestals values
140   AliMUONVCalibParam* ped = 
141     static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
142
143   if (!ped)   
144     {
145       if(fConfig) 
146         {  // Fill out_of_config (buspatch,manu) table
147           if (!(static_cast<AliMUONErrorCounter*>(fManuBPoutofconfigTable->FindObject(busPatchId,manuId))))
148             fManuBPoutofconfigTable->Add(new AliMUONErrorCounter(busPatchId,manuId));
149           cout << " !!! WARNING  : busPatchId = " << busPatchId << " manuId = " << manuId << " not in the Detector configuration " << endl;
150         }
151       else {fNManu++;}
152       fNChannel+=64;
153       // put default wise -1, not connected channel
154       ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.); 
155       fPedestalStore ->Add(ped);  
156     }
157
158   // Initialization for the first value
159   if (ped->ValueAsDouble(channelId, 0) == -1)  
160     { 
161       if(fConfig && channelId == 0){fNManu++;}
162       ped->SetValueAsDouble(channelId, 0, 0.);
163     }
164   if (ped->ValueAsDouble(channelId, 1) == -1) ped->SetValueAsDouble(channelId, 1, 0.);
165
166   Double_t pedMean  = ped->ValueAsDouble(channelId, 0) + (Double_t) charge;
167   Double_t pedSigma = ped->ValueAsDouble(channelId, 1) + (Double_t) charge*charge;
168
169   ped->SetValueAsDouble(channelId, 0, pedMean);
170   ped->SetValueAsDouble(channelId, 1, pedSigma);
171
172   AliMUONErrorCounter* manuCounter;
173   if (!(manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId))))
174     {
175       // New (buspatch,manu)
176       manuCounter = new AliMUONErrorCounter(busPatchId,manuId);
177       fManuBuspatchTable->Add(manuCounter);
178     }
179   else
180     {
181       // Existing buspatch
182       manuCounter->Increment();
183     }   
184 }
185 //______________________________________________________________________________
186 void AliMUONPedestal::Finalize()
187 {
188   /// final polishing of the store
189   
190   Double_t pedMean;
191   Double_t pedSigma;
192   Int_t busPatchId;
193   Int_t manuId;
194   Int_t channelId;
195
196   // print in logfile
197   if (fErrorBuspatchTable->GetSize())
198     {
199       cout<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
200       (*fFilcout)<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
201       TIter nextParityError(fErrorBuspatchTable->CreateIterator());
202       AliMUONErrorCounter* parityerror;
203       while((parityerror = static_cast<AliMUONErrorCounter*>(nextParityError())))
204         {
205           cout<<"  bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
206           (*fFilcout)<<"  bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
207         }
208     }
209
210   // iterator over pedestal
211   TIter next(fPedestalStore ->CreateIterator());
212   AliMUONVCalibParam* ped;
213
214   while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
215     {
216       busPatchId              = ped->ID0();
217       manuId                  = ped->ID1();
218       if(manuId==0)
219         {
220           cout << " !!! BIG WARNING: ManuId = " << manuId << " !!! in  BP = " << busPatchId << endl;
221           (*fFilcout) << " !!! BIG WARNING: ManuId = " << manuId << " !!! in  BP = " << busPatchId << endl;
222         }
223       Int_t eventCounter;
224       // Correct the number of events for buspatch with errors
225       AliMUONErrorCounter* errorCounter;
226       if ((errorCounter = (AliMUONErrorCounter*)fErrorBuspatchTable->FindObject(busPatchId)))
227         {
228           eventCounter = fNEvents - errorCounter->Events();
229         }
230       else
231         {
232           eventCounter = fNEvents;
233         }
234
235       Int_t occupancy=0; // channel missing in raw data or read but rejected (case of parity error)
236       // value of (buspatch, manu) occupancy
237       AliMUONErrorCounter* manuCounter;
238       manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId));
239       if(eventCounter>0)occupancy = manuCounter->Events()/64/eventCounter;
240       if(occupancy>1)
241         {
242           cout << " !!! BIG WARNING: ManuId = " << manuId << " !!! in  BP = " << busPatchId << " occupancy (>1) = " << occupancy << endl;
243           (*fFilcout) << " !!! BIG WARNING: ManuId = " << manuId << " !!! in  BP = " << busPatchId << " occupancy (>1) = " << occupancy <<endl;
244         }
245
246       for (channelId = 0; channelId < ped->Size() ; ++channelId) 
247         {
248           pedMean  = ped->ValueAsDouble(channelId, 0);
249
250           if (pedMean > 0) // connected channels
251             {
252               ped->SetValueAsDouble(channelId, 0, pedMean/(Double_t)eventCounter);
253               pedMean  = ped->ValueAsDouble(channelId, 0);
254               pedSigma = ped->ValueAsDouble(channelId, 1);
255               ped->SetValueAsDouble(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Double_t)eventCounter - pedMean*pedMean)));
256               if(manuId == 0)
257                 {
258                   ped->SetValueAsDouble(channelId, 0, ADCMax());
259                   ped->SetValueAsDouble(channelId, 1, ADCMax());
260                 }
261               if(occupancy>1)
262                 {
263                   ped->SetValueAsDouble(channelId, 0, ADCMax());
264                   ped->SetValueAsDouble(channelId, 1, ADCMax());
265                   if(channelId==0)ped->SetValueAsDouble(channelId, 0, ADCMax()+occupancy);
266                 }
267             }
268           else
269             {
270               ped->SetValueAsDouble(channelId, 0, ADCMax());
271               ped->SetValueAsDouble(channelId, 1, ADCMax());
272             }
273         }
274     }
275 }
276 //______________________________________________________________________________
277 void AliMUONPedestal::MakeASCIIoutput(ostream& out) const
278 {
279   /// put pedestal store in the output stream
280
281   out<<"//===========================================================================" << endl;
282   out<<"//                 Pedestal file calculated by "<< fPrefixDA.Data() << endl;
283   out<<"//===========================================================================" << endl;
284   out<<"//       * Run           : " << fRunNumber << endl; 
285   out<<"//       * Date          : " << fDate->AsString("l") <<endl;
286   out<<"//       * Statictics    : " << fNEvents << endl;
287   if(fConfig)
288     out<<"//       * # of MANUS    : " << fNManuConfig << " read in the Det. config. " << endl;
289   out<<"//       * # of MANUS    : " << fNManu << " read in raw data " << endl;
290   out<<"//       * # of MANUS    : " << fNChannel/64 << " written in pedestal file " << endl;
291   out<<"//       * # of channels : " << fNChannel << endl;
292   if (fErrorBuspatchTable->GetSize())
293     {
294       out<<"//"<<endl;
295       out<<"//    * Buspatches with less statistics (due to parity errors)"<<endl;
296       TIter next(fErrorBuspatchTable->CreateIterator());
297       AliMUONErrorCounter* parityerror;
298       while((parityerror = static_cast<AliMUONErrorCounter*>(next())))
299         {
300           out<<"//      BusPatch = "<<parityerror->BusPatch()<<"\t Nevents used = "<<fNEvents-parityerror->Events()<<endl;
301         }
302     }  
303
304 //   out<<"//"<<endl;
305 //   out<<"//    * Puzzling (Buspatch,Manu) read in raw data ?"<<endl;
306   Int_t writitle=0;
307   Int_t occupancy=1;
308   if(occupancy)
309     {
310       TIter next(fPedestalStore ->CreateIterator());
311       AliMUONVCalibParam* ped;
312       while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
313         {
314           Int_t busPatchId = ped->ID0();
315           Int_t manuId = ped->ID1();
316           Double_t pedMean  = ped->ValueAsDouble(0, 0); // check pedestal value for channelId=0
317
318           if(pedMean>ADCMax()) 
319             {
320               writitle++;
321               if(writitle==1){ 
322                 out<<"//"<<endl;
323                 out<<"//    * Puzzling (Buspatch,Manu) read in raw data ?"<<endl;}
324               occupancy=TMath::Nint(pedMean-ADCMax());
325               ped->SetValueAsDouble(0, 0, ADCMax());
326               out<<"//      BusPatch = "<< busPatchId <<"\t ManuId =  "<< manuId << "\t occupancy = " << occupancy  <<endl;
327             }
328
329           if (manuId==0 || (fConfig && static_cast<AliMUONErrorCounter*>(fManuBPoutofconfigTable->FindObject(busPatchId,manuId))))
330             {
331               writitle++;
332               if(writitle==1){ 
333                 out<<"//"<<endl;
334                 out<<"//    * Puzzling (Buspatch,Manu) read in raw data ?"<<endl;}
335               out<<"//      BusPatch = "<< busPatchId <<"\t ManuId =  "<< manuId << "\t missing in the mapping" << endl;
336             }
337         }
338     }
339
340
341   out<<"//"<<endl;
342   out<<"//---------------------------------------------------------------------------" << endl;
343   out<<"//---------------------------------------------------------------------------" << endl;
344   out<<"//      BP     MANU     CH.      MEAN    SIGMA"<<endl;
345   out<<"//---------------------------------------------------------------------------" << endl;
346
347   // iterator over pedestal
348   TIter next(fPedestalStore ->CreateIterator());
349   AliMUONVCalibParam* ped;
350   
351   while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
352     {
353       Int_t busPatchId = ped->ID0();
354       Int_t manuId = ped->ID1();
355
356       for ( Int_t channelId = 0; channelId < ped->Size(); ++channelId ) 
357         {
358           Double_t pedMean  = ped->ValueAsDouble(channelId, 0);
359           Double_t pedSigma = ped->ValueAsDouble(channelId, 1);
360
361           out << "\t" << busPatchId << "\t" << manuId <<"\t"<< channelId << "\t" << pedMean <<"\t"<< pedSigma << endl;
362         }
363     }
364 }
365
366 //______________________________________________________________________________
367 void AliMUONPedestal::MakeControlHistos()
368 {
369   /// Create control histograms
370   if (fIndex>=0) return; // Pedestal run (fIndex=-1)
371
372   Double_t pedMean;
373   Double_t pedSigma;
374   Int_t busPatchId;
375   Int_t manuId;
376   Int_t channelId;
377
378 // histo
379   TFile*  histoFile = 0;
380   TTree* tree = 0;
381   TH1F* pedMeanHisto = 0;
382   TH1F* pedSigmaHisto = 0;
383     
384   fHistoFileName=Form("%s.root",fPrefixDA.Data());
385   histoFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking pedestals");
386
387   Char_t name[255];
388   Char_t title[255];
389   sprintf(name,"pedmean_allch");
390   sprintf(title,"Pedestal mean all channels");
391   Int_t nx = ADCMax()+1;
392   Int_t xmin = 0;
393   Int_t xmax = ADCMax(); 
394   pedMeanHisto = new TH1F(name,title,nx,xmin,xmax);
395   pedMeanHisto->SetDirectory(histoFile);
396
397   sprintf(name,"pedsigma_allch");
398   sprintf(title,"Pedestal sigma all channels");
399   nx = 201;
400   xmin = 0;
401   xmax = 200; 
402   pedSigmaHisto = new TH1F(name,title,nx,xmin,xmax);
403   pedSigmaHisto->SetDirectory(histoFile);
404
405   tree = new TTree("t","Pedestal tree");
406   tree->Branch("bp",&busPatchId,"bp/I");
407   tree->Branch("manu",&manuId,",manu/I");
408   tree->Branch("channel",&channelId,",channel/I");
409   tree->Branch("pedMean",&pedMean,",pedMean/D");
410   tree->Branch("pedSigma",&pedSigma,",pedSigma/D");
411
412   // iterator over pedestal
413   TIter next(fPedestalStore ->CreateIterator());
414   AliMUONVCalibParam* ped;
415   
416   while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
417   {
418     busPatchId = ped->ID0();
419     manuId = ped->ID1();
420     
421     for ( channelId = 0; channelId < ped->Size(); ++channelId ) 
422     {
423       pedMean  = ped->ValueAsDouble(channelId, 0);
424       pedSigma = ped->ValueAsDouble(channelId, 1);
425           
426       pedMeanHisto->Fill(pedMean);
427       pedSigmaHisto->Fill(pedSigma);
428       tree->Fill();  
429     }
430   }
431     
432   histoFile->Write();  
433   histoFile->Close(); 
434
435 }