ATLAS Offline Software
makeSmallRefFile.cxx
Go to the documentation of this file.
1 
13 #include <string>
14 #include <iostream>
15 
16 
17 #include "TROOT.h"
18 #include "TKey.h"
19 #include "TFile.h"
20 #include "TSystem.h"
21 #include "TTree.h"
22 
23  //Example of script showing how to copy all objects from specified directories
24  //from a source file.
25  //After execution of:
26  // root > .L makeSmallRefFile.C+
27  // root > makeSmallRefFile()
28  //the file new-ref.root will contain the new smaller reference file
29 
30 void CopyDir(TDirectory *source, bool mkdirFlag) {
31  //copy all objects and subdirs of directory source as a subdir of the current directory
32  printf("CopyDir called with sourcem mkdirFlag: %s , %d\n",source->GetName(), mkdirFlag);
33  source->ls();
34  TDirectory *savdir = gDirectory;
35  TDirectory *adir;
36  if (mkdirFlag) {
37  adir = savdir->mkdir(source->GetName());
38  } else {
39  adir = savdir;
40  }
41  adir->cd();
42  //loop on all entries of this directory
43  TKey *key;
44  TIter nextkey(source->GetListOfKeys());
45  while ((key = (TKey*)nextkey())) {
46  const char *classname = key->GetClassName();
47  TClass *cl = gROOT->GetClass(classname);
48  if (!cl) continue;
49  if (cl->InheritsFrom(TDirectory::Class())) {
50  const char *dirname = key->GetName();
51  printf("Directory: %s\n",dirname);
52  if ( (std::string(dirname).find("EF") !=std::string::npos)) printf ("Found EF");
53  if ( (std::string(dirname).find("L2") !=std::string::npos)) printf ("Found L2");
54  if ( (std::string(dirname).find("HLT") !=std::string::npos)) printf ("Found HLT");
55  if ( (std::string(dirname).find("Fast") !=std::string::npos)) printf ("Found Fast");
56  if ( (std::string(dirname).find("vs") !=std::string::npos)) printf ("Found vs");
57 
58  if ( (std::string(dirname).find("EF") !=std::string::npos) || (std::string(dirname).find("L2")!=std::string::npos) || (std::string(dirname).find("HLT")!=std::string::npos) || (std::string(dirname).find("Fast")!=std::string::npos) || (std::string(dirname).find("vs")!=std::string::npos) ) {
59  // if ( std::string(dirname).find("slices") !=std::string::npos || std::string(dirname).find("roi")!=std::string::npos ) {
60  printf("Directory OK will be copied: %s\n",dirname);
61  source->cd(key->GetName());
62  TDirectory *subdir = gDirectory;
63  adir->cd();
64  CopyDir(subdir, true);
65  adir->cd();
66  } else {
67  printf("Directory will NOT be copied: %s\n",dirname);
68  }
69 
70 
71  } else if (cl->InheritsFrom(TTree::Class())) {
72  TTree *T = (TTree*)source->Get(key->GetName());
73  adir->cd();
74  TTree *newT = T->CloneTree(-1,"fast");
75  newT->Write();
76  } else {
77  source->cd();
78  TObject *obj = key->ReadObj();
79  adir->cd();
80  if ( std::string(key->GetClassName()).find("TH1")!=std::string::npos ) obj->Write();
81  delete obj;
82  }
83 
84  }
85  adir->SaveSelf(kTRUE);
86  savdir->cd();
87 }
88 
89 
90 void CopyFile(const char *fname) {
91  //Copy all objects and subdirs of file fname as a subdir of the current directory
92  TDirectory *target = gDirectory;
93  TFile *f = TFile::Open(fname);
94  if (!f || f->IsZombie()) {
95  printf("Cannot copy file: %s\n",fname);
96  target->cd();
97  return;
98  }
99  target->cd();
100  CopyDir(f,false);
101  delete f;
102  target->cd();
103 }
104 
105 
106 void makeSmallRefFile( const std::string& in, const std::string& out ) {
107  //main function copying 4 files as subdirectories of a new file
108  TFile *f = new TFile( out.c_str(), "recreate" );
109  // CopyFile("data-tau-merge.root");
110  CopyFile( in.c_str() );
111  f->ls();
112  delete f;
113 }
114 
115 
116 int usage(int status=0) {
117  std::cout << "Usage: makeSmallRefFile <intputfile> <outputfile>" << std::endl;
118  return status;
119 }
120 
121 
122 int main( int argc, char** argv ) {
123 
124  if ( argc<3 ) return usage(-1);
125 
126  std::string infile = argv[1];
127  std::string outfile = argv[2];
128 
130 
131  return 0;
132 }
python.CaloRecoConfig.f
f
Definition: CaloRecoConfig.py:127
main
int main(int argc, char **argv)
Definition: makeSmallRefFile.cxx:122
run.infile
string infile
Definition: run.py:13
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
dirname
std::string dirname(std::string name)
Definition: utils.cxx:200
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
LArCellConditions.argv
argv
Definition: LArCellConditions.py:112
usage
int usage(int status=0)
Definition: makeSmallRefFile.cxx:116
makeSmallRefFile
void makeSmallRefFile(const std::string &in, const std::string &out)
Definition: makeSmallRefFile.cxx:106
ParseInputs.gDirectory
gDirectory
Definition: Final2012/ParseInputs.py:133
DQHistogramMergeRegExp.argc
argc
Definition: DQHistogramMergeRegExp.py:20
python.AthDsoLogger.fname
string fname
Definition: AthDsoLogger.py:67
CopyDir
void CopyDir(TDirectory *source, bool mkdirFlag)
Definition: makeSmallRefFile.cxx:30
merge.status
status
Definition: merge.py:17
COOLRates.target
target
Definition: COOLRates.py:1106
python.LumiCalcRecover.subdir
subdir
Definition: LumiCalcRecover.py:25
python.PyAthena.obj
obj
Definition: PyAthena.py:135
PrepareReferenceFile.outfile
outfile
Definition: PrepareReferenceFile.py:42
dq_make_web_display.cl
cl
print [x.__class__ for x in toList(dqregion.getSubRegions()) ]
Definition: dq_make_web_display.py:26
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
CopyFile
void CopyFile(const char *fname)
Definition: makeSmallRefFile.cxx:90