ROOT logo
//
//  CpMacroWithFilter.C
//
//  Created by Laurent Aphecetche on 30-sep-2013.
//

Int_t CpMacroWithFilter(TString from, TString to)
{
  /// Copy one file from url "from" into url "to".
  ///
  /// If from contains one of the filter keywords
  /// (in the form of e.g. AliAOD.FILTER_ZZZZ.root)
  /// then we call the external filter macro
  ///
  /// otherwise we do a simple TFile::Cp(from,to)
  ///
  
  if (from.IsNull() || to.IsNull()) return 1;
  
  std::cout << Form("Entering CpMacroWithFilter(\"%s\",\"%s\")",from.Data(),to.Data()) << std::endl;
  
  TObjArray filters;
  filters.SetOwner(kTRUE);
  
  filters.Add(new TObjString("FILTER_AODMUONWITHTRACKLETS"));
  filters.Add(new TObjString("FILTER_RAWMUON"));
  
  TIter next(&filters);
  TObjString* s;
  TString filter;
  
  while ( ( s = static_cast<TObjString*>(next())) )
  {
      if ( from.Contains(s->String()) )
      {
        filter = s->String();
        break;
      }
  }
  
  if (from.Contains("alien:\/\/")) TGrid::Connect("alien:\/\/");

  if ( filter.Length() > 0 )
  {
    // check the required filter is actually available
    
    from.ReplaceAll(filter.Data(),"");
    from.ReplaceAll("..",".");
    
    if ( gSystem->AccessPathName(Form("%s/etc/%s.C",gSystem->Getenv("XRDDMSYS"),filter.Data()) ) )
    {
      std::cout << Form("ERROR: could not find a filter named %s.C",filter.Data()) << std::endl;
      return 2;
    }
    else
    {
      // check also we have a companion macro (to load the relevant libraries and
      // set the additional include paths, if needed)
      
      if ( gSystem->AccessPathName(Form("%s/etc/%s_rootlogon.C",gSystem->Getenv("XRDDMSYS"),filter.Data()) ) )
      {
        std::cout << Form("ERROR: could not find the companion macro %s_rootlogon.C for the filter named %s.C",filter.Data(),filter.Data()) << std::endl;
        return 3;
      }

      else
      {
        // most probably the filter will require AliRoot libs, so add the dynamic path here
        // as well as the include path and the macro path.
        //
        gSystem->AddDynamicPath(Form("%s/aliroot/lib/tgt_%s",gSystem->Getenv("ALICE_PROOF_AAF_DIR"),gSystem->GetBuildArch()));
        gSystem->SetIncludePath(Form("-I%s/etc -I%s/aliroot/include",gSystem->Getenv("XRDDMSYS"),gSystem->Getenv("ALICE_PROOF_AAF_DIR")));
        gROOT->SetMacroPath(Form("%s:%s/etc",gROOT->GetMacroPath(),gSystem->Getenv("XRDDMSYS")));
        
//        gSystem->AddDynamicPath(Form("/pool/PROOF-AAF/aliroot/lib/tgt_%s",gSystem->GetBuildArch()));
//        gSystem->SetIncludePath("-I/pool/PROOF-AAF/xrootd_1.0.50/etc -I/pool/PROOF-AAF/aliroot/include");
//        gROOT->SetMacroPath(Form("%s:%s/etc",gROOT->GetMacroPath(),gSystem->Getenv("XRDDMSYS")));

        // execute the companion macro

        std::cout << Form("Will load companion macro %s_rootlogon.C(\"%s\",\"%s\")",filter.Data(),from.Data(),to.Data()) << std::endl;

        gROOT->Macro(Form("%s_rootlogon.C",filter.Data()));
        
        std::cout << gSystem->GetIncludePath() << std::endl;
        
        // finally delegate the work to the required filter
      
        std::cout << Form("Will compile filter %s.C+(\"%s\",\"%s\")",filter.Data(),from.Data(),to.Data()) << std::endl;

        Int_t fail = gROOT->LoadMacro(Form("%s.C+",filter.Data()));
      
        if ( fail )
        {
          std::cout << Form("Failed to load/compile macro %s.C+",filter.Data()) << std::endl;
          return 4;
        }
        
        std::cout << Form("Will execute filter %s(\"%s\",\"%s\")",filter.Data(),from.Data(),to.Data()) << std::endl;

        return (Int_t)gROOT->ProcessLine(Form("%s(\"%s\",\"%s\")",filter.Data(),from.Data(),to.Data()));
      }
    }
  }
  else
  {
    // "normal" case of a simple copy
    //
    // ! operator since TFile::Cp returns kTRUE(1) in case of success
    std::cout << "Performing a simple TFile::Cp" << std::endl;
    return (!TFile::Cp(from.Data(),to.Data()));
  }
  
}
 CpMacroWithFilter.C:1
 CpMacroWithFilter.C:2
 CpMacroWithFilter.C:3
 CpMacroWithFilter.C:4
 CpMacroWithFilter.C:5
 CpMacroWithFilter.C:6
 CpMacroWithFilter.C:7
 CpMacroWithFilter.C:8
 CpMacroWithFilter.C:9
 CpMacroWithFilter.C:10
 CpMacroWithFilter.C:11
 CpMacroWithFilter.C:12
 CpMacroWithFilter.C:13
 CpMacroWithFilter.C:14
 CpMacroWithFilter.C:15
 CpMacroWithFilter.C:16
 CpMacroWithFilter.C:17
 CpMacroWithFilter.C:18
 CpMacroWithFilter.C:19
 CpMacroWithFilter.C:20
 CpMacroWithFilter.C:21
 CpMacroWithFilter.C:22
 CpMacroWithFilter.C:23
 CpMacroWithFilter.C:24
 CpMacroWithFilter.C:25
 CpMacroWithFilter.C:26
 CpMacroWithFilter.C:27
 CpMacroWithFilter.C:28
 CpMacroWithFilter.C:29
 CpMacroWithFilter.C:30
 CpMacroWithFilter.C:31
 CpMacroWithFilter.C:32
 CpMacroWithFilter.C:33
 CpMacroWithFilter.C:34
 CpMacroWithFilter.C:35
 CpMacroWithFilter.C:36
 CpMacroWithFilter.C:37
 CpMacroWithFilter.C:38
 CpMacroWithFilter.C:39
 CpMacroWithFilter.C:40
 CpMacroWithFilter.C:41
 CpMacroWithFilter.C:42
 CpMacroWithFilter.C:43
 CpMacroWithFilter.C:44
 CpMacroWithFilter.C:45
 CpMacroWithFilter.C:46
 CpMacroWithFilter.C:47
 CpMacroWithFilter.C:48
 CpMacroWithFilter.C:49
 CpMacroWithFilter.C:50
 CpMacroWithFilter.C:51
 CpMacroWithFilter.C:52
 CpMacroWithFilter.C:53
 CpMacroWithFilter.C:54
 CpMacroWithFilter.C:55
 CpMacroWithFilter.C:56
 CpMacroWithFilter.C:57
 CpMacroWithFilter.C:58
 CpMacroWithFilter.C:59
 CpMacroWithFilter.C:60
 CpMacroWithFilter.C:61
 CpMacroWithFilter.C:62
 CpMacroWithFilter.C:63
 CpMacroWithFilter.C:64
 CpMacroWithFilter.C:65
 CpMacroWithFilter.C:66
 CpMacroWithFilter.C:67
 CpMacroWithFilter.C:68
 CpMacroWithFilter.C:69
 CpMacroWithFilter.C:70
 CpMacroWithFilter.C:71
 CpMacroWithFilter.C:72
 CpMacroWithFilter.C:73
 CpMacroWithFilter.C:74
 CpMacroWithFilter.C:75
 CpMacroWithFilter.C:76
 CpMacroWithFilter.C:77
 CpMacroWithFilter.C:78
 CpMacroWithFilter.C:79
 CpMacroWithFilter.C:80
 CpMacroWithFilter.C:81
 CpMacroWithFilter.C:82
 CpMacroWithFilter.C:83
 CpMacroWithFilter.C:84
 CpMacroWithFilter.C:85
 CpMacroWithFilter.C:86
 CpMacroWithFilter.C:87
 CpMacroWithFilter.C:88
 CpMacroWithFilter.C:89
 CpMacroWithFilter.C:90
 CpMacroWithFilter.C:91
 CpMacroWithFilter.C:92
 CpMacroWithFilter.C:93
 CpMacroWithFilter.C:94
 CpMacroWithFilter.C:95
 CpMacroWithFilter.C:96
 CpMacroWithFilter.C:97
 CpMacroWithFilter.C:98
 CpMacroWithFilter.C:99
 CpMacroWithFilter.C:100
 CpMacroWithFilter.C:101
 CpMacroWithFilter.C:102
 CpMacroWithFilter.C:103
 CpMacroWithFilter.C:104
 CpMacroWithFilter.C:105
 CpMacroWithFilter.C:106
 CpMacroWithFilter.C:107
 CpMacroWithFilter.C:108
 CpMacroWithFilter.C:109
 CpMacroWithFilter.C:110
 CpMacroWithFilter.C:111
 CpMacroWithFilter.C:112
 CpMacroWithFilter.C:113
 CpMacroWithFilter.C:114
 CpMacroWithFilter.C:115