]> git.uio.no Git - u/mrichter/AliRoot.git/blame - ITS/AliITSHandleDaSSD.cxx
Bug in number of layers.
[u/mrichter/AliRoot.git] / ITS / AliITSHandleDaSSD.cxx
CommitLineData
223dda26 1/**************************************************************************
2 * Copyright(c) 2007-2009, 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
8bcdc40d 16/* $Id$ */
223dda26 17
18///////////////////////////////////////////////////////////////////////////////
19///
20/// This class provides ITS SSD data handling
21/// used by DA.
2e2c6def 22// Author: Oleksandr Borysov
61a57d07 23// Date: 18/07/2008
223dda26 24///////////////////////////////////////////////////////////////////////////////
25
be4f467e 26#include <string>
27#include <fstream>
61a57d07 28#include <new>
223dda26 29#include <Riostream.h>
f67db810 30#include "AliITSHandleDaSSD.h"
223dda26 31#include <math.h>
c4d90345 32#include <limits.h>
223dda26 33#include "event.h"
34#include "TFile.h"
c4d90345 35#include "TString.h"
292c304e 36#include "TMath.h"
c4d90345 37#include "AliLog.h"
88128115 38#include "AliITSNoiseSSDv2.h"
39#include "AliITSPedestalSSDv2.h"
40#include "AliITSBadChannelsSSDv2.h"
c4d90345 41#include "AliITSRawStreamSSD.h"
223dda26 42#include "AliRawReaderDate.h"
371588bb 43#include "AliITSRawStreamSSD.h"
223dda26 44#include "AliITSChannelDaSSD.h"
f67db810 45
46
47ClassImp(AliITSHandleDaSSD)
48
49using namespace std;
50
51
c4d90345 52const Int_t AliITSHandleDaSSD::fgkNumberOfSSDModules = 1698; // Number of SSD modules in ITS
2e2c6def 53const Int_t AliITSHandleDaSSD::fgkNumberOfSSDModulesPerDdl = 108; // Number of SSD modules in DDL
54const Int_t AliITSHandleDaSSD::fgkNumberOfSSDModulesPerSlot = 12; // Number of SSD modules in Slot
61a57d07 55const Short_t AliITSHandleDaSSD::fgkMinSSDModuleId = 500; // Initial SSD modules number
be4f467e 56const Int_t AliITSHandleDaSSD::fgkNumberOfSSDSlotsPerDDL = 9; // Number of SSD slots per DDL
6e7691a5 57const Int_t AliITSHandleDaSSD::fgkNumberOfSSDDDLs = 16; // Number of SSD modules in Slot
c4d90345 58const Float_t AliITSHandleDaSSD::fgkPedestalThresholdFactor = 3.0; // Defalt value for fPedestalThresholdFactor
59const Float_t AliITSHandleDaSSD::fgkCmThresholdFactor = 3.0; // Defalt value for fCmThresholdFactor
60
be4f467e 61const UInt_t AliITSHandleDaSSD::fgkZsBitMask = 0x0000003F; // Bit mask for FEROM ZS
62const UInt_t AliITSHandleDaSSD::fgkOffSetBitMask = 0x000003FF; // Bit mask for FEROM Offset correction
63const UInt_t AliITSHandleDaSSD::fgkBadChannelMask = 0x00000001; // Mask to suppress the channel from the bad channel list
64const Int_t AliITSHandleDaSSD::fgkAdcPerDBlock = 6; // FEROM configuration file constant
65
c4d90345 66//______________________________________________________________________________
f67db810 67AliITSHandleDaSSD::AliITSHandleDaSSD() :
c4d90345 68 fRawDataFileName(NULL),
f67db810 69 fNumberOfModules(0),
70 fModules(NULL),
c4d90345 71 fModIndProcessed(0),
72 fModIndRead(0),
2e2c6def 73 fModIndex(NULL),
61a57d07 74 fEqIndex(0),
c4d90345 75 fNumberOfEvents(0),
61a57d07 76 fBadChannelsList(NULL),
6e7691a5 77 fDDLModuleMap(NULL),
61a57d07 78 fALaddersOff(0),
79 fCLaddersOff(0),
f67db810 80 fLdcId(0),
c4d90345 81 fRunId(0),
82 fPedestalThresholdFactor(fgkPedestalThresholdFactor),
be4f467e 83 fCmThresholdFactor(fgkCmThresholdFactor),
84 fZsDefault(-1),
85 fOffsetDefault(INT_MAX),
61a57d07 86 fZsMinimum(2),
87 fMergeBCLists(1),
be4f467e 88 fZsFactor(3.0)
f67db810 89{
223dda26 90// Default constructor
f67db810 91}
92
93
c4d90345 94//______________________________________________________________________________
95AliITSHandleDaSSD::AliITSHandleDaSSD(Char_t *rdfname) :
96 fRawDataFileName(NULL),
f67db810 97 fNumberOfModules(0),
98 fModules(NULL),
c4d90345 99 fModIndProcessed(0),
100 fModIndRead(0),
2e2c6def 101 fModIndex(NULL),
61a57d07 102 fEqIndex(0),
c4d90345 103 fNumberOfEvents(0),
61a57d07 104 fBadChannelsList(NULL),
6e7691a5 105 fDDLModuleMap(NULL),
61a57d07 106 fALaddersOff(0),
107 fCLaddersOff(0),
f67db810 108 fLdcId(0),
c4d90345 109 fRunId(0),
110 fPedestalThresholdFactor(fgkPedestalThresholdFactor) ,
be4f467e 111 fCmThresholdFactor(fgkCmThresholdFactor),
112 fZsDefault(-1),
113 fOffsetDefault(INT_MAX),
61a57d07 114 fZsMinimum(2),
115 fMergeBCLists(1),
be4f467e 116 fZsFactor(3.0)
f67db810 117{
c4d90345 118 if (!Init(rdfname)) AliError("AliITSHandleDaSSD::AliITSHandleDaSSD() initialization error!");
f67db810 119}
120
121
c4d90345 122//______________________________________________________________________________
f67db810 123AliITSHandleDaSSD::AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc) :
124 TObject(ssdadldc),
c4d90345 125 fRawDataFileName(ssdadldc.fRawDataFileName),
f67db810 126 fNumberOfModules(ssdadldc.fNumberOfModules),
a69c8ba0 127 fModules(NULL),
c4d90345 128 fModIndProcessed(ssdadldc.fModIndProcessed),
129 fModIndRead(ssdadldc.fModIndRead),
2e2c6def 130 fModIndex(NULL),
61a57d07 131 fEqIndex(0),
c4d90345 132 fNumberOfEvents(ssdadldc.fNumberOfEvents),
61a57d07 133 fBadChannelsList(NULL),
134 fDDLModuleMap(NULL),
135 fALaddersOff(ssdadldc.fALaddersOff),
136 fCLaddersOff(ssdadldc.fCLaddersOff),
f67db810 137 fLdcId(ssdadldc.fLdcId),
c4d90345 138 fRunId(ssdadldc.fRunId),
139 fPedestalThresholdFactor(ssdadldc.fPedestalThresholdFactor),
be4f467e 140 fCmThresholdFactor(ssdadldc.fCmThresholdFactor),
141 fZsDefault(ssdadldc.fZsDefault),
142 fOffsetDefault(ssdadldc.fOffsetDefault),
61a57d07 143 fZsMinimum(ssdadldc.fZsMinimum),
144 fMergeBCLists(ssdadldc.fMergeBCLists),
be4f467e 145 fZsFactor(ssdadldc.fZsFactor)
f67db810 146{
147 // copy constructor
a69c8ba0 148 if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
149 fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
150 if (fModules) {
151 for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
152 if (ssdadldc.fModules[modind]) {
153 fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
154 if (!fModules[modind]) {
155 AliError("AliITSHandleDaSSD: Error copy constructor");
156 for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
157 delete [] fModules;
158 fModules = NULL;
159 break;
160 }
161 } else fModules[modind] = NULL;
162 }
163 } else {
164 AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
165 fNumberOfModules = 0;
166 fModules = NULL;
167 }
168 }
61a57d07 169 if (ssdadldc.fBadChannelsList) AliWarning("fBadChannelsList is not copied by copy constructor, use other methods to init it!");
170 if (ssdadldc.fDDLModuleMap) AliWarning("fDDLModuleMap is not copied by copy constructor, use other methods to init it!");
f67db810 171}
172
173
c4d90345 174//______________________________________________________________________________
f67db810 175AliITSHandleDaSSD& AliITSHandleDaSSD::operator = (const AliITSHandleDaSSD& ssdadldc)
176{
177// assignment operator
a69c8ba0 178 if (this == &ssdadldc) return *this;
61a57d07 179 TObject::operator=(ssdadldc);
a69c8ba0 180 if (fModules) {
181 for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
182 delete [] fModules;
183 fModules = NULL;
184 }
2e2c6def 185 if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
a69c8ba0 186 if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
187 fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
188 if (fModules) {
189 for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
190 if (ssdadldc.fModules[modind]) {
61a57d07 191 fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
192 if (!fModules[modind]) {
193 AliError("AliITSHandleDaSSD: Error assignment operator");
a69c8ba0 194 for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
61a57d07 195 delete [] fModules;
196 fModules = NULL;
197 break;
198 }
199 } else fModules[modind] = NULL;
a69c8ba0 200 }
201 } else {
202 AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
203 fNumberOfModules = 0;
204 fModules = NULL;
205 }
206 }
61a57d07 207 fRawDataFileName = NULL;
208 fModIndProcessed = 0;
209 fModIndRead = 0;
210 fModIndex = NULL;
211 fEqIndex = ssdadldc.fEqIndex;
212 fNumberOfEvents = ssdadldc.fNumberOfEvents;
213 fLdcId = ssdadldc.fLdcId;
214 fRunId = ssdadldc.fRunId;
215 fPedestalThresholdFactor = ssdadldc.fPedestalThresholdFactor;
216 fCmThresholdFactor = ssdadldc.fCmThresholdFactor;
217 fZsDefault = ssdadldc.fZsDefault;
218 fOffsetDefault = ssdadldc.fOffsetDefault;
219 fZsMinimum = ssdadldc.fZsMinimum;
220 fMergeBCLists = ssdadldc.fMergeBCLists;
221 fZsFactor = ssdadldc.fZsFactor;
222 fALaddersOff = ssdadldc.fALaddersOff;
223 fCLaddersOff = ssdadldc.fCLaddersOff;
224 fBadChannelsList = NULL;
225 fDDLModuleMap = NULL;
226 fModIndex = NULL;
227 if (ssdadldc.fBadChannelsList) AliWarning("fBadChannelsList is not copied by assignment operator, use other methods to init it!");
228 if (ssdadldc.fDDLModuleMap) AliWarning("fDDLModuleMap is not copied by assignment operator, use other methods to init it!");
f67db810 229 return *this;
230}
231
232
c4d90345 233//______________________________________________________________________________
f67db810 234AliITSHandleDaSSD::~AliITSHandleDaSSD()
223dda26 235{
236// Default destructor
f67db810 237 if (fModules)
238 {
239 for (Int_t i = 0; i < fNumberOfModules; i++)
240 {
241 if (fModules[i]) delete fModules[i];
242 }
243 delete [] fModules;
244 }
2e2c6def 245 if (fModIndex) delete [] fModIndex;
61a57d07 246 if (fBadChannelsList) delete fBadChannelsList;
6e7691a5 247 if (fDDLModuleMap) delete [] fDDLModuleMap;
f67db810 248}
249
250
251
c4d90345 252//______________________________________________________________________________
253void AliITSHandleDaSSD::Reset()
254{
255// Delete array of AliITSModuleDaSSD* objects.
256 if (fModules) {
257 for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
258 delete [] fModules;
259 fModules = NULL;
2e2c6def 260 }
261 if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
6e7691a5 262/*
61a57d07 263 if (fBadChannelsList) {
264 delete fBadChannelsList;
265 fBadChannelsList = NULL;
6e7691a5 266 }
267 if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL; }
268*/
61a57d07 269 fALaddersOff.Set(0);
270 fCLaddersOff.Set(0);
c4d90345 271 fRawDataFileName = NULL;
272 fModIndProcessed = fModIndRead = 0;
273 fNumberOfEvents = 0;
274 fLdcId = fRunId = 0;
275 fPedestalThresholdFactor = fgkPedestalThresholdFactor;
276 fCmThresholdFactor = fgkCmThresholdFactor;
277}
278
279
280
281//______________________________________________________________________________
be4f467e 282Bool_t AliITSHandleDaSSD::Init(Char_t *rdfname)
c4d90345 283{
284// Read raw data file and set initial and configuration parameters
285 Long_t physeventind = 0, strn = 0, nofstripsev, nofstrips = 0;
286 Int_t nofeqipmentev, nofeqipment = 0, eqn = 0;
287 AliRawReaderDate *rawreaderdate = NULL;
288 UChar_t *data = NULL;
289 Long_t datasize = 0, eqbelsize = 1;
290
c4d90345 291 rawreaderdate = new AliRawReaderDate(rdfname, 0);
292 if (!(rawreaderdate->GetAttributes() || rawreaderdate->GetEventId())) {
293 AliError(Form("AliITSHandleDaSSD: Error reading raw data file %s by RawReaderDate", rdfname));
294 MakeZombie();
295 return kFALSE;
371588bb 296 }
c4d90345 297 if (rawreaderdate->NextEvent()) {
c4d90345 298 fRunId = rawreaderdate->GetRunNumber();
299 rawreaderdate->RewindEvents();
300 } else { MakeZombie(); return kFALSE; }
301 if (fModules) Reset();
61a57d07 302 //rawreaderdate->SelectEvents(-1);
371588bb 303 rawreaderdate->Select("ITSSSD");
c4d90345 304 nofstrips = 0;
305 while (rawreaderdate->NextEvent()) {
306 if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
307 nofstripsev = 0;
308 nofeqipmentev = 0;
309 while (rawreaderdate->ReadNextData(data)) {
2e2c6def 310 fLdcId = rawreaderdate->GetLDCId();
c4d90345 311 nofeqipmentev += 1;
312 datasize = rawreaderdate->GetDataSize();
313 eqbelsize = rawreaderdate->GetEquipmentElementSize();
371588bb 314 if ( datasize % eqbelsize ) {
a5a317a9 315 AliError(Form("AliITSHandleDaSSD: Error Init(%s): event data size %ld is not an integer of equipment data size %ld",
c4d90345 316 rdfname, datasize, eqbelsize));
317 MakeZombie();
be4f467e 318 return kFALSE;
c4d90345 319 }
371588bb 320 nofstripsev += (Int_t) (datasize / eqbelsize);
c4d90345 321 }
322 if (physeventind++) {
a5a317a9 323 if (nofstrips != nofstripsev) AliWarning(Form("AliITSHandleDaSSD: number of strips varies from event to event, ev = %ld, %ld",
371588bb 324 physeventind, nofstripsev));
325 if (nofeqipment != nofeqipmentev) AliWarning("AliITSHandleDaSSD: number of DDLs varies from event to event");
c4d90345 326 }
327 nofstrips = nofstripsev;
328 nofeqipment = nofeqipmentev;
371588bb 329 if (strn < nofstrips) strn = nofstrips;
c4d90345 330 if (eqn < nofeqipment) eqn = nofeqipment;
331 }
332 delete rawreaderdate;
333 if ((physeventind > 0) && (strn > 0))
334 {
335 fNumberOfEvents = physeventind;
336 fRawDataFileName = rdfname;
61a57d07 337 fEqIndex.Set(eqn);
338 fEqIndex.Reset(-1);
2e2c6def 339 fModIndex = new (nothrow) Int_t [fgkNumberOfSSDModulesPerDdl * eqn];
340 if (fModIndex)
341 for (Int_t i = 0; i < fgkNumberOfSSDModulesPerDdl * eqn; i++) fModIndex[i] = -1;
342 else AliWarning(Form("AliITSHandleDaSSD: Error Init(%s): Index array for %i modules was not created",
343 rdfname, fgkNumberOfSSDModulesPerDdl * eqn));
371588bb 344 if (SetNumberOfModules(fgkNumberOfSSDModulesPerDdl * eqn)) {
a5a317a9 345 TString str = TString::Format("Max number of equipment: %d, max number of channels: %ld\n", eqn, strn);
c4d90345 346 DumpInitData(str.Data());
347 return kTRUE;
348 }
349 }
350 MakeZombie();
351 return kFALSE;
352}
353
354
c4d90345 355
356//______________________________________________________________________________
f67db810 357AliITSModuleDaSSD* AliITSHandleDaSSD::GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
358{
223dda26 359// Retrieve AliITSModuleDaSSD object from the array
f67db810 360 if (!fModules) return NULL;
361 for (Int_t i = 0; i < fNumberOfModules; i++) {
362 if (fModules[i]) {
363 if ( (fModules[i]->GetDdlId() == ddlID)
364 && (fModules[i]->GetAD() == ad)
365 && (fModules[i]->GetADC() == adc))
366 return fModules[i];
367 }
368 }
369 return NULL;
370}
371
372
c4d90345 373Int_t AliITSHandleDaSSD::GetModuleIndex (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
374{
375// Retrieve the position of AliITSModuleDaSSD object in the array
376 if (!fModules) return -1;
377 for (Int_t i = 0; i < fNumberOfModules; i++) {
378 if (fModules[i]) {
379 if ( (fModules[i]->GetDdlId() == ddlID)
380 && (fModules[i]->GetAD() == ad)
381 && (fModules[i]->GetADC() == adc))
382 return i;
383 }
384 }
385 return -1;
386}
387
388
f67db810 389
c4d90345 390//______________________________________________________________________________
f67db810 391AliITSChannelDaSSD* AliITSHandleDaSSD::GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const
392{
223dda26 393// Retrieve AliITSChannalDaSSD object from the array
f67db810 394 for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
395 if ( (fModules[modind]->GetDdlId() == ddlID)
396 && (fModules[modind]->GetAD() == ad)
397 && (fModules[modind]->GetADC() == adc) )
398 {
399 return fModules[modind]->GetStrip(stripID);
400 }
401 }
c4d90345 402 AliError(Form("AliITSHandleDaSSD: Error GetStrip (%i, %i, %i, %i), strip not found, returns NULL!",
403 ddlID, ad, adc, stripID));
f67db810 404 return NULL;
405}
406
407
408
c4d90345 409//______________________________________________________________________________
f67db810 410Bool_t AliITSHandleDaSSD::SetModule(AliITSModuleDaSSD *const module, const Int_t index)
411{
223dda26 412// Assign array element with AliITSModuleDaSSD object
f67db810 413 if ((index < fNumberOfModules) && (index >= 0))
414 {
415 if (fModules[index]) delete fModules[index];
416 fModules[index] = module;
417 return kTRUE;
418 }
419 else return kFALSE;
420}
421
422
c4d90345 423
424//______________________________________________________________________________
f67db810 425Bool_t AliITSHandleDaSSD::SetNumberOfModules (const Int_t numberofmodules)
426{
223dda26 427// Allocates memory for AliITSModuleDaSSD objects
428 if (numberofmodules > fgkNumberOfSSDModules)
c4d90345 429 AliWarning(Form("AliITSHandleDaSSD: the number of modules %i you use exceeds the maximum %i for ALICE ITS SSD",
430 numberofmodules, fgkNumberOfSSDModules));
431 if (fModules) {
432 for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
433 delete [] fModules;
434 fModules = NULL;
435 }
223dda26 436 fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
437 if (fModules) {
438 fNumberOfModules = numberofmodules;
439 memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
440 return kTRUE;
441 } else {
c4d90345 442 AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules));
f67db810 443 fNumberOfModules = 0;
444 fModules = NULL;
223dda26 445 }
f67db810 446 return kFALSE;
447}
448
449
c4d90345 450
6e7691a5 451//______________________________________________________________________________
452Bool_t AliITSHandleDaSSD::ReadStaticBadChannelsMap(const Char_t *filename)
453{
454// Reads Static Bad Channels Map from the file
455 TFile *bcfile;
456 if (!filename) {
457 AliWarning("No file name is specified for Static Bad Channels Map!");
458 return kFALSE;
459 }
460 bcfile = new TFile(filename, "READ");
461 if (bcfile->IsZombie()) {
462 AliWarning(Form("Error reading file %s with Static Bad Channels Map!", filename));
463 return kFALSE;
464 }
88128115 465 bcfile->GetObject("AliITSBadChannelsSSDv2;1", fBadChannelsList);
61a57d07 466 if (!fBadChannelsList) {
467 AliWarning("Error fBadChannelsList == NULL!");
6e7691a5 468 bcfile->Close();
469 delete bcfile;
470 return kFALSE;
471 }
472 bcfile->Close();
473 delete bcfile;
474 return kTRUE;
475}
476
477
478
479Bool_t AliITSHandleDaSSD::ReadDDLModuleMap(const Char_t *filename)
480{
481// Reads the SSD DDL Map from the file
482 ifstream ddlmfile;
483 AliRawReaderDate *rwr = NULL;
484 AliITSRawStreamSSD *rsm = NULL;
485 void *event = NULL;
486 if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
487 fDDLModuleMap = new (nothrow) Int_t [fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl];
488 if (!fDDLModuleMap) {
489 AliWarning("Error allocation memory for DDL Map!");
490 return kFALSE;
491 }
492 if (!filename) {
493 AliWarning("No file name is specified for SSD DDL Map, using the one from AliITSRawStreamSSD!");
494 rwr = new AliRawReaderDate(event);
495 rsm = new AliITSRawStreamSSD(rwr);
496 rsm->Setv11HybridDDLMapping();
497 for (Int_t ddli = 0; ddli < fgkNumberOfSSDDDLs; ddli++)
498 for (Int_t mi = 0; mi < fgkNumberOfSSDModulesPerDdl; mi++)
499 fDDLModuleMap[(ddli * fgkNumberOfSSDModulesPerDdl + mi)] = rsm->GetModuleNumber(ddli, mi);
500 if (rsm) delete rsm;
501 if (rwr) delete rwr;
502 return kTRUE;
503 }
504 ddlmfile.open(filename, ios::in);
505 if (!ddlmfile.is_open()) {
506 AliWarning(Form("Error reading file %s with SSD DDL Map!", filename));
507 if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
508 return kFALSE;
509 }
510 Int_t ind = 0;
511 while((!ddlmfile.eof()) && (ind < (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))) {
512 ddlmfile >> fDDLModuleMap[ind++];
a5a317a9 513 if (ddlmfile.fail()) AliError(Form("Error extracting number from the DDL map file %s, ind: %d ", filename, ind));
6e7691a5 514 }
515 if (ind != (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))
516 AliWarning(Form("Only %i (< %i) entries were read from DDL Map!", ind, (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl)));
517 ddlmfile.close();
518 return kTRUE;
519}
520
521
522
c4d90345 523//______________________________________________________________________________
371588bb 524Int_t AliITSHandleDaSSD::ReadCalibrationDataFile (char* fileName, const Long_t eventsnumber)
f67db810 525{
223dda26 526// Reads raw data from file
c4d90345 527 if (!Init(fileName)){
528 AliError("AliITSHandleDaSSD: Error ReadCalibrationDataFile");
f67db810 529 return kFALSE;
530 }
371588bb 531 fNumberOfEvents = eventsnumber;
532 return ReadModuleRawData (fNumberOfModules);
f67db810 533}
534
535
c4d90345 536
537//______________________________________________________________________________
538Int_t AliITSHandleDaSSD::ReadModuleRawData (const Int_t modulesnumber)
539{
540// Reads raw data from file
371588bb 541 AliRawReader *rawreaderdate = NULL;
542 AliITSRawStreamSSD *stream = NULL;
c4d90345 543 AliITSModuleDaSSD *module;
544 AliITSChannelDaSSD *strip;
371588bb 545 Long_t eventind = 0;
61a57d07 546 Int_t nofeqipment, eqind;
547 Short_t equipid, prequipid;
548 Short_t modind;
c4d90345 549 if (!(rawreaderdate = new AliRawReaderDate(fRawDataFileName, 0))) return 0;
550 if (!fModules) {
551 AliError("AliITSHandleDaSSD: Error ReadModuleRawData: no structure was allocated for data");
552 return 0;
553 }
6e7691a5 554 if (!fDDLModuleMap) if (!ReadDDLModuleMap()) AliWarning("DDL map is not defined, ModuleID will be set to 0!");
371588bb 555 stream = new AliITSRawStreamSSD(rawreaderdate);
556 stream->Setv11HybridDDLMapping();
61a57d07 557 //rawreaderdate->SelectEvents(-1);
558 rawreaderdate->Select("ITSSSD");
c4d90345 559 modind = 0;
61a57d07 560 nofeqipment = 0;
c4d90345 561 while (rawreaderdate->NextEvent()) {
562 if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
371588bb 563 prequipid = -1;
61a57d07 564 eqind = -1;
371588bb 565 while (stream->Next()) {
61a57d07 566 equipid = rawreaderdate->GetEquipmentId();
567 if ((equipid != prequipid)) {
568 if ((eqind = GetEqIndex(equipid)) < 0) { fEqIndex.AddAt(equipid, nofeqipment); eqind = nofeqipment++; }
569 prequipid = equipid;
570 }
371588bb 571 Int_t equiptype = rawreaderdate->GetEquipmentType();
572 UChar_t ddlID = (UChar_t)rawreaderdate->GetDDLID();
573 UChar_t ad = stream->GetAD();
574 UChar_t adc = stream->GetADC();
575 UShort_t stripID = stream->GetSideFlag() ? AliITSChannelDaSSD::GetMaxStripIdConst() - stream->GetStrip() : stream->GetStrip();
576 Short_t signal = stream->GetSignal();
577
61a57d07 578 Int_t indpos = (eqind * fgkNumberOfSSDModulesPerDdl)
371588bb 579 + ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
580 Int_t modpos = fModIndex[indpos];
581 if (((modpos > 0) && (modpos < fModIndRead)) || ((modpos < 0) && (modind == modulesnumber))) continue;
582 if ((modpos < 0) && (modind < modulesnumber)) {
583 module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
6e7691a5 584 Int_t mddli;
585 if (fDDLModuleMap) mddli = RetrieveModuleId(ddlID, ad, adc);
586 else mddli = 0;
61a57d07 587 if (!module->SetModuleIdData (ddlID, ad, adc, mddli)) return 0;
588 module->SetModuleRorcId (equipid, equiptype);
61a57d07 589 modpos = fModIndRead + modind;
590 modind += 1;
591 fModules[modpos] = module;
592 fModIndex[indpos] = modpos;
371588bb 593 }
594 if (stripID < AliITSModuleDaSSD::GetStripsPerModuleConst()) {
c4d90345 595 if (!(strip = fModules[modpos]->GetStrip(stripID))) {
596 strip = new AliITSChannelDaSSD(stripID, fNumberOfEvents);
597 fModules[modpos]->SetStrip(strip, stripID);
598 }
599 strip->SetSignal(eventind, signal);
59087bde 600 } else {
601 if (!(fModules[modpos]->GetCMFerom())) {
602 fModules[modpos]->AllocateCMFeromArray();
603 fModules[modpos]->SetCMFeromEventsNumber(fNumberOfEvents);
604 }
605 fModules[modpos]->SetCMFerom(signal, (stripID - AliITSModuleDaSSD::GetStripsPerModuleConst()), eventind);
606 }
c4d90345 607 }
608 if (++eventind > fNumberOfEvents) break;
609 }
371588bb 610 delete stream;
c4d90345 611 delete rawreaderdate;
612 if (modind) cout << "The memory was allocated for " << modind << " modules." << endl;
613 fModIndRead += modind;
614 if (modind < modulesnumber) RelocateModules();
615 return modind;
616}
617
618
619
620//______________________________________________________________________________
f67db810 621Bool_t AliITSHandleDaSSD::RelocateModules()
223dda26 622{
623// Relocate memory for AliITSModuleDaSSD object array
f67db810 624 Int_t nm = 0;
223dda26 625 AliITSModuleDaSSD **marray = NULL;
f67db810 626 for (Int_t modind = 0; modind < fNumberOfModules; modind++)
627 if (fModules[modind]) nm += 1;
628 if (nm == fNumberOfModules) return kTRUE;
223dda26 629 marray = new (nothrow) AliITSModuleDaSSD* [nm];
630 if (!marray) {
c4d90345 631 AliError(Form("AliITSHandleDaSSD: Error relocating memory for %i AliITSModuleDaSSD* objects!", nm));
f67db810 632 return kFALSE;
633 }
634 nm = 0;
635 for (Int_t modind = 0; modind < fNumberOfModules; modind++)
636 if (fModules[modind]) marray[nm++] = fModules[modind];
637 delete [] fModules;
638 fModules = marray;
c4d90345 639 fNumberOfModules = fModIndRead = nm;
f67db810 640 return kTRUE;
641}
642
643
c4d90345 644
371588bb 645//______________________________________________________________________________
292c304e 646Bool_t AliITSHandleDaSSD::AddFeromCm(const AliITSModuleDaSSD *const module)
371588bb 647{
292c304e 648// Restore the original signal value adding CM calculated and subtracted in ferom
371588bb 649 AliITSChannelDaSSD *strip;
650 Short_t *signal, *cmferom;
651
652 if (!module) return kFALSE;
59087bde 653 if (!module->GetCMFerom()) return kTRUE;
371588bb 654 for (Int_t chipind = 0; chipind < AliITSModuleDaSSD::GetChipsPerModuleConst(); chipind++) {
655 if (!(cmferom = module->GetCMFerom(chipind))) {
656 AliWarning(Form("AliITSHandleDaSSD: There is no Ferom CM values for chip %i, module %i!", chipind, module->GetModuleId()));
657 continue;
658 }
659 for (Int_t strind = (chipind * AliITSModuleDaSSD::GetStripsPerChip());
660 strind < ((chipind + 1) * AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
661 if (!(strip = module->GetStrip(strind))) continue;
662 if (!(signal = strip->GetSignal())) continue;
663// if (strip->GetEventsNumber() != module->GetEventsNumber()) return kFALSE;
664 Long_t ovev = 0;
665 for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
666 if (SignalOutOfRange(signal[ev]) || SignalOutOfRange(cmferom[ev])) ovev += 1;
667 else {
668 Short_t signal1 = signal[ev] + cmferom[ev];
669 strip->SetSignal(ev, signal1);
61a57d07 670 }
371588bb 671 }
672 }
673 }
674 return kTRUE;
675}
676
677
678
c4d90345 679//______________________________________________________________________________
292c304e 680Bool_t AliITSHandleDaSSD::CalculatePedestal(const AliITSModuleDaSSD *const module)
f67db810 681{
223dda26 682// Calculates Pedestal
f67db810 683 AliITSChannelDaSSD *strip;
d86972f6 684 Double_t pedestal, noise;
c4d90345 685 Short_t *signal;
686 Long_t ovev, ev, n;
687 if (!module) return kFALSE;
688 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
689 if (!(strip = module->GetStrip(strind))) continue;
690 if (!(signal = strip->GetSignal())) {
691 AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
692 module->GetModuleId(), strind));
693 continue;
f67db810 694 }
c4d90345 695//************* pedestal first pass ****************
d86972f6 696 pedestal = 0.0L;
c4d90345 697 ovev = 0l;
698 for (ev = 0; ev < strip->GetEventsNumber(); ev++)
699 if (SignalOutOfRange(signal[ev])) ovev += 1;
d86972f6 700 else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1) : signal[ev];
c4d90345 701 if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
d86972f6 702 strip->SetPedestal(static_cast<Float_t>(pedestal));
c4d90345 703//************* noise *******************************
704 Double_t nsum = 0.0L;
705 ovev = 0l;
706 for (ev = 0; ev < strip->GetEventsNumber(); ev++) {
707 if (SignalOutOfRange(signal[ev])) ovev += 1;
708 else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
709 }
d86972f6 710 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = sqrt(nsum / (Double_t)(n));
c4d90345 711 else noise = AliITSChannelDaSSD::GetUndefinedValue();
d86972f6 712 strip->SetNoise(static_cast<Float_t>(noise));
c4d90345 713//************* pedestal second pass ****************
d86972f6 714 pedestal = 0.0L;
c4d90345 715 ovev = 0l;
716 for (ev = 0; ev < strip->GetEventsNumber(); ev++)
717 if ( SignalOutOfRange(signal[ev])
718 || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
d86972f6 719 else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1) : signal[ev];
c4d90345 720 if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
d86972f6 721 strip->SetPedestal(static_cast<Float_t>(pedestal));
c4d90345 722 strip->SetOverflowNumber(ovev);
f67db810 723 }
724 return kTRUE;
725}
726
727
c4d90345 728//______________________________________________________________________________
292c304e 729Bool_t AliITSHandleDaSSD::CalculateNoise(const AliITSModuleDaSSD *const module)
f67db810 730{
223dda26 731// Calculates Noise
f67db810 732 AliITSChannelDaSSD *strip;
733 Short_t *signal;
734 Float_t noise;
735 Long_t ovev, n;
c4d90345 736 if (!module) return kFALSE;
737 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
738 if (!(strip = module->GetStrip(strind))) continue;
739 if (!(signal = strip->GetSignal())) {
740 strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
741 AliError(Form("AliITSHandleDaSSD: Error CalculateNoise(): there are no events data for module[%i] strip[%i]->GetSignal()",
742 module->GetModuleId(), strind));
743 continue;
f67db810 744 }
c4d90345 745 Double_t nsum = 0.0L;
746 ovev = 0l;
747 for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
748 if (SignalOutOfRange(signal[ev])) ovev += 1;
749 else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
750 }
d86972f6 751 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = static_cast<Float_t>(sqrt(nsum / (Float_t)(n)));
c4d90345 752 else noise = AliITSChannelDaSSD::GetUndefinedValue();
753 strip->SetNoise(noise);
f67db810 754 }
755 return kTRUE;
756}
757
758
759
c4d90345 760//______________________________________________________________________________
761Bool_t AliITSHandleDaSSD::CalculateNoiseCM(AliITSModuleDaSSD *const module)
f67db810 762{
c4d90345 763// Calculates Noise with CM correction
764 AliITSChannelDaSSD *strip = NULL;
765 Short_t *signal;
766 Float_t noise;
767 Long_t ovev, n;
768 if (!CalculateCM(module)) {
769 AliError("Error: AliITSHandleDaSSD::CalculateCM() returned kFALSE");
770 return kFALSE;
771 }
772 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
773 if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
774 if (!(signal = strip->GetSignal())) {
775 strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
d86972f6 776 AliError(Form("AliITSHandleDaSSD: Error CalculateNoiseCM(): there are no events data for module[%i] strip[%i]->GetSignal()",
c4d90345 777 module->GetModuleId(), strind));
2e2c6def 778 continue; //return kFALSE;
f67db810 779 }
c4d90345 780 Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
781 Double_t nsum = 0.0L;
782 ovev = 0l;
783 for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
784 if (SignalOutOfRange(signal[ev])) ovev += 1;
785 else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
786 }
d86972f6 787 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = static_cast<Float_t>(sqrt(nsum / (Double_t)(n)));
c4d90345 788 else noise = AliITSChannelDaSSD::GetUndefinedValue();
789 strip->SetNoiseCM(noise);
790 }
2e2c6def 791 return kTRUE;
f67db810 792}
793
794
795
c4d90345 796//______________________________________________________________________________
797Bool_t AliITSHandleDaSSD::CalculateCM(AliITSModuleDaSSD *const module)
f67db810 798{
c4d90345 799// Calculates CM
f67db810 800 AliITSChannelDaSSD *strip = NULL;
c4d90345 801 Short_t *signal;
802 Long_t ovstr, n;
803 Int_t stripind;
804 module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
805 for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
806 stripind = chipind * module->GetStripsPerChip();
371588bb 807 module->GetCM()[chipind].Set(fNumberOfEvents);
808 module->GetCM()[chipind].Reset(0.0f);
c4d90345 809 for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
810 // calculate firs approximation of CM.
811 Double_t cm0 = 0.0L;
812 ovstr = 0l;
813 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
814 if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
815 if (!(signal = strip->GetSignal())) {
816 AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()",
817 module->GetModuleId(), strind));
818 return kFALSE;
819 }
61a57d07 820 if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
c4d90345 821 else cm0 += (signal[ev] - strip->GetPedestal());
f67db810 822 }
c4d90345 823 if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cm0 /= (Float_t)(n);
824 else { module->SetCM(0.0f, chipind, ev); continue; }
825 // calculate avarage (cm - (signal - pedestal)) over the chip
826 Double_t cmsigma = 0.0L;
827 ovstr = 0l;
828 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
829 if (!(strip = module->GetStrip(strind))) continue;
830 if (!(signal = strip->GetSignal())) {
831 AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()\n",
832 module->GetModuleId(), strind));
833 return kFALSE;
f67db810 834 }
61a57d07 835 if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
c4d90345 836 else cmsigma += pow((cm0 - (signal[ev] - strip->GetPedestal())), 2);
f67db810 837 }
c4d90345 838 if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsigma = sqrt(cmsigma / (Float_t)(n));
839 else { module->SetCM(0.0f, chipind, ev); continue; }
840 // calculate cm with threshold
841 Double_t cmsum = 0.0L;
842 ovstr = 0l;
843 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
844 if (!(strip = module->GetStrip(strind))) continue;
845 signal = strip->GetSignal();
61a57d07 846 if ( (SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())
847 || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
c4d90345 848 else cmsum += (signal[ev] - strip->GetPedestal());
849 }
850 if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Float_t)(n);
851 else cmsum = 0.0L;
59087bde 852 if (!(module->SetCM(cmsum, chipind, ev)))
a5a317a9 853 AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
59087bde 854 module->GetModuleId(), chipind, ev));
c4d90345 855 }
856 }
857 return kTRUE;
858}
859
860
861//______________________________________________________________________________
d86972f6 862Bool_t AliITSHandleDaSSD::ProcessRawData(const Int_t nmread, const Bool_t usewelford)
c4d90345 863{
864// Performs calculation of calibration parameters (pedestal, noise, ...)
865 Int_t nm = 0;
866 if (nmread <= 0) return kFALSE;
867 if (!fModules) return kFALSE;
868 while ((nm = ReadModuleRawData(nmread)) > 0) {
869 cout << "Processing next " << nm << " modules;" << endl;
870 for (Int_t modind = fModIndProcessed; modind < fModIndRead; modind++) {
871 if (!fModules[modind]) {
872 AliError(Form("AliITSHandleDaSSD: Error ProcessRawData(): No AliITSModuleDaSSD object with index %i is allocated in AliITSHandleDaSSD\n",
873 modind));
874 return kFALSE;
f67db810 875 }
371588bb 876 AddFeromCm(fModules[modind]);
d86972f6 877 if (usewelford) {
878 CalculatePedNoiseW(fModules[modind]);
879 CalculateNoiseCMW(fModules[modind]);
880 } else {
881 CalculatePedestal(fModules[modind]);
882 CalculateNoise(fModules[modind]);
883 CalculateNoiseCM(fModules[modind]);
884 }
f67db810 885 }
c4d90345 886 DeleteSignal();
371588bb 887 DeleteCM();
888 DeleteCMFerom();
c4d90345 889 fModIndProcessed = fModIndRead;
890 cout << fModIndProcessed << " - done" << endl;
d86972f6 891 if (nm < nmread ) break;
c4d90345 892 }
893 return kTRUE;
f67db810 894}
895
896
c4d90345 897//______________________________________________________________________________
898Short_t AliITSHandleDaSSD::RetrieveModuleId(const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
899{
900// Retrieve ModuleId from the DDL map which is defined in AliITSRawStreamSSD class
61a57d07 901 if (!fDDLModuleMap) {
902 AliError("Error DDLMap is not initialized, return 0!");
903 return 0;
904 }
6e7691a5 905 Int_t mddli = ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
906 if ((ddlID < fgkNumberOfSSDDDLs) && (mddli < fgkNumberOfSSDModulesPerDdl)) {
907 mddli = fDDLModuleMap[ddlID * fgkNumberOfSSDModulesPerDdl + mddli];
908 }
c4d90345 909 else {
6e7691a5 910 AliWarning(Form("Module index = %d or ddlID = %d is out of range 0 is rturned", ddlID, mddli));
911 mddli = 0;
c4d90345 912 }
6e7691a5 913 if (mddli > SHRT_MAX) return SHRT_MAX;
914 else return (Short_t)mddli;
c4d90345 915}
916
f67db810 917
c4d90345 918
919//______________________________________________________________________________
88128115 920AliITSNoiseSSDv2* AliITSHandleDaSSD::GetCalibrationOCDBNoise() const
223dda26 921{
922// Fill in the array for OCDB
88128115 923 AliITSNoiseSSDv2 *ldcn = NULL;
61a57d07 924 AliITSModuleDaSSD *module = NULL;
925 AliITSChannelDaSSD *strip = NULL;
926 if (!fModules) return NULL;
88128115 927 ldcn = new AliITSNoiseSSDv2;
61a57d07 928 if (!ldcn) {
88128115 929 AliError("Error allocation mamory for AliITSBadChannelsSSDv2 object, return NULL!");
61a57d07 930 return NULL;
931 }
932 for (Int_t i = 0; i < fNumberOfModules; i++) {
933 if (!(module = fModules[i])) continue;
934 if (module->GetModuleId() < fgkMinSSDModuleId) continue;
935 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
936 if (!(strip = module->GetStrip(strind))) continue;
937 Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
938 if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() )
939 ldcn->AddNoiseP(modid, strip->GetStripId(), strip->GetNoiseCM());
940 else
941 ldcn->AddNoiseN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetNoiseCM());
942 }
943 }
944 return ldcn;
945}
946
947
948//______________________________________________________________________________
88128115 949AliITSBadChannelsSSDv2* AliITSHandleDaSSD::GetCalibrationBadChannels() const
61a57d07 950{
951// Fill in the TObjArray with the list of bad channels
88128115 952 AliITSBadChannelsSSDv2 *ldcbc = NULL;
61a57d07 953 AliITSModuleDaSSD *module = NULL;
954 AliITSChannelDaSSD *strip = NULL;
f67db810 955 if (!fModules) return NULL;
88128115 956 ldcbc = new AliITSBadChannelsSSDv2;
61a57d07 957 if (!ldcbc) {
88128115 958 AliError("Error allocation mamory for AliITSBadChannelsSSDv2 object, return NULL!");
61a57d07 959 return NULL;
960 }
f67db810 961 for (Int_t i = 0; i < fNumberOfModules; i++) {
61a57d07 962 if (!(module = fModules[i])) continue;
963 if (module->GetModuleId() < fgkMinSSDModuleId) continue;
964 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
965 if (!(strip = module->GetStrip(strind))) continue;
966 Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
967 if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() )
d86972f6 968 ldcbc->AddBadChannelP(modid, strip->GetStripId(), EvaluateIfChannelIsBad(module, strip->GetStripId()));
61a57d07 969 else
970 ldcbc->AddBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()),
d86972f6 971 EvaluateIfChannelIsBad(module, strip->GetStripId()));
f67db810 972 }
f67db810 973 }
61a57d07 974 return ldcbc;
f67db810 975}
976
977
61a57d07 978
c4d90345 979//______________________________________________________________________________
61a57d07 980Bool_t AliITSHandleDaSSD::SaveCalibrationSSDLDC(Char_t*& dafname)
f67db810 981{
223dda26 982// Save Calibration data locally
88128115 983 AliITSBadChannelsSSDv2 *ldcbc = NULL;
984 AliITSPedestalSSDv2 *ldcp = NULL;
985 AliITSNoiseSSDv2 *ldcn = NULL;
61a57d07 986 AliITSModuleDaSSD *module = NULL;
987 AliITSChannelDaSSD *strip = NULL;
c4d90345 988 Char_t *tmpfname;
989 TString dadatafilename("");
f67db810 990 if (!fModules) return kFALSE;
88128115 991 ldcn = new AliITSNoiseSSDv2;
992 ldcp = new AliITSPedestalSSDv2;
993 ldcbc = new AliITSBadChannelsSSDv2;
61a57d07 994 if ((!ldcn) || (!ldcp) || (!ldcp)) {
995 AliError("Error allocation mamory for calibration objects, return kFALSE!");
996 return kFALSE;
997 }
f67db810 998 for (Int_t i = 0; i < fNumberOfModules; i++) {
61a57d07 999 if (!(module = fModules[i])) continue;
1000 if (module->GetModuleId() < fgkMinSSDModuleId) continue;
1001 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1002 if (!(strip = module->GetStrip(strind))) continue;
1003 Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
1004 if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() ) {
1005 ldcn->AddNoiseP(modid, strip->GetStripId(), strip->GetNoiseCM());
1006 ldcp->AddPedestalP(modid, strip->GetStripId(), strip->GetPedestal());
d86972f6 1007 ldcbc->AddBadChannelP(modid, strip->GetStripId(), EvaluateIfChannelIsBad(module, strip->GetStripId()));
61a57d07 1008 } else {
1009 ldcn->AddNoiseN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetNoiseCM());
1010 ldcp->AddPedestalN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetPedestal());
1011 ldcbc->AddBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()),
d86972f6 1012 EvaluateIfChannelIsBad(module, strip->GetStripId()));
61a57d07 1013 }
f67db810 1014 }
f67db810 1015 }
a69c8ba0 1016 if (dafname) dadatafilename.Form("%s/", dafname);
371588bb 1017 dadatafilename += TString::Format("ITSSSDda_%i.root", fLdcId);
a69c8ba0 1018 tmpfname = new Char_t[dadatafilename.Length()+1];
443e42aa 1019 Int_t sz = dadatafilename.Sizeof();
1020 dafname = strncpy(tmpfname, dadatafilename.Data(),sz);
c4d90345 1021 TFile *fileRun = new TFile (dadatafilename.Data(),"RECREATE");
f67db810 1022 if (fileRun->IsZombie()) {
c4d90345 1023 AliError(Form("AliITSHandleDaSSD: SaveCalibrationSSDLDC() error open file %s", dadatafilename.Data()));
2e2c6def 1024 ldcn->Delete();
f67db810 1025 delete fileRun;
2e2c6def 1026 delete ldcn;
1027 delete ldcp;
1028 delete ldcbc;
f67db810 1029 return kFALSE;
1030 }
2e2c6def 1031 fileRun->WriteTObject(ldcn);
1032 fileRun->WriteTObject(ldcp);
61a57d07 1033 if (fBadChannelsList)
1034 if (fMergeBCLists) {
1035 MergeBadChannels(ldcbc);
1036 fileRun->WriteTObject(ldcbc);
1037 } else fileRun->WriteTObject(fBadChannelsList);
6e7691a5 1038 else fileRun->WriteTObject(ldcbc);
f67db810 1039 fileRun->Close();
f67db810 1040 delete fileRun;
2e2c6def 1041 delete ldcn;
1042 delete ldcp;
1043 delete ldcbc;
c4d90345 1044 return kTRUE;
1045}
1046
1047
61a57d07 1048//______________________________________________________________________________
88128115 1049Int_t AliITSHandleDaSSD::MergeBadChannels(AliITSBadChannelsSSDv2*& bcl) const
61a57d07 1050{
1051// Merges the statick bad channels list with bad channels got upon calibration
1052 AliITSModuleDaSSD *module = 0;
1053 Int_t nmpch = 0, nmnch = 0, ngpch = 0, ngnch = 0;
1054 if (!fBadChannelsList || !bcl) {
1055 AliWarning("Either fBadChannelsList == NULL or bad_channels_list == NULL, there is nothing to merge!");
1056 return -1;
1057 }
1058 for (Int_t modind = 0; modind < GetNumberOfModules(); modind++) {
1059 if (!(module = fModules[modind])) continue;
1060 if (module->GetModuleId() < fgkMinSSDModuleId) continue;
1061 Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
1062 for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetPNStripsPerModule(); strind++) {
1063 if ( (!(fBadChannelsList->GetBadChannelP(modid, strind) & fgkBadChannelMask))
1064 && (bcl->GetBadChannelP(modid, strind) & fgkBadChannelMask) )
1065 ngpch++;
1066 if ( (!(fBadChannelsList->GetBadChannelN(modid, strind) & fgkBadChannelMask))
1067 && (bcl->GetBadChannelN(modid, strind) & fgkBadChannelMask) )
1068 ngnch++;
1069 if ( (!(bcl->GetBadChannelP(modid, strind) & fgkBadChannelMask))
1070 && (fBadChannelsList->GetBadChannelP(modid, strind) & fgkBadChannelMask) ) {
1071 bcl->AddBadChannelP(modid, strind, fBadChannelsList->GetBadChannelP(modid, strind));
1072 nmpch++;
1073 }
1074 if ( (!(bcl->GetBadChannelN(modid, strind) & fgkBadChannelMask))
1075 && (fBadChannelsList->GetBadChannelN(modid, strind) & fgkBadChannelMask) ) {
1076 bcl->AddBadChannelN(modid, strind, fBadChannelsList->GetBadChannelN(modid, strind));
1077 nmnch++;
1078 }
1079 }
1080 }
1081 AliInfo(Form("Static bad, dynamic good: P%d, N%d", nmpch, nmnch));
1082 AliInfo(Form("Static good, dynamic bad: P%d, N%d", ngpch, ngnch));
1083 return (nmnch + nmpch);
1084}
1085
1086
c4d90345 1087
1088//______________________________________________________________________________
1089Bool_t AliITSHandleDaSSD::DumpModInfo(const Float_t meannosethreshold) const
1090{
1091// Dump calibration parameters
1092 AliITSModuleDaSSD *mod;
1093 AliITSChannelDaSSD *strip;
1094 if (!fModules) {
1095 cout << "SSDDALDC::DumpModInfo(): Error, no modules are allocated!" << endl;
1096 return kFALSE;
1097 }
1098 cout << "Modules with MeanNoise > " << meannosethreshold << endl;
1099 for (Int_t i = 0; i < fNumberOfModules; i++) {
1100 if (!(mod = fModules[i])) continue;
d86972f6 1101 Double_t maxnoise = 0.0L, meannoise = 0.0L, meanovf = 0.0L;
1102 Float_t maxped = 0.0f;
1103 Int_t maxstrind = 0, novfstr = 0, maxovf = 0;
c4d90345 1104 for (Int_t strind = 0; strind < mod->GetNumberOfStrips(); strind++) {
1105 if (!(strip = mod->GetStrip(strind))) {novfstr++; continue; }
1106 if (strip->GetNoiseCM() >= AliITSChannelDaSSD::GetUndefinedValue() ) {novfstr++; continue; }
1107 if (maxnoise < strip->GetNoiseCM()) {
1108 maxnoise = strip->GetNoiseCM();
1109 maxstrind = strind;
1110 }
d86972f6 1111 meannoise = (strind - novfstr) ? meannoise + (strip->GetNoiseCM() - meannoise) / static_cast<Double_t>(strind - novfstr + 1)
c4d90345 1112 : strip->GetNoiseCM();
d86972f6 1113 if (TMath::Abs(maxped) < TMath::Abs(strip->GetPedestal())) maxped = strip->GetPedestal();
1114 meanovf = (strind - novfstr) ? meanovf + (strip->GetOverflowNumber() - meanovf) / static_cast<Double_t>(strind - novfstr + 1)
1115 : strip->GetOverflowNumber();
1116 if (strip->GetOverflowNumber() > maxovf) maxovf = strip->GetOverflowNumber();
c4d90345 1117 }
1118 if (meannoise > meannosethreshold)
1119 cout << "Mod: " << i << "; DDl: " << (int)mod->GetDdlId() << "; AD: " << (int)mod->GetAD()
371588bb 1120 << "; ADC: " << (int)mod->GetADC() << "; MaxPed = " << maxped
1121 << "; MeanNoise = " << meannoise
c4d90345 1122 << "; NOfStrips = " << (mod->GetNumberOfStrips() - novfstr) << endl;
d86972f6 1123 if (maxovf > 10) cout << "Max number of events with overflow : " << maxovf << "; mean : " << meanovf << endl;
c4d90345 1124 }
1125 return kTRUE;
1126}
1127
1128
1129
1130//______________________________________________________________________________
1131Bool_t AliITSHandleDaSSD::PrintModCalibrationData(const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const Char_t *fname) const
1132{
59087bde 1133// Print Module calibration data whether in file or in cout
c4d90345 1134 AliITSChannelDaSSD *strip;
1135 ofstream datafile;
1136 ostream *outputfile;
1137 if (!fname) { outputfile = &cout; }
1138 else {
1139 datafile.open(fname, ios::out);
1140 if (datafile.fail()) return kFALSE;
1141 outputfile = dynamic_cast<ostream*>(&datafile);
1142 }
1143 *outputfile << "DDL = " << (int)ddlID << "; AD = " << (int)ad << "; ADC = " << (int)adc << endl;
1144 for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
8bcdc40d 1145 if ( (strip = GetStrip(ddlID, ad, adc, strind)) ) {
c4d90345 1146 *outputfile << "Str = " << strind << "; ped = " << strip->GetPedestal()
1147 << "; noise = " << strip->GetNoiseCM() << endl;
1148 }
1149 else continue;
1150 }
1151 if (datafile.is_open()) datafile.close();
1152 return kTRUE;
1153}
1154
1155
1156
371588bb 1157//______________________________________________________________________________
c4d90345 1158void AliITSHandleDaSSD::DumpInitData(const Char_t *str) const
1159{
371588bb 1160// Print general information retrieved from raw data file
c4d90345 1161 cout << "Raw data file: " << fRawDataFileName << endl
1162 << "LDC: " << (Int_t)fLdcId << "; RunId: " << fRunId << endl
1163 << "Number of physics events: " << fNumberOfEvents << endl
1164 << str;
1165}
1166
1167
1168//______________________________________________________________________________
1169Bool_t AliITSHandleDaSSD::AllocateSimulatedModules(const Int_t copymodind)
1170{
1171// Used to allocate simulated modules to test the performance
1172 AliITSModuleDaSSD *module;
1173 UChar_t ad, adc, ddlID;
1174 ad = adc = ddlID = 0;
1175 if (!(fModules[copymodind])) return kFALSE;
1176 for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
1177 if (!fModules[modind]) {
1178 module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
1179 if ((adc < 5) || ((adc > 7) && (adc < 13)) ) adc += 1;
1180 else if (adc == 5) adc = 8;
1181 else if (adc == 13) {
1182 adc = 0;
1183 if (ad < 8) ad += 1;
1184 else {
1185 ad = 0;
1186 ddlID +=1;
1187 }
1188 }
1189 if (!module->SetModuleIdData (ddlID, ad, adc, modind)) return kFALSE;
1190 fModules[modind] = module;
1191 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1192 AliITSChannelDaSSD *cstrip = fModules[copymodind]->GetStrip(strind);
443e42aa 1193 if(!cstrip)return kFALSE;
c4d90345 1194 Long_t eventsnumber = cstrip->GetEventsNumber();
1195 AliITSChannelDaSSD *strip = new AliITSChannelDaSSD(strind, eventsnumber);
1196 for (Long_t evind = 0; evind < eventsnumber; evind++) {
61a57d07 1197 Short_t sign = cstrip->GetSignal(evind);
c4d90345 1198 if (!strip->SetSignal(evind, sign)) AliError(Form("AliITSHandleDaSSD: Copy events error! mod = %i, str = %i", modind, strind));
1199 }
1200 module->SetStrip(strip, strind);
1201 }
1202 }
1203 else {
1204 ddlID = fModules[modind]->GetDdlId();
1205 ad = fModules[modind]->GetAD();
1206 adc = fModules[modind]->GetADC();
1207 }
1208 }
1209 for (UShort_t modind = 0; modind < fNumberOfModules; modind++) fModules[modind]->SetModuleId(modind + 1080);
f67db810 1210 return kTRUE;
1211}
be4f467e 1212
1213
1214
1215//___________________________________________________________________________________________
1216Bool_t AliITSHandleDaSSD::AdDataPresent(const Int_t ddl, const Int_t ad) const
1217{
1218// Check if there are calibration data for given ddl and slot
1219 for (Int_t modind = 0; modind < fNumberOfModules; modind++)
1220 if ((GetModule(modind)->GetAD() == ad) && (GetModule(modind)->GetDdlId() == ddl)) return kTRUE;
1221 return kFALSE;
1222}
1223
1224
1225
1226//___________________________________________________________________________________________
1227Bool_t AliITSHandleDaSSD::SaveEqSlotCalibrationData(const Int_t ddl, const Int_t ad, const Char_t *fname) const
be4f467e 1228{
292c304e 1229// Saves calibration files for selected equipment (DDL)
be4f467e 1230 fstream feefile;
1231 Int_t zsml, offsetml;
1232 ULong_t zsth, offset, zsoffset;
1233 if (!fname) {
1234 AliError("File name must be specified!");
1235 return kFALSE;
1236 }
1237 if (!AdDataPresent(ddl, ad)) {
1238 AliError(Form("Error SaveEqSlotCalibrationData(ddl = %i, ad = %i) no data present", ddl, ad));
1239 return kFALSE;
1240 }
1241 feefile.open(fname, ios::out);
1242 if (!feefile.is_open()) {
1243 AliError(Form("Can not open the file %s for output!", fname));
1244 return kFALSE;
1245 }
5af4a2d0 1246 for (zsml = 0; fgkZsBitMask >> zsml; zsml++) ;
1247 for (offsetml = 0; fgkOffSetBitMask >> offsetml; offsetml++) ;
be4f467e 1248 for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
1249 for (Int_t adcb = 0; adcb < fgkAdcPerDBlock; adcb++) {
1250 zsoffset = 0x0;
1251 for (Int_t j = 0; j < 2; j++) {
1252 Int_t adc = adcb + j * 8;
1253 zsth = ZsThreshold(ddl, ad, adc, strind);
1254 offset = OffsetValue(ddl, ad, adc, strind);
1255 zsoffset = zsoffset | (((zsth << offsetml) | offset) << ((j == 0) ? (offsetml + zsml) : 0));
1256 }
1257 feefile << "0x" << ConvBase(static_cast<unsigned long>(zsoffset), 16) << endl;
1258 }
1259 }
1260 feefile.close();
1261 return kTRUE;
1262}
1263
1264
1265//______________________________________________________________________________
1266Int_t AliITSHandleDaSSD::ChannelIsBad(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1267{
1268// Check if the channel is bad
61a57d07 1269 AliITSModuleDaSSD *module = NULL;
1270 Int_t modn = -1;
1271 if (fBadChannelsList && fDDLModuleMap) {
be4f467e 1272 modn = RetrieveModuleId(ddl, ad, adc);
1273 if (modn < 0) return -1;
61a57d07 1274 if (modn < fgkMinSSDModuleId) {
a5a317a9 1275 AliWarning(Form("Module ddl/ad/adc: %d/%d/%d has number %d which is wrong for SSD module %d", ddl, ad, adc, strn, modn));
61a57d07 1276 return -1;
be4f467e 1277 }
61a57d07 1278 Short_t modid = modn - fgkMinSSDModuleId;
1279 if (strn < AliITSModuleDaSSD::GetPNStripsPerModule())
1280 return (fBadChannelsList->GetBadChannelP(modid, strn) & fgkBadChannelMask);
1281 else return (fBadChannelsList->GetBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strn)) & fgkBadChannelMask);
1282 } else {
d86972f6 1283 AliError("Error ether bad channels list or DDLMap is not initialized or both, EvaluateIfChannelIsBad(module, strip) is used!");
32c31f19 1284 if ((module = GetModule(ddl, ad, adc))) {
d86972f6 1285 return (EvaluateIfChannelIsBad(module, strn) & fgkBadChannelMask);
be4f467e 1286 } else {
61a57d07 1287 AliWarning(Form("There is no calibration data for ddl = %i, ad = %i, adc = %i, 0 is used!", ddl, ad, adc));
1288 return 0ul;
be4f467e 1289 }
be4f467e 1290 return 0;
1291 }
1292}
61a57d07 1293
1294
1295
1296//______________________________________________________________________________
1297Int_t AliITSHandleDaSSD::LadderIsOff(const UChar_t ddl, const UChar_t ad, const UChar_t adc) const
1298{
1299//Checks if the module with given ddl, ad, adc is on the ladder which is in the list of ladders which are off
1300 const Int_t nm5 = 500;
1301 const Int_t nm6 = 1248;
1302 const Int_t nml5a = 12;
1303 const Int_t nml5c = 10;
1304 const Int_t nml6a = 12;
1305 const Int_t nml6c = 13;
1306 Int_t modn, ladder, layer, side;
1307 AliITSModuleDaSSD *module;
1308 if (!(module = GetModule(ddl, ad, adc))) return 0;
1309 if ((modn = module->GetModuleId()) <= 0) modn = RetrieveModuleId(ddl, ad, adc);
1310 if (modn <= 0) return 0;
1311 layer = modn >= nm6 ? 1 : 0; // 6 : 5
1312 ladder = (modn - (layer ? nm6 : nm5)) / (layer ? (nml6a + nml6c) : (nml5a + nml5c));
1313 if ( ((modn - (layer ? nm6 : nm5)) % (layer ? (nml6a + nml6c) : (nml5a + nml5c))) < (layer ? nml6a : nml5a))
1314 side = 0; // A
1315 else side = 1; // C
1316 ladder += (layer ? 600 : 500);
1317 layer += 5;
1318 if (side)
1319 if (fCLaddersOff.GetSize()) {
1320 for(Int_t i = 0; i < fCLaddersOff.GetSize(); i++)
1321 if (fCLaddersOff.At(i) == ladder) return fCLaddersOff.At(i);
1322 return 0;
1323 } else return 0;
1324 else
1325 if (fALaddersOff.GetSize()) {
1326 for(Int_t i = 0; i < fALaddersOff.GetSize(); i++)
1327 if (fALaddersOff.At(i) == ladder) return fALaddersOff.At(i);
1328 return 0;
1329 } else return 0;
1330 return 0;
1331}
1332
1333
be4f467e 1334
1335//______________________________________________________________________________
1336ULong_t AliITSHandleDaSSD::OffsetValue(const AliITSChannelDaSSD *strip,
1337 const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1338{
1339// Calculate the offset value to be upload to FEROM
1340 Int_t pedint;
1341 if (fOffsetDefault < INT_MAX) pedint = fOffsetDefault;
1342 else pedint = TMath::Nint(strip->GetPedestal());
1343 if (pedint > static_cast<Int_t>((fgkOffSetBitMask >> 1))) {
d86972f6 1344 if (!ChannelIsBad(ddl, ad, adc, strn) && !((fMergeBCLists) && (EvaluateIfChannelIsBad(GetModule(ddl, ad, adc), strn))))
61a57d07 1345 AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i can not be represented with mask 0x%s, Offset = %i",
1346 pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(), (fgkOffSetBitMask >> 1)));
1347 return (fgkOffSetBitMask >> 1);
be4f467e 1348 }
1349 if ((-pedint) > static_cast<Int_t>(((fgkOffSetBitMask + 1) >> 1))) {
d86972f6 1350 if (!ChannelIsBad(ddl, ad, adc, strn) && !((fMergeBCLists) && (EvaluateIfChannelIsBad(GetModule(ddl, ad, adc), strn))))
61a57d07 1351 AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i can not be represented with mask 0x%s, Offset = %i",
1352 pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(),
1353 ((fgkOffSetBitMask & (~fgkOffSetBitMask >> 1)) - fgkOffSetBitMask - 1)));
be4f467e 1354 return fgkOffSetBitMask & (~fgkOffSetBitMask >> 1);
1355 }
1356 return fgkOffSetBitMask & (pedint >= 0 ? pedint : pedint + fgkOffSetBitMask + 1);
1357}
1358
1359
1360
1361//______________________________________________________________________________
1362ULong_t AliITSHandleDaSSD::OffsetValue(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1363{
1364// Calculate the offset value to be upload to FEROM
1365 AliITSChannelDaSSD *strip = NULL;
1366 AliITSModuleDaSSD *module = NULL;
32c31f19 1367 if ((module = GetModule(ddl, ad, adc))) {
1368 if ((strip = module->GetStrip(strn))) return OffsetValue(strip, ddl, ad, adc, strn);
be4f467e 1369 else {
1370 AliWarning(Form("There is no calibration data for ddl = %i, ad = %i, adc = %i, strip = %i, 0 is used!", ddl, ad, adc, strn));
1371 return 0ul;
1372 }
1373 } else {
1374 AliWarning(Form("There is no calibration data for ddl = %i, ad = %i, adc = %i, 0 is used!", ddl, ad, adc));
1375 return 0ul;
1376 }
1377}
1378
1379
1380
1381//______________________________________________________________________________
292c304e 1382ULong_t AliITSHandleDaSSD::ZsThreshold(const AliITSChannelDaSSD *strip) const
be4f467e 1383{
1384// Calculate the value of zero suppression threshold to be upload to FEROM
1385 ULong_t zs;
61a57d07 1386 if (fZsDefault < 0) {
1387 zs = TMath::Nint(fZsFactor * strip->GetNoiseCM());
1388 if (zs < static_cast<ULong_t>(fZsMinimum)) zs = static_cast<ULong_t>(fZsMinimum);
1389 }
be4f467e 1390 else zs = fZsDefault;
1391 return (zs < fgkZsBitMask) ? (zs & fgkZsBitMask) : fgkZsBitMask;
1392}
1393
1394
1395//______________________________________________________________________________
1396ULong_t AliITSHandleDaSSD::ZsThreshold(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1397{
1398// Calculate the value of zero suppression threshold to be upload to FEROM, account bad channels list
1399 AliITSChannelDaSSD *strip = NULL;
1400 AliITSModuleDaSSD *module = NULL;
61a57d07 1401 if (ChannelIsBad(ddl, ad, adc, strn)) return fgkZsBitMask;
1402 if (LadderIsOff(ddl, ad, adc)) return fgkZsBitMask;
d86972f6 1403 if (fZsDefault > 0) if (static_cast<ULong_t>(fZsDefault) >= fgkZsBitMask) return fgkZsBitMask;
32c31f19 1404 if ((module = GetModule(ddl, ad, adc))) {
d86972f6 1405 if (fMergeBCLists) if (EvaluateIfChannelIsBad(module, strn)) return fgkZsBitMask;
32c31f19 1406 if ((strip = module->GetStrip(strn))) return ZsThreshold(strip);
be4f467e 1407 else {
1408 AliWarning(Form("There is no calibration data for ddl = %i, ad = %i, adc = %i, strip = %i, 0 is used!", ddl, ad, adc, strn));
1409 return 0ul;
1410 }
1411 } else {
1412 AliWarning(Form("There is no calibration data for ddl = %i, ad = %i, adc = %i, 0 is used!", ddl, ad, adc));
1413 return 0ul;
1414 }
1415}
1416
1417
1418//______________________________________________________________________________
1419string AliITSHandleDaSSD::ConvBase(const unsigned long value, const long base) const
1420{
1421// Converts the unsigned long number into that in another base
1422 string digits = "0123456789ABCDEF";
1423 string result;
1424 unsigned long v = value;
1425 if((base < 2) || (base > 16)) {
1426 result = "Error: base out of range.";
1427 }
1428 else {
1429 int i = 0;
1430 do {
1431 result = digits[v % base] + result;
1432 v /= base;
1433 i++;
1434 }
1435 while((v) || (i<8));
1436 }
1437 return result;
1438}
61a57d07 1439
1440
1441
1442//______________________________________________________________________________
1443Int_t AliITSHandleDaSSD::CheckOffChips() const
1444{
1445// Check if the chip, module are off
1446 AliITSChannelDaSSD *strip;
1447 Int_t offthreshold;
1448 Int_t strnd, chipnd, modnd, stroff, chipoff, modoff;
1449 offthreshold = TMath::Nint(fZsMinimum/fZsFactor);
1450 modnd = modoff = 0;
1451 for (Int_t mi = 0; mi < fNumberOfModules; mi++) {
1452 if (!fModules[mi]) { modnd++; continue; }
1453 if (fModules[mi]->GetModuleId() < 0) continue;
1454 if (LadderIsOff(fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()) ) continue;
1455 chipoff = chipnd = 0;
1456 for (Int_t chipind = 0; chipind < AliITSModuleDaSSD::GetChipsPerModuleConst(); chipind++) {
1457 strnd = stroff = 0;
1458 Int_t stripind = chipind * AliITSModuleDaSSD::GetStripsPerChip();
1459 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1460 if (!(strip = fModules[mi]->GetStrip(strind))) { strnd++; continue; }
1461 if (strip->GetNoiseCM() < offthreshold ) stroff++;
1462 }
1463 if (strnd == AliITSModuleDaSSD::GetStripsPerChip()) chipnd++;
1464 else if (stroff == AliITSModuleDaSSD::GetStripsPerChip()) chipoff++;
1465 else if ((stroff + strnd) == AliITSModuleDaSSD::GetStripsPerChip()) chipoff++;
1466 }
1467 if ((!chipoff) && (!chipnd)) continue;
1468 if (chipnd == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1469 AliInfo(Form("Module: (ddl/ad/adc) %i/%i/%i seems to be off and it is not on the ladders which are off!",
1470 fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1471 modnd++;
1472 }
1473 if (chipoff == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1474 AliInfo(Form("Module (ddl/ad/adc): %i/%i/%i seems to be off and it is not on the ladders which are off!",
1475 fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1476 modoff++;
1477 }
1478 else if ((chipoff + chipnd) == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1479 AliInfo(Form("Module: (ddl/ad/adc): %i/%i/%i seems to be off and it is not on the ladders which are off!",
1480 fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1481 modoff++;
1482 }
1483 else if (chipoff) {
1484 AliInfo(Form("Module: (ddl/ad/adc): %i/%i/%i has %i chips which are off!",
1485 fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC(), chipoff));
1486 modoff++;
1487 }
1488 }
1489 return (modoff + modnd);
1490}
59087bde 1491
d86972f6 1492
1493//______________________________________________________________________________
292c304e 1494Bool_t AliITSHandleDaSSD::CalculatePedNoiseW(const AliITSModuleDaSSD *const module)
d86972f6 1495{
1496// Calculates Pedestal and Noise using Welford algorithm
1497 AliITSChannelDaSSD *strip;
1498 Double_t pedestal, noise, p0, s0;
1499 Short_t *signal;
1500 Int_t ovev, n;
1501 if (!module) return kFALSE;
1502 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1503 if (!(strip = module->GetStrip(strind))) continue;
1504 if (!(signal = strip->GetSignal())) {
1505 AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
1506 module->GetModuleId(), strind));
1507 continue;
1508 }
1509//************* pedestal and noise first pass ****************
1510 pedestal = p0 = noise = 0.0L;
1511 ovev = 0;
1512 for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1513 if (SignalOutOfRange(signal[ev])) ovev += 1;
1514 else
1515 if (!(ev - ovev)) {
1516 pedestal = p0 = signal[ev];
1517 noise = 0.0L;
1518 } else {
1519 p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1520 s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1521 pedestal = p0;
1522 noise = s0;
1523 }
1524 }
1525 if (strip->GetEventsNumber() == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
1526 strip->SetPedestal(static_cast<Float_t>(pedestal));
1527 if ((n = strip->GetEventsNumber() - ovev - 1) > 0)
1528 strip->SetNoise( static_cast<Float_t>(sqrt(noise / static_cast<Double_t>(n))) );
1529 else {
1530 strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
1531 continue;
1532 }
1533//************* Second pass excluds event with |p - s|>f*noise *****************
1534 pedestal = p0 = noise = 0.0L;
1535 ovev = 0;
1536 for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1537 if ( SignalOutOfRange(signal[ev])
1538 || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
1539 else
1540 if (!(ev - ovev)) {
1541 pedestal = p0 = signal[ev];
1542 noise = 0.0L;
1543 } else {
1544 p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1545 s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1546 pedestal = p0;
1547 noise = s0;
1548 }
1549 }
1550 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = sqrt(noise / static_cast<Double_t>(n));
1551 else noise = AliITSChannelDaSSD::GetUndefinedValue();
1552 strip->SetNoise(static_cast<Float_t>(noise));
1553 if (strip->GetEventsNumber() == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
1554 strip->SetPedestal(static_cast<Float_t>(pedestal));
1555 strip->SetOverflowNumber(ovev);
1556 }
1557 return kTRUE;
1558}
1559
1560
1561//______________________________________________________________________________
1562Bool_t AliITSHandleDaSSD::CalculateCMW(AliITSModuleDaSSD *const module)
1563{
1564// Calculates CM using Welford algorithm
1565 AliITSChannelDaSSD *strip = NULL;
1566 Short_t *signal;
1567 Int_t ovstr, n;
1568 Int_t stripind;
1569 Double_t cm0, cm1, cmsigma, cms1;
1570 module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
1571 for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
1572 stripind = chipind * module->GetStripsPerChip();
1573 module->GetCM()[chipind].Set(fNumberOfEvents);
1574 module->GetCM()[chipind].Reset(0.0f);
1575 for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
1576 // calculate firs approximation of CM and SigmaCM.
1577 cm0 = cm1 = cmsigma = 0.0L;
1578 ovstr = 0;
1579 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1580 if (!(strip = module->GetStrip(strind))) { ovstr += 1; continue; } //return kFALSE;
1581 if (!(signal = strip->GetSignal())) { ovstr += 1; continue; } //return kFALSE;
1582 if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
1583 else {
1584 if (!(strind - stripind - ovstr)) {
1585 cm0 = cm1 = signal[ev] - strip->GetPedestal();
1586 cmsigma = 0.0L;
1587 } else {
1588 cm1 = cm0 + (signal[ev] - strip->GetPedestal() - cm0) / static_cast<Double_t>(strind - stripind - ovstr + 1);
1589 cms1 = cmsigma + (signal[ev] - strip->GetPedestal() - cm0) * (signal[ev] - strip->GetPedestal() - cm1);
1590 cm0 = cm1;
1591 cmsigma = cms1;
1592 } }
1593 }
1594 if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr - 1) > 0) cmsigma = sqrt(cmsigma / static_cast<Double_t>(n));
1595 else {
a5a317a9 1596 AliWarning(Form("AliITSHandleDaSSD: Too little number of strips have a signal for module:chip:event : [%d]:[%d]:[%ld]\n",
d86972f6 1597 module->GetModuleId(), chipind, ev));
1598 if (!(module->SetCM(0.0f, chipind, ev)))
a5a317a9 1599 AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
d86972f6 1600 module->GetModuleId(), chipind, ev));
1601 continue;
1602 }
1603 // calculate cm with threshold
1604 Double_t cmsum = 0.0L;
1605 ovstr = 0;
1606 for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1607 if (!(strip = module->GetStrip(strind))) { ovstr += 1; continue; }
1608 if (!(signal = strip->GetSignal())) { ovstr += 1; continue; }
1609 if ( (SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())
1610 || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
1611 else cmsum += (signal[ev] - strip->GetPedestal());
1612 }
1613 if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Double_t)(n);
1614 else cmsum = 0.0L;
1615 if (!(module->SetCM(static_cast<Float_t>(cmsum), chipind, ev)))
a5a317a9 1616 AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
d86972f6 1617 module->GetModuleId(), chipind, ev));
1618 }
1619 }
1620 return kTRUE;
1621}
1622
1623
1624//______________________________________________________________________________
1625Bool_t AliITSHandleDaSSD::CalculateNoiseCMW(AliITSModuleDaSSD *const module)
1626{
1627// Calculates Noise with CM correction
1628 AliITSChannelDaSSD *strip = NULL;
1629 Short_t *signal;
1630 Int_t ovev, n;
1631 if (!CalculateCMW(module)) {
1632 AliError("Error: AliITSHandleDaSSD::CalculateCMW() returned kFALSE");
1633 return kFALSE;
1634 }
1635 for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1636 if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
1637 if (!(signal = strip->GetSignal())) {
1638 strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
1639 AliError(Form("AliITSHandleDaSSD: Error CalculateNoiseCMW(): there are no events data for module[%i] strip[%i]->GetSignal()",
1640 module->GetModuleId(), strind));
1641 continue; //return kFALSE;
1642 }
1643//** To get exactly the same set of events as for pedestal and noise calculation **
1644 Double_t pedestal, noise, p0, s0;
1645 pedestal = p0 = noise = 0.0L;
1646 ovev = 0;
1647 for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1648 if (SignalOutOfRange(signal[ev])) ovev += 1;
1649 else
1650 if (!(ev - ovev)) {
1651 pedestal = p0 = signal[ev];
1652 noise = 0.0L;
1653 } else {
1654 p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1655 s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1656 pedestal = p0;
1657 noise = s0;
1658 }
1659 }
1660 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = sqrt(noise / static_cast<Double_t>(n));
1661 else {
1662 strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
1663 continue;
1664 }
1665//** Calculation of CM corrected noise **
1666 Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
1667 Double_t nsum = 0.0L;
1668 ovev = 0;
1669 for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1670 if ( SignalOutOfRange(signal[ev])
1671 || TMath::Abs(signal[ev] - pedestal) > (fPedestalThresholdFactor * noise)) ovev += 1;
1672 else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
1673 }
1674 if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = sqrt(nsum / static_cast<Double_t>(n));
1675 else noise = AliITSChannelDaSSD::GetUndefinedValue();
1676 strip->SetNoiseCM(static_cast<Float_t>(noise));
1677 }
1678 return kTRUE;
1679}
1680
1681
1682
1683//______________________________________________________________________________
1684UChar_t AliITSHandleDaSSD::EvaluateIfChannelIsBad(const AliITSModuleDaSSD *const module, const Int_t stripn) const
1685{
1686//Applies the bad channel creteria and set the appropriate flags for returned value
1687 AliITSChannelDaSSD *strip = 0;
1688 UInt_t bcflags = 0;
1689 if (fZsDefault >= 0) { if (static_cast<ULong_t>(fZsDefault) >= fgkZsBitMask) bcflags |= 3; }
1690 else if (static_cast<ULong_t>(fZsMinimum) >= fgkZsBitMask) bcflags |= 3;
1691 if (LadderIsOff(module->GetDdlId(), module->GetAD(), module->GetADC()) ) bcflags |= 3;
1692
1693 if (!(strip = module->GetStrip(stripn))) bcflags |= 3;
1694 else {
1695 if (strip->GetNoiseCM() == AliITSChannelDaSSD::GetUndefinedValue()) bcflags |= 8;
1696 if (static_cast<ULong_t>(TMath::Nint(fZsFactor * strip->GetNoiseCM())) >= fgkZsBitMask) bcflags |= 8;
1697 if (strip->GetNoiseCM() < 1) bcflags |= 16;
1698 if (strip->GetPedestal() > ((fgkOffSetBitMask >> 1) - 1)) bcflags |= 4;
1699 else if ((-(strip->GetPedestal())) > (fgkOffSetBitMask >> 1)) bcflags |= 4;
1700 if (bcflags) bcflags |= 3;
1701 }
1702 return bcflags;
1703}
1704