ATLAS Offline Software
Loading...
Searching...
No Matches
makeSmallRefFile.cxx
Go to the documentation of this file.
1
11
12
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
30void 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
90void 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
106void 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
116int usage(int status=0) {
117 std::cout << "Usage: makeSmallRefFile <intputfile> <outputfile>" << std::endl;
118 return status;
119}
120
121
122int 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
129 makeSmallRefFile( infile, outfile );
130
131 return 0;
132}
StatusCode usage()
std::string find(const std::string &s)
return a remapped string
Definition hcg.cxx:138
int main()
Definition hello.cxx:18
void makeSmallRefFile(const std::string &in, const std::string &out)
void CopyFile(const char *fname)
void CopyDir(TDirectory *source, bool mkdirFlag)
std::string dirname(std::string name)
Definition utils.cxx:200