Corrected UInt_t <-> Int_t conversion
[u/mrichter/AliRoot.git] / TPC / AliTPCAltroEmulator.cxx
index fcafc6e..bd18eb9 100644 (file)
@@ -17,6 +17,7 @@
  *     \verbinclude Altro/Altro.C.log
  *
  */
+
 /////////////////////////////////////////////////////////////////////////////////////////////////////
 //     Class for emulation of the ALTRO chip (Altro digital Chain) in C++                          //
 //     Author: Roland Bramm                                                                        //
@@ -69,6 +70,7 @@ AliTPCAltroEmulator::AliTPCAltroEmulator(Int_t timebins, short* Channel) :
   fConfiguredAltro(0),   // ConfiguredAltro
   fConfiguredBSL1(0),    // ConfiguredBSL1
   fConfiguredTCF(0),     // ConfiguredTCF
+  fConfiguredTCFraw(0),     // ConfiguredTCF
   fConfiguredBSL2(0),    // ConfiguredBSL2
   fConfiguredZSU(0),     // ConfiguredZSU
   fBSL1mode(0),          // BSL1mode
@@ -104,9 +106,10 @@ AliTPCAltroEmulator::AliTPCAltroEmulator(Int_t timebins, short* Channel) :
   
   fReader(0),           // for Altro Emulation on Raw Reader
   fDecoder(0),
-  fDDLFolderName("/tmp/"),
-  fOutputDateFileName("/tmp/tmp.date"),
-  fOutputRootFileName("/tmp/tmp.root"),
+  fRunNumber(0), 
+  fDDLFolderName("./"),
+  fOutputDateFileName("./tmpRaw.date"),
+  fOutputRootFileName("./tmpRaw.root"),
   fIsRandom(kTRUE),
   fChannels(0),
   fCDHs(0),
@@ -116,92 +119,20 @@ AliTPCAltroEmulator::AliTPCAltroEmulator(Int_t timebins, short* Channel) :
   //
   // Constructor of Altro Class
   //
-  /*
-  ftimebins = timebins;
-  
-  fChannelShort_T = Channel;
-  
-  fOnBSL1 = 0;
-  fOnTCF = 0;
-  fOnBSL2 = 0;
-  fOnClip = 0;
-  fOnZSU = 0;
-  
-  fConfiguredAltro = 0;
-  fConfiguredBSL1 = 0;
-  fConfiguredTCF = 0;
-  fConfiguredBSL2 = 0;
-  fConfiguredZSU = 0;
-  */
-}
-
-
-AliTPCAltroEmulator::AliTPCAltroEmulator(const AliTPCAltroEmulator &altro):
-  TNamed(),
-  ftimebins(altro.ftimebins),
-//  fChannelIn(Channel),
-  fChannelShort(altro.fChannelShort), 
-  fADCkeep(0),     
-  fOnBSL1(0), 
-  fOnTCF(0),  
-  fOnBSL2(0), 
-  fOnClip(0), 
-  fOnZSU(0),  
-
-  fConfiguredAltro(0),   // ConfiguredAltro
-  fConfiguredBSL1(0),    // ConfiguredBSL1
-  fConfiguredTCF(0),     // ConfiguredTCF
-  fConfiguredBSL2(0),    // ConfiguredBSL2
-  fConfiguredZSU(0),     // ConfiguredZSU
-  fBSL1mode(0),          // BSL1mode
-  fBSL1ValuePeDestal(0), // BSL1ValuePeDestal
-  fBSL1PedestalMem(0),  // BSL1PedestalMem
-  fBSL1polarity(0),      // BSL1polarity
-
-  fTCFK1(0), // K1
-  fTCFK2(0), // K2
-  fTCFK3(0), // K3
-  fTCFL1(0), // L1
-  fTCFL2(0), // L2
-  fTCFL3(0), // L3
 
