]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ANALYSIS/AliAnalysisAlien.cxx
RC12, RC17 violation: suppression
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisAlien.cxx
index 66640c800a4f6681a277f962cf083ece62e1b052..b5e5ef09a0a82c8c1166d22d4151f7ad19c87d3c 100644 (file)
@@ -54,17 +54,21 @@ AliAnalysisAlien::AliAnalysisAlien()
                   fMaxMergeFiles(0),
                   fNsubmitted(0),
                   fProductionMode(0),
+                  fOutputToRunNo(0),
                   fRunNumbers(),
                   fExecutable(),
                   fExecutableCommand(),
                   fArguments(),
+                  fExecutableArgs(),
                   fAnalysisMacro(),
                   fAnalysisSource(),
+                  fAdditionalRootLibs(),
                   fAdditionalLibs(),
                   fSplitMode(),
                   fAPIVersion(),
                   fROOTVersion(),
                   fAliROOTVersion(),
+                  fExternalPackages(),
                   fUser(),
                   fGridWorkingDir(),
                   fGridDataDir(),
@@ -80,6 +84,8 @@ AliAnalysisAlien::AliAnalysisAlien()
                   fCloseSE(),
                   fFriendChainName(),
                   fJobTag(),
+                  fOutputSingle(),
+                  fRunPrefix(),
                   fInputFiles(0),
                   fPackages(0)
 {
@@ -101,17 +107,21 @@ AliAnalysisAlien::AliAnalysisAlien(const char *name)
                   fMaxMergeFiles(0),
                   fNsubmitted(0),
                   fProductionMode(0),
+                  fOutputToRunNo(0),
                   fRunNumbers(),
                   fExecutable(),
                   fExecutableCommand(),
                   fArguments(),
+                  fExecutableArgs(),
                   fAnalysisMacro(),
                   fAnalysisSource(),
+                  fAdditionalRootLibs(),
                   fAdditionalLibs(),
                   fSplitMode(),
                   fAPIVersion(),
                   fROOTVersion(),
                   fAliROOTVersion(),
+                  fExternalPackages(),
                   fUser(),
                   fGridWorkingDir(),
                   fGridDataDir(),
@@ -127,6 +137,8 @@ AliAnalysisAlien::AliAnalysisAlien(const char *name)
                   fCloseSE(),
                   fFriendChainName(),
                   fJobTag(),
+                  fOutputSingle(),
+                  fRunPrefix(),
                   fInputFiles(0),
                   fPackages(0)
 {
@@ -148,17 +160,21 @@ AliAnalysisAlien::AliAnalysisAlien(const AliAnalysisAlien& other)
                   fMaxMergeFiles(other.fMaxMergeFiles),
                   fNsubmitted(other.fNsubmitted),
                   fProductionMode(other.fProductionMode),
+                  fOutputToRunNo(other.fOutputToRunNo),
                   fRunNumbers(other.fRunNumbers),
                   fExecutable(other.fExecutable),
                   fExecutableCommand(other.fExecutableCommand),
                   fArguments(other.fArguments),
+                  fExecutableArgs(other.fExecutableArgs),
                   fAnalysisMacro(other.fAnalysisMacro),
                   fAnalysisSource(other.fAnalysisSource),
+                  fAdditionalRootLibs(other.fAdditionalRootLibs),
                   fAdditionalLibs(other.fAdditionalLibs),
                   fSplitMode(other.fSplitMode),
                   fAPIVersion(other.fAPIVersion),
                   fROOTVersion(other.fROOTVersion),
                   fAliROOTVersion(other.fAliROOTVersion),
+                  fExternalPackages(other.fExternalPackages),
                   fUser(other.fUser),
                   fGridWorkingDir(other.fGridWorkingDir),
                   fGridDataDir(other.fGridDataDir),
@@ -174,6 +190,8 @@ AliAnalysisAlien::AliAnalysisAlien(const AliAnalysisAlien& other)
                   fCloseSE(other.fCloseSE),
                   fFriendChainName(other.fFriendChainName),
                   fJobTag(other.fJobTag),
+                  fOutputSingle(other.fOutputSingle),
+                  fRunPrefix(other.fRunPrefix),
                   fInputFiles(0),
                   fPackages(0)
 {
@@ -223,17 +241,21 @@ AliAnalysisAlien &AliAnalysisAlien::operator=(const AliAnalysisAlien& other)
       fMaxMergeFiles           = other.fMaxMergeFiles;
       fNsubmitted              = other.fNsubmitted;
       fProductionMode          = other.fProductionMode;
+      fOutputToRunNo           = other.fOutputToRunNo;
       fRunNumbers              = other.fRunNumbers;
       fExecutable              = other.fExecutable;
       fExecutableCommand       = other.fExecutableCommand;
       fArguments               = other.fArguments;
+      fExecutableArgs          = other.fExecutableArgs;
       fAnalysisMacro           = other.fAnalysisMacro;
       fAnalysisSource          = other.fAnalysisSource;
+      fAdditionalRootLibs      = other.fAdditionalRootLibs;
       fAdditionalLibs          = other.fAdditionalLibs;
       fSplitMode               = other.fSplitMode;
       fAPIVersion              = other.fAPIVersion;
       fROOTVersion             = other.fROOTVersion;
       fAliROOTVersion          = other.fAliROOTVersion;
+      fExternalPackages        = other.fExternalPackages;
       fUser                    = other.fUser;
       fGridWorkingDir          = other.fGridWorkingDir;
       fGridDataDir             = other.fGridDataDir;
@@ -249,6 +271,8 @@ AliAnalysisAlien &AliAnalysisAlien::operator=(const AliAnalysisAlien& other)
       fCloseSE                 = other.fCloseSE;
       fFriendChainName         = other.fFriendChainName;
       fJobTag                  = other.fJobTag;
+      fOutputSingle            = other.fOutputSingle;
+      fRunPrefix               = other.fRunPrefix;
       if (other.fInputFiles) {
          fInputFiles = new TObjArray();
          TIter next(other.fInputFiles);
@@ -281,7 +305,7 @@ void AliAnalysisAlien::AddRunNumber(Int_t run)
 {
 // Add a run number to the list of runs to be processed.
    if (fRunNumbers.Length()) fRunNumbers += " ";
-   fRunNumbers += Form("%d", run);
+   fRunNumbers += Form("%s%d", fRunPrefix.Data(), run);
 }   
 
 //______________________________________________________________________________
@@ -300,7 +324,15 @@ void AliAnalysisAlien::AddDataFile(const char *lfn)
    if (!fInputFiles) fInputFiles = new TObjArray();
    fInputFiles->Add(new TObjString(lfn));
 }
-   
+
+//______________________________________________________________________________
+void AliAnalysisAlien::AddExternalPackage(const char *package)
+{
+// Adds external packages w.r.t to the default ones (root,aliroot and gapi)
+   if (fExternalPackages) fExternalPackages += " ";
+   fExternalPackages += package;
+}   
+      
 //______________________________________________________________________________
 Bool_t AliAnalysisAlien::Connect()
 {
@@ -428,7 +460,7 @@ Bool_t AliAnalysisAlien::CheckInputData()
    TObjArray *arr;
    TObjString *os;
    Int_t nruns = 0;
-   TString schunk;
+   TString schunk, schunk2;
    TString path;
    if (!checked) {
       checked = kTRUE;
@@ -472,12 +504,12 @@ Bool_t AliAnalysisAlien::CheckInputData()
    } else {
       Info("CheckDataType", "Using run range [%d, %d]", fRunRange[0], fRunRange[1]);
       for (Int_t irun=fRunRange[0]; irun<=fRunRange[1]; irun++) {
-         path = Form("%s/%d ", fGridDataDir.Data(), irun);
+         path = Form("%s/%s%d ", fGridDataDir.Data(), fRunPrefix.Data(), irun);
          if (!DirectoryExists(path)) {
 //            Warning("CheckInputData", "Run number %d not found in path: <%s>", irun, path.Data());
             continue;
          }
-         path = Form("%s/%d.xml", workdir.Data(),irun);
+         path = Form("%s/%s%d.xml", workdir.Data(),fRunPrefix.Data(),irun);
          TString msg = "\n#####   file: ";
          msg += path;
          msg += " type: xml_collection;";
@@ -485,17 +517,22 @@ Bool_t AliAnalysisAlien::CheckInputData()
          else          msg += " using_tags: No";
          Info("CheckDataType", msg.Data());
          if (fNrunsPerMaster<2) {
-            AddDataFile(Form("%d.xml",irun));
+            AddDataFile(Form("%s%d.xml",fRunPrefix.Data(),irun));
          } else {
             nruns++;
             if (((nruns-1)%fNrunsPerMaster) == 0) {
-               schunk = Form("%d", irun);
+               schunk = Form("%s%d", fRunPrefix.Data(),irun);
             }
+            schunk2 = Form("_%s%d.xml", fRunPrefix.Data(), irun);
             if ((nruns%fNrunsPerMaster)!=0 && irun != fRunRange[1]) continue;
-            schunk += Form("_%d.xml",  irun);
+            schunk += schunk2;
             AddDataFile(schunk);
          }   
       }
+      if (!fInputFiles) {
+         schunk += schunk2;
+         AddDataFile(schunk);
+      }   
    }
    return kTRUE;      
 }   
@@ -524,7 +561,7 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
    TString file;
    TString path;
    Int_t nruns = 0;
-   TString schunk;
+   TString schunk, schunk2;
    TGridCollection *cbase=0, *cadd=0;
    if (!fRunNumbers.Length() && !fRunRange[0]) {
       if (fInputFiles && fInputFiles->GetEntries()) return kTRUE;
@@ -544,7 +581,7 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
          command += " ";
          command += pattern;
          command += conditions;
-         Printf("command: %s", command.Data());
+         printf("command: %s\n", command.Data());
          TGridResult *res = gGrid->Command(command);
          if (res) delete res;
          // Write standard output to file
@@ -606,7 +643,7 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
                cbase = (TGridCollection*)gROOT->ProcessLine(Form("new TAlienCollection(\"%s\", 1000000);",file.Data()));
             } else {
                cadd = (TGridCollection*)gROOT->ProcessLine(Form("new TAlienCollection(\"%s\", 1000000);",file.Data()));
-               Printf("   Merging collection <%s> into masterjob input...", file.Data());
+               printf("   Merging collection <%s> into masterjob input...\n", file.Data());
                cbase->Add(cadd);
                delete cadd;
             }
@@ -618,7 +655,7 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
                Info("CreateDataset", "\n#####   Dataset %s exist. Skipping creation...", schunk.Data());
                continue;
             }        
-            Printf("Exporting merged collection <%s> and copying to AliEn.", schunk.Data());
+            printf("Exporting merged collection <%s> and copying to AliEn\n", schunk.Data());
             cbase->ExportXML(Form("file://%s", schunk.Data()),kFALSE,kFALSE, schunk, "Merged runs");
             TFile::Cp(Form("file:%s",schunk.Data()), Form("alien://%s/%s",workdir.Data(), schunk.Data()));
             if (!FileExists(schunk)) {
@@ -632,11 +669,11 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
    } else {
       // Process a full run range.
       for (Int_t irun=fRunRange[0]; irun<=fRunRange[1]; irun++) {
-         path = Form("%s/%d ", fGridDataDir.Data(), irun);
+         path = Form("%s/%s%d ", fGridDataDir.Data(), fRunPrefix.Data(), irun);
          if (!DirectoryExists(path)) continue;
 //         CdWork();
          if (TestBit(AliAnalysisGrid::kTest)) file = "wn.xml";
-         else file = Form("%d.xml", irun);
+         else file = Form("%s%d.xml", fRunPrefix.Data(), irun);
          if (FileExists(file) && fNrunsPerMaster<2 && !TestBit(AliAnalysisGrid::kTest)) {
             Info("CreateDataset", "\n#####   Dataset %s exist. Skipping creation...", file.Data());
 //            gGrid->Rm(file); 
@@ -672,24 +709,25 @@ Bool_t AliAnalysisAlien::CreateDataset(const char *pattern)
             // Check if the collection for the chunk exist locally.
             Int_t nchunk = (nruns-1)/fNrunsPerMaster;
             if (FileExists(fInputFiles->At(nchunk)->GetName())) continue;
-            Printf("   Merging collection <%s> into %d runs chunk...",file.Data(),fNrunsPerMaster);
+            printf("   Merging collection <%s> into %d runs chunk...\n",file.Data(),fNrunsPerMaster);
             if (((nruns-1)%fNrunsPerMaster) == 0) {
-               schunk = Form("%d", irun);
+               schunk = Form("%s%d", fRunPrefix.Data(), irun);
                cbase = (TGridCollection*)gROOT->ProcessLine(Form("new TAlienCollection(\"%s\", 1000000);",file.Data()));
             } else {
                cadd = (TGridCollection*)gROOT->ProcessLine(Form("new TAlienCollection(\"%s\", 1000000);",file.Data()));
                cbase->Add(cadd);
                delete cadd;
             }
-            if ((nruns%fNrunsPerMaster)!=0 && irun!=fRunRange[1]) {
+            schunk2 = Form("%s_%s%d.xml", schunk.Data(), fRunPrefix.Data(), irun);
+            if ((nruns%fNrunsPerMaster)!=0 && irun!=fRunRange[1] && schunk2 != fInputFiles->Last()->GetName()) {
                continue;
             }   
-            schunk += Form("_%d.xml", irun);
+            schunk = schunk2;
             if (FileExists(schunk)) {
                Info("CreateDataset", "\n#####   Dataset %s exist. Skipping creation...", schunk.Data());
                continue;
             }        
-            Printf("Exporting merged collection <%s> and copying to AliEn.", schunk.Data());
+            printf("Exporting merged collection <%s> and copying to AliEn.\n", schunk.Data());
             cbase->ExportXML(Form("file://%s", schunk.Data()),kFALSE,kFALSE, schunk, "Merged runs");
             if (FileExists(schunk)) {
                Info("CreateDataset", "\n#####   Dataset %s exist. Skipping copy...", schunk.Data());
@@ -758,6 +796,8 @@ Bool_t AliAnalysisAlien::CreateJDL()
       // Set JDL fields
       fGridJDL->SetValue("User", Form("\"%s\"", fUser.Data()));
       fGridJDL->SetExecutable(fExecutable);
+      if (!fArguments.IsNull())
+         fGridJDL->SetArguments(fArguments, "Arguments for the executable command");
 //      fGridJDL->SetTTL((UInt_t)fTTL);
       fGridJDL->SetValue("TTL", Form("\"%d\"", fTTL));
       if (fMaxInitFailed > 0) 
@@ -773,6 +813,18 @@ Bool_t AliAnalysisAlien::CreateJDL()
          fGridJDL->AddToPackages("ROOT", fROOTVersion);
       if (fAPIVersion.Length()) 
          fGridJDL->AddToPackages("APISCONFIG", fAPIVersion);
+      if (!fExternalPackages.IsNull()) {
+         arr = fExternalPackages.Tokenize(" ");
+         TIter next(arr);
+         while ((os=(TObjString*)next())) {
+            TString pkgname = os->GetString();
+            Int_t index = pkgname.Index("::");
+            TString pkgversion = pkgname(index+2, pkgname.Length());
+            pkgname.Remove(index);
+            fGridJDL->AddToPackages(pkgname, pkgversion);
+         }   
+         delete arr;   
+      }   
       fGridJDL->SetInputDataListFormat(fInputFormat);
       fGridJDL->SetInputDataList("wn.xml");
       fGridJDL->AddToInputSandbox(Form("LF:%s/%s", workdir.Data(), fAnalysisMacro.Data()));
@@ -830,6 +882,30 @@ Bool_t AliAnalysisAlien::CreateJDL()
    }
    // Copy jdl to grid workspace   
    if (copy) {
+      // Check if an output directory was defined and valid
+      if (!fGridOutputDir.Length()) {
+         Error("CreateJDL", "You must define AliEn output directory");
+         return kFALSE;
+      } else {
+         if (!fGridOutputDir.Contains("/")) fGridOutputDir = Form("%s/%s", workdir.Data(), fGridOutputDir.Data());
+         if (!DirectoryExists(fGridOutputDir)) {
+            if (gGrid->Mkdir(fGridOutputDir)) {
+               Info("CreateJDL", "\n#####   Created alien output directory %s", fGridOutputDir.Data());
+            } else {
+               Error("CreateJDL", "Could not create alien output directory %s", fGridOutputDir.Data());
+               return kFALSE;
+            }
+         }
+         gGrid->Cd(workdir);
+      }   
+      if (TestBit(AliAnalysisGrid::kSubmit)) {
+         Info("CreateJDL", "\n#####   Copying JDL file <%s> to your AliEn output directory", fJDLName.Data());
+         TString locjdl = Form("%s/%s", fGridOutputDir.Data(),fJDLName.Data());
+         if (fProductionMode)
+            locjdl = Form("%s/%s", workdir.Data(),fJDLName.Data());
+         if (FileExists(locjdl)) gGrid->Rm(locjdl);
+         TFile::Cp(Form("file:%s",fJDLName.Data()), Form("alien://%s", locjdl.Data()));
+      }
       if (fAdditionalLibs.Length()) {
          arr = fAdditionalLibs.Tokenize(" ");
          TObjString *os;
@@ -846,6 +922,7 @@ Bool_t AliAnalysisAlien::CreateJDL()
          TIter next(fPackages);
          TObject *obj;
          while ((obj=next())) {
+            if (FileExists(obj->GetName())) gGrid->Rm(obj->GetName());
             Info("CreateJDL", "\n#####   Copying dependency: <%s> to your alien workspace", obj->GetName());
             TFile::Cp(Form("file:%s",obj->GetName()), Form("alien://%s/%s", workdir.Data(), obj->GetName()));
          }   
@@ -870,11 +947,19 @@ Bool_t AliAnalysisAlien::WriteJDL(Bool_t copy)
       TIter next(fInputFiles);
       while ((os=(TObjString*)next()))
          fGridJDL->AddToInputDataCollection(Form("LF:%s,nodownload", os->GetString().Data()));
-      fGridJDL->SetOutputDirectory(Form("%s/#alien_counter_03i#", fGridOutputDir.Data()));
+      if (!fOutputSingle.IsNull())
+         fGridJDL->SetOutputDirectory(Form("#alienfulldir#/../%s",fOutputSingle.Data()));
+      else                                    
+         fGridJDL->SetOutputDirectory(Form("%s/#alien_counter_03i#", fGridOutputDir.Data()));
    } else {
       // One jdl to be submitted with 2 input parameters: data collection name and output dir prefix
       fGridJDL->AddToInputDataCollection(Form("LF:%s/$1,nodownload", workdir.Data()));
-      fGridJDL->SetOutputDirectory(Form("%s/$2/#alien_counter_03i#", fGridOutputDir.Data()));
+      if (!fOutputSingle.IsNull()) {
+         if (!fOutputToRunNo) fGridJDL->SetOutputDirectory(Form("#alienfulldir#/%s",fOutputSingle.Data()));
+         else fGridJDL->SetOutputDirectory(Form("%s/$2",fGridOutputDir.Data()));
+      } else {   
+         fGridJDL->SetOutputDirectory(Form("%s/$2/#alien_counter_03i#", fGridOutputDir.Data()));
+      }   
    }
       
 
@@ -884,7 +969,7 @@ Bool_t AliAnalysisAlien::WriteJDL(Bool_t copy)
    index = sjdl.Index("Executable");
    if (index >= 0) sjdl.Insert(index, "\n# This is the startup script\n");
    index = sjdl.Index("Split ");
-   if (index >= 0) sjdl.Insert(index, "\n# We split per storage element\n");
+   if (index >= 0) sjdl.Insert(index, "\n# We split per SE or file\n");
    index = sjdl.Index("SplitMaxInputFileNumber");
    if (index >= 0) sjdl.Insert(index, "\n# We want each subjob to get maximum this number of input files\n");
    index = sjdl.Index("InputDataCollection");
@@ -1156,71 +1241,81 @@ Bool_t AliAnalysisAlien::IsCollection(const char *lfn) const
    return kFALSE;
 }   
 
+//______________________________________________________________________________
+Bool_t AliAnalysisAlien::IsSingleOutput() const
+{
+// Check if single-ouput option is on.
+   return (!fOutputSingle.IsNull());
+}
+   
 //______________________________________________________________________________
 void AliAnalysisAlien::Print(Option_t *) const
 {
 // Print current plugin settings.
-   Printf("### AliEn analysis plugin current settings ###");
-   Printf("=   Production mode:______________________________ %d", fProductionMode);
-   Printf("=   Version of API requested: ____________________ %s", fAPIVersion.Data());
-   Printf("=   Version of ROOT requested: ___________________ %s", fROOTVersion.Data());
-   Printf("=   Version of AliRoot requested: ________________ %s", fAliROOTVersion.Data());
+   printf("### AliEn analysis plugin current settings ###\n");
+   printf("=   Production mode:______________________________ %d\n", fProductionMode);
+   printf("=   Version of API requested: ____________________ %s\n", fAPIVersion.Data());
+   printf("=   Version of ROOT requested: ___________________ %s\n", fROOTVersion.Data());
+   printf("=   Version of AliRoot requested: ________________ %s\n", fAliROOTVersion.Data());
    if (fUser.Length()) 
-   Printf("=   User running the plugin: _____________________ %s", fUser.Data());
-   Printf("=   Grid workdir relative to user $HOME: _________ %s", fGridWorkingDir.Data());
-   Printf("=   Grid output directory relative to workdir: ___ %s", fGridOutputDir.Data());
-   Printf("=   Data base directory path requested: __________ %s", fGridDataDir.Data());
-   Printf("=   Data search pattern: _________________________ %s", fDataPattern.Data());
-   Printf("=   Input data format: ___________________________ %s", fInputFormat.Data());
+   printf("=   User running the plugin: _____________________ %s\n", fUser.Data());
+   printf("=   Grid workdir relative to user $HOME: _________ %s\n", fGridWorkingDir.Data());
+   printf("=   Grid output directory relative to workdir: ___ %s\n", fGridOutputDir.Data());
+   printf("=   Data base directory path requested: __________ %s\n", fGridDataDir.Data());
+   printf("=   Data search pattern: _________________________ %s\n", fDataPattern.Data());
+   printf("=   Input data format: ___________________________ %s\n", fInputFormat.Data());
    if (fRunNumbers.Length()) 
-   Printf("=   Run numbers to be processed: _________________ %s", fRunNumbers.Data());
+   printf("=   Run numbers to be processed: _________________ %s\n", fRunNumbers.Data());
    if (fRunRange[0])
-   Printf("=   Run range to be processed: ___________________ %d-%d", fRunRange[0], fRunRange[1]);
+   printf("=   Run range to be processed: ___________________ %s%d-%s%d\n", fRunPrefix.Data(), fRunRange[0], fRunPrefix.Data(), fRunRange[1]);
    if (!fRunRange[0] && !fRunNumbers.Length()) {
       TIter next(fInputFiles);
       TObject *obj;
       TString list;
       while ((obj=next())) list += obj->GetName();
-      Printf("=   Input files to be processed: _________________ %s", list.Data());
+      printf("=   Input files to be processed: _________________ %s\n", list.Data());
    }
    if (TestBit(AliAnalysisGrid::kTest))
-   Printf("=   Number of input files used in test mode: _____ %d", fNtestFiles);
-   Printf("=   List of output files to be registered: _______ %s", fOutputFiles.Data());
-   Printf("=   List of outputs going to be archived: ________ %s", fOutputArchive.Data());
-   Printf("=   List of outputs that should not be merged: ___ %s", fMergeExcludes.Data());
-   Printf("=====================================================================");
-   Printf("=   Job price: ___________________________________ %d", fPrice);
-   Printf("=   Time to live (TTL): __________________________ %d", fTTL);
-   Printf("=   Max files per subjob: ________________________ %d", fSplitMaxInputFileNumber);
+   printf("=   Number of input files used in test mode: _____ %d\n", fNtestFiles);
+   printf("=   List of output files to be registered: _______ %s\n", fOutputFiles.Data());
+   printf("=   List of outputs going to be archived: ________ %s\n", fOutputArchive.Data());
+   printf("=   List of outputs that should not be merged: ___ %s\n", fMergeExcludes.Data());
+   printf("=====================================================================\n");
+   printf("=   Job price: ___________________________________ %d\n", fPrice);
+   printf("=   Time to live (TTL): __________________________ %d\n", fTTL);
+   printf("=   Max files per subjob: ________________________ %d\n", fSplitMaxInputFileNumber);
    if (fMaxInitFailed>0) 
-   Printf("=   Max number of subjob fails to kill: __________ %d", fMaxInitFailed);
+   printf("=   Max number of subjob fails to kill: __________ %d\n", fMaxInitFailed);
    if (fMasterResubmitThreshold>0) 
-   Printf("=   Resubmit master job if failed subjobs >_______ %d", fMasterResubmitThreshold);
+   printf("=   Resubmit master job if failed subjobs >_______ %d\n", fMasterResubmitThreshold);
    if (fNrunsPerMaster>0)
-   Printf("=   Number of runs per master job: _______________ %d", fNrunsPerMaster);
-   Printf("=   Number of files in one chunk to be merged: ___ %d", fMaxMergeFiles);
-   Printf("=   Name of the generated execution script: ______ %s",fExecutable.Data());
+   printf("=   Number of runs per master job: _______________ %d\n", fNrunsPerMaster);
+   printf("=   Number of files in one chunk to be merged: ___ %d\n", fMaxMergeFiles);
+   printf("=   Name of the generated execution script: ______ %s\n", fExecutable.Data());
+   printf("=   Executable command: __________________________ %s\n", fExecutableCommand.Data());
    if (fArguments.Length()) 
-   Printf("=   Arguments for the execution script: __________ %s",fArguments.Data());
-   Printf("=   Name of the generated analysis macro: ________ %s",fAnalysisMacro.Data());
-   Printf("=   User analysis files to be deployed: __________ %s",fAnalysisSource.Data());
-   Printf("=   Additional libs to be loaded or souces to be compiled runtime: <%s>",fAdditionalLibs.Data());
-   Printf("=   Master jobs split mode: ______________________ %s",fSplitMode.Data());
+   printf("=   Arguments for the execution script: __________ %s\n",fArguments.Data());
+   if (fExecutableArgs.Length()) 
+   printf("=   Arguments after macro name in executable______ %s\n",fExecutableArgs.Data());
+   printf("=   Name of the generated analysis macro: ________ %s\n",fAnalysisMacro.Data());
+   printf("=   User analysis files to be deployed: __________ %s\n",fAnalysisSource.Data());
+   printf("=   Additional libs to be loaded or souces to be compiled runtime: <%s>\n",fAdditionalLibs.Data());
+   printf("=   Master jobs split mode: ______________________ %s\n",fSplitMode.Data());
    if (fDatasetName)
-   Printf("=   Custom name for the dataset to be created: ___ %s", fDatasetName.Data());
-   Printf("=   Name of the generated JDL: ___________________ %s", fJDLName.Data());
+   printf("=   Custom name for the dataset to be created: ___ %s\n", fDatasetName.Data());
+   printf("=   Name of the generated JDL: ___________________ %s\n", fJDLName.Data());
    if (fIncludePath.Data())
-   Printf("=   Include path for runtime task compilation: ___ %s", fIncludePath.Data());
+   printf("=   Include path for runtime task compilation: ___ %s\n", fIncludePath.Data());
    if (fCloseSE.Length())
-   Printf("=   Force job outputs to storage element: ________ %s", fCloseSE.Data());
+   printf("=   Force job outputs to storage element: ________ %s\n", fCloseSE.Data());
    if (fFriendChainName.Length())
-   Printf("=   Open friend chain file on worker: ____________ %s", fFriendChainName.Data());
+   printf("=   Open friend chain file on worker: ____________ %s\n", fFriendChainName.Data());
    if (fPackages) {
       TIter next(fPackages);
       TObject *obj;
       TString list;
       while ((obj=next())) list += obj->GetName();
-      Printf("=   Par files to be used: ________________________ %s", list.Data());
+      printf("=   Par files to be used: ________________________ %s\n", list.Data());
    }   
 }
 
@@ -1244,6 +1339,7 @@ void AliAnalysisAlien::SetDefaults()
    fExecutable                 = "analysis.sh";
    fExecutableCommand          = "root -b -q";
    fArguments                  = "";
+   fExecutableArgs             = "";
    fAnalysisMacro              = "myAnalysis.C";
    fAnalysisSource             = "";
    fAdditionalLibs             = "";
@@ -1308,7 +1404,7 @@ Bool_t AliAnalysisAlien::MergeOutputs()
           fMergeExcludes.Contains(output_file.Data())) continue;
       // Perform a 'find' command in the output directory, looking for registered outputs    
       command = Form("find %s/ *%s", fGridOutputDir.Data(), output_file.Data());
-      Printf("command: %s", command.Data());
+      printf("command: %s\n", command.Data());
       TGridResult *res = gGrid->Command(command);
       if (!res) continue;
       TFileMerger *fm = 0;
@@ -1329,11 +1425,11 @@ Bool_t AliAnalysisAlien::MergeOutputs()
             }
             output_chunk = output_file;
             output_chunk.ReplaceAll(".root", Form("_%04d.root", count_chunk));
-            Printf("%s", output_chunk.Data());
+            printf("%s\n", output_chunk.Data());
             count_chunk++;
             if (gSystem->AccessPathName(output_chunk)) continue;
             // Merged file with chunks up to <count_chunk> found
-            Printf("Resume merging of <%s> from <%s>", output_file.Data(), output_chunk.Data());
+            printf("Resume merging of <%s> from <%s>\n", output_file.Data(), output_chunk.Data());
             previous_chunk = output_chunk;
             break;
          }
@@ -1430,6 +1526,7 @@ Bool_t AliAnalysisAlien::StartAnalysis(Long64_t /*nentries*/, Long64_t /*firstEn
             if (!mgr->GetOutputEventHandler()) continue;
             filename = mgr->GetOutputEventHandler()->GetOutputFileName();
          }
+         if (fOutputFiles.Contains(filename)) continue;
          if (fOutputFiles.Length()) fOutputFiles += " ";
          fOutputFiles += filename;
       }
@@ -1437,7 +1534,7 @@ Bool_t AliAnalysisAlien::StartAnalysis(Long64_t /*nentries*/, Long64_t /*firstEn
       if (mgr->GetExtraFiles().Length()) {
          if (fOutputFiles.Length()) fOutputFiles += " ";
          fOutputFiles += mgr->GetExtraFiles();
-      }   
+      }
    }
 //   if (!fCloseSE.Length()) fCloseSE = gSystem->Getenv("alien_CLOSE_SE");
    if (TestBit(AliAnalysisGrid::kOffline)) {
@@ -1511,7 +1608,7 @@ Bool_t AliAnalysisAlien::StartAnalysis(Long64_t /*nentries*/, Long64_t /*firstEn
    if (!fRunNumbers.Length() && !fRunRange[0]) {
       // Submit a given xml or a set of runs
       res = gGrid->Command(Form("submit %s", fJDLName.Data()));
-      Printf("*************************** %s",Form("submit %s", fJDLName.Data()));
+      printf("*************************** %s\n",Form("submit %s", fJDLName.Data()));
       if (res) {
          const char *cjobId = res->GetKey(0,"jobId");
          if (!cjobId) {
@@ -1571,14 +1668,14 @@ void AliAnalysisAlien::SubmitNext()
    if (!fNsubmitted) ntosubmit = 1;
    else {
       TString status = GetJobStatus(firstmaster, lastmaster, nrunning, nwaiting, nerror, ndone);
-      Printf("=== master %d: %s", lastmaster, status.Data());
+      printf("=== master %d: %s\n", lastmaster, status.Data());
       // If last master not split, just return
       if (status != "SPLIT") {iscalled = kFALSE; return;}
       // No more than 100 waiting jobs
       if (nwaiting>100) {iscalled = kFALSE; return;}
       npermaster = (nrunning+nwaiting+nerror+ndone)/fNsubmitted;      
       if (npermaster) ntosubmit = (100-nwaiting)/npermaster;
-      Printf("=== WAITING(%d) RUNNING(%d) DONE(%d) OTHER(%d) NperMaster=%d => to submit %d jobs", 
+      printf("=== WAITING(%d) RUNNING(%d) DONE(%d) OTHER(%d) NperMaster=%d => to submit %d jobs\n", 
              nwaiting, nrunning, ndone, nerror, npermaster, ntosubmit);
    }
    Int_t nmasterjobs = fInputFiles->GetEntries();
@@ -1586,8 +1683,13 @@ void AliAnalysisAlien::SubmitNext()
       // Submit for a range of enumeration of runs.
       if (fNsubmitted>=nmasterjobs) {iscalled = kFALSE; return;}
       TString query;
-      query = Form("submit %s %s %03d", fJDLName.Data(), fInputFiles->At(fNsubmitted)->GetName(), fNsubmitted);
-      Printf("********* %s",query.Data());
+      TString runOutDir = gSystem->BaseName(fInputFiles->At(fNsubmitted)->GetName());
+      runOutDir.ReplaceAll(".xml", "");
+      if (fOutputToRunNo)
+         query = Form("submit %s %s %s", fJDLName.Data(), fInputFiles->At(fNsubmitted)->GetName(), runOutDir.Data());
+      else
+         query = Form("submit %s %s %03d", fJDLName.Data(), fInputFiles->At(fNsubmitted)->GetName(), fNsubmitted);
+      printf("********* %s\n",query.Data());
       res = gGrid->Command(query);
       if (res) {
          TString cjobId1 = res->GetKey(0,"jobId");
@@ -1635,8 +1737,14 @@ void AliAnalysisAlien::WriteAnalysisFile()
       TDirectory *cdir = gDirectory;
       TFile *file = TFile::Open(analysisFile, "RECREATE");
       if (file) {
+         // Skip task Terminate calls for the grid job
+         mgr->SetSkipTerminate(kTRUE);
+         // Unless merging makes no sense
+         if (IsSingleOutput()) mgr->SetSkipTerminate(kFALSE);
          mgr->Write();
          delete file;
+         // Enable termination for local jobs
+         mgr->SetSkipTerminate(kFALSE);
       }
       if (cdir) cdir->cd();
       Info("WriteAnalysisFile", "\n#####   Analysis manager: %s wrote to file <%s>\n", mgr->GetName(),analysisFile.Data());
@@ -1691,7 +1799,22 @@ void AliAnalysisAlien::WriteAnalysisMacro()
       out << "   gSystem->Load(\"libGeom\");" << endl;
       out << "   gSystem->Load(\"libVMC\");" << endl;
       out << "   gSystem->Load(\"libPhysics\");" << endl << endl;
+      out << "   gSystem->Load(\"libMinuit\");" << endl << endl;
+      if (fAdditionalRootLibs.Length()) {
+       // in principle libtree /lib geom libvmc etc. can go into this list, too
+       out << "// Add aditional libraries" << endl;
+       TObjArray *list = fAdditionalRootLibs.Tokenize(" ");
+       TIter next(list);
+       TObjString *str;
+       while((str=(TObjString*)next())) {
+         if (str->GetString().Contains(".so"))
+           out << "   gSystem->Load(\"" << str->GetString().Data() << "\");" << endl;
+         }
+       if (list) delete list;
+      }
       out << "// Load analysis framework libraries" << endl;
+
+
       if (!fPackages) {
          out << "   gSystem->Load(\"libSTEERBase\");" << endl;
          out << "   gSystem->Load(\"libESD\");" << endl;
@@ -1984,11 +2107,20 @@ void AliAnalysisAlien::WriteExecutable()
       out << "echo $ALICE_ROOT" << endl;
       out << "echo \"############## which aliroot : ##############\"" << endl;
       out << "which aliroot" << endl;
+      out << "echo \"############## system limits : ##############\"" << endl;
+      out << "ulimit -a" << endl;
+      out << "echo \"############## memory : ##############\"" << endl;
+      out << "free -m" << endl;
       out << "echo \"=========================================\"" << endl << endl;
-//      if (TestBit(AliAnalysisGrid::kTest)) out << "root ";
+      // Make sure we can properly compile par files
+      if (TObject::TestBit(AliAnalysisGrid::kUsePars)) out << "export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH" << endl;
       out << fExecutableCommand << " "; 
-      out << fAnalysisMacro.Data() << endl << endl;
-      out << "echo \"======== " << fAnalysisMacro.Data() << " finished ========\"" << endl;
+      out << fAnalysisMacro.Data() << " " << fExecutableArgs.Data() << endl << endl;
+      out << "echo \"======== " << fAnalysisMacro.Data() << " finished with exit code: $? ========\"" << endl;
+      out << "echo \"############## memory after: ##############\"" << endl;
+      out << "free -m" << endl;
+      out << "echo \"############## Last 10 lines from dmesg : ##############\"" << endl;
+      out << "dmesg | tail -n 10" << endl;
    }   
    Bool_t copy = kTRUE;
    if (TestBit(AliAnalysisGrid::kOffline) || TestBit(AliAnalysisGrid::kTest)) copy = kFALSE;
@@ -2028,6 +2160,7 @@ void AliAnalysisAlien::WriteProductionFile(const char *filename) const
       out << Form("%s", fInputFiles->At(i)->GetName()) << " " << Form("%03d", i) << endl;
    }
    Info("WriteProductionFile", "\n#####   Copying production file <%s> to your work directory", filename);
+   if (FileExists(filename)) gGrid->Rm(filename);
    TFile::Cp(Form("file:%s",filename), Form("alien://%s/%s", workdir.Data(),filename));   
 }