+ AliDebug(1,Form("Start converting signals to ADC values for detector=%d",det));
+
+ AliTRDcalibDB *calibration = AliTRDcalibDB::Instance();
+ if (!calibration) {
+ AliFatal("Could not get calibration object");
+ return kFALSE;
+ }
+
+ AliTRDSimParam *simParam = AliTRDSimParam::Instance();
+ if (!simParam) {
+ AliFatal("Could not get simulation parameters");
+ return kFALSE;
+ }
+
+ // Converts number of electrons to fC
+ const Double_t kEl2fC = 1.602e-19 * 1.0e15;
+
+ // Coupling factor
+ Double_t coupling = simParam->GetPadCoupling()
+ * simParam->GetTimeCoupling();
+ // Electronics conversion factor
+ Double_t convert = kEl2fC
+ * simParam->GetChipGain();
+ // ADC conversion factor
+ Double_t adcConvert = simParam->GetADCoutRange()
+ / simParam->GetADCinRange();
+ // The electronics baseline in mV
+ Double_t baseline = simParam->GetADCbaseline()
+ / adcConvert;
+ // The electronics baseline in electrons
+ Double_t baselineEl = baseline
+ / convert;
+
+ Int_t row = 0;
+ Int_t col = 0;
+ Int_t time = 0;
+
+ Int_t nRowMax = fGeo->GetPadPlane(det)->GetNrows();
+ Int_t nColMax = fGeo->GetPadPlane(det)->GetNcols();
+ Int_t nTimeTotal = simParam->GetNTimeBins();
+
+ // The gainfactor calibration objects
+ const AliTRDCalDet *calGainFactorDet = calibration->GetGainFactorDet();
+ AliTRDCalROC *calGainFactorROC = 0;
+ Float_t calGainFactorDetValue = 0.0;
+
+ AliTRDarrayADC *digits = 0x0;
+
+ if (!signals) {
+ AliError(Form("Signals array for detector %d does not exist\n",det));
+ return kFALSE;
+ }
+ if (signals->HasData()) {
+ // Expand the container if neccessary
+ signals->Expand();
+ }
+ else {
+ // Create missing containers
+ signals->Allocate(nRowMax,nColMax,nTimeTotal);
+ }
+
+ // Get the container for the digits of this detector
+ if (fDigitsManager->HasSDigits()) {
+ AliError("Digits manager has s-digits");
+ return kFALSE;
+ }
+
+ digits = (AliTRDarrayADC *) fDigitsManager->GetDigits(det);
+ // Allocate memory space for the digits buffer
+ if (!digits->HasData()) {
+ digits->Allocate(nRowMax,nColMax,nTimeTotal);
+ }
+
+ // Get the calibration objects
+ calGainFactorROC = calibration->GetGainFactorROC(det);
+ calGainFactorDetValue = calGainFactorDet->GetValue(det);
+
+ // Create the digits for this chamber
+ for (row = 0; row < nRowMax; row++ ) {
+ for (col = 0; col < nColMax; col++ ) {
+
+ // Check whether pad is masked
+ // Bridged pads are not considered yet!!!
+ if (calibration->IsPadMasked(det,col,row) || calibration->IsPadNotConnected(det,col,row)) {
+ continue;
+ }
+
+ // The gain factors
+ Float_t padgain = calGainFactorDetValue
+ * calGainFactorROC->GetValue(col,row);
+ if (padgain <= 0) {
+ AliError(Form("Not a valid gain %f, %d %d %d",padgain,det,col,row));
+ }
+
+ for (time = 0; time < nTimeTotal; time++) {
+
+ // Get the signal amplitude
+ Float_t signalAmp = signals->GetData(row,col,time);
+ // Pad and time coupling
+ signalAmp *= coupling;
+ // Gain factors
+ signalAmp *= padgain;
+
+ // Add the noise, starting from minus ADC baseline in electrons
+ signalAmp = TMath::Max((Double_t) gRandom->Gaus(signalAmp,simParam->GetNoise())
+ ,-baselineEl);
+
+ // Convert to mV
+ signalAmp *= convert;
+ // Add ADC baseline in mV
+ signalAmp += baseline;
+
+ // Convert to ADC counts. Set the overflow-bit fADCoutRange if the
+ // signal is larger than fADCinRange
+ Short_t adc = 0;
+ if (signalAmp >= simParam->GetADCinRange()) {
+ adc = ((Short_t) simParam->GetADCoutRange());
+ }
+ else {
+ adc = TMath::Nint(signalAmp * adcConvert);
+ }
+
+ // Saving all digits
+ digits->SetData(row,col,time,adc);
+
+ } // for: time
+
+ } // for: col
+ } // for: row
+
+ return kTRUE;