-  fTCFK1Int(0), // K1Int
-  fTCFK2Int(0), // K2Int
-  fTCFK3Int(0), // K3Int
-  fTCFL1Int(0), // L1Int
-  fTCFL2Int(0), // L2Int
-  fTCFL3Int(0), // L3Int
-
-  fBSL2HighThreshold(0), // BSL2HighThreshold
-  fBSL2LowThreshold(0),  // BSL2LowThreshold
-  fBSL2Offset(0),        // BSL2Offset
-  fBSL2Presamples(0),    // BSL2Presamples(0),
-  fBSL2Postsamples(0),   // BSL2Postsamples
-
-  fZSUThreshold(0),      // ZSUThreshold
-
-  fZSUMinSamplesaboveThreshold(0), // ZSUMinSamplesaboveThreshold
-  fZSUPresamples(0),     // ZSUPresamples
-  fZSUPostsamples(0),     // ZSUPostsamples
+  fADCkeep = new Short_t[1024]; 
 
-  fReader(0),           // for Altro Emulation on Raw Reader
-  fDecoder(0),
-  fDDLFolderName("/tmp/"),
-  fOutputDateFileName("/tmp/tmp.date"),
-  fOutputRootFileName("/tmp/tmp.root"),
-  fIsRandom(kTRUE),
-  fChannels(0),
-  fCDHs(0),
-  fADCs(0),
-  fTrailers(0),
-  fRawData(0) {
-  //
-  // copy constructor of Altro Class
-  //
+  fTCFK1IntROC[0]=0; fTCFK1IntROC[1]=0; // dummy defaults
+  fTCFK2IntROC[0]=0; fTCFK2IntROC[1]=0; // dummy defaults
+  fTCFK3IntROC[0]=0; fTCFK3IntROC[1]=0; // dummy defaults
+  fTCFL1IntROC[0]=0; fTCFL1IntROC[1]=0; // dummy defaults
+  fTCFL2IntROC[0]=0; fTCFL2IntROC[1]=0; // dummy defaults
+  fTCFL3IntROC[0]=0; fTCFL3IntROC[1]=0; // dummy defaults
 
 }
 
 
