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
00022
00023 #include <sstream>
00024 #include <sys/types.h>
00025 #include <unistd.h>
00026 #include <stdio.h>
00027
00028
00029 using namespace std;
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 int main(int argc, char**argv){
00041
00042 if ( argc != 2 ) {
00043 FILE_LOG(logERROR) << "usage:" << endl;
00044 FILE_LOG(logERROR) << "setRunId fileToProcess" << endl;
00045 exit(1);
00046 }
00047 string inputfile = argv[1] ;
00048
00049
00050 pid_t pid = getpid();
00051 std::ostringstream oss;
00052 oss << "/tmp/" << pid << ".root";
00053 string outputfile = oss.str();
00054
00055
00056 TFile infile(inputfile.c_str(),"READ");
00057
00058
00059 TFile oufile(outputfile.c_str(),"RECREATE");
00060
00061
00062 oufile.cd();
00063
00064
00065
00066
00067 TIter nextkey(infile.GetListOfKeys());
00068 TKey *key;
00069 MTEvent *updateEvt = new MTEvent();
00070
00071 while (key = (TKey*)nextkey())
00072 {
00073 TTree *tree = (TTree*)key->ReadObj();
00074
00075
00076 string treeName = tree->GetName();
00077
00078 TTree *updateTree = new TTree(treeName.c_str(),"MicroMegas event");
00079 updateTree->Branch("MTEvent",&updateEvt,16000,2);
00080
00081 MTRun * run = (MTRun*)tree->GetUserInfo()->FindObject("MTRun");
00082
00083
00084 ui32 runId = run->GetRunId();
00085 ui32 sqlrunId = UNDEFINED_ID;
00086 if ( runId != 0 && runId != UNDEFINED_ID )
00087 {
00088 cout << "Run id alread set[" << runId << "]" << endl;
00089 }
00090 else
00091 {
00092 size_t last_slash = inputfile.find_last_of("/") +1 ;
00093 string name = inputfile.substr(last_slash);
00094 string path = inputfile.substr(0,last_slash-1);
00095 cout << "search file FromRawFile: path[" << path << "] , name[" << name <<"]" << endl;
00096
00097 Mysql mysql;
00098 try {
00099 sqlrunId = mysql.getRunIdFromRawFile(path,name);
00100 }
00101 catch (MicroException &e) {; }
00102
00103 if ( sqlrunId == UNDEFINED_ID)
00104 {
00105
00106 try {
00107 sqlrunId = mysql.getRunIdFromRebuildFile(path,name);
00108 }
00109 catch (MicroException &e) {; }
00110 if ( sqlrunId == UNDEFINED_ID )
00111 {
00112
00113 try {
00114 sqlrunId = mysql.getRunIdFromMergeFile(path,name);
00115 }
00116 catch (MicroException &e) {; }
00117 }
00118 }
00119
00120 }
00121
00122 if ( sqlrunId == UNDEFINED_ID )
00123 {
00124 cout << "No run correspond to this fuile in database !" << endl;
00125 exit(-1);
00126 }
00127
00128
00129 run->SetRunId(sqlrunId);
00130 cout << "set RunId[" << dec << sqlrunId << endl;
00131 updateTree->GetUserInfo()->Add(run);
00132 MTEvent *evt = new MTEvent();
00133 TBranch *branch= tree->GetBranch("MTEvent");
00134 branch->SetAddress(&evt);
00135
00136 int nbEntries = tree->GetEntries();
00137 for ( int evtNum = 0; evtNum < 10 ; evtNum++)
00138 {
00139 tree->GetEntry(evtNum);
00140 updateTree->Fill();
00141 }
00142
00143
00144 TFile *curFile =updateTree->GetCurrentFile();
00145 updateTree->Write("", TObject::kOverwrite);
00146 updateTree->SetDirectory(0);
00147
00148
00149 delete evt;
00150 }
00151
00152 infile.Close();
00153 oufile.Close();
00154
00155
00156
00157 string command = "cp -f ";
00158 command = command + outputfile + " " + inputfile ;
00159 int result = system(command.c_str());
00160 if ( result == -1 ) { cout << strerror(errno) << endl; }
00161 command = "rm -f " + outputfile;
00162 result = system(command.c_str());
00163 if ( result == -1 ) { cout << strerror(errno) << endl; }
00164
00165
00166 cout << endl << "Done." << endl;
00167
00168
00169 return 0;
00170 }