ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/DistributedModelCalculations/Limits/TimedLimitCapsule.C
Revision: 1.1
Committed: Mon Oct 24 15:07:18 2011 UTC (13 years, 6 months ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
Limit Capsule that reads roostats output and aborts quickly instead of running into timeout situation

File Contents

# User Rev Content
1 buchmann 1.1 #include <iostream>
2     #include <vector>
3     #include <sys/stat.h>
4     #include <fstream>
5    
6     #include "../../Plotting/Modules/LimitDroplet.C"
7     #include "../../Plotting/Modules/GeneralToolBox.C"
8     #include "../../Plotting/Modules/SampleClass.C"
9     #include "../../Plotting/Modules/Setup.C"
10    
11     #include <TSystem.h>
12     #include <TROOT.h>
13    
14     /*
15    
16     This capsule computes the limits; the advantage is that in this way the capsule can fail (segfault/...) without problems, while the main program is not affected.
17    
18     */
19    
20    
21     bool isThreadActive=false;
22     vector<string> all_args;
23     void* do_limit_computation( void *arg );
24    
25     void* hello_thread( void *arg ) {
26     printf( "hello " );
27     return( 0 );
28     }
29    
30     string threadoutputfile;
31    
32    
33     bool log_is_freaking_out() {
34     //this function basically checks if the last ten lines are mostly "WARNING:Generation -- skip: -nan" (i.e. problematic)
35     // it returns true if there are too many such errors, false if everything is ok.
36     ifstream ifs(threadoutputfile.c_str());
37     string temp;
38     int nlines=0;
39     while( getline( ifs, temp ) ) nlines++;
40    
41     cout << "Going to read from file : " << threadoutputfile << " containing " << nlines << " lines " << endl;
42     int nwarnings=0;
43     int iline=0;
44     ifstream ifs2(threadoutputfile.c_str());
45     while( getline( ifs2, temp ) ) {
46     iline++;
47     if(nlines-iline>20) continue;
48     if(Contains(temp,"WARNING:Generation")&&Contains(temp,"nan")) nwarnings++;
49     }
50     cout << "Have found " << nwarnings << " warnings in the log ... " << endl;
51    
52    
53     if(nwarnings>10) return true;
54     else return false;
55    
56    
57    
58     }
59    
60     bool CapsuleWrapper() {
61     pthread_t limitthread;
62     long t=0;
63     //pthread_create( &limitthread, NULL, (void*) do_limit_computation, NULL);
64     pthread_create( &limitthread, NULL, do_limit_computation, (void*) NULL );
65     int counter=0;
66     int counterinterval=10;
67     sleep(1); //waiting a second for the process to become active
68     while(counter<PlottingSetup::limitpatience*60 && isThreadActive) {
69     std::cout << "Limits are being calculated; Checking round " << counter/counterinterval << " ( corresponds to " << seconds_to_time(counter) << " ) , patience will end in " << seconds_to_time(60*PlottingSetup::limitpatience-counter) << std::endl;
70     counter+=counterinterval;
71     if(log_is_freaking_out()) counter=PlottingSetup::limitpatience*60+1000;
72     sleep(counterinterval);
73     }
74     gROOT->ProcessLine(".>");
75     std::cout << "\e[1;34m Limit calculation is finishing."<< "\e[0m" << std::endl;
76     remove(threadoutputfile.c_str());
77    
78     if(!isThreadActive) {
79     write_info(__FUNCTION__,"Thread finished sucessfully");
80     pthread_join( limitthread, NULL );
81     return true;
82     } else {
83     pthread_cancel(limitthread);
84     write_error(__FUNCTION__,"DID NOT TERMINATE IN TIME - ABORTED!");
85     return false;
86     }
87     }
88    
89    
90     ///***********************************************************************************************************************************************
91    
92    
93     void* do_limit_computation( void *arg ) {
94    
95     isThreadActive=true;
96     string reportname=all_args[1];
97     float luminosity=atof(all_args[2].c_str());
98     float lumiuncert=atof(all_args[3].c_str());
99     float mceff=atof(all_args[4].c_str());
100     float mcefferr=atof(all_args[5].c_str());
101     float Npred=atof(all_args[6].c_str());
102     float Nprederr=atof(all_args[7].c_str());
103     float Nobs=atof(all_args[8].c_str());
104     int JZBcutused=atoi(all_args[9].c_str());
105     string plotname=all_args[10];
106     int doexpected=atoi(all_args[11].c_str());
107     threadoutputfile=(plotname+"__threadoutput.txt");
108     dout << "Should expected limits be calculated? " << doexpected << endl;
109    
110     /*
111     // TODO:
112     - *everything* needs to have absolute paths
113     - need to create a directory for every process (otherwise ws.root's will be overwritten!) --> Good possibility: use the reportname and add some time specific string.
114     */
115     char currentpath[1024];
116     char *path = getcwd(currentpath,1024);
117     extract_cbaf_dir(currentpath);
118     stringstream loadroostats;
119     loadroostats << ".L " << PlottingSetup::cbafbasedir << "/Plotting/Modules/external/roostats_cl95.C+";
120     stringstream loadLimitDroplet;
121     loadLimitDroplet << ".L " << PlottingSetup::cbafbasedir << "/Plotting/Modules/LimitDroplet.C";
122    
123     cout << "The output of this thread is written to : " << threadoutputfile << "; this Limit Capsule will monitor the output for signs of crahes and if necessary abort. After termination, the log will be deleted to avoid unnecessary clutter." << endl;
124    
125     gROOT->ProcessLine("#include <exception>");
126     gROOT->ProcessLine(((string)".> "+threadoutputfile).c_str());
127     gROOT->ProcessLine(loadroostats.str().c_str());
128     gROOT->ProcessLine(loadLimitDroplet.str().c_str());
129    
130     stringstream allargs95;
131     allargs95 << "("<<luminosity<<","<<lumiuncert<<","<<mceff<<","<<mcefferr<<","<<Npred<<","<<Nprederr<<","<<Nobs<<","<<false<<","<<PlottingSetup::nuisancemodel<<",\""<<PlottingSetup::limitmethod<<"\",\""<<plotname<<"\"";
132     stringstream allargslm;
133     allargslm << "("<<luminosity<<","<<lumiuncert<<","<<mceff<<","<<mcefferr<<","<<Npred<<","<<Nprederr<<","<<Nobs<<","<<false<<","<<PlottingSetup::nuisancemodel<<",\""<<PlottingSetup::limitmethod<<"\",\"" << plotname << "\",0";
134    
135     stringstream obscommand;
136     obscommand <<"float observed=roostats_cl95"<<allargs95.str()<<");";
137     gROOT->ProcessLine(obscommand.str().c_str());
138     stringstream expcommand;
139     expcommand <<"LimitResult limit = roostats_limit"<<allargslm.str()<<");";
140     dout << expcommand.str() << endl;
141     if(doexpected!=0) gROOT->ProcessLine(expcommand.str().c_str());
142     gROOT->ProcessLine("LimitDroplet limres;");
143     stringstream setjzb;
144     setjzb<<"limres.JZB="<<JZBcutused<<";";
145     gROOT->ProcessLine(setjzb.str().c_str());
146     gROOT->ProcessLine("limres.observed=observed;");
147    
148     if(doexpected!=0) {
149     gROOT->ProcessLine("limres.upper68=limit.GetOneSigmaHighRange();");
150     gROOT->ProcessLine("limres.lower68=limit.GetOneSigmaLowRange();");
151     gROOT->ProcessLine("limres.expected=limit.GetExpectedLimit();");
152     gROOT->ProcessLine("limres.upper95= limit.GetTwoSigmaHighRange();");
153     gROOT->ProcessLine("limres.lower95=limit.GetTwoSigmaLowRange();");
154     }
155     gROOT->ProcessLine(".>");
156     stringstream savecommand;
157     savecommand << "limres.saveDroplet(\""<<reportname<<"\");";
158     gROOT->ProcessLine(savecommand.str().c_str());
159     gROOT->ProcessLine(".q");
160    
161     isThreadActive=false;
162    
163     }
164    
165     int main(int nargs, char* args[]) {
166     /* we obtain lumi etc. directly from the template; the only things passed are the following:
167     - report filename [1]
168     - luminosity [2]
169     - lumi uncert [3]
170     - MC efficiency [4]
171     - MC efficiency error [5]
172     - Npred [6]
173     - Nprederr [7]
174     - Nobs [8]
175     - JZB cut [9]
176     - plot name [10]
177     */
178    
179     if(nargs<12) {
180     dout << "NOT ENOUGH ARGUMENTS!" << endl;
181     dout << "You're supposed to provide: " << endl;
182     dout << "LUMI LUMIUNCERT MCEFF MCEFFERR NPRED NPREDERR NOBS JZBCUT PLOTNAME DOEXPECTED" << endl;
183     return -1;
184     }
185    
186     for(int i=0;i<nargs;i++) all_args.push_back((string)args[i]);
187    
188     if(CapsuleWrapper()) {
189     cout << "Timed limit capsule finished successfully." << endl;
190     return 0;
191     } else {
192     cout << "Timed limit capsule had to be aborted." << endl;
193     return 1;
194     }
195     return 1;
196     }