+
 /**    @brief Destructor of Altro Class
  *
  *     Destructor of Altro Class\n
@@ -211,8 +142,8 @@ AliTPCAltroEmulator::~AliTPCAltroEmulator() {
   // Destructor of Altro Class
   //
 
-  if(fConfiguredZSU == 1)
-    delete fADCkeep;
+  //  if(fConfiguredZSU == 1)
+  delete[] fADCkeep;
 
   delete[] fChannels;
   delete[] fCDHs    ;
@@ -223,21 +154,6 @@ AliTPCAltroEmulator::~AliTPCAltroEmulator() {
 
 }
 
-//_____________________________________________________________________________
-AliTPCAltroEmulator& AliTPCAltroEmulator::operator = (const AliTPCAltroEmulator &source)
-{
-  //
-  // AliTPCAltroEmulator assignment operator
-  //
-
-  if (&source == this) return *this;
-  new (this) AliTPCAltroEmulator(source);
-
-  return *this;
-
-}
-
-
 
 /**  @brief Configures which modules of the Altro should be on.
  *
@@ -284,7 +200,7 @@ void AliTPCAltroEmulator::ConfigBaselineCorrection1(Int_t mode, Int_t ValuePeDes
   //
   // Configures the Base Line Correction 1 (BSL1) Module
   //
-  fBSL1mode          = InRange(mode,0,10,"AliTPCAltroEmulator::ConfigBaselineCorrection1","mode");
+  fBSL1mode          = InRange(mode,0,16,"AliTPCAltroEmulator::ConfigBaselineCorrection1","mode");
   fBSL1ValuePeDestal = InRange(ValuePeDestal,0,1023,"AliTPCAltroEmulator::BaselineCorrection1","ValuePeDestal");
   fBSL1PedestalMem = PedestalMem;
   fBSL1polarity = InRange(polarity,0,1,"AliTPCAltroEmulator::BaselineCorrection1","polarity");
@@ -322,6 +238,33 @@ void AliTPCAltroEmulator::ConfigTailCancellationFilter(Int_t K1, Int_t K2, Int_t
   fTCFL3Int = InRange(L3,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L3");
   fConfiguredTCF = 1;
 }
+void AliTPCAltroEmulator::ConfigTailCancellationFilterForRAWfiles(const Int_t *K1, const Int_t *K2, const Int_t *K3, 
+                                                                 const Int_t *L1, const Int_t *L2, const Int_t *L3){
+  //
+  // Configures the Tail Cancellation Filter (TCF) Module - Different settings for IROC and OROC
+  //
+  // conf from Int_t to fp:  (int)*(pow(2,-16)-1)
+  //             backway:  (Float_t)*(pow(2,16)-1)
+
+  // IROC
+  fTCFK1IntROC[0] = InRange(K1[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K1[0]");
+  fTCFK2IntROC[0] = InRange(K2[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K2[0]");
+  fTCFK3IntROC[0] = InRange(K3[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K3[0]");
+  fTCFL1IntROC[0] = InRange(L1[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L1[0]");
+  fTCFL2IntROC[0] = InRange(L2[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L2[0]");
+  fTCFL3IntROC[0] = InRange(L3[0],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L3[0]");
+  // OROC
+  fTCFK1IntROC[1] = InRange(K1[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K1[1]");
+  fTCFK2IntROC[1] = InRange(K2[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K2[1]");
+  fTCFK3IntROC[1] = InRange(K3[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K3[1]");
+  fTCFL1IntROC[1] = InRange(L1[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L1[1]");
+  fTCFL2IntROC[1] = InRange(L2[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L2[1]");
+  fTCFL3IntROC[1] = InRange(L3[1],0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L3[1]");
+
+
+  fConfiguredTCFraw = 1;
+}
+
 
 /**  @brief Configures the Moving Average Filter (BSL2) Module
  *
@@ -366,7 +309,6 @@ void AliTPCAltroEmulator::ConfigZerosuppression(Int_t Threshold, Int_t MinSample
   fZSUMinSamplesaboveThreshold = InRange(MinSamplesaboveThreshold,1,3,"AliTPCAltroEmulator::BaselineCorrection1","MinSamplesaboveThreshold");
   fZSUPresamples               = InRange(Presamples,0,3,"AliTPCAltroEmulator::BaselineCorrection1","Presamples");
   fZSUPostsamples              = InRange(Postsamples,0,7,"AliTPCAltroEmulator::BaselineCorrection1","Postsamples");
-  fADCkeep = (Short_t *)calloc(sizeof(short),ftimebins);
   
   for(Int_t i = 0; i < ftimebins; i++){
     fADCkeep[i] = 0;
@@ -449,12 +391,11 @@ void AliTPCAltroEmulator::SetChannelData(Int_t timebins, Short_t* channelData) {
  *     Runs the emulation of all configured Modules. This changes then the content of the
  *     input Array
  */
