00001
00002
00003
00004 #include <sstream>
00005 #include <iostream>
00006 #include <map>
00007 #include <string>
00008 #include <set>
00009 #include <sstream>
00010
00011 #include "tools/MicroException.hh"
00012 #include "tools/Toolbox.hh"
00013 #include "XMLTool.hh"
00014 #include "Log.hh"
00015 #include "Detector.hh"
00016 #include "Mysql.hh"
00017
00018
00019 #include <iostream>
00020 #include <fstream>
00021
00022
00023 #include <stdlib.h>
00024 #include <errno.h>
00025
00026
00027
00028
00029 using namespace std;
00030
00031
00032 Toolbox::Toolbox()
00033 {
00034 }
00035
00036
00037 Toolbox::~Toolbox()
00038 {
00039 }
00040
00041
00042 void Toolbox::printRepere ( bool xRotation, bool yRotation, int zRotation )
00043 {
00044 cout <<" \n ------------- ROTATIONS" << endl;
00045
00046 int result = (xRotation== false) ? 0 : 180;
00047 cout << "### xRotation " << result << " degree." << endl;
00048
00049 result = (yRotation== false) ? 0 : 180;
00050 cout << "### yRotation " << result << " degree." << endl;
00051
00052 cout << "### zRotation " << zRotation << " degree." << endl;
00053
00054
00055 cout << endl << "### Nous utilisons un repere irthonorme directe ###" <<endl;
00056 cout << "" << endl;
00057 cout << " X" << endl;
00058 cout << " " << endl;
00059 cout << " ^" << endl;
00060 cout << " / \\ " << endl;
00061 cout << " |" << endl;
00062 cout << " |" << endl;
00063 cout << " |" << endl;
00064 cout << " |" << endl;
00065 cout << " |" << endl;
00066 cout << " |" << endl;
00067 cout << "SYSTEM de COORDONNEES: (0,0) au centre ----X----------> Y " << endl;
00068 cout << " Z rentrant. |" << endl;
00069 cout << " |" << endl;
00070 cout << " |" << endl;
00071 cout << " |" << endl;
00072
00073 }
00074
00075
00076
00077
00078
00079 unsigned short Toolbox::bigTolittle ( short value )
00080 {
00081
00082 unsigned short lsb = value & MASK_LSB ;
00083 lsb = lsb << 8 ;
00084
00085 unsigned short msb = value & MASK_MSB;
00086 msb = msb >> 8 ;
00087
00088
00089
00090 unsigned short result = lsb + msb;
00091 return result;
00092 }
00093
00094
00095 void Toolbox::addFloatToString(std::string& chaine, float value)
00096 {
00097
00098 std::string s;
00099 std::stringstream out;
00100 out << value;
00101 s = out.str();
00102 chaine = chaine.append(s);
00103 }
00104
00105 void Toolbox::addIntToString(std::string& chaine, int value)
00106 {
00107
00108 std::string s;
00109 std::stringstream out;
00110 out << value;
00111 s = out.str();
00112 chaine = chaine.append(s);
00113 }
00114
00115
00116 void Toolbox::readSteerFile(string steerName , bool debug = false)
00117 {
00118
00119 FILE *steerFile;
00120
00121
00122
00123 cout << "Steering File: " << steerName << endl;
00124
00125
00126 steerFile=fopen(steerName.data(),"r");
00127
00128
00129 int nbOfFile = 0;
00130 fscanf (steerFile, "%d", &nbOfFile);
00131 if ( debug ) cout << "Nb input data file(s)\t" << nbOfFile << endl;
00132
00133
00134 string fileNam[nbOfFile];
00135 for ( int i = 0 ; i < nbOfFile ; i++ )
00136 {
00137 fscanf (steerFile, "%s",fileNam[i].data());
00138 if ( debug ) cout << "input data file " << i << " \t" << fileNam[i].data() << endl;
00139 }
00140
00141
00142 string rootFileName;
00143 fscanf (steerFile, "%s", rootFileName.data());
00144 if ( debug ) cout << "root file name \t\t" << rootFileName.data() << endl;
00145
00146
00147 string verbose;
00148 fscanf (steerFile, "%s",verbose.data());
00149 if ( debug ) cout << "verbose Mode\t\t" << verbose.data() << endl;
00150
00151
00152 int Nmicro = 0;
00153 fscanf (steerFile, "%d", &Nmicro);
00154 if ( debug ) cout << "Nb of MicroMegas Chambers " <<Nmicro<< endl;
00155
00156
00157 int NGplex[Nmicro];
00158
00159 int NTotGplex = 0;
00160 for ( int i = 0 ; i < Nmicro ; i++ )
00161 {
00162 fscanf (steerFile, "%d", &NGplex[i]);
00163 if ( debug ) cout << "Nb of Gassiplex Cards for MicroMegas no="<<i<< " " <<NGplex[i]<< endl;
00164 if ( debug ) cout << "Nb of Channels for MicroMegas no="<<i<< " " <<NGplex[i]*96<< endl;
00165 NTotGplex += NGplex[i]; }
00166 if ( debug ) cout << "TOTAL Nb of Gassiplex Cards " << NTotGplex << endl;
00167
00168
00169 unsigned int Nchamber[NTotGplex];
00170 int GplCount=-1;
00171 for ( int i = 0 ; i < Nmicro ; i++ )
00172 {
00173 for ( int j = 0 ; j < NGplex[i] ; j++ )
00174 {
00175 GplCount++;
00176 Nchamber[GplCount]=i;
00177 if ( debug ) cout << "ID of MicroMegas for gassiplex nb" << GplCount << " : " << Nchamber[GplCount]<<endl;
00178 }
00179 }
00180
00181
00182 fclose (steerFile);
00183 float d=3;
00184 int nBeta2=0;
00185 int nBeta24=0;
00186 for(int i=0;i<Nmicro;i++){if(NGplex[i]==1){nBeta2++;}else{nBeta24++;}}
00187 if ( debug ) cout<<nBeta2<<" chambres beta 2\n"
00188 <<nBeta24<<" chambres beta 2.4\n";
00189
00190 if ( strcmp(verbose.data(),"-v") == 0 ) { verbose = true; }
00191 if ( debug ) cout << "Verbose mode\t\t\t\t" << verbose << endl;
00192
00193
00194
00195 }
00196
00197
00198
00199
00200 std::ostream& operator <<(std::ostream &out, const BinOut& x) {
00201
00202
00203 for (unsigned int digit = x._Digits; digit > 0; digit--) {
00204 if ((digit != x._Digits) && (digit % 4 == 0))
00205 out << " ";
00206 out << ((x._Num >> (digit - 1)) & 0x1);
00207 }
00208 return(out);
00209 }
00210
00211
00212
00213
00214
00215 string Toolbox::getTempFileName(void)
00216 {
00217
00218 FILE *sfp = NULL;
00219 int fd = -1;
00220 char sfn[22] = "";
00221 strncpy(sfn, "/tmp/micromegas.XXXXXX", sizeof sfn);
00222 if ((fd = mkstemp(sfn)) == -1 )
00223 {
00224 unlink(sfn);
00225 close(fd);
00226 fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
00227 throw MicroException("Cannot create temporaly file");
00228 }
00229
00230 string result = sfn;
00231 return result;
00232 }
00233
00234
00235 bool Toolbox::getDetector(ui32 runId,Detector &detector)
00236 {
00237 Mysql mysql;
00238 MYSQL_ROW row;
00239
00240 string detectorXMLValue = mysql.getDetectorStringFromRunId(runId) ;
00241
00242
00243 string fileName = Toolbox::getTempFileName();
00244
00245
00246
00247 for (unsigned int i = 0 ; i < detectorXMLValue.size(); i++ )
00248 {
00249 if( detectorXMLValue[i] == '\r' ) detectorXMLValue[i] = ' ';
00250 }
00251
00252 ofstream myfile;
00253 myfile.open (fileName.c_str());
00254 myfile << "<micromegas xmlns:run=\"http://lappweb.in2p3.fr/LC\">" ;
00255 myfile << detectorXMLValue + "\n" ;
00256 myfile << "</micromegas>" ;
00257 myfile.close();
00258
00259 SteerDesc steerDesc;
00260 XMLTool xml;
00261 xml.parse(steerDesc, fileName);
00262
00263
00264 if ( ! detector.build(steerDesc) == steerDesc.chambers.size() )
00265 {
00266 FILE_LOG(logERROR) << "Geometry Error. Please check your XML file." << endl;
00267 return false;
00268 }
00269
00270
00271 unlink(fileName.c_str());
00272 return true;
00273 }