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 AddCaloHit(CaloEvent& caloEvent,MTChannel* channel);
00029
00030
00031
00032
00033 void usage(void)
00034 {
00035 cout << " : buildCaloHits inputfilename outputfileName acceptation -l eventNum" << endl;
00036 }
00037
00038 int main(int argc, char** argv) {
00039
00040
00041 Arguments arg(argc,argv,"buildCaloHits inputfilename outputfileName acceptation -l eventNum");
00042 if (arg.getNbOfArgs() != 3 || arg.getNbOfOptions() > 1 )
00043 {
00044 arg.usage();
00045 return -1;
00046 }
00047
00048
00049 string outputFileName = arg.getArgument(2);
00050
00051
00052 TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00053 caloFile.Write();
00054 caloFile.Close();
00055
00056 string inputRootFile = arg.getArgument(1);
00057 cout<<"Read ROOT file : "<<inputRootFile<<endl;
00058 UInt_t acceptation = atoi(arg.getArgument(3).c_str());
00059
00060 unsigned int limit = 0;
00061 if ( arg.getOption("-l").size() != 0 )
00062 {
00063 limit = atoi(arg.getOption("-l").c_str());
00064 }
00065
00066 cout << inputRootFile << endl;
00067 cout << outputFileName << endl;
00068
00069 selectAndCopyEvent(inputRootFile,outputFileName,acceptation,limit);
00070 return 0;
00071 }
00072
00073
00074
00075 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,UInt_t acceptation,UInt_t limit)
00076 {
00077
00078 TFile readFile(inputFileName.c_str(),"READONLY");
00079 TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00080
00081
00082
00083 TIter nextkey(readFile.GetListOfKeys());
00084 TKey *key = NULL;
00085 CaloRun run;
00086 ui32 skipCandidate = 0;
00087 ui32 nbHits = 0;
00088
00089 UInt_t eventId = 0;
00090 while (key = (TKey*)nextkey())
00091 {
00092
00093 TTree *readTree = (TTree*)key->ReadObj();
00094 MTEvent *readEvt = new MTEvent();
00095 TBranch *branch= readTree->GetBranch("MTEvent");
00096 branch->SetAddress(&readEvt);
00097
00098
00099 MTRun * readRun = (MTRun*)readTree->GetUserInfo()->FindObject("MTRun");
00100 int runId = readRun->GetRunId();
00101 cout << "read MTRun id[ " << readRun->GetRunId() << "]" << endl;
00102 float offset_x = 0;
00103 float offset_y = 0;
00104
00105
00106 stringstream label;
00107 label<< inputFileName << "_" << runId ;
00108 TTree tree(label.str().c_str(),"Calo tree");
00109 cout << "work on :" << label.str() << endl;
00110 tree.SetMaxTreeSize(40000000000);
00111
00112
00113
00114 CaloEvent *evt = new CaloEvent();
00115
00116 tree.Branch("CaloEvent",&evt,16000,2);
00117 run.SetRunId(runId);
00118 cout << "write CaloRun id[ " << run.GetRunId() << "]" << endl;
00119
00120
00121 int nbEntries = readTree->GetEntries();
00122 ui16 evtNum = 0;
00123
00124 if ( limit == 0 ) { limit = nbEntries; }
00125 for ( evtNum = 0; evtNum < limit ; evtNum++)
00126 {
00127 UInt_t lastCandidate = 0;
00128
00129
00130
00131 readTree->GetEntry(evtNum);
00132
00133
00134 if ( readEvt->IsValid() == true )
00135 {
00136 bool findCaloEvent = false;
00137 int nbChannel = readEvt->GetNchannel();
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 bool endOfCandidate = false;
00148 const std::vector<UInt_t> &dtCanditade = readEvt->GetDtCandidate();
00149 if ( dtCanditade.size() > 0 )
00150 {
00151 vector<UInt_t>::const_iterator iter = dtCanditade.begin() ;
00152 UInt_t candidate = *iter;
00153 lastCandidate = candidate;
00154
00155
00156
00157
00158 for(int i=0;i<nbChannel && endOfCandidate == false ;i++)
00159 {
00160 MTChannel* channel = (MTChannel*)readEvt->GetChannels()->UncheckedAt(i);
00161 Long64_t dt = channel->GetBcIdDif() - channel->GetBcIdHit();
00162
00163
00164 if (dt > 0 && fabs(dt-candidate) <= acceptation)
00165 {
00166 AddCaloHit(*evt,channel);
00167 nbHits++;
00168 findCaloEvent = true;
00169
00170 }
00171 if ( i == nbChannel-1 )
00172 {
00173 evt->SetEventId(eventId++);
00174 evt->SetDeltaTmax(candidate);
00175 evt->SetBcIdAbs(channel->GetBcIdAbs());
00176
00177 tree.Fill();
00178 evt->Clear();
00179 findCaloEvent = false;
00180 }
00181 else if ( (fabs(dt-candidate) > acceptation && findCaloEvent == true) )
00182 {
00183 evt->SetEventId(eventId++);
00184 evt->SetDeltaTmax(candidate);
00185 evt->SetBcIdAbs(channel->GetBcIdAbs());
00186
00187
00188 tree.Fill();
00189 evt->Clear();
00190 findCaloEvent = false;
00191 iter++;
00192 if ( iter == dtCanditade.end() ) { endOfCandidate = true; }
00193 while ( iter != dtCanditade.end() && *iter <= (lastCandidate + acceptation ) )
00194 {
00195
00196
00197 skipCandidate++;
00198 iter++;
00199 if ( iter == dtCanditade.end() ) { endOfCandidate = true; }
00200 }
00201 if ( ! endOfCandidate )
00202 {
00203 candidate = *iter;
00204 lastCandidate = candidate;
00205
00206
00207 i--;
00208 }
00209 }
00210
00211 }
00212 }
00213 }
00214 cout << "Done entry " << evtNum + 1 << " / " << nbEntries << "\r" << endl;
00215 }
00216
00217
00218 tree.GetUserInfo()->Add(&run);
00219 TFile *curFile =tree.GetCurrentFile() ;
00220
00221 tree.Write("", TObject::kOverwrite);
00222 cout << "Write TTree" <<endl;
00223
00224 delete readEvt;
00225 delete evt;
00226
00227 }
00228 caloFile.Close();
00229 readFile.Close();
00230 cout << "Done: " << eventId << " events reconstructed." << endl;
00231 cout << "nb skipped events " << skipCandidate << endl;
00232 cout << "nb hits " << nbHits << endl;
00233 }
00234
00235
00236
00237
00238 bool AddCaloHit(CaloEvent& caloEvent,MTChannel* channel)
00239 {
00240
00241
00242
00243
00244
00245
00246
00247 Long64_t dt = channel->GetBcIdDif() - channel->GetBcIdHit();
00248
00249
00250
00251 int digit = channel->GetDigitalValue();
00252 int xpos = (int)channel->GetX();
00253 int ypos = (int)channel->GetY();
00254 int zpos = (int)channel->GetZ();
00255 int layer = channel->GetChamberId();
00256
00257 const MTChannelSoftId &soft = channel->GetSoftId();
00258
00259 CaloHit hit;
00260
00261 hit.SetX(xpos);
00262 hit.SetY(ypos);
00263 hit.SetZ(zpos);
00264 hit.SetRowInChamber(channel->GetRowInChamber());
00265 hit.SetColInChamber(channel->GetColInChamber());
00266 hit.SetDeltaT(dt);
00267 hit.SetThreshold(digit);
00268 hit.SetLayer(layer);
00269 hit.SetSoftId(soft);
00270
00271
00272 caloEvent.AddHit(hit);
00273
00274
00275 return true;
00276 }