-void AliTPCAltroEmulator::RunEmulation(){
+void AliTPCAltroEmulator::RunEmulation(Int_t roc){
   //
   // Runs the emulation of all configured Modules.
   //
 
-
   if (!fChannelShort) {
     printf("ERROR cant run Altro Emulation: Channel input not set.\nUse for example: SetChannelData(Int_t timebins, Short_t* Channel)\n");
     return;
@@ -478,11 +419,28 @@ void AliTPCAltroEmulator::RunEmulation(){
   
   //cout << "AliTPCAltroEmulator::RunEmulation | start TCF on: " << fOnTCF << " configures: " << fConfiguredTCF << endl;
   if(fOnTCF == 1){
-    if(fConfiguredTCF == 1){
-      TailCancellationFilterFixedPoint(fTCFK1Int, fTCFK2Int, fTCFK3Int, fTCFL1Int, fTCFL2Int, fTCFL3Int);
-    }else{
-      cout << "ERROR cant run Tail Cancellation Filter because not configured" << endl;
-      return;
+    if (roc==-1) { // use one set of TCF params
+      if(fConfiguredTCF == 1){
+       TailCancellationFilterFixedPoint(fTCFK1Int, fTCFK2Int, fTCFK3Int, fTCFL1Int, fTCFL2Int, fTCFL3Int);
+      }else{
+       cout << "ERROR cant run Tail Cancellation Filter because not configured" << endl;
+       return;
+      }
+    } else { // use different TCF params for IROC and OROC
+      if(fConfiguredTCFraw == 1){
+       if (roc==0)      //IROC
+         TailCancellationFilterFixedPoint(fTCFK1IntROC[0], fTCFK2IntROC[0], fTCFK3IntROC[0], 
+                                          fTCFL1IntROC[0], fTCFL2IntROC[0], fTCFL3IntROC[0]);
+       else if (roc==1) // OROC
+         TailCancellationFilterFixedPoint(fTCFK1IntROC[1], fTCFK2IntROC[1], fTCFK3IntROC[1], 
+                                          fTCFL1IntROC[1], fTCFL2IntROC[1], fTCFL3IntROC[1]);
+       else
+         cout << "ERROR cant run Tail Cancellation Filter because TCF settings for ROC not found" << endl;
+      } else {
+       cout << "ERROR cant run Tail Cancellation Filter because not configured (for RAW data files!)" << endl;
+       return;
+      }
+
     }
   }
   
@@ -520,7 +478,18 @@ void AliTPCAltroEmulator::BaselineCorrection1(Int_t mode, Int_t ValuePeDestal, I
 
   //VPD == 0 !!
   Int_t fixedPeDestal = 0;
-  
+
+  // take first and last bins to calculate a mean pedestal value
+  Int_t window = 3;
+  Int_t meanPeDestal = 0;
+  if (mode == kDINxMPD && ftimebins>=6) {
+    for(Int_t i = 0; i < window; i++) {
+      meanPeDestal += fChannelShort[i];
+      meanPeDestal += fChannelShort[ftimebins-1-i];
+    }
+    meanPeDestal /= (window*2);
+  }
+    
   if(polarity ==1){
     for(Int_t i = 0; i < ftimebins; i++){
       fChannelShort[i]  = 1023 - fChannelShort[i];
@@ -572,12 +541,16 @@ void AliTPCAltroEmulator::BaselineCorrection1(Int_t mode, Int_t ValuePeDestal, I
     for(Int_t i = 0; i < ftimebins; i++)
       fChannelShort[i]  = PedestalMem[i] - fixedPeDestal;
     break;
+  case kDINxMPD:
+    for(Int_t i = 0; i < ftimebins; i++)
+      fChannelShort[i]  = fChannelShort[i] - meanPeDestal;
+    break;
   }
 }
 
 Int_t AliTPCAltroEmulator::Multiply36(Int_t P, Int_t N){
   //
-  // multiply function to emulate the 36 bit fixed poInt_t multiplication of the Altro.
+  // multiply function to emulate the 36 bit fixed point multiplication of the Altro.
   //
   long long retval =0;
   long long temp = 0;
@@ -625,6 +598,9 @@ void AliTPCAltroEmulator::TailCancellationFilterFixedPoint(Int_t K1, Int_t K2, I
   Int_t dout = 0;
   Int_t din = 0;
   Int_t bit = 0;
+
+  //  printf("%5d %5d %5d %5d %5d %5d\n",K1,K2,K3,L1,L2,L3);
+
   for(Int_t i = 0; i < ftimebins; i++){
     din = fChannelShort[i];
     
@@ -819,8 +795,8 @@ void AliTPCAltroEmulator::Clipping(){
   // implement if no BC2 clipping has to run
   //
   for(Int_t i = 0; i < ftimebins; i++){
-    if(fChannelShort[i] < 0)
-      fChannelShort[i] = 0;
+    if(fChannelShort[i] < -1)
+      fChannelShort[i] = -1;
   }
 }
 
@@ -832,10 +808,12 @@ void AliTPCAltroEmulator::Zerosuppression(Int_t Threshold, Int_t MinSamplesabove
   //TODO: Implement "Altro zsu merging"
   //Int_t Postsamplecounter = 0;
   //Int_t setPostsample = 0;
+
   for(Int_t i = 0; i < ftimebins; i++){
-    if(fChannelShort[i] >= Threshold){
+    if(fChannelShort[i] >= Threshold)
       fADCkeep[i] = 1;
-    }
+    else
+      fADCkeep[i] = 0;
   }
 
   Int_t startofclustersequence = -1;
@@ -901,7 +879,7 @@ void AliTPCAltroEmulator::Zerosuppression(Int_t Threshold, Int_t MinSamplesabove
 
   for(Int_t i = 0; i < ftimebins; i++){
     if( !GetKeepChannel(i) ) {
-      SetElement(fChannelShort,i+1,-1); // set non relevant data to -1
+      SetElement(fChannelShort,i,-1); // set non relevant data to -1
     }
   }
 
@@ -1002,11 +980,11 @@ Int_t AliTPCAltroEmulator::InRange(Int_t parameter,Int_t Low,Int_t High,const ch
   char out[255];
   Int_t retval;
   if(parameter > High){
-    sprintf(out,"Error | %s | Parameter %s is to big, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,High);
+    snprintf(out,255,"Error | %s | Parameter %s is to big, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,High);
     cout << out << endl;
     retval = High;
   }else if(parameter < Low){
-    sprintf(out,"Error | %s | Parameter %s is to small, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,Low);
+    snprintf(out,255,"Error | %s | Parameter %s is to small, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,Low);
     cout << out << endl;
     retval = Low;
   }else{
@@ -1043,79 +1021,82 @@ Bool_t AliTPCAltroEmulator::WriteEvent(Int_t ievent) {
     FILE *file=fopen(Form("%s/raw%d/TPC_%03d.ddl",
                          fDDLFolderName.Data(),ievent,768+ddlID),
                     "wb");
-    if (!file) return kFALSE;
-    Int_t i32;
-    // write CDH (first word to be altered later)
-    for (i32=0;i32<8;++i32)
-      fRawData[i32]=cdhDDL[i32];
-
-    // process payload
-    for (Int_t hwaddr=0;hwaddr<4096;++hwaddr) if (channelsDDL[hwaddr]) {
-      Short_t *adcsChannel=adcsDDL+hwaddr*1024;
-      // merge custers
-      // TODO: acqusition window
-      for (Int_t it=0;it<1024-3;++it) {
-       if (adcsChannel[it]>=0&&adcsChannel[it+3]>=0) {
-         if (adcsChannel[it+1]<0) {
-           //      printf("merge");
-           adcsChannel[it+1]=0;
-         }
-         if (adcsChannel[it+2]<0) {
-           //      printf("merge");
-           adcsChannel[it+2]=0;
+    if (!file) {
+      return kFALSE;
+    } else { 
+      Int_t i32;
+      // write CDH (first word to be altered later)
+      for (i32=0;i32<8;++i32)
+       fRawData[i32]=cdhDDL[i32];
+      
+      // process payload
+      for (Int_t hwaddr=0;hwaddr<4096;++hwaddr) if (channelsDDL[hwaddr]) {
+       Short_t *adcsChannel=adcsDDL+hwaddr*1024;
+       // merge custers
+       // TODO: acqusition window
+       for (Int_t it=0;it<1024-3;++it) {
+         if (adcsChannel[it]>=0&&adcsChannel[it+3]>=0) {
+           if (adcsChannel[it+1]<0) {
+             //            printf("merge");
+             adcsChannel[it+1]=0;
+           }
+           if (adcsChannel[it+2]<0) {
+             //            printf("merge");
+             adcsChannel[it+2]=0;
+           }
          }
        }
-      }
-      Int_t i10=3;
-      Int_t icw=0;
-      Int_t its=1;
-      Int_t cw =0;
-      Int_t ts =0;
-      for (Int_t it=1023;it>=0;--it) {
-       Short_t w10=adcsChannel[it];
-       if (w10>=0) {
-         if (cw<0) {
-           icw=i10++;
-           its=i10++;
-           cw =0    ;
-           ts=it    ;
+       Int_t i10=3;
+       Int_t icw=0;
+       Int_t its=1;
+       Int_t cw =0;
+       Int_t ts =0;
+       for (Int_t it=1023;it>=0;--it) {
+         Short_t w10=adcsChannel[it];
+         if (w10>=0) {
+           if (cw<0) {
+             icw=i10++;
+             its=i10++;
+             cw =0    ;
+             ts=it    ;
+           }
+           fRawData[i32+i10/3]|=w10<<(10*(2-i10%3));
+           ++i10;
+           ++cw;
          }
-         fRawData[i32+i10/3]|=w10<<(10*(2-i10%3));
-         ++i10;
-         ++cw;
-       }
-       else {
-         if (cw>=0) {
-           cw+=2;
-           fRawData[i32+icw/3]|=cw <<(10*(2-icw%3));
-           fRawData[i32+its/3]|=ts <<(10*(2-its%3));
-           cw=-1;
+         else {
+           if (cw>=0) {
+             cw+=2;
+             fRawData[i32+icw/3]|=cw <<(10*(2-icw%3));
+             fRawData[i32+its/3]|=ts <<(10*(2-its%3));
+             cw=-1;
+           }
          }
        }
+       fRawData[i32]=0x1<<30|(i10-3)<<16|hwaddr;
+       i32+=(i10+2)/3;
+       
+       // clean up
+       for (Int_t i=0;i<1024;++i) adcsChannel[i]=-1;
+       channelsDDL[hwaddr]=kFALSE;
       }
-      fRawData[i32]=0x1<<30|(i10-3)<<16|hwaddr;
-      i32+=(i10+2)/3;
+      
+      // write RCU trailer
+      fRawData[i32]=0x2<<30|(i32-8);i32++;
+      for (Int_t i=0;i<8;++i)
+       fRawData[i32++]=trailerDDL[i];
+      
+      // write first word of CDH
+      fRawData[0]=i32*4;
+      
+      Int_t nwritten=fwrite(fRawData,sizeof(UInt_t),i32,file);
+      fclose(file);
 
+      if (nwritten!=i32) return kFALSE;
+      
       // clean up
-      for (Int_t i=0;i<1024;++i) adcsChannel[i]=-1;
-      channelsDDL[hwaddr]=kFALSE;
+      do {fRawData[--i32]=0;} while (i32>0);
     }
-
-    // write RCU trailer
-    fRawData[i32]=0x2<<30|(i32-8);i32++;
-    for (Int_t i=0;i<8;++i)
-      fRawData[i32++]=trailerDDL[i];
-
-    // write first word of CDH
-    fRawData[0]=i32*4;
-    
-    Int_t nwritten=fwrite(fRawData,sizeof(UInt_t),i32,file);
-    if (nwritten!=i32) return kFALSE;
-
-    // clean up
-    do {fRawData[--i32]=0;} while (i32>0);
-
-    fclose(file);
   }
   return kTRUE;
 }
@@ -1161,11 +1142,14 @@ Bool_t AliTPCAltroEmulator::ConvertRawFilesToDate(Int_t nevents) {
   
   char command[100];
   FILE *pipe;
-  if (fReader->GetRunNumber()>0)
+
+  printf(" RAW to DATE CONVERSION: Run Number %d\n",fRunNumber);
+
+  if (fRunNumber>0)
     pipe=gSystem->OpenPipe(Form("dateStream -c -s -D -o %s -C -# %d -run %d", 
                                fOutputDateFileName.Data(),
                                nevents,
-                               fReader->GetRunNumber()),
+                               fRunNumber),
                           "w");
   else
     pipe=gSystem->OpenPipe(Form("dateStream -c -s -D -o %s -C -# %d", 
@@ -1176,7 +1160,7 @@ Bool_t AliTPCAltroEmulator::ConvertRawFilesToDate(Int_t nevents) {
     fprintf(stderr,"error: cannot execute command: %s",command);
     return kFALSE;
   }
-  
+
   for (Int_t ievent=0;ievent<nevents;++ievent) {
     UInt_t detectorPattern = 0xFFFFFFFF;
     fprintf(pipe, "GDC DetectorPattern %u\n", detectorPattern);
@@ -1246,6 +1230,8 @@ Bool_t AliTPCAltroEmulator::ConvertDateToRoot() {
 
   // from $ALICE_ROOT/STEER/AliSimulation.cxx
 
+  printf(" DATE to ROOT CONVERSION: Run Number %d\n",fRunNumber);
+
   // ALIMDC setup
   const Int_t kDBSize    = 2000000000; //2GB
   const Int_t kTagDBSize = 1000000000;
@@ -1302,39 +1288,46 @@ void AliTPCAltroEmulator::RunEmulationOnRAWdata(AliRawReader *reader, Int_t plot
 
   InitBuffers();
   
-  TH1F hisO("DINO","DINO",1014,0,1014); 
-  TH1F his("DIN","DIN",1014,0,1014); 
-  TCanvas c1("c1","c1");
   Int_t chanCount=0;
+  TH1F hisO("DINO","DINO",1024,0,1024); 
+  TH1F his("DIN","DIN",1024,0,1024); 
   his.GetYaxis()->SetRangeUser(-20,90);
-  Short_t *data = new Short_t[1014]; 
-
+  Short_t *data = new Short_t[1024]; 
+  TCanvas *c1 =0;
+  if (plotFlag) {
+    c1 = new TCanvas("c1","c1");
+    c1->SetGridx(); c1->SetGridy();
+  }
 
   // event loop
   Int_t ievent=0;
   while (fReader->NextEvent()) {
     
+    if (fReader->GetRunNumber()>0) fRunNumber=fReader->GetRunNumber();
     gSystem->Exec(Form("mkdir -p %s/raw%d/",fDDLFolderName.Data(),ievent));
     GDC2DDLs(const_cast<AliRawVEvent*>(fReader->GetEvent()),ievent);
     
     Int_t ddlC =0;
     while (fDecoder->NextDDL()) {
       Int_t ddlID=fDecoder->GetDDLNumber();
-      printf("ddl: %d (%d)\n",ddlID,ddlC++);
+      printf("ddl: %d (%d/216)\n",ddlID,++ddlC);
      
-      Bool_t  *channelsDDL=fChannels+ddlID*4096     ;
-      Short_t *adcsDDL    =fADCs    +ddlID*4096*1024;
-      UInt_t  *cdhDDL     =fCDHs    +ddlID*8        ;
-      UInt_t  *trailerDDL =fTrailers+ddlID*9        ;
+      Bool_t  *channelsDDL=fChannels+ddlID*4096      ;
+      Short_t *adcsDDL    =fADCs    +ddlID*4096*1024 ;
+      UInt_t  *cdhDDL     =fCDHs    +ddlID*8         ;
+      UInt_t  *trailerDDL =fTrailers+ddlID*9         ;
       
       // CDH 
       for (Int_t i=0;i<8;++i)
-       // just to show how ugly it is...
-       cdhDDL[i]=reinterpret_cast<UInt_t*>(const_cast<AliRawDataHeader*>(fReader->GetDataHeader()))[i]; 
+       // just to show how ugly it is...
+       cdhDDL[i]=reinterpret_cast<UInt_t*>(const_cast<AliRawDataHeader*>(fReader->GetDataHeader()))[i]; 
       
       // PAYLOAD
       while (fDecoder->NextChannel()) {
        Int_t hwaddr=fDecoder->GetHWAddress();
+       Int_t sector=fDecoder->GetSector();
+       Int_t row=fDecoder->GetRow();
+       Int_t pad=fDecoder->GetPad();
        Short_t *adcsChannel=adcsDDL+hwaddr*1024;
        while (fDecoder->NextBunch()) {
          UInt_t          ts     =fDecoder->GetStartTimeBin();
@@ -1344,7 +1337,7 @@ void AliTPCAltroEmulator::RunEmulationOnRAWdata(AliRawReader *reader, Int_t plot
            Short_t s=signals[ci];
            Int_t   t=ts-ci;
            // TODO aqcuisition window
-           if (20<=t&&t<=1014) {
+           if (0<=t&&t<(1024-3)) {
              channelsDDL[hwaddr]=kTRUE;
              if (adcsChannel[t]<0)
                adcsChannel[t]=s;
@@ -1356,50 +1349,48 @@ void AliTPCAltroEmulator::RunEmulationOnRAWdata(AliRawReader *reader, Int_t plot
          }
        }
 
-
-       if (1) {
-
-         // search start of aquisition
-         Int_t t0 = 0;          while (adcsChannel[t0]==-1) t0++;
-         // search end of aquisition
-         Int_t tE = 1014; while (adcsChannel[tE]==-1) tE--;
-         //    printf("S:%d  E:%d\n",t0,tE);
+       // search start of aquisition
+       Int_t t0 = 0;    while (adcsChannel[t0]==-1) t0++;
+       // search end of aquisition
+       Int_t tE = 1024; while (adcsChannel[tE]==-1) tE--;
        
-         // SR TEST:
-         // channel is complete - Perform Altro Emulation
-         if (plotFlag && !(chanCount%1000)) {
-           for (Int_t t=0; t<1014; t++) {
-             his.SetBinContent(t+1,adcsChannel[t]);
-           }
-           his.SetStats(0); his.GetXaxis()->SetTitle("timebin"); 
-           his.DrawCopy();
+       // channel is complete - Perform Altro Emulation
+       if (plotFlag!=0 && !(chanCount%plotFlag) ) {
+         for (Int_t t=0; t<1024; t++) {
+           his.SetBinContent(t+1,adcsChannel[t]);
          }
-         // FEED THE ALTRO EMULATOR WITH CLEAN SIGNAL (not aquisition window ghosts)
-         Int_t timebins = tE-t0;
-
-         for (Int_t t=t0;t<(t0+timebins);t++)
-           data[t-t0]=adcsChannel[t];
-         //SetChannelData(timebins, adcsChannel);//data);
-         SetChannelData(timebins,data);
-         RunEmulation(); // emulation on single channel
-         for (Int_t t=t0;t<(t0+timebins);t++) 
-           adcsChannel[t]=data[t-t0];
+         his.SetTitle(Form("sig_sec%d_row%d_pad%d",sector,row,pad));
+         his.SetStats(0); his.GetXaxis()->SetTitle("timebin"); 
+         his.DrawCopy();
+       }
+
+       // FEED THE ALTRO EMULATOR WITH CLEAN SIGNAL (no aquisition-window ghosts)
+       Int_t timebins = tE-t0+1;
+       for (Int_t t=t0;t<(t0+timebins);t++)
+         data[t-t0]=adcsChannel[t];
+       SetChannelData(timebins,data);
+       
+       Int_t roc = (sector%36)>=18; // 0 for IROC, 1 for OROC
+       RunEmulation(roc); 
+       for (Int_t t=t0;t<(t0+timebins);t++) 
+         adcsChannel[t]=data[t-t0];
          
-         // SR TEST:
-         if (plotFlag && !(chanCount%1000) ) {
-           for (Int_t t=0; t<1014; t++)
-             hisO.SetBinContent(t+1,adcsChannel[t]);
-           hisO.SetStats(0); hisO.SetLineColor(2);
-           hisO.DrawCopy("same");
+       if (plotFlag!=0 && !(chanCount%plotFlag) ) {
+         for (Int_t t=0; t<1024; t++)
+           hisO.SetBinContent(t+1,adcsChannel[t]);
+         hisO.SetStats(0); hisO.SetLineColor(2);
+         hisO.DrawCopy("same");
            
-           c1.SaveAs(Form("/tmp/chanCount_%07d.png",chanCount));
+         c1->SaveAs(Form("%s/sig_sec%02d_row%02d_pad%03d_%s_%d%d%d%d%d.png",
+                         fDDLFolderName.Data(),sector,row,pad,
+                         fOutputRootFileName.Data(),
+                         fOnBSL1,fOnTCF,fOnBSL2,fOnClip,fOnZSU));
            
-           his.Reset();
-           hisO.Reset();
+         his.Reset();
+         hisO.Reset();
 
-         }
-         chanCount++;
        }
+       chanCount++;
 
       }
       
@@ -1407,14 +1398,14 @@ void AliTPCAltroEmulator::RunEmulationOnRAWdata(AliRawReader *reader, Int_t plot
       UChar_t *rcuTrailer;
       fDecoder->GetRCUTrailerData(rcuTrailer);
       for (Int_t i=0;i<= /* (!) */ fDecoder->GetRCUTrailerSize()/4;++i)
-       trailerDDL[i]=reinterpret_cast<UInt_t*>(rcuTrailer)[i]; // again: UGLY!
+       trailerDDL[i]=reinterpret_cast<UInt_t*>(rcuTrailer)[i]; // again: UGLY!
       
     }
             
-    WriteEvent(ievent++);
+    if (ddlC>0) WriteEvent(ievent++);
   }
 
-  delete data; // free space
+  delete[] data; // free space
 
   // convert to date and back
   ConvertRawFilesToDate(ievent);