{
// generates some fake data to test the algorithms

TRandom *rnd = new TRandom();

UInt_t runNr;
TString command;




// NOISE SCAN:
runNr = 99;
for (UInt_t ldc=0; ldc<4; ldc++) {
  TString mkdir1 = Form("mkdir Run%d >& /dev/null",runNr);
  system(mkdir1.Data());
  TString ldcDir = Form("./Run%d/LDC%d",runNr,ldc);
  TString mkdir2 = Form("mkdir %s >& /dev/null",ldcDir.Data());
  system(mkdir2.Data());
  TString idFileName = Form("%s/FXSids_run_%d.txt",ldcDir.Data(),runNr);
  for (UInt_t eq=0+ldc*5; eq<5+ldc*5; eq++) {
    AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
    TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",ldcDir.Data(),runNr,eq);
    AliITSOnlineSPDscanSingle* single = new AliITSOnlineSPDscanSingle(fileName.Data());
    single->SetTriggers(10000);
    for (UInt_t hs=0; hs<6; hs++) {
      single->SetHitEventsTot(hs,(UInt_t)(rnd->Rndm()*10000));
      for (UInt_t chip=0; chip<10; chip++) {
	single->SetHitEvents(hs,chip,(UInt_t)(rnd->Rndm()*5000));
	for (UInt_t col=0; col<32; col++) {
	  for (UInt_t row=0; row<256; row++) {
	    if (rnd->Rndm()<0.0001) {
	      single->SetHits(hs,chip,col,row,(UInt_t)(rnd->Rndm()*1000));
	      handler->SetNoisyPixel(eq,hs,chip,col,row);
	    }
	  }
	}
      }
    }
    handler->SetFileLocation(ldcDir.Data());
    handler->WriteNoisyToFile(eq);
    delete handler;
    delete single;
    TString id = Form("SPD_ref_scan_%d",eq);
    TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
    system(command.Data());
  }
  // tar the noisy files...:
  command = Form("cd %s; tar -cf noisy_scan.tar SPD_Noisy*.root",ldcDir.Data());
  system(command.Data());
  command = Form("cd %s; rm -f SPD_Noisy*.root",ldcDir.Data());
  system(command.Data());
  TString id = "SPD_scan_noisy";
  TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
  system(command.Data());
}

// FO SCAN:
UInt_t scanNr=0;
runNr = 991+scanNr;
for (UInt_t ldc=0; ldc<4; ldc++) {
  TString mkdir1 = Form("mkdir Run%d >& /dev/null",runNr);
  system(mkdir1.Data());
  TString ldcDir = Form("./Run%d/LDC%d",runNr,ldc);
  TString mkdir2 = Form("mkdir %s >& /dev/null",ldcDir.Data());
  system(mkdir2.Data());
  TString idFileName = Form("%s/FXSids_run_%d.txt",ldcDir.Data(),runNr);
  for (UInt_t eq=0+ldc*5; eq<5+ldc*5; eq++) {
    TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",ldcDir.Data(),runNr,eq);
    TString fileNameLink = Form("../../Run%d/LDC%d/SPDcal_run_%d_eq_%d.root",99,ldc,99,eq);
    TString command = Form("ln -s %s %s",fileNameLink.Data(),fileName.Data());
    system(command.Data());
    TString id = Form("SPD_ref_fo_%d",eq);
    command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
    system(command.Data());
  }
}

// ALL OTHER SCANS:
//for (UInt_t scanNr=0; scanNr<5; scanNr++) {
UInt_t scanNr=0;
runNr = 990+scanNr;
for (UInt_t ldc=0; ldc<4; ldc++) {
  TString mkdir1 = Form("mkdir Run%d >& /dev/null",runNr);
  system(mkdir1.Data());
  TString ldcDir = Form("./Run%d/LDC%d",runNr,ldc);
  TString mkdir2 = Form("mkdir %s >& /dev/null",ldcDir.Data());
  system(mkdir2.Data());
  TString idFileName = Form("%s/FXSids_run_%d.txt",ldcDir.Data(),runNr);
  for (UInt_t eq=0+ldc*5; eq<5+ldc*5; eq++) {
    TString fileName = Form("%s/SPDcal_run_%d_eq_%d.root",ldcDir.Data(),runNr,eq);
    TString fileNameLink = Form("../../Run%d/LDC%d/SPDcal_run_%d_eq_%d.root",99,ldc,99,eq);
    TString command = Form("ln -s %s %s",fileNameLink.Data(),fileName.Data());
    system(command.Data());
    TString id = Form("SPD_ref_scan_%d",eq);
    command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
    system(command.Data());
  }
}
//}


// PHYSICS RUN:
runNr = 999;
AliITSOnlineCalibrationSPDhandler* handler = new AliITSOnlineCalibrationSPDhandler();
TString mkdir1 = Form("mkdir Run%d >& /dev/null",runNr);
system(mkdir1.Data());
TString monDir = Form("./Run%d/MON%d",runNr,0);
TString mkdir2 = Form("mkdir %s >& /dev/null",monDir.Data());
system(mkdir2.Data());
TString idFileName = Form("%s/FXSids_run_%d.txt",monDir.Data(),runNr);
for (UInt_t eq=0; eq<20; eq++) {

  TString fileNameN = Form("%s/SPDphys_run_%d_eq_%d.root",monDir.Data(),runNr,eq);
  AliITSOnlineSPDphys* physN = new AliITSOnlineSPDphys(fileNameN.Data());
  physN->SetEqNr(eq);
  physN->AddRunNr(runNr);
  physN->SetNrEvents(100000);
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      for (UInt_t col=0; col<32; col++) {
	for (UInt_t row=0; row<256; row++) {
	  physN->SetHits(hs,chip,col,row,(UInt_t)(rnd->Rndm()*1000));
	}
      }
    }
  }
  delete physN;
  TString fileNameD = Form("%s/SPDphys_dead_run_%d_%d_eq_%d.root",monDir.Data(),runNr-3,runNr,eq);
  AliITSOnlineSPDphys* physD = new AliITSOnlineSPDphys(fileNameD.Data());
  physD->SetEqNr(eq);
  physD->AddRunNr(runNr-3);
  physD->AddRunNr(runNr-2);
  physD->AddRunNr(runNr-1);
  physD->AddRunNr(runNr);
  physD->SetNrEvents(1000000);
  for (UInt_t hs=0; hs<6; hs++) {
    for (UInt_t chip=0; chip<10; chip++) {
      for (UInt_t col=0; col<32; col++) {
	for (UInt_t row=0; row<256; row++) {
	  physD->SetHits(hs,chip,col,row,(UInt_t)(rnd->Rndm()*10000));
	}
      }
    }
  }
  delete physD;
  for (UInt_t i=0; i<(UInt_t)(rnd->Rndm()*10); i++) {
    UInt_t hs   = (UInt_t)(rnd->Rndm()*6);
    UInt_t chip = (UInt_t)(rnd->Rndm()*10);
    UInt_t col  = (UInt_t)(rnd->Rndm()*32);
    UInt_t row  = (UInt_t)(rnd->Rndm()*256);
    handler->SetNoisyPixel(eq,hs,chip,col,row);
  }
  for (UInt_t i=0; i<(UInt_t)(rnd->Rndm()*10000); i++) {
    UInt_t hs   = (UInt_t)(rnd->Rndm()*6);
    UInt_t chip = (UInt_t)(rnd->Rndm()*10);
    UInt_t col  = (UInt_t)(rnd->Rndm()*32);
    UInt_t row  = (UInt_t)(rnd->Rndm()*256);
    handler->SetDeadPixel(eq,hs,chip,col,row);
  }
  handler->SetFileLocation(monDir.Data());
  handler->WriteNoisyToFile(eq);
  handler->WriteDeadToFile(eq);
}

