ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/Development/DistributedModelCalculations/Limits/TimedLimitCapsule.C
Revision: 1.1
Committed: Wed Mar 28 09:02:40 2012 UTC (13 years, 1 month ago) by buchmann
Content type: text/plain
Branch: MAIN
Log Message:
Added limit capsule

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     int doasymptotic=false;
108     if(all_args.size()>12) doasymptotic=atoi(all_args[12].c_str());
109    
110     if(doasymptotic) write_warning(__FUNCTION__,"Doing asymptotic limit");
111     threadoutputfile=(plotname+"__threadoutput.txt");
112    
113     dout << "\e[0;31m " << endl;
114     dout << " " << endl;
115     dout << " .+8OOOO? " << endl;
116     dout << " ..OOOOOOOO$ZZZZZI " << endl;
117     dout << " ...=:: :ZZZZZZZZZZZZO? " << endl;
118     dout << " ..~:~ ,ZZZZZZZZZZOO$. " << endl;
119     dout << " +:, :ZZZZZZOOOOO$. " << endl;
120     dout << " =: ,ZZZOOOZZ$Z$I. " << endl;
121     dout << " :~ red +??ZZZZZZZZ$. " << endl;
122     dout << " :~ pill :ZZZZZZZ$7, " << endl;
123     dout << " :: =ZZZZZ8$?: " << endl;
124     dout << " ,: ~ZZ8ZI " << endl;
125     dout << " :::,, ,,::::~=8Z7+~ " << endl;
126     dout << " :::::: " << endl;
127     dout << " " << endl;
128     dout << " \e[00m" << endl;
129    
130     dout << "Limit capsule : " << endl;
131     dout << "Initialized with the following parameters: " << endl;
132     dout << "Compute expected limits? " << doexpected << endl;
133     dout << "Luminosity: " << luminosity << " +/- " << lumiuncert << endl;
134     dout << "Efficiency: " << mceff << " +/- " << mcefferr << endl;
135     dout << "Observed: " << Nobs << endl;
136     dout << "Predicted: " << Npred << " +/- " << Nprederr << endl;
137     dout << "Asymptotic limit? " << doasymptotic << endl;
138    
139    
140     /*
141     // TODO:
142     - *everything* needs to have absolute paths
143     - 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.
144     */
145     char currentpath[1024];
146     char *path = getcwd(currentpath,1024);
147     extract_cbaf_dir(currentpath);
148     stringstream loadroostats;
149     if(!doasymptotic) loadroostats << ".L " << PlottingSetup::cbafbasedir << "/Plotting/Modules/external/roostats_cl95.C+";
150     else loadroostats << ".L " << PlottingSetup::cbafbasedir << "/Plotting/Modules/external/roostats_cl95_ASYMPTOTIC.C+";
151     stringstream loadLimitDroplet;
152     loadLimitDroplet << ".L " << PlottingSetup::cbafbasedir << "/Plotting/Modules/LimitDroplet.C";
153    
154     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;
155    
156     gROOT->ProcessLine("#include <exception>");
157     gROOT->ProcessLine(((string)".> "+threadoutputfile).c_str());
158     gROOT->ProcessLine(loadroostats.str().c_str());
159     gROOT->ProcessLine(loadLimitDroplet.str().c_str());
160    
161     stringstream allargs95;
162     allargs95 << "("<<luminosity<<","<<lumiuncert<<","<<mceff<<","<<mcefferr<<","<<Npred<<","<<Nprederr<<","<<Nobs<<","<<false<<","<<PlottingSetup::nuisancemodel<<",\""<<PlottingSetup::limitmethod<<"\",\""<<plotname<<"\"";
163     stringstream allargslm;
164     allargslm << "("<<luminosity<<","<<lumiuncert<<","<<mceff<<","<<mcefferr<<","<<Npred<<","<<Nprederr<<","<<Nobs<<","<<false<<","<<PlottingSetup::nuisancemodel<<",\""<<PlottingSetup::limitmethod<<"\",\"" << plotname << "\",0";
165    
166     stringstream obscommand;
167     obscommand <<"float observed=roostats_cl95"<<allargs95.str()<<");";
168     gROOT->ProcessLine(obscommand.str().c_str());
169     stringstream expcommand;
170     expcommand <<"LimitResult limit = roostats_limit"<<allargslm.str()<<");";
171     dout << expcommand.str() << endl;
172     if(doexpected!=0) gROOT->ProcessLine(expcommand.str().c_str());
173     gROOT->ProcessLine("LimitDroplet limres;");
174     stringstream setjzb;
175     setjzb<<"limres.JZB="<<JZBcutused<<";";
176     gROOT->ProcessLine(setjzb.str().c_str());
177     gROOT->ProcessLine("limres.observed=observed;");
178    
179     if(doexpected!=0) {
180     gROOT->ProcessLine("limres.upper68=limit.GetOneSigmaHighRange();");
181     gROOT->ProcessLine("limres.lower68=limit.GetOneSigmaLowRange();");
182     gROOT->ProcessLine("limres.expected=limit.GetExpectedLimit();");
183     gROOT->ProcessLine("limres.upper95= limit.GetTwoSigmaHighRange();");
184     gROOT->ProcessLine("limres.lower95=limit.GetTwoSigmaLowRange();");
185     }
186     gROOT->ProcessLine(".>");
187     stringstream savecommand;
188     savecommand << "limres.saveDroplet(\""<<reportname<<"\");";
189     gROOT->ProcessLine(savecommand.str().c_str());
190     gROOT->ProcessLine(".q");
191    
192     isThreadActive=false;
193    
194     }
195    
196     int main(int nargs, char* args[]) {
197     /* we obtain lumi etc. directly from the template; the only things passed are the following:
198     - report filename [1]
199     - luminosity [2]
200     - lumi uncert [3]
201     - MC efficiency [4]
202     - MC efficiency error [5]
203     - Npred [6]
204     - Nprederr [7]
205     - Nobs [8]
206     - JZB cut [9]
207     - plot name [10]
208     */
209    
210     if(nargs<12) {
211     dout << "NOT ENOUGH ARGUMENTS!" << endl;
212     dout << "You're supposed to provide: " << endl;
213     dout << "LUMI LUMIUNCERT MCEFF MCEFFERR NPRED NPREDERR NOBS JZBCUT PLOTNAME DOEXPECTED" << endl;
214     return -1;
215     }
216    
217     for(int i=0;i<nargs;i++) all_args.push_back((string)args[i]);
218    
219     if(CapsuleWrapper()) {
220     cout << "Timed limit capsule finished successfully." << endl;
221     return 0;
222     } else {
223     cout << "Timed limit capsule had to be aborted." << endl;
224     return 1;
225     }
226     return 1;
227     }