ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/FGolf/BatchSubmitCMS2/sweepRoot.C
Revision: 1.1
Committed: Thu Apr 19 00:45:03 2012 UTC (13 years ago) by fgolf
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Log Message:
first version of new batch submit based almost entirely on scripts form d.evans

File Contents

# User Rev Content
1 fgolf 1.1 /***************************************/
2     /* sweepRoot */
3     /* Sweep bad root files under the rug! */
4     /* */
5     /* Author: Jacob Ribnik */
6     /* jribnik@cern.ch */
7     /***************************************/
8    
9     #include <cstdlib>
10     #include <iostream>
11     #include <string>
12     #include <vector>
13     #include "TFile.h"
14     #include "TObject.h"
15    
16     void usage() {
17     std::cout << "Usage: sweepRoot [-b] [-g] [-x] [-r] [-d] [-o TObject::GetName()] file1 [file2]..." << std::endl;
18     std::cout << std::endl;
19     std::cout << " -b print list of bad files" << std::endl;
20     std::cout << " -g print list of good files" << std::endl;
21     std::cout << " -x move bad files" << std::endl;
22     std::cout << " -r prefix rfio" << std::endl;
23     std::cout << " -d prefix dcache" << std::endl;
24     std::cout << " -o check for TObject of given name" << std::endl;
25     std::cout << std::endl;
26     exit(1);
27     }
28    
29     void move(std::string& target) {
30     std::string cmd = "mv ";
31     cmd += target;
32     target += ".bad";
33     cmd += " ";
34     cmd += target;
35     system(cmd.c_str());
36     }
37    
38     int main(int argc, char** argv) {
39     std::vector<std::string> printbad;
40     std::vector<std::string> printgood;
41    
42     /**************************/
43     /* command line arguments */
44     /**************************/
45     bool doPrintBad = false;
46     bool doPrintGood = false;
47     bool doMove = false;
48     bool useRfio = false;
49     bool useDcache = false;
50     std::string name = "";
51    
52     int shift = 1;
53     if (argc > 1) {
54     std::string tmp;
55     for (int it = 1; it < argc; it++) {
56     tmp = std::string(argv[it]);
57     // print list of bad files
58     if (tmp == "-b") {
59     doPrintBad = true;
60     ++shift;
61     continue;
62     }
63     // print list of good files
64     if (tmp == "-g") {
65     doPrintGood = true;
66     ++shift;
67     continue;
68     }
69     // move bad files
70     if (tmp == "-x") {
71     doMove = true;
72     ++shift;
73     continue;
74     }
75     // prefix rfio
76     if (tmp == "-r") {
77     useRfio = true;
78     ++shift;
79     std::cout << "rfio: bad file moving disabled" << std::endl;
80     doMove = false;
81     continue;
82     }
83     // prefix dcache
84     if (tmp == "-d") {
85     useDcache = true;
86     ++shift;
87     std::cout << "dcache: bad file moving disabled" << std::endl;
88     doMove = false;
89     continue;
90     }
91     // check for TObject of given name
92     if (tmp == "-o") {
93     if (it == argc-1) usage();
94     name = std::string(argv[it+1]);
95     shift += 2;
96     }
97     }
98     if (argc <= shift) usage();
99     if (useRfio && useDcache) {
100     std::cout << "rfio and dcache? Make up your mind!" << std::endl;
101     usage();
102     }
103     } else usage();
104     //std::cout << "Checking for TObject named: " << name << std::endl;
105     std::cout << "sweepRooting " << argc-shift << " files" << std::endl;
106    
107     int nbad = 0;
108     for (int it = shift; it < argc; it++) {
109     std::cout << "\rsweepRooted " << it-shift << " files";
110     std::cout.flush();
111     //std::cout << "File " << argv[it] << " is... ";
112     std::string target(argv[it]);
113    
114     /**************/
115     /* check file */
116     /**************/
117     TFile* f = 0;
118     char fname[200];
119     if (useRfio) sprintf(fname, "rfio:%s", argv[it]);
120     else if (useDcache) sprintf(fname, "dcache:%s", argv[it]);
121     else strcpy(fname, argv[it]);
122    
123     f = TFile::Open(fname, "READ");
124    
125     if (! f || f->IsZombie()) {
126     //std::cout << "not a ROOT file!" << std::endl;
127     if (doMove) move(target);
128     if (doPrintBad) printbad.push_back(target);
129     ++nbad;
130     continue;
131     }
132    
133     /****************/
134     /* check object */
135     /****************/
136     if (name != "") {
137     TObject* obj = f->Get(name.c_str());
138     if (! obj || obj->IsZombie()) {
139     //std::cout << "bad!" << std::endl;
140     if (doMove) move(target);
141     if (doPrintBad) printbad.push_back(target);
142     ++nbad;
143     continue;
144     }
145     }
146    
147     //std::cout << "good!" << std::endl;
148     if (doPrintGood) printgood.push_back(target);
149     f->Close();
150     }
151    
152     std::cout << std::endl;
153     std::cout << std::endl;
154     std::cout << "SUMMARY: " << nbad << " bad, " << argc-shift-nbad << " good" << std::endl;
155     if (! printbad.empty()) {
156     std::cout << "BAD FILES" << std::endl;
157     for (unsigned int i = 0; i < printbad.size(); i++)
158     std::cout << printbad.at(i) << std::endl;
159     }
160     if (! printgood.empty()) {
161     std::cout << "GOOD FILES" << std::endl;
162     for (unsigned int i = 0; i < printgood.size(); i++)
163     std::cout << printgood.at(i) << std::endl;
164     }
165    
166     return 0;
167     }