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
00015
00016 #include "tools/Arguments.hh"
00017
00018 #include "root/CaloHit.hh"
00019 #include "root/CaloRun.hh"
00020 #include "root/CaloEvent.hh"
00021 #include "root/MTRun.hh"
00022 #include "root/MTEvent.hh"
00023 #include "root/MTChannel.hh"
00024 #include "root/MTTrack.hh"
00025 #include "root/MTChannelSoftId.hh"
00026
00027 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,UInt_t acceptation,UInt_t limit);
00028 bool buildCaliceEvent(MTEvent& evt, CaloEvent& caloEvent,UInt_t dtCanditade,UInt_t acceptation);
00029
00030
00031
00032 void usage(void)
00033 {
00034 cout << " : buildCaloHits inputfilename outputfileName acceptation -l eventNum" << endl;
00035 }
00036
00037 int main(int argc, char** argv) {
00038
00039
00040 Arguments arg(argc,argv,"buildCaloHits inputfilename outputfileName acceptation -l eventNum");
00041 if (arg.getNbOfArgs() != 3 || arg.getNbOfOptions() > 1 )
00042 {
00043 arg.usage();
00044 return -1;
00045 }
00046
00047
00048 string outputFileName = arg.getArgument(2);
00049
00050
00051 TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00052 caloFile.Write();
00053 caloFile.Close();
00054
00055 string inputRootFile = arg.getArgument(1);
00056 cout<<"Read ROOT file : "<<inputRootFile<<endl;
00057 UInt_t acceptation = atoi(arg.getArgument(3).c_str());
00058
00059 unsigned int limit = 0;
00060 if ( arg.getOption("-l").size() != 0 )
00061 {
00062 limit = atoi(arg.getOption("-l").c_str());
00063 }
00064
00065 cout << inputRootFile << endl;
00066 cout << outputFileName << endl;
00067
00068 selectAndCopyEvent(inputRootFile,outputFileName,acceptation,limit);
00069 return 0;
00070 }
00071
00072
00073
00074 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,UInt_t acceptation,UInt_t limit)
00075 {
00076
00077 TFile readFile(inputFileName.c_str(),"READONLY");
00078 TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00079
00080 ui32 nbEmptyEvent = 0;
00081
00082
00083 TIter nextkey(readFile.GetListOfKeys());
00084 TKey *key = NULL;
00085 CaloRun run;
00086
00087 UInt_t eventId = 0;
00088 while (key = (TKey*)nextkey())
00089 {
00090
00091 TTree *readTree = (TTree*)key->ReadObj();
00092 MTEvent *readEvt = new MTEvent();
00093 TBranch *branch= readTree->GetBranch("MTEvent");
00094 branch->SetAddress(&readEvt);
00095
00096
00097 MTRun * readRun = (MTRun*)readTree->GetUserInfo()->FindObject("MTRun");
00098 int runId = readRun->GetRunId();
00099 cout << "read MTRun id[ " << readRun->GetRunId() << "]" << endl;
00100 float offset_x = 0;
00101 float offset_y = 0;
00102
00103
00104 stringstream label;
00105 label<< inputFileName << "_" << runId ;
00106 TTree tree(label.str().c_str(),"Calo tree");
00107 cout << "work on :" << label.str() << endl;
00108 tree.SetMaxTreeSize(40000000000);
00109
00110
00111
00112 CaloEvent *evt = new CaloEvent();
00113
00114 tree.Branch("CaloEvent",&evt,16000,2);
00115 run.SetRunId(runId);
00116 cout << "write CaloRun id[ " << run.GetRunId() << "]" << endl;
00117
00118
00119 int nbEntries = readTree->GetEntries();
00120 ui16 evtNum = 0;
00121
00122 if ( limit == 0 ) { limit = nbEntries; }
00123
00124 for ( evtNum = 0; evtNum < limit ; evtNum++)
00125 {
00126 cout << "Done for entry " << evtNum + 1 << " / " << nbEntries << "\r" << endl;
00127
00128
00129 readTree->GetEntry(evtNum);
00130 if ( readEvt->IsValid() == true )
00131 {
00132 const std::vector<UInt_t> &dtCanditade = readEvt->GetDtCandidate();
00133 for ( vector<UInt_t>::const_iterator iter = dtCanditade.begin() ; iter != dtCanditade.end() ; iter++ )
00134 {
00135 UInt_t candidate = *iter;
00136
00137 evt->SetDeltaTmax(candidate);
00138
00139 buildCaliceEvent(*readEvt,*evt,candidate,acceptation);
00140
00141 if ( evt->GetNHits() == 0 )
00142 {
00143 nbEmptyEvent++;
00144 }
00145 else
00146 {
00147 evt->SetEventId(eventId++);
00148 tree.Fill();
00149 }
00150 evt->Clear();
00151 }
00152 }
00153 }
00154
00155
00156 tree.GetUserInfo()->Add(&run);
00157 TFile *curFile =tree.GetCurrentFile() ;
00158
00159 tree.Write("", TObject::kOverwrite);
00160 cout << "Write TTree" <<endl;
00161
00162 delete readEvt;
00163 delete evt;
00164
00165 }
00166 caloFile.Close();
00167 readFile.Close();
00168 cout << "Done: " << eventId << " events reconstructed. Empty event:" << nbEmptyEvent << endl;
00169 }
00170
00171
00172
00173
00174 bool buildCaliceEvent(MTEvent& evt, CaloEvent& caloEvent, UInt_t dtCanditade, UInt_t acceptation)
00175 {
00176
00177 int nchannel = evt.GetNchannel();
00178
00179 for (int j=0;j<nchannel;j++)
00180 {
00181 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00182
00183 Long64_t dt = channel->GetBcId_Dif() - channel->GetBcId_Hit();
00184
00185 if (fabs(dt-dtCanditade) <= acceptation)
00186 {
00187 int digit = channel->GetDigitalValue();
00188 int xpos = (int)channel->GetX();
00189 int ypos = (int)channel->GetY();
00190 int zpos = (int)channel->GetZ();
00191 int layer = channel->GetChamberId();
00192
00193 const MTChannelSoftId &soft = channel->GetSoftId();
00194
00195 CaloHit hit;
00196
00197 hit.SetX(xpos);
00198 hit.SetY(ypos);
00199 hit.SetZ(zpos);
00200 hit.SetRowInChamber(channel->GetRowInChamber());
00201 hit.SetColInChamber(channel->GetColInChamber());
00202 hit.SetDeltaT(dt);
00203 hit.SetThreshold(digit);
00204 hit.SetLayer(layer);
00205 hit.SetSoftId(soft);
00206
00207
00208 caloEvent.AddHit(hit);
00209 }
00210 }
00211 return true;
00212 }
00213
00214
00215
00216
00217
00218
00219 void buildCaloEvent(MTEvent& evt, CaloEvent& caloEvent,int deltaTMin, int deltaTMax, float offset_x, float offset_y)
00220 {
00221
00222
00223 int dt_min = 5;
00224 int dt_max = 10;
00225
00226 if (deltaTMin != -1) { deltaTMin = dt_min; }
00227 if (deltaTMax != -1) { deltaTMax = dt_max; }
00228
00229
00230
00231 float dist_z = 156.;
00232
00233
00234
00235 MTTrack * trk = evt.GetTrack();
00236
00237 int trk_quality = 0;
00238 if ( trk != NULL )
00239 {
00240 if (trk->GetFromPad())
00241 {
00242 trk_quality = 10;
00243 }
00244
00245 if (trk->GetFromStrip())
00246 {
00247 trk_quality = 20;
00248 }
00249
00250 if (trk->GetStraight())
00251 {
00252 trk_quality++;
00253
00254 if (trk->GetSingleHit())
00255 {
00256 trk_quality++;
00257 }
00258 }
00259
00260
00261 caloEvent.SetTrkQuality(trk_quality);
00262
00263 float extrapol_x = offset_x;
00264 float extrapol_y = offset_y;
00265
00266
00267
00268
00269 if (trk_quality%10 != 0)
00270 {
00271 extrapol_x += trk->GetBx();
00272 extrapol_y += trk->GetBy();
00273 }
00274
00275
00276 else
00277 {
00278 extrapol_x += trk->GetAx() * dist_z + trk->GetBx();
00279 extrapol_y += trk->GetAy() * dist_z + trk->GetBy();
00280 }
00281
00282
00283 caloEvent.SetXExtrapol(extrapol_x);
00284 caloEvent.SetYExtrapol(extrapol_y);
00285 }
00286
00287
00288
00289
00290
00291
00292 int nchannel = evt.GetNchannel();
00293 int nhit_m2_cut = 0;
00294
00295 for (int j=0;j<nchannel;j++)
00296 {
00297 MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00298
00299
00300 if (channel->GetChamberId() == 10)
00301 {
00302 Long64_t dt = channel->GetBcId_Dif() - channel->GetBcId_Hit();
00303
00304 int zpos = (int)channel->GetZ() / 10000;
00305
00306 if (dt>=dt_min && dt<=dt_max)
00307 {
00308 int digit = channel->GetDigitalValue();
00309 int xpos = (int)channel->GetX() / 10000;
00310 int ypos = (int)channel->GetY() / 10000;
00311
00312 int t = (int)dt;
00313
00314 CaloHit hit;
00315
00316 hit.SetX(xpos);
00317 hit.SetY(ypos);
00318 hit.SetZ(zpos);
00319 hit.SetDeltaT(t);
00320 hit.SetThreshold(digit);
00321
00322 caloEvent.AddHit(hit);
00323 }
00324 }
00325 }
00326 }
00327