00001
00002 #include "tools/Log.hh"
00003 #include "tools/Toolbox.hh"
00004
00005 #include "root/MTRun.hh"
00006 #include "root/MTChannel.hh"
00007 #include "root/MTEvent.hh"
00008 #include "root/MTTrack.hh"
00009 #include "MicroException.hh"
00010
00011 #include "mysql/Mysql.hh"
00012
00013
00014 #include "mysql/Mysql.hh"
00015
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TKey.h"
00019 #include "TBranch.h"
00020 #include "TRandom.h"
00021 #include "TProfile.h"
00022 #include "TH2I.h"
00023 #include "TF1.h"
00024
00025
00026 #include <sstream>
00027 #include <sys/types.h>
00028 #include <unistd.h>
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031
00032
00033 int errsv = errno;
00034
00035
00036 using namespace std;
00037
00038
00039
00040
00041
00042
00043
00044
00045 int main(int argc, char**argv){
00046
00047 if ( argc != 3 ) {
00048 FILE_LOG(logERROR) << "usage:" << endl;
00049 FILE_LOG(logERROR) << ":Tag_ramfull fileToProcess threshold" << endl;
00050 exit(1);
00051 }
00052 string inputfile = argv[1] ;
00053
00054 UInt_t threshold = atoi(argv[2]);
00055
00056
00057 pid_t pid = getpid();
00058 std::ostringstream oss;
00059 oss << "/tmp/" << pid << ".root";
00060 string outputfile = oss.str();
00061
00062 cout<<"Read "<<inputfile<<endl;
00063
00064
00065
00066
00067 TFile infile(inputfile.c_str(),"READ");
00068
00069
00070 TFile oufile(outputfile.c_str(),"RECREATE");
00071
00072
00073 oufile.cd();
00074
00075
00076
00077
00078 TIter nextkey(infile.GetListOfKeys());
00079 MTEvent *updateEvt = new MTEvent();
00080
00081 TKey* key = NULL;
00082 TTree *updateTree = NULL;
00083 while (key = (TKey*)nextkey())
00084
00085 {
00086 TTree *tree = (TTree*)key->ReadObj();
00087
00088
00089 string treeName = tree->GetName();
00090 treeName = treeName + "_preprocessed";
00091 updateTree = new TTree(treeName.c_str(),"MicroMegas event");
00092 updateTree->Branch("MTEvent",&updateEvt,16000,2);
00093
00094 MTRun * run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00095
00096
00097 run->SetProcessed(true);
00098
00099
00100 updateTree->GetUserInfo()->Add(run);
00101 MTEvent *evt = new MTEvent();
00102 TBranch *branch= tree->GetBranch("MTEvent");
00103 branch->SetAddress(&evt);
00104
00105
00106
00107 int nbEntries = tree->GetEntries();
00108 MTEvent* prev_evt = NULL;
00109
00110 cout<<endl;
00111 cout<<"NUMBER OF EVENTS = "<<nbEntries<<endl;
00112 cout<<endl;
00113
00114
00115 for ( int evtNum = 0; evtNum < nbEntries ; evtNum++)
00116 {
00117 map<UInt_t, UInt_t> dtMap;
00118 tree->GetEntry(evtNum);
00119
00120
00121
00122 *updateEvt = *evt;
00123
00124 MTChannel* channel = NULL;
00125 int nbChannel = evt->GetNchannel();
00126
00127 for(int i=0;i<nbChannel ;i++)
00128 {
00129 channel = (MTChannel*)evt->GetChannels()->UncheckedAt(i);
00130 UInt_t t2 = channel->GetBcIdDif();
00131 UInt_t t3 = channel->GetBcIdHit();
00132 UInt_t dt = t2 - t3;
00133
00134
00135 if (dtMap.find(dt) == dtMap.end())
00136 {
00137 dtMap.insert(make_pair(dt,1));
00138 }
00139 else
00140 {
00141 dtMap[dt] = dtMap[dt]++;
00142
00143 }
00144 }
00145
00146
00147 std::vector<UInt_t>& dtValid = updateEvt->GetDtCandidate();
00148 dtValid.clear();
00149 for ( map<UInt_t, UInt_t>::const_iterator iter= dtMap.begin(); iter != dtMap.end(); iter++)
00150 {
00151 UInt_t dt = (*iter).first;
00152 UInt_t value = (*iter).second;
00153
00154 if ( value > threshold )
00155 {
00156 dtValid.push_back(dt);
00157
00158 }
00159 }
00160 sort(dtValid.begin(),dtValid.end());
00161
00162 cout << "Write Event[" << evtNum << "] \r" << flush;
00163 updateTree->Fill();
00164 }
00165
00166
00167
00168 TFile * curFile = updateTree->GetCurrentFile();
00169 updateTree->Write("", TObject::kOverwrite);
00170 updateTree->SetDirectory(0);
00171 }
00172
00173
00174
00175
00176 infile.Close();
00177 oufile.Close();
00178
00179
00180
00181
00182 string command = "cp -f ";
00183 command = command + outputfile + " " + inputfile ;
00184 int result = system(command.c_str());
00185 if ( result == -1 ) { cout << strerror(errno) << endl; }
00186 command = "rm -f " + outputfile;
00187 result = system(command.c_str());
00188 if ( result == -1 ) { cout << strerror(errno) << endl; }
00189
00190 cout << endl << "Done." << endl;
00191
00192
00193 return 0;
00194 }