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