/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/root/buildCaloHits.cpp

Go to the documentation of this file.
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 
00016 //#include <TROOT.h>
00017 //#include <TRint.h>
00018 #include "TH2I.h"
00019 //#include "TNtuple.h"
00020 
00021 
00022 #include "root/CaloRun.hh"
00023 #include "root/CaloEvent.hh"
00024 #include "root/MTRun.hh"
00025 #include "root/MTEvent.hh"
00026 #include "root/MTChannel.hh"
00027 #include "root/MTTrack.hh"
00028 
00029 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,int deltaTMin, int deltaTMax);
00030 void buildCaloEvent(MTEvent& evt, CaloEvent&, int deltaTMin, int deltaTMax, float offset_x, float offset_y);
00031 
00032 void usage(void)
00033 {
00034     cout  << "usage: buildCaloHits -t timestampMin timestampMax outputfileName [deltaTMin deltaTMax]" << endl;
00035     cout  << "     : buildCaloHits -f filename outputfileName [deltaTMin deltaTMax]" << endl;
00036     cout << "1312577280 1312590660" << endl;
00037 }
00038 
00039 int main(int argc, char** argv) {
00040 
00041   if ( argc < 4 || argc > 7 )
00042   {
00043     usage();
00044     return -1;
00045   }
00046 
00047 
00048   int deltaTMin = -1;
00049   int deltaTMax = -1;
00050 
00051 
00052 
00053 
00054 
00055   // Get inputFile name in database by timestamp limit
00056   if ( strcmp (argv[1] ,"-t" )== 0 )
00057   {
00058     if ( argc < 5 ) { usage(); return -1; } 
00059 
00060     if ( argc == 7 ) 
00061     {
00062       deltaTMin = atoi ( argv[5] );
00063       deltaTMax = atoi ( argv[6] );
00064     }
00065     
00066     string outputFileName = argv[4];
00067     // RECREATE Root file to clear its contains if it alread exist
00068     TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00069     caloFile.Write();
00070     caloFile.Close();
00071 
00072     Mysql sql;
00073     string start = argv[2];
00074     string end = argv[3];
00075     
00076     try 
00077     {
00078       // Get list of TFile to filter from Database
00079       const vector<string>& ids = sql.getMergedFilesByTimestamp(start,end);
00080       vector<string>::const_iterator cii;
00081 
00082       // Loop over input TFile
00083       for(cii=ids.begin(); cii!=ids.end(); cii++)
00084       {
00085 
00086         string inputFileName = *cii;
00087         selectAndCopyEvent(inputFileName,outputFileName,deltaTMin,deltaTMax);
00088       }
00089       delete &ids;
00090     }
00091     catch (MicroException e) { }
00092   }
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100   // Get input file name in a input file
00101   else if ( strcmp (argv[1] ,"-f") == 0 )
00102   {
00103      if ( argc < 4 ) { usage(); return -1; } 
00104      string outputFileName = argv[3];
00105 
00106     if ( argc == 6 ) 
00107     {
00108       deltaTMin = atoi ( argv[4] );
00109       deltaTMax = atoi ( argv[5] );
00110     }
00111 
00112     
00113 
00114 
00115 
00116     // RECREATE Root file to clear its contains if it alread exist
00117     TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00118     caloFile.Write();
00119     caloFile.Close();
00120 
00121     ifstream myReadFile;
00122     myReadFile.open(argv[2]);
00123     string inputRootFile;
00124     if (myReadFile.is_open()) {
00125       while (!myReadFile.eof()) {
00126         getline(myReadFile,inputRootFile);
00127         if ( inputRootFile.size() > 0 ) 
00128         {
00129           cout<<"Read ROOT file : "<<inputRootFile<<endl;
00130           selectAndCopyEvent(inputRootFile,outputFileName,deltaTMin,deltaTMax);
00131         }
00132       }
00133     }
00134     myReadFile.close();
00135     return 0;
00136   }
00137   else 
00138   {
00139     usage();
00140     return -1;
00141   }
00142   return 0;
00143 }
00144 
00145 
00146 
00147 
00148 
00149 
00150 
00151 
00152 
00153 
00154 /*-----------------------------------------------------------------------------------------------*/
00155 
00156 void selectAndCopyEvent(const string& inputFileName,const string& outputFileName,int deltaTMin, int deltaTMax)
00157 {
00158 
00159  
00160   TFile readFile(inputFileName.c_str(),"READONLY");
00161   TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00162 
00163   ui16 runId = 0;
00164 
00165 
00166   TIter nextkey(readFile.GetListOfKeys());
00167   TKey *key = NULL;
00168   CaloRun run;
00169 
00170   key= (TKey*)nextkey();
00171   
00172 
00173   //Get tree to read
00174   TTree *readTree = (TTree*)key->ReadObj();
00175   MTEvent *readEvt =  new MTEvent();
00176   TBranch *branch= readTree->GetBranch("MTEvent");
00177   branch->SetAddress(&readEvt);
00178 
00179 
00180   //Get RunId
00181   MTRun * readRun  = (MTRun*)readTree->GetUserInfo()->FindObject("MTRun");
00182   int runid = readRun->GetRunId();
00183   if (runid==0)
00184     {
00185       //cout<<"RunId not known, please enter: ";
00186       //cin>>runid;
00187       //cout<<endl;
00188       //cout<<endl;
00189       runid = 10227;
00190     }
00191 
00192   //Get telescope/prototype offset from database
00193   Mysql mysql;
00194   float offset_x = mysql.getRunOffsetX(runid);
00195   float offset_y = mysql.getRunOffsetY(runid);
00196 
00197   cout<<"OFFSETS = "<<offset_x<<" "<<offset_y<<endl;
00198 
00199 
00200 
00201   // Create a new TTree which will contain  filtered events
00202   stringstream label;
00203   label<< inputFileName << "_" << runId ; 
00204   TTree tree(label.str().c_str(),"Calo tree");
00205   cout << "work on :" << label.str() << endl;
00206   tree.SetMaxTreeSize(4000000000); // 4 Mb
00207 
00208   CaloEvent *evt = new CaloEvent();
00209   // create a branch with run 
00210   tree.Branch("CaloEvent",&evt,16000,2);
00211   run.SetRunId(runId++);
00212 
00213 
00214   // Loop over event in current input TTree
00215   int nbEntries = readTree->GetEntries();
00216   ui16 evtNum = 0;
00217   cout<<"Tree entries = "<<nbEntries<<endl;
00218 
00219   MTTrack * trk = NULL;
00220 
00221 
00222   //for ( evtNum = 0; evtNum < 100 ; evtNum++)
00223   for ( evtNum = 0; evtNum < nbEntries ; evtNum++)
00224     {
00225       cout << "Done for entry " << evtNum + 1 << " / " << nbEntries << "\r" << flush;
00226 
00227       // Get input Event as readEvt
00228       readTree->GetEntry(evtNum);
00229 
00230       bool valid_time = readEvt->GetTimeInfo();
00231       bool square_m2 = readEvt->GetSquareEvt();
00232 
00233       if (valid_time && !square_m2)
00234         {
00235 
00236           trk = readEvt->GetTrack();
00237 
00238           if (trk != NULL)
00239             {
00240               buildCaloEvent(*readEvt,*evt,deltaTMin,deltaTMax,offset_x,offset_y);
00241               tree.Fill();
00242               evt->Clear();
00243             }
00244         }
00245     }
00246 
00247 
00248   tree.GetUserInfo()->Add(&run); 
00249   TFile *curFile =tree.GetCurrentFile() ;
00250   //curFile->Write("", TObject::kOverwrite);
00251   tree.Write();
00252 
00253   delete readEvt;
00254   delete evt;
00255   
00256 
00257   caloFile.Close();
00258   readFile.Close();
00259   // delete run;
00260 }
00261 
00262 
00263 
00264 
00265 
00266 
00267 // Build Tree with position, time and digit of M2 prototype hits
00268 // + cut on the dt distribution between 5 and 10
00269 
00270 void buildCaloEvent(MTEvent& evt, CaloEvent& caloEvent,int deltaTMin, int deltaTMax, float offset_x, float offset_y)
00271   {
00272 
00273     /*m2 prototype time cut*/
00274     int dt_min = 5;
00275     int dt_max = 10;
00276 
00277     if (deltaTMin != -1) { deltaTMin = dt_min; }
00278     if (deltaTMax != -1) { deltaTMax = dt_max; }
00279 
00280 
00281     //Distance from first telescope chamber to m2 prototype
00282     float dist_z = 156.;
00283 
00284 
00285     //Get track
00286     MTTrack * trk = evt.GetTrack();
00287     
00288     int trk_quality = 0;
00289 
00290     if (trk->GetFromPad())
00291       {
00292         trk_quality = 10;
00293       }
00294 
00295     if (trk->GetFromStrip())
00296       {
00297         trk_quality = 20;
00298       }
00299 
00300     if (trk->GetStraight())
00301       {
00302         trk_quality++;
00303 
00304         if (trk->GetSingleHit())
00305           {
00306             trk_quality++;
00307           }
00308       }
00309 
00310     
00311     caloEvent.SetTrkQuality(trk_quality);
00312 
00313     float extrapol_x = offset_x;
00314     float extrapol_y = offset_y;
00315     
00316     //Extrapolate at prototype
00317     
00318     //straight tracks
00319     if (trk_quality%10 != 0)
00320       {
00321         extrapol_x += trk->GetBx();
00322         extrapol_y += trk->GetBy();     
00323       }
00324 
00325     //inclined tracks
00326     else
00327       {
00328         extrapol_x += trk->GetAx() * dist_z + trk->GetBx();
00329         extrapol_y += trk->GetAy() * dist_z + trk->GetBy();
00330       }
00331 
00332 
00333     caloEvent.SetXExtrapol(extrapol_x);
00334     caloEvent.SetYExtrapol(extrapol_y);
00335 
00336 
00337   
00338 
00339     //loop over hits
00340 
00341     int nchannel = evt.GetNchannel();
00342     int nhit_m2_cut = 0;
00343 
00344     for (int j=0;j<nchannel;j++)
00345       {
00346         MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00347           
00348         //look in m2 prototype
00349         if (channel->GetChamberId() == 10)
00350           {
00351             Long64_t dt = channel->GetBcId_Dif() - channel->GetBcId_Hit();
00352               
00353             int zpos = (int)channel->GetZ() / 10000;
00354 
00355             if (dt>=dt_min && dt<=dt_max)
00356               {
00357                 int digit = channel->GetDigitalValue();
00358                 int xpos = (int)channel->GetX() / 10000;
00359                 int ypos = (int)channel->GetY() / 10000;
00360                   
00361                 int t = (int)dt;
00362                   
00363                 CaloHit hit;
00364 
00365                 hit.SetX(xpos);
00366                 hit.SetY(ypos);           
00367                 hit.SetZ(zpos);
00368                 hit.SetDeltaT(t);
00369                 hit.SetThreshold(digit);
00370 
00371                 caloEvent.AddHit(hit);  
00372               }
00373           }
00374       }
00375   }
00376 

Generated on Mon Jun 11 16:55:46 2012 for MicromegasFramework by  doxygen 1.4.7