<commit_before>class AliAnalysisGrid;
const char *dataset   = "";
TString anaLibs = "";
//Int_t iESDfilter       = 1;
Int_t iESDfilter       = 0;
Int_t iAODTagCreation  = 1;
Int_t iAODAddMCBranch  = 0;

//______________________________________________________________________________
void RunGrid(
         const char* runtype = "local", // local, proof or grid
         const char *gridmode = "test", // Set the run mode (can be "full", "test", "offline", "submit" or "terminate"). Full & Test work for proof
         const bool bMCtruth = 1, // 1 = MCEvent handler is on (MC truth), 0 = MCEvent handler is off (MC reconstructed/real data)
         const bool bMCphyssel = 1, // 1 = looking at MC truth or reconstructed, 0 = looking at real data
         const Long64_t nentries = 2000, // for local and proof mode, ignored in grid mode. Set to 1234567890 for all events.
         const Long64_t firstentry = 0, // for local and proof mode, ignored in grid mode
         const char *proofdataset = "/alice/data/LHC10c_000120821_p1", // path to dataset on proof cluster, for proof analysis
         const char *proofcluster = "alice-caf.cern.ch", // which proof cluster to use in proof mode
         const char *taskname = "Flowd_PbPb2011"
         )
{
  // check run type
  if(runtype != "local" && runtype != "proof" && runtype != "grid"){
    Printf("\n\tIncorrect run option, check first argument of run macro");
    Printf("\tint runtype = local, proof or grid\n");
    return;
  }
  Printf("%s analysis chosen",runtype);

  gROOT->ProcessLine(".include $ALICE_ROOT/include");

  gSystem->AddIncludePath("-I$ALICE_ROOT/include");
  gSystem->AddIncludePath("-I$ALICE_ROOT/PWGHF/vertexingHF");
  // Load analysis specific libraries
  //=====================================================================

  gSystem->SetIncludePath("-I. -I$ROOTSYS/include -I$ALICE_ROOT -I$ALICE_ROOT/include -I$ALICE_ROOT/ITS -I$ALICE_ROOT/TPC -I$ALICE_ROOT/CONTAINERS -I$ALICE_ROOT/STEER/STEER -I$ALICE_ROOT/STEER/STEERBase -I$ALICE_ROOT/STEER/ESD -I$ALICE_ROOT/STEER/AOD -I$ALICE_ROOT/TRD -I$ALICE_ROOT/macros -I$ALICE_ROOT/ANALYSIS  -I$ALICE_ROOT/OADB -I$ALICE_ROOT/PWGHF -I$ALICE_ROOT/PWGHF/base -I$ALICE_ROOT/PWGHF/vertexingHF -I$ALICE_ROOT/PWG/FLOW/Base -I$ALICE_ROOT/PWG/FLOW/Tasks -g");

  gSystem->Load("libTree.so");
  gSystem->Load("libGeom.so");
  gSystem->Load("libPhysics.so");
  gSystem->Load("libVMC.so");
  gSystem->Load("libMinuit.so");
  gSystem->Load("libSTEERBase.so");
  gSystem->Load("libESD.so");
  gSystem->Load("libAOD.so");
  gSystem->Load("libANALYSIS.so");
  gSystem->Load("libOADB.so");
  gSystem->Load("libANALYSISalice.so");
  gSystem->Load("libCORRFW.so");
  gSystem->Load("libPWGHFbase.so");
  gSystem->Load("libPWGflowBase.so");
  gSystem->Load("libPWGflowTasks.so");
  gSystem->Load("libPWGHFvertexingHF.so");

  // Load analysis specific libraries
  //=====================================================================
  //------ Create AlienPlugin ---------------------
  AliAnalysisGrid *plugin = 0x0;
  TChain *chain = 0x0;
  if (runtype != "local") {
    plugin = CreateAlienHandler(taskname, gridmode, proofcluster, proofdataset);
    if(!plugin) return;
  } else {
    gROOT->LoadMacro("$ALICE_ROOT/PWGCF/Correlations/macros/dphicorrelations/CreateESDChain.C");
    chain = CreateESDChain("ESDs.txt");
  }

  //---- Create the analysis manager
  AliAnalysisManager* mgr = new AliAnalysisManager(taskname);
  if(plugin) mgr->SetGridHandler(plugin);

  //  Input

  AliESDInputHandler* iH = new AliESDInputHandler("handler","handler for my analisys");
  mgr->SetInputEventHandler(iH);

  //--------------------------------------------------------------
  // Other tasks

  // Physics selection
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
  AliPhysicsSelectionTask *physSel = AddTaskPhysicsSelection(kFALSE); // useMC
  // Centrality selection
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
  AliCentralitySelectionTask *taskCentr = AddTaskCentrality();

  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
  AliAnalysisTaskPIDResponse *pidTask = AddTaskPIDResponse(kFALSE); // useMC

  gROOT->LoadMacro("./AliAnalysisTaskFlowd.cxx+g");//$ALICE_ROOT/PWGLF/STRANGENESS/Cascades/AliAnalysisTaskCheckCascadePbPb.cxx++g");
  gROOT->LoadMacro("./AddTaskFlowd.C");//$ALICE_ROOT/PWGLF/STRANGENESS/Cascades/macros/AddTaskCheckCascadePbPb.C");
  AliAnalysisTaskFlowd *task = AddTaskFlowd(kTRUE);

  //__________________________________________________________________________
  // Disable debug printouts
  mgr->SetDebugLevel(3);
  AliLog::SetGlobalLogLevel(AliLog::kFatal);
  AliLog::SetGlobalDebugLevel(0);

  //__________________________________________________________________________
  if (!mgr->InitAnalysis()) return;
  mgr->PrintStatus();
  // Start analysis in grid.
  if (runtype == "local")
    mgr->StartAnalysis(runtype,chain,nentries,firstentry);
  else
    mgr->StartAnalysis(runtype,nentries,firstentry);
}

