ROOT logo
#ifndef ALIITSHANDLEDASSD_H
#define ALIITSHANDLEDASSD_H

/* Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
 * See cxx source for full Copyright notice                               */
/*                                                                        */
/* $Id$ */

#include <string>
#include "TObject.h"
#include "TArrayS.h"
#include "AliITSModuleDaSSD.h"

///////////////////////////////////////////////////////////////////////////////
///
/// This class provides ITS SSD data handling
/// used by DA. 
//  Author: Oleksandr Borysov
//  Date: 09/02/2010
///////////////////////////////////////////////////////////////////////////////

using namespace std;

class AliITSBadChannelsSSDv2;
class AliITSNoiseSSDv2;

class AliITSHandleDaSSD : public TObject {
  public :
    AliITSHandleDaSSD();
    explicit AliITSHandleDaSSD(Char_t *rdfname);
    AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc);
    AliITSHandleDaSSD& operator = (const AliITSHandleDaSSD& ssdadldc);
    virtual ~AliITSHandleDaSSD();

    virtual Bool_t Init(Char_t *rdfname);
    Bool_t  SetRawFileName (Char_t *rdfname) {return Init(rdfname); }

    void    SetZsDefaul(const Int_t zs)        { fZsDefault = zs;       }
    void    SetOffsetDefault(const Int_t offs) { fOffsetDefault = offs; }
    void    SetZsMinimum(const Int_t zsm)      { if (zsm >= 0) if (static_cast<UInt_t>(zsm) <= fgkZsBitMask) fZsMinimum = zsm; }
    void    SetMergeBCFlag(const Byte_t mbcf)  { fMergeBCLists = mbcf;  }
    void    SetZsFactor(const Float_t zsf)     { fZsFactor = zsf;       }
    void    SetPedestalThresholdFactor(const Float_t pthf) { fPedestalThresholdFactor = pthf; }
    void    SetCmThresholdFactor(const Float_t cmthf)      { fCmThresholdFactor = cmthf;      }
    void    SetALaddersOff(const Int_t n, const Short_t *allist) { fALaddersOff.Set(n, allist); }
    void    SetCLaddersOff(const Int_t n, const Short_t *cllist) { fCLaddersOff.Set(n, cllist); }
    void    SetLaddersOff(const Int_t na, const Short_t *allist, const Int_t nc, const Short_t *cllist) 
                          { SetALaddersOff(na, allist); SetCLaddersOff(nc, cllist); }

    Int_t   GetNumberOfEvents() const  { return fNumberOfEvents; }
    Int_t   GetZsDefault() const       { return fZsDefault;     }
    Int_t   GetOffsetDefault() const   { return fOffsetDefault; }
    Float_t GetZsFactor() const        { return fZsFactor;      }
    Int_t   GetZsMinimum() const       { return fZsMinimum;     }
    Bool_t  GetMergeBCFlag() const     { return static_cast<Bool_t>(fMergeBCLists); }
    Float_t GetPedestalThresholdFactor() const { return fPedestalThresholdFactor; }
    Float_t GetCmThresholdFactor() const       { return fCmThresholdFactor;       }
    TArrayS GetALaddersOff () const { return fALaddersOff; }
    TArrayS GetCLaddersOff () const { return fCLaddersOff; }
    Int_t   GetEqIndex(const Short_t eq) const { for(Int_t i = 0; i < fEqIndex.GetSize(); i++) if (eq == fEqIndex.At(i)) return i; return -1; }
        
    Int_t              GetNumberOfModules() const { return fNumberOfModules; }
    UInt_t             GetLdcId() const { return fLdcId; }
    UInt_t             GetRunId() const { return fRunId; }
    AliITSModuleDaSSD* GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const;
    AliITSModuleDaSSD* GetModule (const Int_t index) const 
                               {if ((fModules) && (index < fNumberOfModules)) return fModules[index]; else return NULL;}
    Int_t GetModuleIndex (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const;
    AliITSChannelDaSSD*    GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const;
    AliITSNoiseSSDv2*        GetCalibrationOCDBNoise()  const;
    AliITSBadChannelsSSDv2*  GetCalibrationBadChannels() const;
    Bool_t      SaveCalibrationSSDLDC(Char_t*& dafname);
    Int_t       MergeBadChannels(AliITSBadChannelsSSDv2*& bcl) const;    
    
    void    SetModIndProcessed(Int_t mi) {fModIndProcessed = mi;}
    void    SetModIndRead (Int_t mr)  {fModIndRead = mr;}
    Bool_t  SetNumberOfModules (const Int_t numberofmodules);
    Bool_t  SetModule(AliITSModuleDaSSD *const module, const Int_t index); 
    virtual Bool_t  ReadStaticBadChannelsMap(const Char_t *filename = NULL);  
    virtual Bool_t  ReadDDLModuleMap(const Char_t *filename = NULL);  
    Int_t   ReadCalibrationDataFile (char* fileName, const Long_t eventsnumber);
    virtual Int_t   ReadModuleRawData (const Int_t modulesnumber);  

    virtual Bool_t  CalculatePedestal(const AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculateNoise(const AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculateNoiseCM(AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculateCM(AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculatePedNoiseW(const AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculateCMW(AliITSModuleDaSSD *const module);
    virtual Bool_t  CalculateNoiseCMW(AliITSModuleDaSSD *const module);
    virtual Bool_t  AddFeromCm(const AliITSModuleDaSSD *const module);
    virtual Bool_t  ProcessRawData(const Int_t nmread = fgkNumberOfSSDModulesPerDdl,  const Bool_t usewelford = kTRUE);
    virtual Bool_t  RelocateModules();
    virtual Bool_t  AllocateSimulatedModules(const Int_t copymodind = 0);

    Bool_t  AdDataPresent(const Int_t ddl, const Int_t ad) const;
    Int_t   DdlToEquipmentId (Int_t ddl) const { return (512 + ddl); }
    Int_t   ChannelIsBad(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const;
    UChar_t EvaluateIfChannelIsBad(const AliITSModuleDaSSD *const module, const Int_t stripn) const;
    Int_t   LadderIsOff(const UChar_t ddl, const UChar_t ad, const UChar_t adc) const;
    Bool_t  SaveEqSlotCalibrationData(const Int_t ddl, const Int_t ad, const Char_t *fname) const;
    ULong_t OffsetValue(const AliITSChannelDaSSD *strip, const UChar_t ddl = 0, const UChar_t ad = 0, 
                                 const UChar_t adc = 0, const Int_t strn = -1) const;
    ULong_t OffsetValue(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const;
    ULong_t ZsThreshold(const AliITSChannelDaSSD *strip) const;
    ULong_t ZsThreshold(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const;
    
    virtual void    Reset();
    virtual Short_t RetrieveModuleId(const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const;
    Bool_t  DumpModInfo(const Float_t meannosethreshold) const;
    Bool_t  PrintModCalibrationData(const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const Char_t *fname = NULL) const;
    Int_t   CheckOffChips() const;
    void    DumpInitData(const Char_t *str = "") const;
    void    DeleteSignalAll() { if (fModules) for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) fModules[i]->DeleteSignal();}
    void    DeleteSignal() { if (fModules) for (Int_t i = fModIndProcessed; i < fModIndRead; i++) if (fModules[i]) fModules[i]->DeleteSignal();}
    void    DeleteCMAll() { if (fModules) for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) fModules[i]->DeleteCM();}
    void    DeleteCM() { if (fModules) for (Int_t i = fModIndProcessed; i < fModIndRead; i++) if (fModules[i]) fModules[i]->DeleteCM();}
    void    DeleteCMFerom() { if (fModules) for (Int_t i = fModIndProcessed; i < fModIndRead; i++) if (fModules[i]) fModules[i]->DeleteCMFerom ();}

    static Int_t GetNumberOfSSDModulesConst() { return fgkNumberOfSSDModules; }

  protected :

    static const Int_t    fgkNumberOfSSDModules ;        // Number of SSD modules in ITS
    static const Int_t    fgkNumberOfSSDModulesPerDdl;   // Number of SSD modules in DDL
    static const Int_t    fgkNumberOfSSDModulesPerSlot;  // Number of SSD modules in Slot
    static const Short_t  fgkMinSSDModuleId;             // Initial SSD modules number
    static const Int_t    fgkNumberOfSSDDDLs;            // Number of DDLs in SSD
    static const Int_t    fgkNumberOfSSDSlotsPerDDL;     // Number of SSD slots per DDL
    static const Float_t  fgkPedestalThresholdFactor;    // Defalt value for fPedestalThresholdFactor 
    static const Float_t  fgkCmThresholdFactor;          // Defalt value for fCmThresholdFactor 
   
    static const UInt_t   fgkZsBitMask ;           // Bit mask for FEROM ZS
    static const UInt_t   fgkOffSetBitMask;        // Bit mask for FEROM Offset correction
    static const UInt_t   fgkBadChannelMask;       // Mask to suppress the channel from the bad channel list
    static const Int_t    fgkAdcPerDBlock;         // FEROM configuration file constant
     
    Char_t               *fRawDataFileName;       // Name of the file with raw data
    Int_t                 fNumberOfModules;       // number of AliITSModuleDaSSD to allocate
    AliITSModuleDaSSD   **fModules;               //[fNumberOfModules] array of pointer on AliITSModuleDaSSD objects (1698 SSD  Modules)
    Int_t                 fModIndProcessed;       //! index of the last module in fModules array with processed data
    Int_t                 fModIndRead;            //! index of the last module in fModules array with adc data present (read)
    Int_t                *fModIndex;              //! index array for fModules
    TArrayS               fEqIndex;               //! index array of equipmnts (DDLs).
    Long_t                fNumberOfEvents;        // Number of physics or calibration events in raw data file fRawDataFileName

    AliITSBadChannelsSSDv2 *fBadChannelsList;       //! List of bad channels: static or created on base of calculated noise and pedestal
    Int_t                *fDDLModuleMap;          //! DDL map  
    TArrayS               fALaddersOff;           //! Lisst of ladders of side A that are off and should be suppressed
    TArrayS               fCLaddersOff;           //! Lisst of ladders of side C that are off and should be suppressed
    
    UInt_t                fLdcId;                 //  LDC number, read from header
    UInt_t                fRunId;                 //  Run number, read from header

    Float_t         fPedestalThresholdFactor;        // configuration parameter: ThresholdFactor for pedestal calculation 
    Float_t         fCmThresholdFactor;              // configuration parameter: ThresholdFactor for CM calculation 
    Int_t           fZsDefault;                      // default value for ZS threshold
    Int_t           fOffsetDefault;                  // default value for offset correction
    Int_t           fZsMinimum;                      // minimum value for ZS threshold
    Byte_t          fMergeBCLists;                   // Flag, if it is not zero the static bad channels list is merged with dynamic one
    Float_t         fZsFactor;                       // zs factor 3.0
    
  protected :
    Bool_t   SignalOutOfRange (const Short_t signal) const { return ((signal >= AliITSChannelDaSSD::GetOverflowConst()) || 
                                                                     (signal <= AliITSChannelDaSSD::GetUnderflowConst())); }
    string   ConvBase(const unsigned long value, const long base) const;

    ClassDef(AliITSHandleDaSSD, 8)

};

#endif
 AliITSHandleDaSSD.h:1
 AliITSHandleDaSSD.h:2
 AliITSHandleDaSSD.h:3
 AliITSHandleDaSSD.h:4
 AliITSHandleDaSSD.h:5
 AliITSHandleDaSSD.h:6
 AliITSHandleDaSSD.h:7
 AliITSHandleDaSSD.h:8
 AliITSHandleDaSSD.h:9
 AliITSHandleDaSSD.h:10
 AliITSHandleDaSSD.h:11
 AliITSHandleDaSSD.h:12
 AliITSHandleDaSSD.h:13
 AliITSHandleDaSSD.h:14
 AliITSHandleDaSSD.h:15
 AliITSHandleDaSSD.h:16
 AliITSHandleDaSSD.h:17
 AliITSHandleDaSSD.h:18
 AliITSHandleDaSSD.h:19
 AliITSHandleDaSSD.h:20
 AliITSHandleDaSSD.h:21
 AliITSHandleDaSSD.h:22
 AliITSHandleDaSSD.h:23
 AliITSHandleDaSSD.h:24
 AliITSHandleDaSSD.h:25
 AliITSHandleDaSSD.h:26
 AliITSHandleDaSSD.h:27
 AliITSHandleDaSSD.h:28
 AliITSHandleDaSSD.h:29
 AliITSHandleDaSSD.h:30
 AliITSHandleDaSSD.h:31
 AliITSHandleDaSSD.h:32
 AliITSHandleDaSSD.h:33
 AliITSHandleDaSSD.h:34
 AliITSHandleDaSSD.h:35
 AliITSHandleDaSSD.h:36
 AliITSHandleDaSSD.h:37
 AliITSHandleDaSSD.h:38
 AliITSHandleDaSSD.h:39
 AliITSHandleDaSSD.h:40
 AliITSHandleDaSSD.h:41
 AliITSHandleDaSSD.h:42
 AliITSHandleDaSSD.h:43
 AliITSHandleDaSSD.h:44
 AliITSHandleDaSSD.h:45
 AliITSHandleDaSSD.h:46
 AliITSHandleDaSSD.h:47
 AliITSHandleDaSSD.h:48
 AliITSHandleDaSSD.h:49
 AliITSHandleDaSSD.h:50
 AliITSHandleDaSSD.h:51
 AliITSHandleDaSSD.h:52
 AliITSHandleDaSSD.h:53
 AliITSHandleDaSSD.h:54
 AliITSHandleDaSSD.h:55
 AliITSHandleDaSSD.h:56
 AliITSHandleDaSSD.h:57
 AliITSHandleDaSSD.h:58
 AliITSHandleDaSSD.h:59
 AliITSHandleDaSSD.h:60
 AliITSHandleDaSSD.h:61
 AliITSHandleDaSSD.h:62
 AliITSHandleDaSSD.h:63
 AliITSHandleDaSSD.h:64
 AliITSHandleDaSSD.h:65
 AliITSHandleDaSSD.h:66
 AliITSHandleDaSSD.h:67
 AliITSHandleDaSSD.h:68
 AliITSHandleDaSSD.h:69
 AliITSHandleDaSSD.h:70
 AliITSHandleDaSSD.h:71
 AliITSHandleDaSSD.h:72
 AliITSHandleDaSSD.h:73
 AliITSHandleDaSSD.h:74
 AliITSHandleDaSSD.h:75
 AliITSHandleDaSSD.h:76
 AliITSHandleDaSSD.h:77
 AliITSHandleDaSSD.h:78
 AliITSHandleDaSSD.h:79
 AliITSHandleDaSSD.h:80
 AliITSHandleDaSSD.h:81
 AliITSHandleDaSSD.h:82
 AliITSHandleDaSSD.h:83
 AliITSHandleDaSSD.h:84
 AliITSHandleDaSSD.h:85
 AliITSHandleDaSSD.h:86
 AliITSHandleDaSSD.h:87
 AliITSHandleDaSSD.h:88
 AliITSHandleDaSSD.h:89
 AliITSHandleDaSSD.h:90
 AliITSHandleDaSSD.h:91
 AliITSHandleDaSSD.h:92
 AliITSHandleDaSSD.h:93
 AliITSHandleDaSSD.h:94
 AliITSHandleDaSSD.h:95
 AliITSHandleDaSSD.h:96
 AliITSHandleDaSSD.h:97
 AliITSHandleDaSSD.h:98
 AliITSHandleDaSSD.h:99
 AliITSHandleDaSSD.h:100
 AliITSHandleDaSSD.h:101
 AliITSHandleDaSSD.h:102
 AliITSHandleDaSSD.h:103
 AliITSHandleDaSSD.h:104
 AliITSHandleDaSSD.h:105
 AliITSHandleDaSSD.h:106
 AliITSHandleDaSSD.h:107
 AliITSHandleDaSSD.h:108
 AliITSHandleDaSSD.h:109
 AliITSHandleDaSSD.h:110
 AliITSHandleDaSSD.h:111
 AliITSHandleDaSSD.h:112
 AliITSHandleDaSSD.h:113
 AliITSHandleDaSSD.h:114
 AliITSHandleDaSSD.h:115
 AliITSHandleDaSSD.h:116
 AliITSHandleDaSSD.h:117
 AliITSHandleDaSSD.h:118
 AliITSHandleDaSSD.h:119
 AliITSHandleDaSSD.h:120
 AliITSHandleDaSSD.h:121
 AliITSHandleDaSSD.h:122
 AliITSHandleDaSSD.h:123
 AliITSHandleDaSSD.h:124
 AliITSHandleDaSSD.h:125
 AliITSHandleDaSSD.h:126
 AliITSHandleDaSSD.h:127
 AliITSHandleDaSSD.h:128
 AliITSHandleDaSSD.h:129
 AliITSHandleDaSSD.h:130
 AliITSHandleDaSSD.h:131
 AliITSHandleDaSSD.h:132
 AliITSHandleDaSSD.h:133
 AliITSHandleDaSSD.h:134
 AliITSHandleDaSSD.h:135
 AliITSHandleDaSSD.h:136
 AliITSHandleDaSSD.h:137
 AliITSHandleDaSSD.h:138
 AliITSHandleDaSSD.h:139
 AliITSHandleDaSSD.h:140
 AliITSHandleDaSSD.h:141
 AliITSHandleDaSSD.h:142
 AliITSHandleDaSSD.h:143
 AliITSHandleDaSSD.h:144
 AliITSHandleDaSSD.h:145
 AliITSHandleDaSSD.h:146
 AliITSHandleDaSSD.h:147
 AliITSHandleDaSSD.h:148
 AliITSHandleDaSSD.h:149
 AliITSHandleDaSSD.h:150
 AliITSHandleDaSSD.h:151
 AliITSHandleDaSSD.h:152
 AliITSHandleDaSSD.h:153
 AliITSHandleDaSSD.h:154
 AliITSHandleDaSSD.h:155
 AliITSHandleDaSSD.h:156
 AliITSHandleDaSSD.h:157
 AliITSHandleDaSSD.h:158
 AliITSHandleDaSSD.h:159
 AliITSHandleDaSSD.h:160
 AliITSHandleDaSSD.h:161
 AliITSHandleDaSSD.h:162
 AliITSHandleDaSSD.h:163
 AliITSHandleDaSSD.h:164
 AliITSHandleDaSSD.h:165
 AliITSHandleDaSSD.h:166
 AliITSHandleDaSSD.h:167
 AliITSHandleDaSSD.h:168
 AliITSHandleDaSSD.h:169
 AliITSHandleDaSSD.h:170
 AliITSHandleDaSSD.h:171
 AliITSHandleDaSSD.h:172