00001 #include <TRandom.h>
00002 #include <TTree.h>
00003 #include <TFile.h>
00004 #include <TStyle.h>
00005 #include <TKey.h>
00006
00007 #include <Riostream.h>
00008 #include <sstream>
00009 #include <string>
00010 #include "mysql/Mysql.hh"
00011 #include "MicroException.hh"
00012 #include <iostream>
00013 #include <fstream>
00014 #include "root/CaloHit.hh"
00015 #include "root/MTRun.hh"
00016 #include "root/MTEvent.hh"
00017 #include "root/MTChannel.hh"
00018 #include "root/MTMicrorocChip.hh"
00019
00020
00021 #include "TH2I.h"
00022
00023
00024 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName);
00025 Long64_t buildCaloEvent(const MTEvent& evt, CaloEvent&, Long64_t bcidAbsPrev, bool &track,TH1F& hResidualX, TH1F& hResidualY);
00026
00027 void usage(void)
00028 {
00029 cout << "usage: buildCaloHits -t timestampMin timestampMax outputfileName" << endl;
00030 cout << " : buildCaloHits -f filename outputfileName" << endl;
00031 cout << "1312577280 1312590660" << endl;
00032 }
00033
00034 int main(int argc, char** argv) {
00035
00036 if ( argc < 4 || argc > 5 )
00037 {
00038 usage();
00039 return -1;
00040 }
00041
00042
00043
00044 if ( strcmp (argv[1] ,"-t" )== 0 )
00045 {
00046 if ( argc != 5 ) { usage(); return -1; }
00047 string outputFileName = argv[4];
00048 Mysql sql;
00049 string start = argv[2];
00050 string end = argv[3];
00051
00052
00053
00054
00055 try
00056 {
00057
00058 const vector<string>& ids = sql.getMergedFilesByTimestamp(start,end);
00059 vector<string>::const_iterator cii;
00060
00061
00062 for(cii=ids.begin(); cii!=ids.end(); cii++)
00063 {
00064
00065 string inputFileName = *cii;
00066 selectAndCopyEvent(inputFileName,outputFileName);
00067 }
00068 delete &ids;
00069 }
00070 catch (MicroException e) { }
00071 }
00072
00073
00074 else if ( strcmp (argv[1] ,"-f") == 0 )
00075 {
00076 if ( argc != 4 ) { usage(); return -1; }
00077 string outputFileName = argv[3];
00078
00079
00080 TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00081 caloFile.Write();
00082 caloFile.Close();
00083
00084 ifstream myReadFile;
00085 myReadFile.open(argv[2]);
00086 string inputRootFile;
00087 if (myReadFile.is_open()) {
00088 while (!myReadFile.eof()) {
00089 getline(myReadFile,inputRootFile);
00090 if ( inputRootFile.size() > 0 )
00091 {
00092 selectAndCopyEvent(inputRootFile,outputFileName);
00093 }
00094 }
00095 }
00096 myReadFile.close();
00097 return 0;
00098 }
00099 else
00100 {
00101 usage();
00102 return -1;
00103 }
00104 return 0;
00105 }
00106
00107
00108
00109
00110 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName)
00111 {
00112
00113
00114 TFile readFile(inputFileName.c_str(),"READONLY");
00115
00116 ui16 runId = 0;
00117
00118 TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00119 TIter nextkey(readFile.GetListOfKeys());
00120 TKey *key = NULL;
00121 CaloRun run;
00122 while (key= (TKey*)nextkey() )
00123 {
00124 TTree *readTree = (TTree*)key->ReadObj();
00125
00126 MTEvent *readEvt = new MTEvent();
00127 TBranch *branch= readTree->GetBranch("MTEvent");
00128 branch->SetAddress(&readEvt);
00129
00130
00131 stringstream label;
00132 label<< inputFileName << "_" << runId ;
00133 TTree tree(label.str().c_str(),"Purged MicroMegas event");
00134 cout << "work on :" << label.str() << endl;
00135 tree.SetMaxTreeSize(4000000000);
00136
00137 Double32_t temp=0;
00138 Double32_t pres=0;
00139
00140 Long64_t bcIdAbsPrev = -1;
00141
00142 CaloEvent *evt = new CaloEvent();
00143
00144 tree.Branch("CaloEvent",&evt,16000,2);
00145
00146 run.SetRunId(runId++);
00147
00148
00149 int nbEntries = readTree->GetEntries();
00150 ui16 evtNum = 0;
00151 TH1F hResidualX("hResidualX" , "hResidualX;xtel - xm2 (pad)" ,201,-100,100);
00152 TH1F hResidualY("hResidualY" , "hResidualY;ytel - ym2 (pad)" ,201,-100,100);
00153
00154 for ( evtNum = 0; evtNum < nbEntries ; evtNum++)
00155 {
00156
00157 readTree->GetEntry(evtNum);
00158 if ( evtNum % 1 == 0)
00159 {
00160 cout << "Done for entry " << evtNum +1 << " / " << nbEntries << "\r" << flush;
00161 }
00162
00163 temp = temp + readEvt->GetTemperature();
00164 pres = pres + readEvt->GetPressure();
00165 bool track = false;
00166 bcIdAbsPrev = buildCaloEvent(*readEvt,*evt,bcIdAbsPrev, track, hResidualX,hResidualY);
00167 if( track )
00168 {
00169 tree.Fill();
00170 evt->Clear();
00171 }
00172 }
00173 pres = pres / evtNum;
00174 temp = temp / evtNum;
00175 run.SetTemperature(temp);
00176 run.SetPressure(pres);
00177
00178 run.SetOffsetX(-101 + hResidualX.GetMaximumBin());
00179 run.SetOffsetY(-101 + hResidualY.GetMaximumBin());
00180
00181 tree.GetUserInfo()->Add(&run);
00182 TFile *curFile =tree.GetCurrentFile() ;
00183
00184 tree.Write();
00185
00186 delete readEvt;
00187 delete evt;
00188 }
00189 caloFile.Close();
00190 readFile.Close();
00191
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 Long64_t buildCaloEvent(const MTEvent& evt, CaloEvent& caloEvent, Long64_t bcIdAbsPrev ,bool& track,TH1F& hResidualX, TH1F& hResidualY)
00207 {
00208
00209 int nchannel = 0;
00210
00211 if (bcIdAbsPrev == -1)
00212 {
00213 nchannel = evt.GetNchannel();
00214
00215 for (int j=0;j<nchannel;j++)
00216 {
00217 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00218
00219 if (channel->GetChamberId() == 10)
00220 {
00221 return channel->GetBcId_Abs();
00222 }
00223 }
00224 }
00225
00226
00227
00228 int dt_min = 4;
00229 int dt_max = 10;
00230
00231
00232 int nhit_spark_cut = 1000;
00233
00234
00235 int adc_cut = 25;
00236
00237 int i = 0;
00238 int t = 0;
00239 int adc = 0;
00240 int xpos = 0;
00241 int ypos = 0;
00242 int zpos = 0;
00243 int zmax = 0;
00244 int digit = 0;
00245 int chbid = 0;
00246 int nhit_m2_cut = 0;
00247 double time = 0;
00248 TString name,title;
00249 Long64_t tprev,t0,t1,t2,t3,dt;
00250
00251 int nhit[3];
00252 int xmax[3];
00253 int ymax[3];
00254
00255 TH2I * hxy_tel_adc[3];
00256
00257
00258 for (int i=0;i<3;i++)
00259 {
00260 nhit[i] = 0;
00261 xmax[i] = 0;
00262 ymax[i] = 0;
00263
00264 name = Form("hxy_tel_adc_chamber_%i",i+7);
00265 title = Form("Hit pattern (adc>%i) chamber %i;y (cm);x (cm)",adc_cut,i+7);
00266 hxy_tel_adc[i] = new TH2I(name,title,32,0,32,32,0,32);
00267 }
00268
00269
00270
00271 nchannel = evt.GetNchannel();
00272
00273 if (nchannel!=0)
00274 {
00275
00276 for (int j=0;j<nchannel;j++)
00277 {
00278 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00279 if (channel->GetChamberId() == 10)
00280 {
00281 t1 = channel->GetBcId_Abs();
00282 j = nchannel;
00283 }
00284 }
00285
00286 time = t1 - bcIdAbsPrev;
00287 time *= 200e-9;
00288 bcIdAbsPrev = t1;
00289
00290
00291
00292
00293 if (time <= 3.35)
00294 {
00295
00296 for (int j=0;j<nchannel;j++)
00297 {
00298 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00299 chbid = channel->GetChamberId();
00300
00301
00302 if (chbid == 7 || chbid == 8 || chbid == 9)
00303 {
00304 adc = channel->GetAnalogValue();
00305
00306 if (adc >= adc_cut)
00307 {
00308 nhit[chbid-7]++;
00309 hxy_tel_adc[chbid-7]->Fill(channel->GetY()*1e-4,channel->GetX()*1e-4,adc);
00310 }
00311 }
00312 }
00313
00314
00315 if (nhit[0]>0 && nhit[1]>0 && nhit[2]>0)
00316 {
00317
00318
00319 for (int k=0;k<3;k++)
00320 {
00321 hxy_tel_adc[k]->GetMaximumBin(ymax[k],xmax[k],zmax);
00322 }
00323
00324
00325
00326 if (ymax[0]==ymax[1] && ymax[0]==ymax[2] &&
00327 xmax[0]==xmax[1] && xmax[0]==xmax[2])
00328 {
00329
00330 nhit_m2_cut = 0;
00331
00332 for (int j=0;j<nchannel;j++)
00333 {
00334 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00335
00336
00337 if (channel->GetChamberId() == 10)
00338 {
00339 t2 = channel->GetBcId_Dif();
00340 t3 = channel->GetBcId_Hit();
00341 dt = t2 - t3;
00342
00343 if (dt>=0 && dt<=20)
00344 {
00345 nhit_m2_cut++;
00346 }
00347 }
00348 }
00349
00350
00351 if (nhit_m2_cut < nhit_spark_cut)
00352 {
00353 track = 1;
00354 caloEvent.SetXTelescope(xmax[0]);
00355 caloEvent.SetYTelescope(ymax[0]);
00356
00357 for (int j=0;j<nchannel;j++)
00358 {
00359 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00360
00361
00362 if (channel->GetChamberId() == 10)
00363 {
00364 t3 = channel->GetBcId_Hit();
00365 dt = t2 - t3;
00366
00367 if (dt>=dt_min && dt<=dt_max)
00368 {
00369 digit = channel->GetDigitalValue();
00370 xpos = (int)channel->GetX();
00371 ypos = (int)channel->GetY();
00372 zpos = (int)channel->GetZ();
00373
00374 xpos /= 10000;
00375 ypos /= 10000;
00376
00377 t = (int)dt;
00378
00379 CaloHit hit;
00380 hit.SetX(xpos);
00381 hit.SetY(ypos);
00382 hResidualX.Fill(xmax[0]-xpos);
00383 hResidualY.Fill(ymax[0]-ypos);
00384 hit.SetZ(0);
00385 hit.SetDeltaT(t);
00386 hit.SetThreshold(digit);
00387 caloEvent.AddHit(hit);
00388 }
00389 }
00390 }
00391 }
00392 else { track = 0; }
00393 }
00394 }
00395 }
00396 }
00397
00398 for (int i=0;i<3;i++)
00399 {
00400 delete hxy_tel_adc[i];
00401 }
00402
00403 return bcIdAbsPrev;
00404 }
00405