I committed changes in setting of the magnetic field
[u/mrichter/AliRoot.git] / MUON / AliMUONPedestal.cxx
CommitLineData
5253c153 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"
f3cfa63e 23#include "AliMpConstants.h"
5253c153 24#include <TString.h>
5253c153 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
46ClassImp(AliMUONPedestal)
47/// \endcond
48
49//______________________________________________________________________________
50AliMUONPedestal::AliMUONPedestal()
51: TObject(),
f3cfa63e 52//fN(0),
5253c153 53fNEvents(0),
54fRunNumber(0),
55fNChannel(0),
56fNManu(0),
f3cfa63e 57fNManuConfig(0),
d9e6dc5e 58fConfig(1),
a54854fc 59fErrorBuspatchTable(new AliMUON2DMap(kFALSE)),
60fManuBuspatchTable(new AliMUON2DMap(kFALSE)),
1b15b395 61fManuBPoutofconfigTable(new AliMUON2DMap(kFALSE)),
5253c153 62fDate(new TTimeStamp()),
63fFilcout(0),
f3cfa63e 64fHistoFileName(),
a54854fc 65fPedestalStore(new AliMUON2DMap(kTRUE)),
f3cfa63e 66fIndex(-1),
67fPrefixDA()
5253c153 68{
69/// Default constructor
5253c153 70}
5253c153 71
72//______________________________________________________________________________
73AliMUONPedestal::~AliMUONPedestal()
74{
75/// Destructor
a54854fc 76 delete fErrorBuspatchTable;
77 delete fManuBuspatchTable;
dbbb2c64 78 delete fPedestalStore;
1b15b395 79 delete fManuBPoutofconfigTable;
5253c153 80}
81
1b15b395 82//______________________________________________________________________________
f3cfa63e 83const char*
84AliMUONPedestal::GetHistoFileName() const
85{
86 /// Return the name of file we use to store histograms
87 return fHistoFileName.Data();
88}
89
90//______________________________________________________________________________
91void AliMUONPedestal::LoadConfig(const char* dbfile)
1b15b395 92{
93 /// Load MUONTRK configuration from ascii file "dbfile" (in DetDB)
94
95 Int_t manuId;
96 Int_t busPatchId;
97
f3cfa63e 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())
1b15b395 114 {
f3cfa63e 115 filein >> busPatchId >> manuId;
1b15b395 116
117 AliMUONErrorCounter* manuCounter;
118 AliMUONVCalibParam* ped =
119 static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
120
121 if (!ped) {
f3cfa63e 122 fNManuConfig++;
1b15b395 123 fNChannel+=64;
f3cfa63e 124 ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.); // put default wise -1, not connected channel
1b15b395 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}
5253c153 136//______________________________________________________________________________
137void 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
1b15b395 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
f3cfa63e 154 ped = new AliMUONCalibParamND(2, AliMpConstants::ManuNofChannels(),busPatchId, manuId, -1.);
1b15b395 155 fPedestalStore ->Add(ped);
156 }
5253c153 157
158 // Initialization for the first value
1b15b395 159 if (ped->ValueAsDouble(channelId, 0) == -1)
160 {
161 if(fConfig && channelId == 0){fNManu++;}
162 ped->SetValueAsDouble(channelId, 0, 0.);
163 }
5253c153 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);
5431405e 171
5431405e 172 AliMUONErrorCounter* manuCounter;
a54854fc 173 if (!(manuCounter = static_cast<AliMUONErrorCounter*>(fManuBuspatchTable->FindObject(busPatchId,manuId))))
5431405e 174 {
175 // New (buspatch,manu)
176 manuCounter = new AliMUONErrorCounter(busPatchId,manuId);
a54854fc 177 fManuBuspatchTable->Add(manuCounter);
5431405e 178 }
179 else
180 {
181 // Existing buspatch
182 manuCounter->Increment();
183 }
5253c153 184}
5253c153 185//______________________________________________________________________________
dbbb2c64 186void AliMUONPedestal::Finalize()
5253c153 187{
f3cfa63e 188 /// final polishing of the store
189
5253c153 190 Double_t pedMean;
191 Double_t pedSigma;
5253c153 192 Int_t busPatchId;
193 Int_t manuId;
194 Int_t channelId;
195
5253c153 196 // print in logfile
197 if (fErrorBuspatchTable->GetSize())
5253c153 198 {
1b15b395 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 }
5253c153 208 }
5253c153 209
1b15b395 210 // iterator over pedestal
5253c153 211 TIter next(fPedestalStore ->CreateIterator());
212 AliMUONVCalibParam* ped;
213
214 while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
dbbb2c64 215 {
1b15b395 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 }
5431405e 234
1b15b395 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 }
5253c153 245
1b15b395 246 for (channelId = 0; channelId < ped->Size() ; ++channelId)
247 {
5431405e 248 pedMean = ped->ValueAsDouble(channelId, 0);
1b15b395 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 {
f3cfa63e 258 ped->SetValueAsDouble(channelId, 0, ADCMax());
259 ped->SetValueAsDouble(channelId, 1, ADCMax());
1b15b395 260 }
261 if(occupancy>1)
262 {
f3cfa63e 263 ped->SetValueAsDouble(channelId, 0, ADCMax());
264 ped->SetValueAsDouble(channelId, 1, ADCMax());
265 if(channelId==0)ped->SetValueAsDouble(channelId, 0, ADCMax()+occupancy);
1b15b395 266 }
267 }
268 else
269 {
f3cfa63e 270 ped->SetValueAsDouble(channelId, 0, ADCMax());
271 ped->SetValueAsDouble(channelId, 1, ADCMax());
1b15b395 272 }
273 }
dbbb2c64 274 }
dbbb2c64 275}
dbbb2c64 276//______________________________________________________________________________
277void AliMUONPedestal::MakeASCIIoutput(ostream& out) const
278{
1b15b395 279 /// put pedestal store in the output stream
dbbb2c64 280
281 out<<"//===========================================================================" << endl;
f3cfa63e 282 out<<"// Pedestal file calculated by "<< fPrefixDA.Data() << endl;
dbbb2c64 283 out<<"//===========================================================================" << endl;
284 out<<"// * Run : " << fRunNumber << endl;
285 out<<"// * Date : " << fDate->AsString("l") <<endl;
286 out<<"// * Statictics : " << fNEvents << endl;
1b15b395 287 if(fConfig)
f3cfa63e 288 out<<"// * # of MANUS : " << fNManuConfig << " read in the Det. config. " << endl;
1b15b395 289 out<<"// * # of MANUS : " << fNManu << " read in raw data " << endl;
290 out<<"// * # of MANUS : " << fNChannel/64 << " written in pedestal file " << endl;
dbbb2c64 291 out<<"// * # of channels : " << fNChannel << endl;
292 if (fErrorBuspatchTable->GetSize())
dbbb2c64 293 {
1b15b395 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
f3cfa63e 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)
1b15b395 309 {
f3cfa63e 310 TIter next(fPedestalStore ->CreateIterator());
311 AliMUONVCalibParam* ped;
312 while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
1b15b395 313 {
f3cfa63e 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
1b15b395 317
f3cfa63e 318 if(pedMean>ADCMax())
319 {
320 writitle++;
321 if(writitle==1){
322 out<<"//"<<endl;
323 out<<"// * Puzzling (Buspatch,Manu) read in raw data ?"<<endl;}
1ccd531d 324 occupancy=TMath::Nint(pedMean-ADCMax());
f3cfa63e 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 }
1b15b395 337 }
dbbb2c64 338 }
1b15b395 339
340
dbbb2c64 341 out<<"//"<<endl;
342 out<<"//---------------------------------------------------------------------------" << endl;
343 out<<"//---------------------------------------------------------------------------" << endl;
344 out<<"// BP MANU CH. MEAN SIGMA"<<endl;
345 out<<"//---------------------------------------------------------------------------" << endl;
5253c153 346
dbbb2c64 347 // iterator over pedestal
348 TIter next(fPedestalStore ->CreateIterator());
349 AliMUONVCalibParam* ped;
350
351 while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
dbbb2c64 352 {
1b15b395 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 }
5253c153 363 }
dbbb2c64 364}
365
366//______________________________________________________________________________
367void AliMUONPedestal::MakeControlHistos()
368{
f3cfa63e 369 /// Create control histograms
dbbb2c64 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
f3cfa63e 384 fHistoFileName=Form("%s.root",fPrefixDA.Data());
dbbb2c64 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");
f3cfa63e 391 Int_t nx = ADCMax()+1;
dbbb2c64 392 Int_t xmin = 0;
f3cfa63e 393 Int_t xmax = ADCMax();
dbbb2c64 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 }
5253c153 430 }
dbbb2c64 431
432 histoFile->Write();
433 histoFile->Close();
434
4af2c34a 435}