ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/cbrown/AnalysisFramework/DistributedModelCalculations/Limits/TimedLimitCapsule.C
Revision: 1.2
Committed: Mon Nov 7 15:47:49 2011 UTC (13 years, 6 months ago) by buchmann
Content type: text/plain
Branch: MAIN
CVS Tags: cbaf_4_98ifb_paper, cbaf_4p7ifb, HEAD
Changes since 1.1: +8 -1 lines
Log Message:
Slightly more verbose

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