Exemple #1
0
void PrintLeaves(Node *psRoot)
{
    if (psRoot)
    {
        PrintLeaves(psRoot->psLeft);
        
        if ((psRoot->psLeft == NULL) && (psRoot->psRight == NULL))
        {
            printf("%d ", psRoot->iData);
        }
        
        PrintLeaves(psRoot->psRight);
    }
}
Exemple #2
0
void PrintBoundry(Node *psRoot)
{
    if (psRoot)
    {
        printf("%d ", psRoot->iData);
        
        // Print Left Boundry
        PrintLeftBoundry(psRoot->psLeft);
        
        // Print Leaf nodes
        PrintLeaves(psRoot->psLeft);
        PrintLeaves(psRoot->psRight);
        
        // Print Right Boundry
        PrintRightBoundry(psRoot->psRight);
    }
}
int main(int argc, char *argv[]){
  if(argc>1){
    TFile inFile(argv[1],"read");
    if(inFile.IsOpen() && !inFile.IsZombie()){
      std::ofstream cppFile("src/cfa.cpp"), hppFile("inc/cfa.hpp");

      {TTree t("a","b");}//Magically make ROOT link things correctly...

      TChain *chainA(static_cast<TChain*>(inFile.Get("configurableAnalysis/eventA"))), *chainB(static_cast<TChain*>(inFile.Get("configurableAnalysis/eventB")));

      if(chainA!=NULL && chainB!=NULL){
        hppFile << "#ifndef H_CFA\n";
        hppFile << "#define H_CFA\n\n";
        hppFile << "#include <vector>\n";
        hppFile << "#include <string>\n";
        hppFile << "#include \"TChain.h\"\n";
        hppFile << "#include \"TBranch.h\"\n\n";

        hppFile << "class cfA{\n";
        hppFile << "public:\n";
        hppFile << "  int GetTotalEntries() const;\n";
        hppFile << "protected:\n";
        hppFile << "  cfA(const std::string&, const bool);\n";
        hppFile << "  TChain chainA, chainB;\n";
        hppFile << "  TChain* GetChainA();\n";
        hppFile << "  TChain* GetChainB();\n";
        hppFile << "  std::string GetSampleName() const;\n";
        hppFile << "  int GetEntry(const unsigned int);\n";
        hppFile << "  void SetFile(const std::string&, const bool);\n\n";

        hppFile << "  std::string sampleName;\n";
        hppFile << "  int totalEntries;\n";
        hppFile << "  short cfAVersion;\n\n";
        hppFile << "  void GetVersion();\n";
        hppFile << "  void AddFiles(const std::string&, const bool);\n";
        hppFile << "  void CalcTotalEntries();\n";
        hppFile << "  void PrepareNewChains();\n";
        hppFile << "  void InitializeA();\n";
        hppFile << "  void InitializeB();\n\n";
    
        PrintLeaves(chainA, hppFile);
        PrintBranches(chainA, hppFile);
        PrintLeaves(chainB, hppFile);
        PrintBranches(chainB, hppFile);
    
        hppFile << "};\n\n";
        hppFile << "#endif" << std::endl;
    
        cppFile << "#include \"cfa.hpp\"\n";
        cppFile << "#include <vector>\n";
        cppFile << "#include <string>\n";
        cppFile << "#include <fstream>\n";
        cppFile << "#include <sstream>\n";
        cppFile << "#include \"TChain.h\"\n";
        cppFile << "#include \"TBranch.h\"\n\n";
        cppFile << "cfA::cfA(const std::string& fileIn, const bool isList):\n";
        cppFile << "  chainA(\"eventA\"),\n";
        cppFile << "  chainB(\"eventB\"),\n";
        cppFile << "  sampleName(fileIn),\n";
        cppFile << "  totalEntries(0),\n";
        cppFile << "  cfAVersion(-1),\n";
        PrintNullInit(chainA, cppFile);
        PrintBranchInit(chainA, cppFile);
        cppFile << ",\n";
        PrintNullInit(chainB, cppFile);
        PrintBranchInit(chainB, cppFile);
        cppFile << "{\n";
        cppFile << "  GetVersion();\n";
        cppFile << "  AddFiles(fileIn, isList);\n";
        cppFile << "  PrepareNewChains();\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::GetVersion(){\n";
        cppFile << "  size_t pos(sampleName.rfind(\"_v\"));\n";
        cppFile << "  if(pos!=std::string::npos && pos<sampleName.size()-2){\n";
        cppFile << "    std::istringstream iss(sampleName.substr(pos+2));\n";
        cppFile << "    iss >> cfAVersion;\n";
        cppFile << "    if(iss.fail() || iss.bad()){\n";
        cppFile << "      cfAVersion=-1;\n";
        cppFile << "    }\n";
        cppFile << "  }\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::PrepareNewChains(){\n";
        cppFile << "  InitializeA();\n";
        cppFile << "  InitializeB();\n";
        cppFile << "  CalcTotalEntries();\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::AddFiles(const std::string& fileIn, const bool isList){\n";
        cppFile << "  if(isList){\n";
        cppFile << "    std::ifstream infile(fileIn.c_str());\n";
        cppFile << "    std::string file(\"\");\n";
        cppFile << "    while(infile >> file){\n";
        cppFile << "      chainA.Add((file+\"/configurableAnalysis/eventA\").c_str());\n";
        cppFile << "      chainB.Add((file+\"/configurableAnalysis/eventB\").c_str());\n";
        cppFile << "    }\n";
        cppFile << "    infile.close();\n";
        cppFile << "  }else{\n";
        cppFile << "    chainA.Add((fileIn+\"/configurableAnalysis/eventA\").c_str());\n";
        cppFile << "    chainB.Add((fileIn+\"/configurableAnalysis/eventB\").c_str());\n";
        cppFile << "  }\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::SetFile(const std::string& fileIn, const bool isList){\n";
        cppFile << "  chainA.Reset(); chainB.Reset();\n";
        cppFile << "  AddFiles(fileIn, isList);\n";
        cppFile << "}\n\n";

        cppFile << "int cfA::GetEntry(const unsigned int entryIn){\n";
        cppFile << "  return chainA.GetEntry(entryIn)+chainB.GetEntry(entryIn);\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::CalcTotalEntries(){\n";
        cppFile << "  const int nEntriesA(chainA.GetEntries()), nEntriesB(chainB.GetEntries());\n";
        cppFile << "  if (nEntriesA!=nEntriesB){\n";
        cppFile << "    totalEntries=-1;\n";
        cppFile << "  }else{\n";
        cppFile << "    totalEntries=nEntriesA;\n";
        cppFile << "  }\n";
        cppFile << "}\n\n";

        cppFile << "TChain* cfA::GetChainA(){\n";
        cppFile << "  return &chainA;\n";
        cppFile << "}\n\n";

        cppFile << "TChain* cfA::GetChainB(){\n";
        cppFile << "  return &chainB;\n";
        cppFile << "}\n\n";

        cppFile << "std::string cfA::GetSampleName() const{\n";
        cppFile << "  return sampleName;\n";
        cppFile << "}\n\n";

        cppFile << "int cfA::GetTotalEntries() const{\n";
        cppFile << "  return totalEntries;\n";
        cppFile << "}\n\n";

        cppFile << "void cfA::InitializeA(){\n";
        PrintSetNull(chainA, cppFile);
        PrintSetBranchAddressA(chainA, cppFile);
        cppFile << "}\n\n";

        cppFile << "void cfA::InitializeB(){\n";
        PrintSetNull(chainB, cppFile);
        PrintSetBranchAddressB(chainB, cppFile);
        cppFile << "}\n\n";
      }else{
        std::cout << "Warning in " << argv[0] << ": one or both of chainA and chainB are NULL (" << chainA << " and " << chainB << ").\n";
      }

      inFile.Close();
      cppFile.close();
      hppFile.close();
    }else{
      std::cout << "Warning in " << argv[0] << ": Could not open " << argv[1] << ".\n";
    }
  }
}