//______________________________________________________________________________
AliAnalysisGrid* CreateAlienHandler(const char *taskname, const char *gridmode, const char *proofcluster, const char *proofdataset)
{
  AliAnalysisAlien *plugin = new AliAnalysisAlien();
  // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
  plugin->SetRunMode(gridmode);

  // Set versions of used packages

  plugin->SetAPIVersion("V1.1x");
  plugin->SetROOTVersion("v5-34-08");
  plugin->SetAliROOTVersion("vAN-20140915");
  plugin->SetExecutableCommand("aliroot -b -q");

  // Declare input data to be processed.

  // Method 1: Create automatically XML collections using alien 'find' command.
  // Define production directory LFN
  //    plugin->SetGridDataDir("/alice/data/2010/LHC10h");

  // plugin->SetGridDataDir(" /alice/data/2011/LHC11h_2/"); //sim
  // plugin->SetDataPattern("pass2/*AliAOD.root"); // sim

  plugin->SetGridDataDir("/alice/data/2011/LHC11h_2/"); //sim
  plugin->SetDataPattern("*/pass2/*AliESDs.root"); // sim
  plugin->SetRunPrefix("000");   // real data

  Int_t runlist[1]={167693};
  for(Int_t i=0;i<1;i++)
    plugin->AddRunNumber(runlist[i]);

  //    plugin->AddRunNumber(139505);
  //  plugin->AddRunNumber(138652);
  plugin->SetNrunsPerMaster(1);
  plugin->SetOutputToRunNo();

  // Method 2: Declare existing data files (raw collections, xml collections, root file)
  //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");

  // Define alien work directory where all files will be copied. Relative to alien $HOME.
  plugin->SetGridWorkingDir(taskname);

  // Declare alien output directory. Relative to working directory.
  plugin->SetGridOutputDir("output"); // In this case will be $HOME/taskname/out

  plugin->SetAdditionalLibs("libTree.so libGeom.so libPhysics.so libVMC.so libMinuit.so libSTEERBase.so libESD.so libAOD.so  libANALYSIS.so libOADB.so libANALYSISalice.so libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  // plugin->SetAdditionalLibs("libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  plugin->SetAnalysisSource("AliAnalysisTaskFlowd.cxx");

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 1"<<endl;

  // Declare the analysis source files names separated by blancs. To be compiled runtime
  // using ACLiC on the worker nodes.
  //plugin->SetAdditionalLibs("libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so AliAODMuonReplicator0.so");

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 2"<<endl;
  // plugin->SetAdditionalLibs("libTree.so libGeom.so libPhysics.so libVMC.so libMinuit.so libSTEERBase.so libESD.so libAOD.so libANALYSIS.so libOADB.so libANALYSISalice.so libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  // plugin->SetAnalysisSource("AliAnalysisTaskESDMuonFilterO.cxx");
  //plugin->SetAnalysisSource("AliAODMuonReplicator0.cxx");

  // Declare all libraries (other than the default ones for the framework. These will be
  // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
  // plugin->SetAdditionalLibs("AliAODMuonReplicator0_cxx.so");
  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO_cxx.so");

  //questo
  plugin->SetAdditionalLibs("libPWGflowBase.so libPWGflowTasks.so libPWGHFbase.so libPWGHFvertexingHF.so");
  plugin->AddIncludePath("-I. -I$ROOTSYS/include -I$ALICE_ROOT -I$ALICE_ROOT/include -I$ALICE_ROOT/ITS -I$ALICE_ROOT/TPC -I$ALICE_ROOT/CONTAINERS -I$ALICE_ROOT/STEER/STEER -I$ALICE_ROOT/STEER/STEERBase -I$ALICE_ROOT/STEER/ESD -I$ALICE_ROOT/STEER/AOD -I$ALICE_ROOT/TRD -I$ALICE_ROOT/macros -I$ALICE_ROOT/ANALYSIS  -I$ALICE_ROOT/OADB -I$ALICE_ROOT/PWGHF -I$ALICE_ROOT/PWGHF/base -I$ALICE_ROOT/PWGHF/vertexingHF -I$ALICE_ROOT/PWG/FLOW/Base -I$ALICE_ROOT/PWG/FLOW/Tasks -g");

  // plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx");
  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  //plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 3"<<endl;

  // plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx");

  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  // Declare the output file names separated by blancs.
  // (can be like: file.root or file.root@ALICE::Niham::File)
  // To only save certain files, use SetDefaultOutputs(kFALSE), and then
  // SetOutputFiles("list.root other.filename") to choose which files to save
  plugin->SetDefaultOutputs();
  //plugin->SetOutputFiles("list.root");

  // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
  plugin->SetAnalysisMacro(Form("%s.C",taskname));

  // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
  plugin->SetSplitMaxInputFileNumber(100);

  // Optionally modify the executable name (default analysis.sh)
  plugin->SetExecutable(Form("%s.sh",taskname));

  // set number of test files to use in "test" mode
  plugin->SetNtestFiles(10);

  // Optionally resubmit threshold.
  plugin->SetMasterResubmitThreshold(90);

  // Optionally set time to live (default 30000 sec)
  plugin->SetTTL(30000);

  // Optionally set input format (default xml-single)
  plugin->SetInputFormat("xml-single");

  // Optionally modify the name of the generated JDL (default analysis.jdl)
  plugin->SetJDLName(Form("%s.jdl",taskname));

  // Optionally modify job price (default 1)
  plugin->SetPrice(1);

  // Optionally modify split mode (default 'se')
  plugin->SetSplitMode("se");

  //----------------------------------------------------------
  //---      PROOF MODE SPECIFIC SETTINGS         ------------
  //----------------------------------------------------------
  // Proof cluster
  plugin->SetProofCluster(proofcluster);
  // Dataset to be used
  plugin->SetProofDataSet(proofdataset);
  // May need to reset proof. Supported modes: 0-no reset, 1-soft, 2-hard
  plugin->SetProofReset(0);
  // May limit number of workers
  plugin->SetNproofWorkers(0);
  // May limit the number of workers per slave
  plugin->SetNproofWorkersPerSlave(1);
  // May use a specific version of root installed in proof
  plugin->SetRootVersionForProof("current");
  // May set the aliroot mode. Check http://aaf.cern.ch/node/83
  plugin->SetAliRootMode("default"); // Loads AF libs by default
  // May request ClearPackages (individual ClearPackage not supported)
  plugin->SetClearPackages(kFALSE);
  // Plugin test mode works only providing a file containing test file locations, used in "local" mode also
  plugin->SetFileForTestMode("files.txt"); // file should contain path name to a local directory containg *ESDs.root etc
  // Request connection to alien upon connection to grid
  plugin->SetProofConnectGrid(kFALSE);

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 4"<<endl;

  return plugin;

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 5"<<endl;

}
<commit_msg>Added additional libs<commit_after>class AliAnalysisGrid;
const char *dataset   = "";
TString anaLibs = "";
//Int_t iESDfilter       = 1;
Int_t iESDfilter       = 0;
Int_t iAODTagCreation  = 1;
Int_t iAODAddMCBranch  = 0;

//______________________________________________________________________________
void RunGrid(
         const char* runtype = "local", // local, proof or grid
         const char *gridmode = "test", // Set the run mode (can be "full", "test", "offline", "submit" or "terminate"). Full & Test work for proof
         const bool bMCtruth = 1, // 1 = MCEvent handler is on (MC truth), 0 = MCEvent handler is off (MC reconstructed/real data)
         const bool bMCphyssel = 1, // 1 = looking at MC truth or reconstructed, 0 = looking at real data
         const Long64_t nentries = 2000, // for local and proof mode, ignored in grid mode. Set to 1234567890 for all events.
         const Long64_t firstentry = 0, // for local and proof mode, ignored in grid mode
         const char *proofdataset = "/alice/data/LHC10c_000120821_p1", // path to dataset on proof cluster, for proof analysis
         const char *proofcluster = "alice-caf.cern.ch", // which proof cluster to use in proof mode
         const char *taskname = "Flowd_PbPb2011"
         )
{
  // check run type
  if(runtype != "local" && runtype != "proof" && runtype != "grid"){
    Printf("\n\tIncorrect run option, check first argument of run macro");
    Printf("\tint runtype = local, proof or grid\n");
    return;
  }
  Printf("%s analysis chosen",runtype);

  gROOT->ProcessLine(".include $ALICE_ROOT/include");

  gSystem->AddIncludePath("-I$ALICE_ROOT/include");
  gSystem->AddIncludePath("-I$ALICE_ROOT/PWGHF/vertexingHF");
  // Load analysis specific libraries
  //=====================================================================

  gSystem->SetIncludePath("-I. -I$ROOTSYS/include -I$ALICE_ROOT -I$ALICE_ROOT/include -I$ALICE_ROOT/ITS -I$ALICE_ROOT/TPC -I$ALICE_ROOT/CONTAINERS -I$ALICE_ROOT/STEER/STEER -I$ALICE_ROOT/STEER/STEERBase -I$ALICE_ROOT/STEER/ESD -I$ALICE_ROOT/STEER/AOD -I$ALICE_ROOT/TRD -I$ALICE_ROOT/macros -I$ALICE_ROOT/ANALYSIS  -I$ALICE_ROOT/OADB -I$ALICE_ROOT/PWGHF -I$ALICE_ROOT/PWGHF/base -I$ALICE_ROOT/PWGHF/vertexingHF -I$ALICE_ROOT/PWG/FLOW/Base -I$ALICE_ROOT/PWG/FLOW/Tasks -g");

  gSystem->Load("libTree.so");
  gSystem->Load("libGeom.so");
  gSystem->Load("libPhysics.so");
  gSystem->Load("libVMC.so");
  gSystem->Load("libMinuit.so");
  gSystem->Load("libSTEERBase.so");
  gSystem->Load("libESD.so");
  gSystem->Load("libAOD.so");
  gSystem->Load("libANALYSIS.so");
  gSystem->Load("libOADB.so");
  gSystem->Load("libANALYSISalice.so");
  gSystem->Load("libCORRFW.so");
  gSystem->Load("libPWGHFbase.so");
  gSystem->Load("libPWGflowBase.so");
  gSystem->Load("libPWGflowTasks.so");
  gSystem->Load("libPWGHFvertexingHF.so");

  // Load analysis specific libraries
  //=====================================================================
  //------ Create AlienPlugin ---------------------
  AliAnalysisGrid *plugin = 0x0;
  TChain *chain = 0x0;
  if (runtype != "local") {
    plugin = CreateAlienHandler(taskname, gridmode, proofcluster, proofdataset);
    if(!plugin) return;
  } else {
    gROOT->LoadMacro("$ALICE_ROOT/PWGCF/Correlations/macros/dphicorrelations/CreateESDChain.C");
    chain = CreateESDChain("ESDs.txt");
  }

  //---- Create the analysis manager
  AliAnalysisManager* mgr = new AliAnalysisManager(taskname);
  if(plugin) mgr->SetGridHandler(plugin);

  //  Input

  AliESDInputHandler* iH = new AliESDInputHandler("handler","handler for my analisys");
  mgr->SetInputEventHandler(iH);

  //--------------------------------------------------------------
  // Other tasks

  // Physics selection
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPhysicsSelection.C");
  AliPhysicsSelectionTask *physSel = AddTaskPhysicsSelection(kFALSE); // useMC
  // Centrality selection
  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskCentrality.C");
  AliCentralitySelectionTask *taskCentr = AddTaskCentrality();

  gROOT->LoadMacro("$ALICE_ROOT/ANALYSIS/macros/AddTaskPIDResponse.C");
  AliAnalysisTaskPIDResponse *pidTask = AddTaskPIDResponse(kFALSE); // useMC

  gROOT->LoadMacro("./AliAnalysisTaskFlowd.cxx+g");//$ALICE_ROOT/PWGLF/STRANGENESS/Cascades/AliAnalysisTaskCheckCascadePbPb.cxx++g");
  gROOT->LoadMacro("./AddTaskFlowd.C");//$ALICE_ROOT/PWGLF/STRANGENESS/Cascades/macros/AddTaskCheckCascadePbPb.C");
  AliAnalysisTaskFlowd *task = AddTaskFlowd(kTRUE);

  //__________________________________________________________________________
  // Disable debug printouts
  mgr->SetDebugLevel(3);
  AliLog::SetGlobalLogLevel(AliLog::kFatal);
  AliLog::SetGlobalDebugLevel(0);

  //__________________________________________________________________________
  if (!mgr->InitAnalysis()) return;
  mgr->PrintStatus();
  // Start analysis in grid.
  if (runtype == "local")
    mgr->StartAnalysis(runtype,chain,nentries,firstentry);
  else
    mgr->StartAnalysis(runtype,nentries,firstentry);
}

//______________________________________________________________________________
AliAnalysisGrid* CreateAlienHandler(const char *taskname, const char *gridmode, const char *proofcluster, const char *proofdataset)
{
  AliAnalysisAlien *plugin = new AliAnalysisAlien();
  // Set the run mode (can be "full", "test", "offline", "submit" or "terminate")
  plugin->SetRunMode(gridmode);

  // Set versions of used packages

  plugin->SetAPIVersion("V1.1x");
  plugin->SetROOTVersion("v5-34-08");
  plugin->SetAliROOTVersion("vAN-20140915");
  plugin->SetExecutableCommand("aliroot -b -q");

  // Declare input data to be processed.

  // Method 1: Create automatically XML collections using alien 'find' command.
  // Define production directory LFN
  //    plugin->SetGridDataDir("/alice/data/2010/LHC10h");

  // plugin->SetGridDataDir(" /alice/data/2011/LHC11h_2/"); //sim
  // plugin->SetDataPattern("pass2/*AliAOD.root"); // sim

  plugin->SetGridDataDir("/alice/data/2011/LHC11h_2/"); //sim
  plugin->SetDataPattern("*/pass2/*AliESDs.root"); // sim
  plugin->SetRunPrefix("000");   // real data

  Int_t runlist[1]={167693};
  for(Int_t i=0;i<1;i++)
    plugin->AddRunNumber(runlist[i]);

  //    plugin->AddRunNumber(139505);
  //  plugin->AddRunNumber(138652);
  plugin->SetNrunsPerMaster(1);
  plugin->SetOutputToRunNo();

  // Method 2: Declare existing data files (raw collections, xml collections, root file)
  //   plugin->AddDataFile("/alice/data/2008/LHC08c/000057657/raw/Run57657.Merged.RAW.tag.root");

  // Define alien work directory where all files will be copied. Relative to alien $HOME.
  plugin->SetGridWorkingDir(taskname);

  // Declare alien output directory. Relative to working directory.
  plugin->SetGridOutputDir("output"); // In this case will be $HOME/taskname/out

//  plugin->SetAdditionalLibs("libTree.so libGeom.so libPhysics.so libVMC.so libMinuit.so libSTEERBase.so libESD.so libAOD.so  libANALYSIS.so libOADB.so libANALYSISalice.so libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  // plugin->SetAdditionalLibs("libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  plugin->SetAnalysisSource("AliAnalysisTaskFlowd.cxx");
  plugin->SetAdditionalLibs("AliAnalysisTaskCheckCascadePbPb.h AliAnalysisTaskCheckCascadePbPb.cxx ");
  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 1"<<endl;

  // Declare the analysis source files names separated by blancs. To be compiled runtime
  // using ACLiC on the worker nodes.
  //plugin->SetAdditionalLibs("libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so AliAODMuonReplicator0.so");

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 2"<<endl;
  // plugin->SetAdditionalLibs("libTree.so libGeom.so libPhysics.so libVMC.so libMinuit.so libSTEERBase.so libESD.so libAOD.so libANALYSIS.so libOADB.so libANALYSISalice.so libCORRFW.so libPWGHFbase.so libPWGflowBase.so libPWGflowTasks.so libPWGHFvertexingHF.so");

  // plugin->SetAnalysisSource("AliAnalysisTaskESDMuonFilterO.cxx");
  //plugin->SetAnalysisSource("AliAODMuonReplicator0.cxx");

  // Declare all libraries (other than the default ones for the framework. These will be
  // loaded by the generated analysis macro. Add all extra files (task .cxx/.h) here.
  // plugin->SetAdditionalLibs("AliAODMuonReplicator0_cxx.so");
  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO_cxx.so");

  //questo
  plugin->SetAdditionalLibs("libPWGflowBase.so libPWGflowTasks.so libPWGHFbase.so libPWGHFvertexingHF.so");
  plugin->AddIncludePath("-I. -I$ROOTSYS/include -I$ALICE_ROOT -I$ALICE_ROOT/include -I$ALICE_ROOT/ITS -I$ALICE_ROOT/TPC -I$ALICE_ROOT/CONTAINERS -I$ALICE_ROOT/STEER/STEER -I$ALICE_ROOT/STEER/STEERBase -I$ALICE_ROOT/STEER/ESD -I$ALICE_ROOT/STEER/AOD -I$ALICE_ROOT/TRD -I$ALICE_ROOT/macros -I$ALICE_ROOT/ANALYSIS  -I$ALICE_ROOT/OADB -I$ALICE_ROOT/PWGHF -I$ALICE_ROOT/PWGHF/base -I$ALICE_ROOT/PWGHF/vertexingHF -I$ALICE_ROOT/PWG/FLOW/Base -I$ALICE_ROOT/PWG/FLOW/Tasks -g");

  // plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx");
  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  //plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 3"<<endl;

  // plugin->SetAdditionalLibs("AliAODMuonReplicator0.h AliAODMuonReplicator0.cxx");

  // plugin->SetAdditionalLibs("AliAnalysisTaskESDMuonFilterO.h AliAnalysisTaskESDMuonFilterO.cxx");

  // Declare the output file names separated by blancs.
  // (can be like: file.root or file.root@ALICE::Niham::File)
  // To only save certain files, use SetDefaultOutputs(kFALSE), and then
  // SetOutputFiles("list.root other.filename") to choose which files to save
  plugin->SetDefaultOutputs();
  //plugin->SetOutputFiles("list.root");

  // Optionally set a name for the generated analysis macro (default MyAnalysis.C)
  plugin->SetAnalysisMacro(Form("%s.C",taskname));

  // Optionally set maximum number of input files/subjob (default 100, put 0 to ignore)
  plugin->SetSplitMaxInputFileNumber(100);

  // Optionally modify the executable name (default analysis.sh)
  plugin->SetExecutable(Form("%s.sh",taskname));

  // set number of test files to use in "test" mode
  plugin->SetNtestFiles(10);

  // Optionally resubmit threshold.
  plugin->SetMasterResubmitThreshold(90);

  // Optionally set time to live (default 30000 sec)
  plugin->SetTTL(30000);

  // Optionally set input format (default xml-single)
  plugin->SetInputFormat("xml-single");

  // Optionally modify the name of the generated JDL (default analysis.jdl)
  plugin->SetJDLName(Form("%s.jdl",taskname));

  // Optionally modify job price (default 1)
  plugin->SetPrice(1);

  // Optionally modify split mode (default 'se')
  plugin->SetSplitMode("se");

  //----------------------------------------------------------
  //---      PROOF MODE SPECIFIC SETTINGS         ------------
  //----------------------------------------------------------
  // Proof cluster
  plugin->SetProofCluster(proofcluster);
  // Dataset to be used
  plugin->SetProofDataSet(proofdataset);
  // May need to reset proof. Supported modes: 0-no reset, 1-soft, 2-hard
  plugin->SetProofReset(0);
  // May limit number of workers
  plugin->SetNproofWorkers(0);
  // May limit the number of workers per slave
  plugin->SetNproofWorkersPerSlave(1);
  // May use a specific version of root installed in proof
  plugin->SetRootVersionForProof("current");
  // May set the aliroot mode. Check http://aaf.cern.ch/node/83
  plugin->SetAliRootMode("default"); // Loads AF libs by default
  // May request ClearPackages (individual ClearPackage not supported)
  plugin->SetClearPackages(kFALSE);
  // Plugin test mode works only providing a file containing test file locations, used in "local" mode also
  plugin->SetFileForTestMode("files.txt"); // file should contain path name to a local directory containg *ESDs.root etc
  // Request connection to alien upon connection to grid
  plugin->SetProofConnectGrid(kFALSE);

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 4"<<endl;

  return plugin;

  cout<<"-->>>>>>>>>>>>>>>>>>>>>>>>> 5"<<endl;

}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 * REALM CONFIDENTIAL
 * __________________
 *
 *  [2011] - [2015] Realm Inc
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Realm Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Realm Incorporated
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Realm Incorporated.
 *
 **************************************************************************/

#pragma once
#ifndef REALM_UTIL_PRIORITY_QUEUE_HPP
#define REALM_UTIL_PRIORITY_QUEUE_HPP

#include <vector>
#include <functional>
#include <algorithm>

namespace realm {
namespace util {

/// PriorityQueue corresponds exactly to `std::priority_queue`, but has the extra feature
/// of allowing iteration and erasure of elements in the queue.
///
/// PriorityQueue only allows const access to its elements, because non-const access
/// would open up the risk of changing the ordering of the elements.
///
/// Note: As opposed to `std::priority_queue`, this does not store elements in a heap
/// internally. Instead, elements are stored in sorted order. Users of this class are
/// allowed to operate on this assumption.
template <class T, class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>>
class PriorityQueue : private Compare {
public:
    using container_type  = Container;
    using value_type      = typename Container::value_type;
    using size_type       = typename Container::size_type;
    using reference       = typename Container::reference;
    using const_reference = typename Container::const_reference;
    using const_iterator         = typename Container::const_reverse_iterator;
    using const_reverse_iterator = typename Container::const_iterator;

    //{@
    /// Construct a PriorityQueue, optionally providing a comparator object.
    PriorityQueue(const Compare& compare, const Container& cont);

    explicit PriorityQueue(const Compare& compare = Compare{}, Container&& cont = Container{});

    template<class InputIt>
    PriorityQueue(InputIt first, InputIt last, const Compare& compare, const Container& cont);

    template<class InputIt>
    PriorityQueue(InputIt first, InputIt last, const Compare& compare = Compare{},
                  Container&& cont = Container{});
    //@}
    // Skipping Allocator-specific template constructors.

    PriorityQueue(const PriorityQueue&) = default;
    PriorityQueue(PriorityQueue&&) = default;
    PriorityQueue& operator=(const PriorityQueue&) = default;
    PriorityQueue& operator=(PriorityQueue&&) = default;

    bool empty() const;
    size_type size() const;

    //{@
    /// Push an element to the priority queue.
    ///
    /// If insertion to the underlying `Container` invalidates
    /// iterators and references, any iterators and references into this
    /// priority queue are also invalidated. By default, this is the case.
    void push(const T& value);
    void push(T&& value);
    //@}

    /// Pop the largest element from the priority queue.
    ///
    /// If `pop_back` on the underlying `Container` invalidates
    /// iterators and references, any iterators and reference into this
    /// priority queue are also invalidated. By default, this is *NOT* the case.
    ///
    /// Calling `pop()` on an empty priority queue is undefined.
    void pop();

    /// Return a reference to the largest element of the priority queue.
    ///
    /// Calling `top()` on an empty priority queue is undefined.
    const_reference top() const;

    // FIXME: emplace() deliberately omitted for simplicity.

    /// Swap the contents of this priority queue with the contents of \a other.
    void swap(PriorityQueue& other);

    // Not in std::priority_queue:

    /// Return an iterator to the beginning of the queue (largest element first).
    const_iterator begin() const;

    /// Return an iterator to the end of the queue (smallest element last);
    const_iterator end() const;

    /// Return a reverse iterator into the priority queue (smallest element first).
    const_reverse_iterator rbegin() const;

    /// Return a reverse iterator representing the end of the priority queue (largest element last).
    const_reverse_iterator rend() const;

    /// Erase element pointed to by \a it.
    ///
    /// Calling `erase()` with a beyond-end iterator (such as what is returned by `end()`)
    /// is undefined.
    void erase(const_iterator it);

    /// Remove all elements from the priority queue.
    void clear();

    /// Calls `reserve()` on the underlying `Container`.
    void reserve(size_type);

private:
    Container m_queue;

    const Compare& compare() const;
    Compare& compare();
};

/// Implementation

template<class T, class Container, class Compare>
PriorityQueue<T, Container, Compare>::PriorityQueue(const Compare& compare, const Container& cont):
    Compare(compare), m_queue(cont)
{
}

template<class T, class Container, class Compare>
PriorityQueue<T, Container, Compare>::PriorityQueue(const Compare& compare, Container&& cont):
    Compare(compare), m_queue(std::move(cont))
{
}

template<class T, class Container, class Compare>
template<class InputIt>
PriorityQueue<T, Container, Compare>::PriorityQueue(InputIt first, InputIt last,
                                                    const Compare& compare, const Container& cont):
    Compare(compare), m_queue(cont)
{
    for (auto it = first; it != last; ++it) {
        push(*it);
    }
}

template<class T, class Container, class Compare>
template<class InputIt>
PriorityQueue<T, Container, Compare>::PriorityQueue(InputIt first, InputIt last,
                                                    const Compare& compare, Container&& cont):
    Compare(compare), m_queue(std::move(cont))
{
    for (auto it = first; it != last; ++it) {
        push(*it);
    }
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::push(const T& element)
{
    auto it = std::lower_bound(m_queue.begin(), m_queue.end(), element, compare());
    m_queue.insert(it, element);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::push(T&& element)
{
    auto it = std::lower_bound(m_queue.begin(), m_queue.end(), element, compare());
    m_queue.insert(it, std::move(element));
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::pop()
{
    m_queue.pop_back();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reference
PriorityQueue<T, Container, Compare>::top() const
{
    return m_queue.back();
}

template<class T, class Container, class Compare>
Compare& PriorityQueue<T, Container, Compare>::compare()
{
    return *this;
}

template<class T, class Container, class Compare>
const Compare& PriorityQueue<T, Container, Compare>::compare() const
{
    return *this;
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_iterator
PriorityQueue<T, Container, Compare>::begin() const
{
    return m_queue.rbegin();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_iterator
PriorityQueue<T, Container, Compare>::end() const
{
    return m_queue.rend();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reverse_iterator
PriorityQueue<T, Container, Compare>::rbegin() const
{
    return m_queue.begin();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reverse_iterator
PriorityQueue<T, Container, Compare>::rend() const
{
    return m_queue.end();
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::erase(const_iterator it)
{
    m_queue.erase(it);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::clear()
{
    m_queue.clear();
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::reserve(size_type sz)
{
    m_queue.reserve(sz);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::swap(PriorityQueue& other)
{
    using std::swap;
    swap(m_queue, other.m_queue);
    swap(compare(), other.compare());
}

}
}

#endif // REALM_UTIL_PRIORITY_QUEUE_HPP

<commit_msg>Reverted the reverse_iterator semantics<commit_after>/*************************************************************************
 *
 * REALM CONFIDENTIAL
 * __________________
 *
 *  [2011] - [2015] Realm Inc
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Realm Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Realm Incorporated
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Realm Incorporated.
 *
 **************************************************************************/

#pragma once
#ifndef REALM_UTIL_PRIORITY_QUEUE_HPP
#define REALM_UTIL_PRIORITY_QUEUE_HPP

#include <vector>
#include <functional>
#include <algorithm>

namespace realm {
namespace util {

/// PriorityQueue corresponds exactly to `std::priority_queue`, but has the extra feature
/// of allowing iteration and erasure of elements in the queue.
///
/// PriorityQueue only allows const access to its elements, because non-const access
/// would open up the risk of changing the ordering of the elements.
///
/// Note: As opposed to `std::priority_queue`, this does not store elements in a heap
/// internally. Instead, elements are stored in sorted order. Users of this class are
/// allowed to operate on this assumption.
template <class T, class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>>
class PriorityQueue : private Compare {
public:
    using container_type  = Container;
    using value_type      = typename Container::value_type;
    using size_type       = typename Container::size_type;
    using reference       = typename Container::reference;
    using const_reference = typename Container::const_reference;
    using const_reverse_iterator = typename Container::const_reverse_iterator;
    using const_iterator         = typename Container::const_iterator;

    //{@
    /// Construct a PriorityQueue, optionally providing a comparator object.
    PriorityQueue(const Compare& compare, const Container& cont);

    explicit PriorityQueue(const Compare& compare = Compare{}, Container&& cont = Container{});

    template<class InputIt>
    PriorityQueue(InputIt first, InputIt last, const Compare& compare, const Container& cont);

    template<class InputIt>
    PriorityQueue(InputIt first, InputIt last, const Compare& compare = Compare{},
                  Container&& cont = Container{});
    //@}
    // Skipping Allocator-specific template constructors.

    PriorityQueue(const PriorityQueue&) = default;
    PriorityQueue(PriorityQueue&&) = default;
    PriorityQueue& operator=(const PriorityQueue&) = default;
    PriorityQueue& operator=(PriorityQueue&&) = default;

    bool empty() const;
    size_type size() const;

    //{@
    /// Push an element to the priority queue.
    ///
    /// If insertion to the underlying `Container` invalidates
    /// iterators and references, any iterators and references into this
    /// priority queue are also invalidated. By default, this is the case.
    void push(const T& value);
    void push(T&& value);
    //@}

    /// Pop the largest element from the priority queue.
    ///
    /// If `pop_back` on the underlying `Container` invalidates
    /// iterators and references, any iterators and reference into this
    /// priority queue are also invalidated. By default, this is *NOT* the case.
    ///
    /// Calling `pop()` on an empty priority queue is undefined.
    void pop();

    /// Return a reference to the largest element of the priority queue.
    ///
    /// Calling `top()` on an empty priority queue is undefined.
    const_reference top() const;

    // FIXME: emplace() deliberately omitted for simplicity.

    /// Swap the contents of this priority queue with the contents of \a other.
    void swap(PriorityQueue& other);

    // Not in std::priority_queue:

    /// Return an iterator to the beginning of the queue (smallest element first).
    const_iterator begin() const;

    /// Return an iterator to the end of the queue (largest element last);
    const_iterator end() const;

    /// Return a reverse iterator into the priority queue (largest element first).
    const_reverse_iterator rbegin() const;

    /// Return a reverse iterator representing the end of the priority queue (smallest element last).
    const_reverse_iterator rend() const;

    /// Erase element pointed to by \a it.
    ///
    /// Calling `erase()` with a beyond-end iterator (such as what is returned by `end()`)
    /// is undefined.
    void erase(const_iterator it);

    /// Remove all elements from the priority queue.
    void clear();

    /// Calls `reserve()` on the underlying `Container`.
    void reserve(size_type);

private:
    Container m_queue;

    const Compare& compare() const;
    Compare& compare();
};

/// Implementation

template<class T, class Container, class Compare>
PriorityQueue<T, Container, Compare>::PriorityQueue(const Compare& compare, const Container& cont):
    Compare(compare), m_queue(cont)
{
}

template<class T, class Container, class Compare>
PriorityQueue<T, Container, Compare>::PriorityQueue(const Compare& compare, Container&& cont):
    Compare(compare), m_queue(std::move(cont))
{
}

template<class T, class Container, class Compare>
template<class InputIt>
PriorityQueue<T, Container, Compare>::PriorityQueue(InputIt first, InputIt last,
                                                    const Compare& compare, const Container& cont):
    Compare(compare), m_queue(cont)
{
    for (auto it = first; it != last; ++it) {
        push(*it);
    }
}

template<class T, class Container, class Compare>
template<class InputIt>
PriorityQueue<T, Container, Compare>::PriorityQueue(InputIt first, InputIt last,
                                                    const Compare& compare, Container&& cont):
    Compare(compare), m_queue(std::move(cont))
{
    for (auto it = first; it != last; ++it) {
        push(*it);
    }
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::push(const T& element)
{
    auto it = std::lower_bound(m_queue.begin(), m_queue.end(), element, compare());
    m_queue.insert(it, element);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::push(T&& element)
{
    auto it = std::lower_bound(m_queue.begin(), m_queue.end(), element, compare());
    m_queue.insert(it, std::move(element));
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::pop()
{
    m_queue.pop_back();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reference
PriorityQueue<T, Container, Compare>::top() const
{
    return m_queue.back();
}

template<class T, class Container, class Compare>
Compare& PriorityQueue<T, Container, Compare>::compare()
{
    return *this;
}

template<class T, class Container, class Compare>
const Compare& PriorityQueue<T, Container, Compare>::compare() const
{
    return *this;
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_iterator
PriorityQueue<T, Container, Compare>::begin() const
{
    return m_queue.begin();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_iterator
PriorityQueue<T, Container, Compare>::end() const
{
    return m_queue.end();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reverse_iterator
PriorityQueue<T, Container, Compare>::rbegin() const
{
    return m_queue.rbegin();
}

template<class T, class Container, class Compare>
typename PriorityQueue<T, Container, Compare>::const_reverse_iterator
PriorityQueue<T, Container, Compare>::rend() const
{
    return m_queue.rend();
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::erase(const_iterator it)
{
    m_queue.erase(it);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::clear()
{
    m_queue.clear();
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::reserve(size_type sz)
{
    m_queue.reserve(sz);
}

template<class T, class Container, class Compare>
void PriorityQueue<T, Container, Compare>::swap(PriorityQueue& other)
{
    using std::swap;
    swap(m_queue, other.m_queue);
    swap(compare(), other.compare());
}

}
}

#endif // REALM_UTIL_PRIORITY_QUEUE_HPP

<|endoftext|>


<commit_before>//
//  inpal_prime.cpp
//  InversePalindrome
//
//  Created by Bryan Triana on 7/13/16.
//  Copyright © 2016 Inverse Palindrome. All rights reserved.
//

#include "inpal_prime.hpp"
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

std::vector<std::size_t> inpal::prime::prime_list(std::size_t range)
{
    const auto primes = prime_sieve(range);
    std::vector<std::size_t> p_list;

    for(std::size_t i=2; i<=range; i++) if(primes[i]) p_list.push_back(i);

    return p_list;
}

std::vector<std::size_t> inpal::prime::factor_list(std::size_t num)
{
    std::vector<std::size_t> p_fac;
    std::size_t prime_factor = 2;

    //trial division
    while(prime_factor<=num)
    {
        while(num%prime_factor==0)
        {
            p_fac.push_back(prime_factor);
            num=num/prime_factor;
        }
        prime_factor += prime_factor==2 ? 1 : 2;
    }

    return p_fac;
}

std::size_t inpal::prime::prime_locate(std::size_t pos)
{
    //index starts at 1 instead of 0, eg 1st prime is 2
    pos = pos-1;

    //return values for input less or equal to 10
    const auto small_primes = prime_list(29);
    if(pos<10) return small_primes[pos];

    //denotes the limit of the sieve
    std::size_t limit = pos*log(pos)+pos*log(log(pos));
    const auto primes = prime_list(limit);

    return primes[pos];
}

std::size_t inpal::prime::max_prime(std::size_t num)
{
    const auto primes = prime_sieve(num);
    auto it = std::find(primes.rbegin(), primes.rend(), true);

    return primes.size()-std::distance(primes.rbegin(), it)-1;
}

std::size_t inpal::prime::prime_count(std::size_t range)
{
    const auto primes = prime_sieve(range);

    return std::count(primes.begin(), primes.end(), true);
}

double inpal::prime::prime_density(double range)
{
    return prime_count(range)/range;
}

bool inpal::prime::prime_test(std::size_t num)
{
    return num == max_prime(num);
}

bool inpal::prime::twin_test(std::size_t num)
{
    const auto primes = prime_sieve(num+2);

    return num!=2 && primes[primes.size()-3] && (primes[primes.size()-1] || primes[primes.size()-5]);
}

bool inpal::prime::cousin_test(std::size_t num)
{
    const auto primes = prime_sieve(num+4);

    return  num!=2 && primes[primes.size()-5] && (primes[primes.size()-1] || primes[primes.size()-9]);
}

bool inpal::prime::sexy_test(std::size_t num)
{
    const auto primes = prime_sieve(num+6);

    return (num!=2 && num!=3) && primes[primes.size()-7] && (primes[primes.size()-1] || primes[primes.size()-13]);
}

std::size_t inpal::prime::max_palprime(std::size_t num)
{
    const auto primes = prime_sieve(num);

    for(std::size_t i=num; i>=2; --i) if(primes[i] && pal_test(i)) return i;

    return 2;
}

std::size_t inpal::prime::max_factor(std::size_t num)
{
    return factor_list(num).back();
}

std::size_t inpal::prime::factor_count(std::size_t num)
{
    return factor_list(num).size();
}

std::vector<bool> inpal::prime::prime_sieve(std::size_t range)
{
    std::vector<bool> p_test(range+1, false);

    //defines square root of range for future usage
    const std::size_t root = ceil(sqrt(range));

    //sieve axioms
    for(std::size_t x=1; x<=root; x++)
        for(std::size_t y=1; y<=root; y++)
        {
            std::size_t i = (4*x*x)+(y*y);
            if (i<=range && (i%12==1 || i%12==5))
            {
                p_test[i].flip();
            }

            i = (3*x*x)+(y*y);
            if(i<=range && i%12==7)
            {
                p_test[i].flip();
            }

            i = (3*x*x)-(y*y);
            if(x>y && i<=range && i%12==11)
            {
                p_test[i].flip();
            }
        }

    //marks 2,3,5 and 7 as prime numbers to deal with input smaller than 7
    p_test[2]=p_test[3]=p_test[5]=p_test[7]=true;

    //marks all multiples of primes as non primes
    for(std::size_t r=5; r<=root; r++)
    {
        if((p_test[r]))
        {
            for(std::size_t j=r*r; j<=range; j+=r*r)
            {
                p_test[j]=false;
            }
        }
    }

    return p_test;
}

bool inpal::prime::pal_test(std::size_t num)
{
    std::string rev = std::to_string(num);

    //checks if half the reverse of rev is equal to the other half of rev
    if(std::equal(rev.begin(), rev.begin()+rev.size()/2, rev.rbegin()))
    {
        return true;
    }

    return false;
}
<commit_msg>performance improvement for prime_test function<commit_after>//
//  inpal_prime.cpp
//  InversePalindrome
//
//  Created by Bryan Triana on 7/13/16.
//  Copyright © 2016 Inverse Palindrome. All rights reserved.
//

#include "inpal_prime.hpp"
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

std::vector<std::size_t> inpal::prime::prime_list(std::size_t range)
{
    const auto primes = prime_sieve(range);
    std::vector<std::size_t> p_list;

    for(std::size_t i=2; i<=range; i++) if(primes[i]) p_list.push_back(i);

    return p_list;
}

std::vector<std::size_t> inpal::prime::factor_list(std::size_t num)
{
    std::vector<std::size_t> p_fac;
    std::size_t prime_factor = 2;

    //trial division
    while(prime_factor<=num)
    {
        while(num%prime_factor==0)
        {
            p_fac.push_back(prime_factor);
            num=num/prime_factor;
        }
        prime_factor += prime_factor==2 ? 1 : 2;
    }

    return p_fac;
}

std::size_t inpal::prime::prime_locate(std::size_t pos)
{
    //index starts at 1 instead of 0, eg 1st prime is 2
    pos = pos-1;

    //return values for input less or equal to 10
    const auto small_primes = prime_list(29);
    if(pos<10) return small_primes[pos];

    //denotes the limit of the sieve
    std::size_t limit = pos*log(pos)+pos*log(log(pos));
    const auto primes = prime_list(limit);

    return primes[pos];
}

std::size_t inpal::prime::max_prime(std::size_t num)
{
    const auto primes = prime_sieve(num);
    auto it = std::find(primes.rbegin(), primes.rend(), true);

    return primes.size()-std::distance(primes.rbegin(), it)-1;
}

std::size_t inpal::prime::prime_count(std::size_t range)
{
    const auto primes = prime_sieve(range);

    return std::count(primes.begin(), primes.end(), true);
}

double inpal::prime::prime_density(double range)
{
    return prime_count(range)/range;
}

bool inpal::prime::prime_test(std::size_t num)
{
   if(num==2) return true;
   if(num%2==0 || num==1) return false;

   for(std::size_t divisor=3; divisor<=sqrt(num); divisor+=2) if(num%divisor==0) return false;

   return true;
}

bool inpal::prime::twin_test(std::size_t num)
{
   return prime_test(num) && (prime_test(num-2) || prime_test(num+2));
}

bool inpal::prime::cousin_test(std::size_t num)
{
   return  prime_test(num) && (prime_test(num-4) || prime_test(num+4));
}

bool inpal::prime::sexy_test(std::size_t num)
{
   return num!=3 && prime_test(num) && (prime_test(num-6) || prime_test(num+6));
}

std::size_t inpal::prime::max_palprime(std::size_t num)
{
    const auto primes = prime_sieve(num);

    for(std::size_t i=num; i>=2; --i) if(primes[i] && pal_test(i)) return i;

    return 2;
}

std::size_t inpal::prime::max_factor(std::size_t num)
{
    return factor_list(num).back();
}

std::size_t inpal::prime::factor_count(std::size_t num)
{
    return factor_list(num).size();
}

std::vector<bool> inpal::prime::prime_sieve(std::size_t range)
{
    std::vector<bool> p_test(range+1, false);

    //defines square root of range for future usage
    const std::size_t root = ceil(sqrt(range));

    //sieve axioms
    for(std::size_t x=1; x<=root; x++)
        for(std::size_t y=1; y<=root; y++)
        {
            std::size_t i = (4*x*x)+(y*y);
            if (i<=range && (i%12==1 || i%12==5))
            {
                p_test[i].flip();
            }

            i = (3*x*x)+(y*y);
            if(i<=range && i%12==7)
            {
                p_test[i].flip();
            }

            i = (3*x*x)-(y*y);
            if(x>y && i<=range && i%12==11)
            {
                p_test[i].flip();
            }
        }

    //marks 2,3,5 and 7 as prime numbers to deal with input smaller than 7
    p_test[2]=p_test[3]=p_test[5]=p_test[7]=true;

    //marks all multiples of primes as non primes
    for(std::size_t r=5; r<=root; r++)
    {
        if((p_test[r]))
        {
            for(std::size_t j=r*r; j<=range; j+=r*r)
            {
                p_test[j]=false;
            }
        }
    }

    return p_test;
}

bool inpal::prime::pal_test(std::size_t num)
{
    std::string rev = std::to_string(num);

    //checks if half the reverse of rev is equal to the other half of rev
    if(std::equal(rev.begin(), rev.begin()+rev.size()/2, rev.rbegin()))
    {
        return true;
    }

    return false;
}
<|endoftext|>


<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2011 Sergey Lisitsyn
 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
 */

#include <shogun/lib/config.h>
#ifdef HAVE_ARPACK
#ifdef HAVE_LAPACK
#include <shogun/mathematics/arpack.h>
#include <shogun/mathematics/arpack_s.h>
#include <shogun/mathematics/arpack_d.h>

using namespace shogun;

namespace shogun
{

template <>
void arpack_xsxupd<float32_t>(float32_t* matrix, float32_t* rhs_diag, int n, int nev, const char* which,
                              int mode, bool pos, float32_t shift, float32_t tolerance,
                              float32_t* eigenvalues, float32_t* eigenvectors, int& status)
{
	arpack_ssxupd(matrix,rhs_diag,n,nev,which,mode,pos,shift,tolerance,eigenvalues,eigenvectors,status);
}

template <>
void arpack_xsxupd<float64_t>(float64_t* matrix, float64_t* rhs_diag, int n, int nev, const char* which,
                              int mode, bool pos, float64_t shift, float64_t tolerance,
                              float64_t* eigenvalues, float64_t* eigenvectors, int& status)
{
	arpack_dsxupd(matrix,rhs_diag,n,nev,which,mode,pos,shift,tolerance,eigenvalues,eigenvectors,status);
}
}
#endif
#endif
<commit_msg>Added common.h include for arpack wrap impl<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2011 Sergey Lisitsyn
 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
 */

#include <shogun/lib/config.h>
#ifdef HAVE_ARPACK
#ifdef HAVE_LAPACK
#include <shogun/lib/common.h>
#include <shogun/mathematics/arpack.h>
#include <shogun/mathematics/arpack_s.h>
#include <shogun/mathematics/arpack_d.h>

using namespace shogun;

namespace shogun
{

template <>
void arpack_xsxupd<float32_t>(float32_t* matrix, float32_t* rhs_diag, int n, int nev, const char* which,
                              int mode, bool pos, float32_t shift, float32_t tolerance,
                              float32_t* eigenvalues, float32_t* eigenvectors, int& status)
{
	arpack_ssxupd(matrix,rhs_diag,n,nev,which,mode,pos,shift,tolerance,eigenvalues,eigenvectors,status);
}

template <>
void arpack_xsxupd<float64_t>(float64_t* matrix, float64_t* rhs_diag, int n, int nev, const char* which,
                              int mode, bool pos, float64_t shift, float64_t tolerance,
                              float64_t* eigenvalues, float64_t* eigenvectors, int& status)
{
	arpack_dsxupd(matrix,rhs_diag,n,nev,which,mode,pos,shift,tolerance,eigenvalues,eigenvectors,status);
}
}
#endif
#endif
<|endoftext|>


<commit_before>#include "common/test.hpp"
#include "utils/color.hpp"

int main() {
  using namespace polybar;

  "rgb"_test = []{
    unsigned int color{0x123456};
    expect(color_util::alpha_channel<unsigned char>(color) == 0);
    expect(color_util::red_channel<unsigned char>(color) == 0x12);
    expect(color_util::green_channel<unsigned char>(color) == 0x34);
    expect(color_util::green_channel<unsigned short int>(color) == 0x3434);
    expect(color_util::blue_channel<unsigned char>(color) == 0x56);

    expect(rgb{0xFF112233}.b == 0x33 / 255.0);
    expect(rgb{0x88449933}.g == 0x51 / 255.0);
    expect(rgb{0xee111111} == 0xff0f0f0f);
    expect(rgb{0x99112233} == 0xff0a141e);
  };

  "rgba"_test = []{
    unsigned int color{0xCC123456};
    expect(color_util::alpha_channel<unsigned short int>(color) == 0xCCCC);
    expect(color_util::red_channel<unsigned short int>(color) == 0x1212);
    expect(color_util::red_channel<unsigned char>(color) == 0x12);
    expect(color_util::green_channel<unsigned short int>(color) == 0x3434);
    expect(color_util::blue_channel<unsigned short int>(color) == 0x5656);

    expect(rgba{0xCC112233}.a == 0xCC / 255.0);
    expect(rgba{0x88449933}.g == 0x99 / 255.0);
    expect(static_cast<unsigned int>(rgba{0xFF111111}) == 0xFF111111);
    expect(static_cast<unsigned int>(rgba{0x00FFFFFF}) == 0x00FFFFFF);
  };

  "hex"_test = [] {
    unsigned int colorA{0x123456};
    expect(color_util::hex<unsigned char>(colorA) == "#123456"s);
    unsigned int colorB{0xCC123456};
    expect(color_util::hex<unsigned short int>(colorB) == "#cc123456"s);
    unsigned int colorC{0x00ffffff};
    expect(color_util::hex<unsigned short int>(colorC) == "#00ffffff"s);
  };

  "parse_hex"_test = [] {
    expect(color_util::parse_hex("#fff") == "#ffffffff");
    expect(color_util::parse_hex("#123") == "#ff112233");
    expect(color_util::parse_hex("#888888") == "#ff888888");
    expect(color_util::parse_hex("#00aa00aa") == "#00aa00aa");
  };

  "parse"_test = [] {
    expect(color_util::parse("invalid") == 0);
    expect(color_util::parse("#f") == 0);
    expect(color_util::parse("#ff") == 0);
    expect(color_util::parse("invalid", 0xFF999999) == 0xFF999999);
    expect(color_util::parse("invalid", 0x00111111) == 0x00111111);
    expect(color_util::parse("invalid", 0xFF000000) == 0xFF000000);
    expect(color_util::parse("#fff") == 0xffffffff);
    expect(color_util::parse("#890") == 0xFF889900);
    expect(color_util::parse("#55888777") == 0x55888777);
    expect(color_util::parse("#88aaaaaa") == 0x88aaaaaa);
    expect(color_util::parse("#00aaaaaa") == 0x00aaaaaa);
    expect(color_util::parse("#00FFFFFF") == 0x00FFFFFF);
  };

  "simplify"_test = [] {
    expect(color_util::simplify_hex("#FF111111") == "#111");
    expect(color_util::simplify_hex("#ff223344") == "#234");
    expect(color_util::simplify_hex("#ee223344") == "#ee223344");
    expect(color_util::simplify_hex("#ff234567") == "#234567");
    expect(color_util::simplify_hex("#00223344") == "#00223344");
  };
}
<commit_msg>tests: Convert color test to gtest<commit_after>#include "common/test.hpp"
#include "utils/color.hpp"

using namespace polybar;

TEST(String, rgb) {
  unsigned int color{0x123456};
  EXPECT_EQ(0, color_util::alpha_channel<unsigned char>(color));
  EXPECT_EQ(0x12, color_util::red_channel<unsigned char>(color));
  EXPECT_EQ(0x34, color_util::green_channel<unsigned char>(color));
  EXPECT_EQ(0x3434, color_util::green_channel<unsigned short int>(color));
  EXPECT_EQ(0x56, color_util::blue_channel<unsigned char>(color));

  EXPECT_TRUE(0x33 / 255.0 == rgb{0xFF112233}.b);
  EXPECT_TRUE(0x51 / 255.0 == rgb{0x88449933}.g);
  EXPECT_TRUE(0xff0f0f0f == rgb{0xee111111});
  EXPECT_TRUE(0xff0a141e == rgb{0x99112233});
}

TEST(String, rgba) {
  unsigned int color{0xCC123456};
  EXPECT_EQ(0xCCCC, color_util::alpha_channel<unsigned short int>(color));
  EXPECT_EQ(0x1212, color_util::red_channel<unsigned short int>(color));
  EXPECT_EQ(0x12, color_util::red_channel<unsigned char>(color));
  EXPECT_EQ(0x3434, color_util::green_channel<unsigned short int>(color));
  EXPECT_EQ(0x5656, color_util::blue_channel<unsigned short int>(color));

  EXPECT_EQ(0xCC / 255.0, rgba{0xCC112233}.a);
  EXPECT_EQ(0x99 / 255.0, rgba{0x88449933}.g);
  EXPECT_EQ(0xFF111111, static_cast<unsigned int>(rgba{0xFF111111}));
  EXPECT_EQ(0x00FFFFFF, static_cast<unsigned int>(rgba{0x00FFFFFF}));
}

TEST(String, hex) {
  unsigned int colorA{0x123456};
  EXPECT_EQ("#123456"s, color_util::hex<unsigned char>(colorA));
  unsigned int colorB{0xCC123456};
  EXPECT_EQ("#cc123456"s, color_util::hex<unsigned short int>(colorB));
  unsigned int colorC{0x00ffffff};
  EXPECT_EQ("#00ffffff"s, color_util::hex<unsigned short int>(colorC));
}

TEST(String, parseHex) {
  EXPECT_EQ("#ffffffff", color_util::parse_hex("#fff"));
  EXPECT_EQ("#ff112233", color_util::parse_hex("#123"));
  EXPECT_EQ("#ff888888", color_util::parse_hex("#888888"));
  EXPECT_EQ("#00aa00aa", color_util::parse_hex("#00aa00aa"));
}

TEST(String, parse) {
  EXPECT_EQ(0, color_util::parse("invalid"));
  EXPECT_EQ(0, color_util::parse("#f"));
  EXPECT_EQ(0, color_util::parse("#ff"));
  EXPECT_EQ(0xFF999999, color_util::parse("invalid", 0xFF999999));
  EXPECT_EQ(0x00111111, color_util::parse("invalid", 0x00111111));
  EXPECT_EQ(0xFF000000, color_util::parse("invalid", 0xFF000000));
  EXPECT_EQ(0xffffffff, color_util::parse("#fff"));
  EXPECT_EQ(0xFF889900, color_util::parse("#890"));
  EXPECT_EQ(0x55888777, color_util::parse("#55888777"));
  EXPECT_EQ(0x88aaaaaa, color_util::parse("#88aaaaaa"));
  EXPECT_EQ(0x00aaaaaa, color_util::parse("#00aaaaaa"));
  EXPECT_EQ(0x00FFFFFF, color_util::parse("#00FFFFFF"));
}

TEST(String, simplify) {
  EXPECT_EQ("#111", color_util::simplify_hex("#FF111111"));
  EXPECT_EQ("#234", color_util::simplify_hex("#ff223344"));
  EXPECT_EQ("#ee223344", color_util::simplify_hex("#ee223344"));
  EXPECT_EQ("#234567", color_util::simplify_hex("#ff234567"));
  EXPECT_EQ("#00223344", color_util::simplify_hex("#00223344"));
}
<|endoftext|>


<commit_before>/*
 * Copyright (c) 2011 Vertica Systems, an HP Company
 *
 * Description: Example User Defined Transform Function: IISParserize a string
 *
 * Create Date: Nov 2, 2011
 */
#include "Vertica.h"
#include <iostream>
#include <sstream>
#include <vector>
#include <map>

/* There are 22 total possible fields in the w3c spec */
#define NUMFIELDS 22

using namespace Vertica;
using namespace std;

/*
 * Parse a standard w3c log file.
 */

// This is a list of all field names, in the same order as our output record
static const string incolumns[] = { "date", "time", "c-ip", "cs-username", "s-sitename", "s-computername",
                                    "s-ip", "s-port", "cs-method", "cs-uri-stem", "cs-uri-query", "sc-status",
                                    "sc-win32-status", "sc-bytes", "cs-bytes", "time-taken", "cs-version",
                                    "cs-host", "cs(User-Agent)", "cs(Cookie)", "cs(Referer)", "sc-substatus" };

// This is a list of all field names, as used by the output record
static const string outcolumns[] = { "date", "time", "c_ip", "cs_username", "s_sitename", "s_computername",
                                     "s_ip", "s_port", "cs_method", "cs_uri_stem", "cs_uri_query", "sc_status",
                                     "sc_win32_status", "sc_bytes", "cs_bytes", "time_taken", "cs_version",
                                     "cs_host", "cs_user_agent", "cs_cookie", "cs_referer", "sc_substatus" };

static bool loggingSet = false;

static void log(char *str) {
   if(loggingSet) {
      if(!str) {
         printf("INFO: NULL\n");
      } else {
         printf("INFO: %s\n", str);
      }
   }
}

static void log(string str) {
   log(str.c_str());
}

static void logValue(char *cstr, string str) {
   if(loggingSet) {
      if(str.empty()) {
         printf("VALU: [%s] [NULL]\n", cstr);
      } else {
         printf("VALU: [%s] [%s]\n", cstr, str.c_str());
      }
   }
}

static void logValue(char *cstr, int val) {
   if(loggingSet) {
      string sval = "" + val;
      logValue(cstr, val);
   }
}

static void logFunction(char *cstr) {
   if(loggingSet) {
      printf("FUNC: %s\n", cstr);
   }
}

class w3cLogParser : public TransformFunction
{
   // When we get a #Fields: directive, this variable gets filled with the field list
   vector<string> fields;

   // Map for holding positions
   typedef std::map<string, int> FieldMap;

   // This is a mapping of column names (from incolumns) to record position in the output record
   FieldMap positions;

   // Offsets for the mapping
   vector<int> offsets;

   // fieldsSet is set to true after the #Fields: directive is received
   bool fieldsSet;

   virtual void processPartition(ServerInterface &srvInterface,
      PartitionReader &input_reader,
      PartitionWriter &output_writer)
   {
      try {

      // Until we get a Fields: directive, we can't process data rows
      logValue("fieldSet", "false");
      fieldsSet = false;

      // Pre-fill the positions map.  This maps the values in incolumns to values 0..n.
      // The mapping of incoming fields to positions is done in setFields()
      // e.g. incolumns[0] = 'date', so positions['date'] = 0.
      // This is really just for faster lookup...
      log("Pre-fill positions map");
      for (int i = 0; i < NUMFIELDS; i++) {
         positions[incolumns[i]] = i;
      }

      if (input_reader.getNumCols() != 1)
         vt_report_error(0, "Function only accepts 1 argument, but %zu provided", input_reader.getNumCols());

      // There will be one line for each line of data in the partition.  This function is called
      // exactly once per partition.  Because each log file is a partition, this function will be
      // called exactly once.  First will be some directives (#...), including the #Fields: directive.
      //
      // All the other rows should be data rows.
      log("Begin processing rows");

      do
      {
         // The sentence variable contains the raw data (list of space-separated values)
         const VString &sentence = input_reader.getStringRef(0);
         std::string line = sentence.str();

         // Set all fields to null, as we may not have values for all of them
         log("Blanking all output fields");
         for(int i = 0; i < NUMFIELDS; i++)
         {
            VString &word = output_writer.getStringRef(i);
            word.setNull();
         }

         // If input string is NULL, then output is NULL as well.
         if (sentence.isNull())
         {
	    // Don't do anything with this row
         }

         // If the line starts with an #, it is a directive.  Normally we will ignore these lines,
         // but the #Fields: directive is critical, as it tells us what fields are being sent,
         // and in what order.  Don't call output_writer, because there is no output row.
         else if (line.at(0) == '#') {
            logValue("Directive", line);

            if (line.find("#Fields:") == 0)
            {
               log("Setting fields");
               setFields(line);
            }
            else log("Ignoring");
         }

         else {
            log("Recieved a data row");
            // This is a data row.  Make sure we are ready for it!
            if(!fieldsSet) {
               stringstream ss;
               ss << "Received a data row, but the #Fields directive has not yet been received";
               throw ss.str();
            }
            // Parse the line into values (assumes space-delimited)
            vector<string> values;
            istringstream iss(line);

            do {
               string token;
               iss >> token;
               if(!token.empty()) {
                 values.push_back(token);
                 logValue("token", token);
               }
            } while (iss);

            // We should have exactly the same number of values as we have output fields
            if (values.size() != fields.size()) {
               stringstream ss;
               ss << "Input row has " << values.size() << " fields, expecting " << fields.size();
               throw ss.str();
            }

            // Now map the values into the correct placeholder in the result
            for (unsigned int i = 0; i < values.size(); i++)
            {
               // Get the field offset
               VString &word = output_writer.getStringRef(offsets[i]);

               // If the value is "-", then set the resulting column to null
               if( values[i].compare("-") == 0 )
               {
                  word.setNull();
               }
               // Set the appropriate output field to the found value
               else
               {
                  word.copy(values[i]);
               }
            }

            // Move to the next record
            output_writer.next();
         }
      } while (input_reader.next());

      }
      catch(string se) {
        vt_report_error(0, se.c_str());
      }
   }

   void setFields(const string &ref) {
      logFunction("setFields");
      logValue("ref", ref);

      // Note which fields will now be parsed.
      // Line looks like this:
      // #Fields: time cs-method cs-uri

      // This is our list of fields.  It needs to be reset every time we see a #Fields: line
      fields.clear();

      string input = ref;

      // Chop off the beginning and tokenize the rest
      input.erase(0, 9);

      istringstream iss(input);
      int i = 0;

      do {
         string token;
         iss >> token;

         if(!token.empty()) {
            fields.push_back(token);
            //printf("fields[%d]=%s\n", i++, token.c_str());
         }
      } while (iss);

      // Now that we have the field list, we need to create a map of incoming fields to positions
      // in the output record.
      log("Assign offset values");

      vector<string>::iterator it;

      for ( it=fields.begin() ; it < fields.end(); it++ ) {
         map<string, int>::iterator mip;
         // Find the field name (*it) in the positions map
         mip = positions.find(*it);

         if(mip == positions.end()) {
            // Whoops, not in our list!  Throw big error!!!
            string val = *it;
            stringstream ss;
            ss << "This field is not recognized by the parser: " << val.c_str();
            throw ss.str();
         } else {
            offsets.push_back(mip->second);
         }
      }

      // Print out the offset mapping
      vector<int>::iterator vit;
      i = 0;
      for(vit=offsets.begin(); vit < offsets.end(); vit++) {
         //printf("offsets[%d]=%d\n", i++, *vit);
      }

      // Fields are set, ready to process data!
      logValue("fieldsSet", "true");
      fieldsSet = true;
   }
};

class w3cLogParserFactory : public TransformFunctionFactory
{
   // Tell Vertica that we take in a row with 1 string, and return a row with 15 strings
   virtual void getPrototype(ServerInterface &srvInterface, ColumnTypes &argTypes, ColumnTypes &returnType)
   {
      argTypes.addVarchar();

      returnType.addVarchar();  // Date
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
   }

   // Tell Vertica what our return string length will be, given the input
   // string length
   virtual void getReturnType(ServerInterface &srvInterface,
      const SizedColumnTypes &input_types,
      SizedColumnTypes &output_types)
   {
      // Error out if we're called with anything but 1 argument
      if (input_types.getColumnCount() != 1)
         vt_report_error(0, "Function only accepts 1 argument, but %zu provided", input_types.getColumnCount());

      int input_len = 2048;

      // Our output size will never be more than the input size
      output_types.addVarchar(input_len, outcolumns[0]);  // date
      output_types.addVarchar(input_len, outcolumns[1]);
      output_types.addVarchar(input_len, outcolumns[2]);
      output_types.addVarchar(input_len, outcolumns[3]);
      output_types.addVarchar(input_len, outcolumns[4]);
      output_types.addVarchar(input_len, outcolumns[5]);
      output_types.addVarchar(input_len, outcolumns[6]);
      output_types.addVarchar(input_len, outcolumns[7]);  // int
      output_types.addVarchar(input_len, outcolumns[8]);
      output_types.addVarchar(input_len, outcolumns[9]);
      output_types.addVarchar(input_len, outcolumns[10]);
      output_types.addVarchar(input_len, outcolumns[11]); // int
      output_types.addVarchar(input_len, outcolumns[12]);
      output_types.addVarchar(input_len, outcolumns[13]); // int
      output_types.addVarchar(input_len, outcolumns[14]); // int
      output_types.addVarchar(input_len, outcolumns[15]);
      output_types.addVarchar(input_len, outcolumns[16]);
      output_types.addVarchar(input_len, outcolumns[17]);
      output_types.addVarchar(input_len, outcolumns[18]);
      output_types.addVarchar(input_len, outcolumns[19]);
      output_types.addVarchar(input_len, outcolumns[20]);
      output_types.addVarchar(input_len, outcolumns[21]);
   }

   virtual TransformFunction *createTransformFunction(ServerInterface &srvInterface)
      { return vt_createFuncObj(srvInterface.allocator, w3cLogParser); }

};

RegisterFactory(w3cLogParserFactory);
<commit_msg>Reset field offsets when new #Fields is seen<commit_after>/*
 * Copyright (c) 2011 Vertica Systems, an HP Company
 *
 * Description: Example User Defined Transform Function: IISParserize a string
 *
 * Create Date: Nov 2, 2011
 */
#include "Vertica.h"
#include <iostream>
#include <sstream>
#include <vector>
#include <map>

/* There are 22 total possible fields in the w3c spec */
#define NUMFIELDS 22

using namespace Vertica;
using namespace std;

/*
 * Parse a standard w3c log file.
 */

// This is a list of all field names, in the same order as our output record
static const string incolumns[] = { "date", "time", "c-ip", "cs-username", "s-sitename", "s-computername",
                                    "s-ip", "s-port", "cs-method", "cs-uri-stem", "cs-uri-query", "sc-status",
                                    "sc-win32-status", "sc-bytes", "cs-bytes", "time-taken", "cs-version",
                                    "cs-host", "cs(User-Agent)", "cs(Cookie)", "cs(Referer)", "sc-substatus" };

// This is a list of all field names, as used by the output record
static const string outcolumns[] = { "date", "time", "c_ip", "cs_username", "s_sitename", "s_computername",
                                     "s_ip", "s_port", "cs_method", "cs_uri_stem", "cs_uri_query", "sc_status",
                                     "sc_win32_status", "sc_bytes", "cs_bytes", "time_taken", "cs_version",
                                     "cs_host", "cs_user_agent", "cs_cookie", "cs_referer", "sc_substatus" };

static bool loggingSet = false;

static void log(char *str) {
   if(loggingSet) {
      if(!str) {
         printf("INFO: NULL\n");
      } else {
         printf("INFO: %s\n", str);
      }
   }
}

static void log(string str) {
   log(str.c_str());
}

static void logValue(char *cstr, string str) {
   if(loggingSet) {
      if(str.empty()) {
         printf("VALU: [%s] [NULL]\n", cstr);
      } else {
         printf("VALU: [%s] [%s]\n", cstr, str.c_str());
      }
   }
}

static void logValue(char *cstr, int val) {
   if(loggingSet) {
      string sval = "" + val;
      logValue(cstr, val);
   }
}

static void logFunction(char *cstr) {
   if(loggingSet) {
      printf("FUNC: %s\n", cstr);
   }
}

class w3cLogParser : public TransformFunction
{
   // When we get a #Fields: directive, this variable gets filled with the field list
   vector<string> fields;

   // Map for holding positions
   typedef std::map<string, int> FieldMap;

   // This is a mapping of column names (from incolumns) to record position in the output record
   FieldMap positions;

   // Offsets for the mapping
   vector<int> offsets;

   // fieldsSet is set to true after the #Fields: directive is received
   bool fieldsSet;

   virtual void processPartition(ServerInterface &srvInterface,
      PartitionReader &input_reader,
      PartitionWriter &output_writer)
   {
      try {

      // Until we get a Fields: directive, we can't process data rows
      logValue("fieldSet", "false");
      fieldsSet = false;

      // Pre-fill the positions map.  This maps the values in incolumns to values 0..n.
      // The mapping of incoming fields to positions is done in setFields()
      // e.g. incolumns[0] = 'date', so positions['date'] = 0.
      // This is really just for faster lookup...
      log("Pre-fill positions map");
      for (int i = 0; i < NUMFIELDS; i++) {
         positions[incolumns[i]] = i;
      }

      if (input_reader.getNumCols() != 1)
         vt_report_error(0, "Function only accepts 1 argument, but %zu provided", input_reader.getNumCols());

      // There will be one line for each line of data in the partition.  This function is called
      // exactly once per partition.  Because each log file is a partition, this function will be
      // called exactly once.  First will be some directives (#...), including the #Fields: directive.
      //
      // All the other rows should be data rows.
      log("Begin processing rows");

      do
      {
         // The sentence variable contains the raw data (list of space-separated values)
         const VString &sentence = input_reader.getStringRef(0);
         std::string line = sentence.str();

         // Set all fields to null, as we may not have values for all of them
         log("Blanking all output fields");
         for(int i = 0; i < NUMFIELDS; i++)
         {
            VString &word = output_writer.getStringRef(i);
            word.setNull();
         }

         // If input string is NULL, then output is NULL as well.
         if (sentence.isNull())
         {
	    // Don't do anything with this row
         }

         // If the line starts with an #, it is a directive.  Normally we will ignore these lines,
         // but the #Fields: directive is critical, as it tells us what fields are being sent,
         // and in what order.  Don't call output_writer, because there is no output row.
         else if (line.at(0) == '#') {
            logValue("Directive", line);

            if (line.find("#Fields:") == 0)
            {
               log("Setting fields");
               setFields(line);
            }
            else log("Ignoring");
         }

         else {
            log("Recieved a data row");
            // This is a data row.  Make sure we are ready for it!
            if(!fieldsSet) {
               stringstream ss;
               ss << "Received a data row, but the #Fields directive has not yet been received";
               throw ss.str();
            }
            // Parse the line into values (assumes space-delimited)
            vector<string> values;
            istringstream iss(line);

            do {
               string token;
               iss >> token;
               if(!token.empty()) {
                 values.push_back(token);
                 logValue("token", token);
               }
            } while (iss);

            // We should have exactly the same number of values as we have output fields
            if (values.size() != fields.size()) {
               stringstream ss;
               ss << "Input row has " << values.size() << " fields, expecting " << fields.size();
               throw ss.str();
            }

            // Now map the values into the correct placeholder in the result
            for (unsigned int i = 0; i < values.size(); i++)
            {
               // Get the field offset
               VString &word = output_writer.getStringRef(offsets[i]);

               // If the value is "-", then set the resulting column to null
               if( values[i].compare("-") == 0 )
               {
                  word.setNull();
               }
               // Set the appropriate output field to the found value
               else
               {
                  word.copy(values[i]);
               }
            }

            // Move to the next record
            output_writer.next();
         }
      } while (input_reader.next());

      }
      catch(string se) {
        vt_report_error(0, se.c_str());
      }
   }

   void setFields(const string &ref) {
      logFunction("setFields");
      logValue("ref", ref);

      // Note which fields will now be parsed.
      // Line looks like this:
      // #Fields: time cs-method cs-uri

      // This is our list of fields.  It needs to be reset every time we see a #Fields: line
      fields.clear();

      string input = ref;

      // Chop off the beginning and tokenize the rest
      input.erase(0, 9);

      istringstream iss(input);
      int i = 0;

      do {
         string token;
         iss >> token;

         if(!token.empty()) {
            fields.push_back(token);
            //printf("fields[%d]=%s\n", i++, token.c_str());
         }
      } while (iss);

      // Now that we have the field list, we need to create a map of incoming fields to positions
      // in the output record.
      log("Assign offset values");
      vector<string>::iterator it;
      offsets.clear();

      for ( it=fields.begin() ; it < fields.end(); it++ ) {
         map<string, int>::iterator mip;
         // Find the field name (*it) in the positions map
         mip = positions.find(*it);

         if(mip == positions.end()) {
            // Whoops, not in our list!  Throw big error!!!
            string val = *it;
            stringstream ss;
            ss << "This field is not recognized by the parser: " << val.c_str();
            throw ss.str();
         } else {
            offsets.push_back(mip->second);
         }
      }

      // Print out the offset mapping
      vector<int>::iterator vit;
      i = 0;
      for(vit=offsets.begin(); vit < offsets.end(); vit++) {
         //printf("offsets[%d]=%d\n", i++, *vit);
      }

      // Fields are set, ready to process data!
      logValue("fieldsSet", "true");
      fieldsSet = true;
   }
};

class w3cLogParserFactory : public TransformFunctionFactory
{
   // Tell Vertica that we take in a row with 1 string, and return a row with 15 strings
   virtual void getPrototype(ServerInterface &srvInterface, ColumnTypes &argTypes, ColumnTypes &returnType)
   {
      argTypes.addVarchar();

      returnType.addVarchar();  // Date
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();  // int
      returnType.addVarchar();  // int
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
      returnType.addVarchar();
   }

   // Tell Vertica what our return string length will be, given the input
   // string length
   virtual void getReturnType(ServerInterface &srvInterface,
      const SizedColumnTypes &input_types,
      SizedColumnTypes &output_types)
   {
      // Error out if we're called with anything but 1 argument
      if (input_types.getColumnCount() != 1)
         vt_report_error(0, "Function only accepts 1 argument, but %zu provided", input_types.getColumnCount());

      int input_len = 2048;

      // Our output size will never be more than the input size
      output_types.addVarchar(input_len, outcolumns[0]);  // date
      output_types.addVarchar(input_len, outcolumns[1]);
      output_types.addVarchar(input_len, outcolumns[2]);
      output_types.addVarchar(input_len, outcolumns[3]);
      output_types.addVarchar(input_len, outcolumns[4]);
      output_types.addVarchar(input_len, outcolumns[5]);
      output_types.addVarchar(input_len, outcolumns[6]);
      output_types.addVarchar(input_len, outcolumns[7]);  // int
      output_types.addVarchar(input_len, outcolumns[8]);
      output_types.addVarchar(input_len, outcolumns[9]);
      output_types.addVarchar(input_len, outcolumns[10]);
      output_types.addVarchar(input_len, outcolumns[11]); // int
      output_types.addVarchar(input_len, outcolumns[12]);
      output_types.addVarchar(input_len, outcolumns[13]); // int
      output_types.addVarchar(input_len, outcolumns[14]); // int
      output_types.addVarchar(input_len, outcolumns[15]);
      output_types.addVarchar(input_len, outcolumns[16]);
      output_types.addVarchar(input_len, outcolumns[17]);
      output_types.addVarchar(input_len, outcolumns[18]);
      output_types.addVarchar(input_len, outcolumns[19]);
      output_types.addVarchar(input_len, outcolumns[20]);
      output_types.addVarchar(input_len, outcolumns[21]);
   }

   virtual TransformFunction *createTransformFunction(ServerInterface &srvInterface)
      { return vt_createFuncObj(srvInterface.allocator, w3cLogParser); }

};

RegisterFactory(w3cLogParserFactory);
<|endoftext|>