00001 #include <iostream>
00002 #include <sstream>
00003 #include <cstdio>
00004 #include <vector>
00005 #include <cmath>
00006 #include <cstdlib>
00007
00008 #include "TFile.h"
00009 #include "TTree.h"
00010
00011 #include "include/MdcCalibConst.h"
00012 #include "include/fun.h"
00013
00014 using namespace std;
00015
00016 vector<double> XMEAS;
00017 vector<double> TBINCEN;
00018 vector<double> ERR;
00019 double Tmax;
00020 double Dmax;
00021 vector<double> XMEASED;
00022 vector<double> TBINCENED;
00023 vector<double> ERRED;
00024
00025 int gNEntr[43];
00026
00027
00028 double gTimeShift = 0.0;
00029 double gTesMin = 0.0;
00030 double gTesMax = 9999.0;
00031 int gFgIniCalConst = 2;
00032 bool gPreT0SetTm = true;
00033 double gInitT0 = 50.0;
00034 double gT0Shift = 0.0;
00035 double gTminFitChindf = 20.0;
00036 double gTmaxFitChindf = 20.0;
00037 int gResiType = 0;
00038 int gCalSigma = 1;
00039 int gFixXtC0 = 0;
00040 int gFgCalib[NLAYER];
00041 double gTminFitRange[NLAYER][2];
00042 double gTmaxFitRange[NLAYER][2];
00043 double gInitTm[NLAYER];
00044 double gQmin[NLAYER];
00045 double gQmax[NLAYER];
00046
00047 double xtFun(double t, double xtpar[]){
00048 int ord;
00049 double dist = 0.0;
00050 double tm = xtpar[6];
00051
00052 if(t < tm ){
00053 for(ord=0; ord<=5; ord++){
00054 dist += xtpar[ord] * pow(t, ord);
00055 }
00056 }else{
00057 for(ord=0; ord<=5; ord++){
00058 dist += xtpar[ord] * pow(tm, ord);
00059 }
00060 dist += xtpar[7] * (t - tm);
00061 }
00062
00063 return dist;
00064 }
00065
00066 void fcnXT(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag){
00067 unsigned int i;
00068 int ord;
00069 Double_t deta;
00070 Double_t chisq = 0.0;
00071 Double_t dfit;
00072
00073 for(i=0; i<TBINCEN.size(); i++){
00074 dfit = 0;
00075 for(ord=0; ord<=5; ord++){
00076 dfit += par[ord] * pow(TBINCEN[i], ord);
00077 }
00078 deta = (dfit - XMEAS[i]) / ERR[i];
00079 chisq += deta * deta;
00080 }
00081
00082 f = chisq;
00083 }
00084
00085 void fcnXtEdge(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag){
00086 unsigned int i;
00087 Double_t deta;
00088 Double_t chisq = 0.0;
00089 Double_t dfit;
00090
00091 for(i=0; i<TBINCENED.size(); i++){
00092 dfit = par[0] * (TBINCENED[i] - Tmax) + Dmax;
00093 deta = (dfit - XMEASED[i]) / ERRED[i];
00094 chisq += deta * deta;
00095 }
00096
00097 f = chisq;
00098 }
00099
00100 Double_t xtFitFun(Double_t *x, Double_t par[]){
00101 Double_t val = 0.0;
00102 for(Int_t ord=0; ord<6; ord++){
00103 val += par[ord] * pow(x[0], ord);
00104 }
00105 return val;
00106 }
00107
00108 Double_t xtFitEdge(Double_t *x, Double_t par[]){
00109 double val = Dmax + (x[0] - Tmax) * par[0];
00110 return val;
00111 }
00112
00113 void writeConst(MdcCalibConst* calconst, TObjArray* newXtList, TObjArray* r2tList){
00114 TFile fout("MdcCalibConst_new.root", "recreate");
00115
00116 int key;
00117 double xtpar;
00118 TTree *xttree = new TTree("XtTree", "XtTree");
00119 xttree -> Branch("xtkey", &key, "key/I");
00120 xttree -> Branch("xtpar", &xtpar, "xtpar/D");
00121 for(int lay=0; lay<43; lay++){
00122 for(int entr=0; entr<18; entr++){
00123 for(int lr=0; lr<3; lr++){
00124 for(int ord=0; ord<8; ord++){
00125 key = calconst->getXtKey(lay, entr, lr, ord);
00126 xtpar = calconst->getXtpar(lay, entr, lr, ord);
00127 xttree -> Fill();
00128 }
00129 }
00130 }
00131 }
00132
00133 double t0;
00134 double delt0;
00135 TTree *t0tree = new TTree("T0Tree", "T0Tree");
00136 t0tree -> Branch("t0", &t0, "t0/D");
00137 t0tree -> Branch("delt0", &delt0, "delt0/D");
00138 for(int wid=0; wid<6796; wid++){
00139 t0 = calconst->getT0(wid);
00140 delt0 = calconst->getDelT0(wid);
00141 t0tree -> Fill();
00142 }
00143
00144 double qtval[2];
00145 TTree *qttree = new TTree("QtTree", "QtTree");
00146 qttree -> Branch("qtpar0", &(qtval[0]), "qtpar0/D");
00147 qttree -> Branch("qtpar1", &(qtval[1]), "qtpar1/D");
00148 for(int lay=0; lay<43; lay++){
00149 qtval[0] = calconst->getQtpar0(lay);
00150 qtval[1] = calconst->getQtpar1(lay);
00151 qttree -> Fill();
00152 }
00153
00154 double sdpar;
00155 TTree *sdtree = new TTree("SdTree", "SdTree");
00156 sdtree -> Branch("sdkey", &key, "key/I");
00157 sdtree -> Branch("sdpar", &sdpar, "sdpar/D");
00158 for(int lay=0; lay<43; lay++){
00159 for(int entr=0; entr<6; entr++){
00160 for(int lr=0; lr<2; lr++){
00161 for(int bin=0; bin<24; bin++){
00162 key = calconst->getSdKey(lay, entr, lr, bin);
00163 sdpar = calconst->getSdpar(lay, entr, lr, bin);
00164 sdtree -> Fill();
00165 }
00166 }
00167 }
00168 }
00169
00170 fout.cd();
00171 xttree -> Write();
00172 t0tree -> Write();
00173 qttree -> Write();
00174 sdtree -> Write();
00175 if((newXtList->GetEntries()) > 0) newXtList -> Write();
00176 if((r2tList->GetEntries()) > 0) r2tList -> Write();
00177 fout.Close();
00178 }
00179
00180 vector<string> getHistList()
00181 {
00182 vector<string> fnames;
00183
00184 string command(
00185 "JobOutputDir=`/bin/ls -dt1 joboutput-* 2>/dev/null | head -1`\n"
00186 "if [ -d \"${JobOutputDir}\" ]; then\n"
00187 " find ${JobOutputDir} -name hist.root\n"
00188 "fi\n"
00189 );
00190
00191 stringstream fnstream;
00192
00193 char* fnbuf = new char[1024];
00194 FILE* fstream = popen(command.c_str(), "r");
00195
00196 while ( fgets(fnbuf, 1024, fstream) != NULL ) {
00197 fnstream << fnbuf;
00198 }
00199
00200 string fname;
00201 while ( ! (fnstream>>fname).eof() ) {
00202 fnames.push_back(fname);
00203 }
00204
00205 pclose(fstream);
00206 delete [] fnbuf;
00207
00208 if ( fnames.empty() ) {
00209 cout << "WARNING: Failed to retrieve hist files in the current directory!" << endl;
00210
00211 }
00212 return fnames;
00213 }
00214
00215 vector<string> getHistList(string path)
00216 {
00217 vector<string> fnames;
00218 string newpath = path;
00219 string::size_type strl = newpath.length();
00220 if((strl>1) && ('/'==newpath[strl-1])) newpath.erase(strl-1);
00221
00222 char com1[500];
00223 sprintf(com1, "JobOutputDir=`/bin/ls -dt1 %s/joboutput-* 2>/dev/null | head -1`\n", newpath.c_str());
00224 string command1(com1);
00225 string command2(
00226 "if [ -d \"${JobOutputDir}\" ]; then\n"
00227 " find ${JobOutputDir} -name hist.root\n"
00228 "fi\n"
00229 );
00230 string command = command1 + command2;
00231 stringstream fnstream;
00232
00233 char* fnbuf = new char[1024];
00234 FILE* fstream = popen(command.c_str(), "r");
00235
00236 while ( fgets(fnbuf, 1024, fstream) != NULL ) {
00237 fnstream << fnbuf;
00238 }
00239
00240 string fname;
00241 while ( ! (fnstream>>fname).eof() ) {
00242 fnames.push_back(fname);
00243 }
00244
00245 pclose(fstream);
00246 delete [] fnbuf;
00247
00248 if ( fnames.empty() ) {
00249 cout << "ERROR: Failed to retrieve hist files!" << endl;
00250 exit(1);
00251 }
00252 return fnames;
00253 }