// tar the ref files...:
command = Form("cd %s; tar -cf ref_phys.tar SPDphys_run*.root",monDir.Data());
system(command.Data());
command = Form("cd %s; rm -f SPDphys_run*.root",monDir.Data());
system(command.Data());
TString id = "SPD_ref_phys";
TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
system(command.Data());

command = Form("cd %s; tar -cf ref_phys_dead.tar SPDphys_dead_run*.root",monDir.Data());
system(command.Data());
command = Form("cd %s; rm -f SPDphys_dead_run*.root",monDir.Data());
system(command.Data());
TString id = "SPD_ref_phys_dead";
TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
system(command.Data());

// tar the noisy and dead files...:
command = Form("cd %s; tar -cf noisy_phys.tar SPD_Noisy*.root",monDir.Data());
system(command.Data());
command = Form("cd %s; rm -f SPD_Noisy*.root",monDir.Data());
system(command.Data());
TString id = "SPD_phys_noisy";
TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
system(command.Data());

command = Form("cd %s; tar -cf dead_phys.tar SPD_Dead*.root",monDir.Data());
system(command.Data());
command = Form("cd %s; rm -f SPD_Dead*.root",monDir.Data());
system(command.Data());
TString id = "SPD_phys_dead";
TString command = Form("echo '%s' >> %s",id.Data(),idFileName.Data());
system(command.Data());


delete handler;





}
