ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/Tools/makeCMS2ClassFiles.C
(Generate patch)

Comparing UserCode/FGolf/Tools/makeCMS2ClassFiles.C (file contents):
Revision 1.1 by fgolf, Sun Aug 29 18:25:19 2010 UTC vs.
Revision 1.2 by fgolf, Thu May 26 03:34:52 2011 UTC

# Line 38 | Line 38 | void makeSrcFile(std::string Classname,
38   void makeBranchFile(std::string branchNamesFile);
39   void makeDriverFile(std::string fname);
40  
41 + struct hltcompare {
42 +  bool operator() (const TString& lhs, const TString& rhs) const {
43 +    string s_match = "hlt_bits";
44 +    string s_lhs   = (string) lhs;
45 +    string s_rhs   = (string) rhs;
46 +    s_lhs          = s_lhs.replace( s_lhs.find(s_match), s_match.length(), "" );
47 +    s_rhs          = s_rhs.replace( s_rhs.find(s_match), s_match.length(), "" );
48 +    int i_lhs      = atoi( s_lhs.c_str() );
49 +    int i_rhs      = atoi( s_rhs.c_str() );
50 +    return i_lhs < i_rhs;
51 +  }
52 + };
53 +
54 +
55  
56   //-------------------------------------------------------------------------------------------------
57   void makeCMS2ClassFiles (std::string fname, bool paranoid = true,
# Line 133 | Line 147 | void makeHeaderFile(TFile *f, bool paran
147      TString branchtitle(branch->GetTitle());
148      if(!branchname.BeginsWith("int") &&
149         !branchname.BeginsWith("uint") &&
150 +       !branchname.BeginsWith("bool") &&
151         !branchname.BeginsWith("float") &&
152         !branchname.BeginsWith("double") &&
153         !branchtitle.EndsWith("/F") &&
# Line 412 | Line 427 | void makeHeaderFile(TFile *f, bool paran
427      headerf << "\t\t"   << "}" << endl << endl;
428      //get the list of branches that hold the HLT bitmasks
429      //store in a set 'cause its automatically sorted
430 <    set<TString> s_HLTbitmasks;
430 >    //set<TString> s_HLTbitmasks;
431 >    set<TString, hltcompare> s_HLTbitmasks;
432      set<TString> s_L1bitmasks;
433      for(int j = 0; j < aliasarray->GetSize(); j++) {
434        TString aliasname(aliasarray->At(j)->GetName());
435        TBranch *branch = ev->GetBranch(ev->GetAlias(aliasname.Data()));
436        TString classname = branch->GetClassName();
437        if(aliasname.Contains("hlt_bits") && classname.Contains("int")) {
438 <        s_HLTbitmasks.insert(aliasname);
438 >              s_HLTbitmasks.insert(aliasname);
439        }
424    
440      }
441      int i = 0;
442 <    for(set<TString>::const_iterator s_it = s_HLTbitmasks.begin();
428 <        s_it != s_HLTbitmasks.end(); s_it++, i++) {
429 <      
442 >    for( set<TString>::const_iterator s_it = s_HLTbitmasks.begin(); s_it != s_HLTbitmasks.end(); s_it++, i++ ) {
443        if(i==0) {
444 <        headerf << "\t\t" << "if(trigIndx <= 31) {" << endl;
445 <        headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
446 <        headerf << "\t\t\t" << "bitmask <<= trigIndx;" << endl;
447 <        headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
448 <        headerf << "\t\t" << "}" << endl;
449 <        continue;
444 >              headerf << "\t\t" << "if(trigIndx <= 31) {" << endl;
445 >              headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
446 >              headerf << "\t\t\t" << "bitmask <<= trigIndx;" << endl;  
447 >              headerf << "\t\t\t" << "return " << *s_it << "() & bitmask;" << endl;
448 >              headerf << "\t\t" << "}" << endl;
449 >              continue;
450        }
451        headerf << "\t\t" << "if(trigIndx >= " << Form("%d && trigIndx <= %d", 32*i, 32*i+31) << ") {" << endl;
452        headerf << "\t\t\t" << "unsigned int bitmask = 1;" << endl;
# Line 541 | Line 554 | void makeHeaderFile(TFile *f, bool paran
554      headerf << "\t" << "}" << endl;
555    }//if(haveL1Info)
556      
557 +  headerf << endl;
558 +  headerf << "  static void progress( int nEventsTotal, int nEventsChain ){" << endl;
559 +  headerf << "    int period = 1000;" << endl;
560 +  headerf << "    if(nEventsTotal%1000 == 0) {" << endl;
561 +  headerf << "      // xterm magic from L. Vacavant and A. Cerri" << endl;
562 +  headerf << "      if (isatty(1)) {" << endl;
563 +  headerf << "        if( ( nEventsChain - nEventsTotal ) > period ){" << endl;
564 +  headerf << "          float frac = (float)nEventsTotal/(nEventsChain*0.01);" << endl;
565 +  headerf << "          printf(\"\\015\\033[32m ---> \\033[1m\\033[31m%4.1f%%\"" << endl;
566 +  headerf << "               \"\\033[0m\\033[32m <---\\033[0m\\015\", frac);" << endl;
567 +  headerf << "          fflush(stdout);" << endl;
568 +  headerf << "        }" << endl;
569 +  headerf << "        else {" << endl;
570 +  headerf << "          printf(\"\\015\\033[32m ---> \\033[1m\\033[31m%4.1f%%\"" << endl;
571 +  headerf << "                 \"\\033[0m\\033[32m <---\\033[0m\\015\", 100.);" << endl;
572 +  headerf << "          cout << endl;" << endl;
573 +  headerf << "        }" << endl;
574 +  headerf << "      }" << endl;
575 +  headerf << "    }" << endl;
576 +  headerf << "  }" << endl;
577 +  headerf << "  " << endl;
578 +
579    headerf << "};" << endl << endl;
580      
581    headerf << "#ifndef __CINT__" << endl;
# Line 606 | Line 641 | void makeHeaderFile(TFile *f, bool paran
641      headerf << "\t" << "bool passL1Trigger(TString trigName);" << endl;
642      implf   << "\t" << "bool passL1Trigger(TString trigName) { return cms2.passL1Trigger(trigName); }" << endl;
643    }//if(haveL1Info)
644 <    
644 >
645   }
646    
647   //-------------------------------------------------------------------------------------------------
# Line 616 | Line 651 | void makeSrcFile(std::string Classname,
651    
652    codef << "/* Usage:" << endl;
653    codef << "   root [0] .L ScanChain.C++" << endl;
654 <  codef << "   root [1] TFile *_file0 = TFile::Open(\"merged_ntuple.root\")" << endl;
655 <  codef << "   root [2] TChain *chain = new TChain(\"Events\")" << endl;
656 <  codef << "   root [3] chain->Add(\"merged_ntuple.root\")" << endl;
622 <  codef << endl;
623 <  codef << "   There are several places where one may create " << Classname << " cms2" << endl;
624 <  codef << "   It can be done here (in a doAll.C script), i.e.:" << endl;
625 <  codef << endl;
626 <  codef << "   root [4] " << Classname << " cms2 " << endl;
627 <  codef << endl;
628 <  codef << "   It can be done in the source as is done below, or it can be" << endl;
629 <  codef << "   ascertained by including CORE/CMS2.cc as is commented out" << endl;
630 <  codef << "   below.  They are all the same, and everything will work so" << endl;
631 <  codef << "   long as it is created somewhere globally." << endl;
632 <  codef << endl;
633 <  codef << "   root [5] ScanChain(chain)" << endl;
654 >  codef << "   root [1] TChain *chain = new TChain(\"Events\")" << endl;
655 >  codef << "   root [2] chain->Add(\"merged_ntuple.root\")" << endl;
656 >  codef << "   root [3] ScanChain(chain)" << endl;
657    codef << "*/" << endl;
658 +  codef << "" << endl;
659 +  codef << "// C++" << endl;
660    codef << "#include <iostream>" << endl;
661    codef << "#include <vector>" << endl;
662    codef << "" << endl;
663 +  codef << "// ROOT" << endl;
664    codef << "#include \"TChain.h\"" << endl;
665    codef << "#include \"TFile.h\"" << endl;
666    codef << "#include \"TDirectory.h\"" << endl;
667    codef << "#include \"TROOT.h\"" << endl;
668    codef << "" << endl;
669 +  codef << "// CMS2" << endl;
670    codef << "#include \"" + Classname+".cc\"" << endl;
671    if(branchNamesFile!="")
672      codef << "#include \"branches.h\"" << endl;
646  
647  codef << endl;
673    codef << "using namespace tas;" << endl;
674    codef << endl;
675 <  
675 >  codef << endl;
676    codef << "int ScanChain( TChain* chain, int nEvents = -1, std::string skimFilePrefix=\"\") {" << endl;
677    codef << "" << endl;
678 <  codef << "  TObjArray *listOfFiles = chain->GetListOfFiles();" << endl;
678 >  codef << "  // Example Histograms" << endl;
679 >  codef << "  TDirectory *rootdir = gDirectory->GetDirectory(\"Rint:\");" << endl;
680 >  codef << "  TH1F *samplehisto = new TH1F(\"samplehisto\", \"Example histogram\", 200,0,200);" << endl;
681 >  codef << "  samplehisto->SetDirectory(rootdir);" << endl;
682    codef << "" << endl;
683 <  codef << "  unsigned int nEventsChain=0;" << endl;
684 <  codef << "  if(nEvents==-1) " << endl << "    nEvents = chain->GetEntries();" << endl;
685 <  codef << "  nEventsChain = nEvents;" << endl;
658 <  
683 >  codef << "  // File Loop" << endl;
684 >  codef << "  if( nEvents == -1 ) nEvents = chain->GetEntries();" << endl;
685 >  codef << "  unsigned int nEventsChain = nEvents;" << endl;
686    codef << "  unsigned int nEventsTotal = 0;" << endl;
687    if(branchNamesFile!="")
688 <    codef << "  InitSkimmedTree(skimFilePrefix);" << endl;
689 <  codef << "  TDirectory *rootdir = gDirectory->GetDirectory(\"Rint:\");" << endl << endl;
663 <  codef << "  TH1F *samplehisto = new TH1F(\"samplehisto\", \"Example histogram\", 200,0,200);" << endl;
664 <  codef << "  samplehisto->SetDirectory(rootdir);" << endl;
665 <
666 <  codef << "  // file loop" << endl;
688 >  codef << "  InitSkimmedTree(skimFilePrefix);" << endl;
689 >  codef << "  TObjArray *listOfFiles = chain->GetListOfFiles();" << endl;
690    codef << "  TIter fileIter(listOfFiles);" << endl;
691    codef << "  TFile *currentFile = 0;" << endl;
692 <  codef << "  while ( currentFile = (TFile*)fileIter.Next() ) {" << endl;
693 <  codef << "    TFile f(currentFile->GetTitle());" << endl;
692 >  codef << "  while ( (currentFile = (TFile*)fileIter.Next()) ) {" << endl;
693 >  codef << "    // Get File Content" << endl;
694 >  codef << "    TFile f( currentFile->GetTitle() );" << endl;
695    codef << "    TTree *tree = (TTree*)f.Get(\"Events\");" << endl;
696    codef << "    cms2.Init(tree);" << endl;
697    codef << "    " << endl;
698 <  codef << "    //Event Loop" << endl;
698 >  codef << "    // Event Loop" << endl;
699    codef << "    unsigned int nEvents = tree->GetEntries();" << endl;
700    codef << "    for( unsigned int event = 0; event < nEvents; ++event) {" << endl;
701 +  codef << "    " << endl;
702 +  codef << "      // Get Event Content" << endl;
703    codef << "      cms2.GetEntry(event);" << endl;
704    codef << "      ++nEventsTotal;" << endl;
705 <
706 <  codef << "      // Progress feedback to the user" << endl;
707 <  codef << "      if(nEventsTotal%1000 == 0) {" << endl;
682 <  codef << "        // xterm magic from L. Vacavant and A. Cerri" << endl;
683 <  codef << "        if (isatty(1)) {" << endl;
684 <  codef << "          printf(\"\\015\\033[32m ---> \\033[1m\\033[31m%4.1f%%\"" << endl;
685 <  codef << "          \"\\033[0m\\033[32m <---\\033[0m\\015\", (float)nEventsTotal/(nEventsChain*0.01));" << endl;
686 <  codef << "          fflush(stdout);" << endl;
687 <  codef << "        }" << endl;
688 <  codef << "      }//if(nEventsTotal%20000 == 0) {\n\n\n";
689 <                                                                                      
690 <
705 >  codef << "    " << endl;
706 >  codef << "      // Progress" << endl;
707 >  codef << "      CMS2::progress( nEventsTotal, nEventsChain );" << endl;
708    codef << "    }" << endl;
709 +  codef << "  " << endl;
710    codef << "    delete tree;" << endl;
711    codef << "    f.Close();" << endl;
712    codef << "  }" << endl;
695  codef << "" << endl;
713    codef << "  if ( nEventsChain != nEventsTotal ) {" << endl;
714    codef << "    std::cout << \"ERROR: number of events from files is not equal to total number of events\" << std::endl;" << endl;
715    codef << "  }" << endl;
# Line 702 | Line 719 | void makeSrcFile(std::string Classname,
719      codef << "  outTree_->Write();" << endl;
720      codef << "  outFile_->Close();" << endl;
721    }
722 +  codef << "  " << endl;
723 +  codef << "  // Example Histograms" << endl;
724    codef << "  samplehisto->Draw();" << endl;
725 +  codef << "  " << endl;
726 +  codef << "  // return" << endl;
727    codef << "  return 0;" << endl;
728    codef << "}" << endl;
729    

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines