Adding new classes to be used in new MUON Tracking DA
[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"
23
24#include <TString.h>
25#include <THashTable.h>
26#include <TTimeStamp.h>
27#include <TMath.h>
28#include <TTree.h>
29#include <TFile.h>
30#include <TH1F.h>
31#include <Riostream.h>
32
33#include <sstream>
34
35//-----------------------------------------------------------------------------
36/// \class AliMUONPedestal
37///
38/// Implementation of the pedestal computing
39///
40/// add
41///
42///
43/// \author Alberto Baldisseri, JL Charvet (05/05/2009)
44//-----------------------------------------------------------------------------
45
46/// \cond CLASSIMP
47ClassImp(AliMUONPedestal)
48/// \endcond
49
50//______________________________________________________________________________
51AliMUONPedestal::AliMUONPedestal()
52: TObject(),
53fNEvents(0),
54fRunNumber(0),
55fNChannel(0),
56fNManu(0),
57fErrorBuspatchTable(new THashTable(100,2)),
58fDate(new TTimeStamp()),
59fFilcout(0),
60fPedestalStore(new AliMUON2DMap(kFALSE)),
61fIndex(-1)
62{
63/// Default constructor
64
65// sprintf(fOutFolder,".");
66 sprintf(fHistoFileName,"");
67 sprintf(fprefixDA,"");
68}
69// AliMUONPedestal& operator=(const AliMUONPedestal& other); Copy ctor
70
71//______________________________________________________________________________
72AliMUONPedestal::~AliMUONPedestal()
73{
74/// Destructor
75}
76
77//______________________________________________________________________________
78void AliMUONPedestal::MakePed(Int_t busPatchId, Int_t manuId, Int_t channelId, Int_t charge)
79{
80 /// Compute pedestals values
81 AliMUONVCalibParam* ped =
82 static_cast<AliMUONVCalibParam*>(fPedestalStore ->FindObject(busPatchId, manuId));
83
84 if (!ped) {
85 fNManu++;
86 ped = new AliMUONCalibParamND(2, kNChannels,busPatchId, manuId, -1.); // put default wise -1, not connected channel
87 fPedestalStore ->Add(ped);
88 }
89
90 // Initialization for the first value
91 if (ped->ValueAsDouble(channelId, 0) == -1) ped->SetValueAsDouble(channelId, 0, 0.);
92 if (ped->ValueAsDouble(channelId, 1) == -1) ped->SetValueAsDouble(channelId, 1, 0.);
93
94 Double_t pedMean = ped->ValueAsDouble(channelId, 0) + (Double_t) charge;
95 Double_t pedSigma = ped->ValueAsDouble(channelId, 1) + (Double_t) charge*charge;
96
97 ped->SetValueAsDouble(channelId, 0, pedMean);
98 ped->SetValueAsDouble(channelId, 1, pedSigma);
99}
100
101//______________________________________________________________________________
102TString AliMUONPedestal::WritePedHeader(void)
103{
104///
105
106 ostringstream stream;
107 stream<<"//===========================================================================" << endl;
108 stream<<"// Pedestal file calculated by MUONTRKda"<<endl;
109 stream<<"//===========================================================================" << endl;
110 stream<<"// * Run : " << fRunNumber << endl;
111 stream<<"// * Date : " << fDate->AsString("l") <<endl;
112 stream<<"// * Statictics : " << fNEvents << endl;
113 stream<<"// * # of MANUS : " << fNManu << endl;
114 stream<<"// * # of channels : " << fNChannel << endl;
115 if (fErrorBuspatchTable->GetSize())
116 {
117 stream<<"//"<<endl;
118 stream<<"// * Buspatches with less statistics (due to parity errors)"<<endl;
119 TIterator* iter = fErrorBuspatchTable->MakeIterator();
120 AliMUONErrorCounter* parityerror;
121 while((parityerror = (AliMUONErrorCounter*) iter->Next()))
122 {
123 stream<<"// bp "<<parityerror->BusPatch()<<" events used "<<fNEvents-parityerror->Events()<<endl;
124 }
125 }
126 stream<<"//"<<endl;
127 stream<<"//---------------------------------------------------------------------------" << endl;
128 stream<<"//---------------------------------------------------------------------------" << endl;
129 stream<<"// BP MANU CH. MEAN SIGMA"<<endl;
130 stream<<"//---------------------------------------------------------------------------" << endl;
131
132 return TString(stream.str().c_str());
133}
134
135//______________________________________________________________________________
136TString AliMUONPedestal::WritePedData(Int_t BP, Int_t Manu, Int_t ch, Double_t pedMean, Double_t pedSigma)
137{
138///
139
140 ostringstream stream("");
141 stream << "\t" << BP << "\t" << Manu <<"\t"<< ch << "\t"
142 << pedMean <<"\t"<< pedSigma << endl;
143 return TString(stream.str().c_str());
144
145}
146
147//______________________________________________________________________________
148void AliMUONPedestal::MakePedStore(TString shuttleFile_1 = "")
149{
150
151 /// Store pedestals in ASCII files
152 Double_t pedMean;
153 Double_t pedSigma;
154 ofstream fileout;
155#ifdef ALI_AMORE
156 ostringstream stringout; // String to be sent to AMORE_DB
157#endif
158 TString tempstring;
159 Int_t busPatchId;
160 Int_t manuId;
161 Int_t channelId;
162
163// histo
164 TFile* histoFile = 0;
165 TTree* tree = 0;
166 TH1F* pedMeanHisto = 0;
167 TH1F* pedSigmaHisto = 0;
168
169 if (fIndex<0) // Pedestal run (fIndex=-1)
170 {
171 sprintf(fHistoFileName,"%s_%d.root",fprefixDA,fRunNumber);
172 histoFile = new TFile(fHistoFileName,"RECREATE","MUON Tracking pedestals");
173
174 Char_t name[255];
175 Char_t title[255];
176 sprintf(name,"pedmean_allch");
177 sprintf(title,"Pedestal mean all channels");
178 Int_t nx = 4096;
179 Int_t xmin = 0;
180 Int_t xmax = 4095;
181 pedMeanHisto = new TH1F(name,title,nx,xmin,xmax);
182 pedMeanHisto->SetDirectory(histoFile);
183
184 sprintf(name,"pedsigma_allch");
185 sprintf(title,"Pedestal sigma all channels");
186 nx = 201;
187 xmin = 0;
188 xmax = 200;
189 pedSigmaHisto = new TH1F(name,title,nx,xmin,xmax);
190 pedSigmaHisto->SetDirectory(histoFile);
191
192 tree = new TTree("t","Pedestal tree");
193 tree->Branch("bp",&busPatchId,"bp/I");
194 tree->Branch("manu",&manuId,",manu/I");
195 tree->Branch("channel",&channelId,",channel/I");
196 tree->Branch("pedMean",&pedMean,",pedMean/D");
197 tree->Branch("pedSigma",&pedSigma,",pedSigma/D");
198 }
199
200 if (!shuttleFile_1.IsNull()) {
201 fileout.open(shuttleFile_1.Data());
202 tempstring = WritePedHeader();
203 fileout << tempstring;
204#ifdef ALI_AMORE
205 stringout << tempstring;
206#endif
207 }
208 // print in logfile
209 if (fErrorBuspatchTable->GetSize())
210 {
211 cout<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
212 (*fFilcout)<<"\n* Buspatches with less statistics (due to parity errors)"<<endl;
213 TIterator* iter = fErrorBuspatchTable->MakeIterator();
214 AliMUONErrorCounter* parityerror;
215 while((parityerror = (AliMUONErrorCounter*) iter->Next()))
216 {
217 cout<<" bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
218 (*fFilcout)<<" bp "<<parityerror->BusPatch()<<": events used = "<<fNEvents-parityerror->Events()<<endl;
219 }
220
221 }
222
223
224// iterator over pedestal
225 TIter next(fPedestalStore ->CreateIterator());
226 AliMUONVCalibParam* ped;
227
228 while ( ( ped = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
229 {
230 busPatchId = ped->ID0();
231 manuId = ped->ID1();
232 Int_t eventCounter;
233
234 // Correct the number of events for buspatch with errors
235 char bpname[256];
236 AliMUONErrorCounter* errorCounter;
237 sprintf(bpname,"bp%d",busPatchId);
238 if ((errorCounter = (AliMUONErrorCounter*)fErrorBuspatchTable->FindObject(bpname)))
239 {
240 eventCounter = fNEvents - errorCounter->Events();
241 }
242 else
243 {
244 eventCounter = fNEvents;
245 }
246
247 for (channelId = 0; channelId < ped->Size() ; ++channelId) {
248 pedMean = ped->ValueAsDouble(channelId, 0);
249
250 if (pedMean > 0) { // connected channels
251
252 ped->SetValueAsDouble(channelId, 0, pedMean/(Double_t)eventCounter);
253
254 pedMean = ped->ValueAsDouble(channelId, 0);
255 pedSigma = ped->ValueAsDouble(channelId, 1);
256
257 ped->SetValueAsDouble(channelId, 1, TMath::Sqrt(TMath::Abs(pedSigma/(Double_t)eventCounter - pedMean*pedMean)));
258
259 pedMean = ped->ValueAsDouble(channelId, 0);
260 pedSigma = ped->ValueAsDouble(channelId, 1);
261
262
263 if (!shuttleFile_1.IsNull()) {
264 tempstring = WritePedData(busPatchId,manuId,channelId,pedMean,pedSigma);
265 fileout << tempstring;
266#ifdef ALI_AMORE
267 stringout << tempstring;
268#endif
269 }
270 if(fIndex<0) // Pedestal Run
271 {
272 pedMeanHisto->Fill(pedMean);
273 pedSigmaHisto->Fill(pedSigma);
274 tree->Fill();
275 }
276 }
277 }
278 }
279
280// file outputs
281 if (!shuttleFile_1.IsNull()) fileout.close();
282
283// Outputs to root file and AMORE DB
284#ifdef ALI_AMORE
285 //
286 //Send objects to the AMORE DB
287 //
288 const char *role=gSystem->Getenv("AMORE_DA_NAME");
289 if ( role ){
290 amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
291// TObjString peddata(stringout.str().c_str());
292 TObjString peddata(stringout.str().c_str());
293 Int_t status =0;
294 status = amoreDA.Send("Pedestals",&peddata);
295 if ( status )
296 cout << "Warning: Failed to write Pedestals in the AMORE database : " << status << endl;
297 }
298 else {
299 cout << "Warning: environment variable 'AMORE_DA_NAME' not set. Cannot write to the AMORE database" << endl;
300 }
301#endif
302 if(fIndex<0) // Pedestal Run
303 {
304 histoFile->Write();
305 histoFile->Close();
306 delete fPedestalStore ;
307 }
308
309}