/data3/calcul/jacquem/working_dir/Micromegas/micromegasFrameWork/src/analyse/lcio/buildCaloHits.cpp File Reference

#include <TRandom.h>
#include <TTree.h>
#include <TFile.h>
#include <TStyle.h>
#include <TKey.h>
#include <Riostream.h>
#include <sstream>
#include <string>
#include "mysql/Mysql.hh"
#include "MicroException.hh"
#include <iostream>
#include <fstream>
#include "root/CaloHit.hh"
#include "root/MTRun.hh"
#include "root/MTEvent.hh"
#include "root/MTChannel.hh"
#include "root/MTMicrorocChip.hh"
#include "TH2I.h"

Include dependency graph for buildCaloHits.cpp:

Go to the source code of this file.

Functions

void selectAndCopyEvent (const string &inputFileName, const string &outputFileName)
Long64_t buildCaloEvent (const MTEvent &evt, CaloEvent &, Long64_t bcidAbsPrev, bool &track, TH1F &hResidualX, TH1F &hResidualY)
void usage (void)
int main (int argc, char **argv)


Function Documentation

void selectAndCopyEvent ( const string &  inputFileName,
const string &  outputFileName 
)

Definition at line 110 of file buildCaloHits.cpp.

Referenced by main().

00111 {
00112 
00113  
00114   TFile readFile(inputFileName.c_str(),"READONLY");
00115 
00116   ui16 runId = 0;
00117 
00118   TFile caloFile(outputFileName.c_str(), "UPDATE","Micromegas calorimeter data");
00119   TIter nextkey(readFile.GetListOfKeys());
00120   TKey *key = NULL;
00121   CaloRun run;
00122   while (key= (TKey*)nextkey() )
00123   {
00124     TTree *readTree = (TTree*)key->ReadObj();
00125 
00126     MTEvent *readEvt =  new MTEvent();
00127     TBranch *branch= readTree->GetBranch("MTEvent");
00128     branch->SetAddress(&readEvt);
00129 
00130     // Create a new TTree which will contain  filtered events
00131     stringstream label;
00132     label<< inputFileName << "_" << runId ; 
00133     TTree tree(label.str().c_str(),"Purged MicroMegas event");
00134     cout << "work on :" << label.str() << endl;
00135     tree.SetMaxTreeSize(4000000000); // 4 Mb
00136 
00137     Double32_t temp=0;
00138     Double32_t pres=0;
00139 
00140     Long64_t bcIdAbsPrev = -1;
00141 
00142     CaloEvent *evt = new CaloEvent();
00143     // create a branch with run 
00144     tree.Branch("CaloEvent",&evt,16000,2);
00145 
00146     run.SetRunId(runId++);
00147 
00148     // Loop over event in current input TTree
00149     int nbEntries = readTree->GetEntries();
00150     ui16 evtNum = 0;
00151     TH1F hResidualX("hResidualX" , "hResidualX;xtel - xm2 (pad)" ,201,-100,100); 
00152     TH1F hResidualY("hResidualY" , "hResidualY;ytel - ym2 (pad)" ,201,-100,100); 
00153 
00154     for ( evtNum = 0; evtNum < nbEntries ; evtNum++)
00155     {
00156       // Get input Event throw readEvt
00157       readTree->GetEntry(evtNum);
00158       if ( evtNum % 1 == 0)
00159       {
00160         cout << "Done for entry " << evtNum  +1 << " / " << nbEntries << "\r" << flush;
00161       }
00162 
00163       temp = temp + readEvt->GetTemperature();
00164       pres = pres + readEvt->GetPressure();
00165       bool track = false;
00166       bcIdAbsPrev = buildCaloEvent(*readEvt,*evt,bcIdAbsPrev, track, hResidualX,hResidualY);
00167       if( track )
00168       {
00169         tree.Fill();  // fill the tree with the current event
00170         evt->Clear();
00171       } 
00172     }
00173     pres = pres / evtNum;
00174     temp = temp / evtNum;
00175     run.SetTemperature(temp);
00176     run.SetPressure(pres);
00177     
00178     run.SetOffsetX(-101 + hResidualX.GetMaximumBin()); 
00179     run.SetOffsetY(-101 + hResidualY.GetMaximumBin()); 
00180 
00181     tree.GetUserInfo()->Add(&run); 
00182     TFile *curFile =tree.GetCurrentFile() ;
00183     //curFile->Write("", TObject::kOverwrite);
00184     tree.Write();
00185   
00186     delete readEvt;
00187     delete evt;
00188   }
00189   caloFile.Close();
00190   readFile.Close();
00191  // delete run;
00192 }

Long64_t buildCaloEvent ( const MTEvent evt,
CaloEvent ,
Long64_t  bcidAbsPrev,
bool &  track,
TH1F &  hResidualX,
TH1F &  hResidualY 
)

Definition at line 206 of file buildCaloHits.cpp.

Referenced by selectAndCopyEvent().

00207   {
00208 
00209   int nchannel = 0;
00210   //Define bcIdAbsPrev, bcid_abs of first readout
00211   if (bcIdAbsPrev == -1)
00212   {
00213     nchannel = evt.GetNchannel();
00214 
00215     for (int j=0;j<nchannel;j++)
00216     {             
00217       MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00218 
00219       if (channel->GetChamberId() == 10)
00220       {
00221        return  channel->GetBcId_Abs();
00222       }
00223     }
00224   }
00225 
00226 
00227   /*m2 prototype time cut*/
00228   int dt_min = 4;
00229   int dt_max = 10;
00230 
00231   /*spark cut = Nhit inside 0-20 dt region*/
00232   int nhit_spark_cut = 1000;
00233 
00234   /*telescope energy cut*/
00235   int adc_cut = 25;
00236 
00237   int i = 0;
00238   int t = 0;
00239   int adc = 0;
00240   int xpos = 0;
00241   int ypos = 0;
00242   int zpos = 0;
00243   int zmax = 0;
00244   int digit = 0;
00245   int chbid = 0;
00246   int nhit_m2_cut = 0;
00247   double time = 0;
00248   TString name,title;
00249   Long64_t tprev,t0,t1,t2,t3,dt;
00250 
00251   int nhit[3];
00252   int xmax[3];
00253   int ymax[3];
00254 
00255   TH2I * hxy_tel_adc[3];
00256 
00257 
00258   for (int i=0;i<3;i++)
00259   {
00260     nhit[i] = 0;
00261     xmax[i] = 0;
00262     ymax[i] = 0;
00263 
00264     name = Form("hxy_tel_adc_chamber_%i",i+7);
00265     title = Form("Hit pattern (adc>%i) chamber %i;y (cm);x (cm)",adc_cut,i+7);
00266     hxy_tel_adc[i] = new TH2I(name,title,32,0,32,32,0,32);
00267   }
00268 
00269   //if t0 is defined start loop over events
00270 
00271   nchannel = evt.GetNchannel();
00272 
00273   if (nchannel!=0)
00274   {
00275     //determine time to previous readout
00276     for (int j=0;j<nchannel;j++)
00277     {
00278       MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00279       if (channel->GetChamberId() == 10)
00280       {
00281         t1 = channel->GetBcId_Abs();
00282         j = nchannel;
00283       }
00284     }
00285 
00286     time = t1 - bcIdAbsPrev;
00287     time *= 200e-9;
00288     bcIdAbsPrev = t1;
00289 
00290 
00291     //if readout occured after less than 3.35 s of previous readout
00292     //skip also first event as hit time info is wrong
00293     if (time <= 3.35)
00294     {
00295       //look for a track in telescope
00296       for (int j=0;j<nchannel;j++)
00297       {
00298         MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00299         chbid = channel->GetChamberId();
00300 
00301         //look in pad chambers
00302         if (chbid == 7 || chbid == 8 || chbid == 9)
00303         {
00304           adc = channel->GetAnalogValue();
00305 
00306           if (adc >= adc_cut)
00307           {
00308             nhit[chbid-7]++;
00309             hxy_tel_adc[chbid-7]->Fill(channel->GetY()*1e-4,channel->GetX()*1e-4,adc);
00310           }
00311         }
00312       }
00313 
00314       //require that 3 pad chambers have at least 1 hit
00315       if (nhit[0]>0 && nhit[1]>0 && nhit[2]>0)
00316       {
00317         //find position of ADC maxima
00318 
00319         for (int k=0;k<3;k++)
00320         {
00321           hxy_tel_adc[k]->GetMaximumBin(ymax[k],xmax[k],zmax);
00322         }
00323 
00324         //require aligned maxima
00325 
00326         if (ymax[0]==ymax[1] && ymax[0]==ymax[2] &&
00327             xmax[0]==xmax[1] && xmax[0]==xmax[2])
00328         {
00329           //reloop over hits
00330           nhit_m2_cut = 0;
00331 
00332           for (int j=0;j<nchannel;j++)
00333           {
00334             MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00335 
00336             //look in m2 prototype
00337             if (channel->GetChamberId() == 10)
00338             {                                 
00339               t2 = channel->GetBcId_Dif();
00340               t3 = channel->GetBcId_Hit();
00341               dt = t2 - t3;
00342 
00343               if (dt>=0 && dt<=20)
00344               {
00345                 nhit_m2_cut++;
00346               }
00347             }
00348           }
00349 
00350           //reject high multiplicity event in time region of signal
00351           if (nhit_m2_cut < nhit_spark_cut)
00352           {
00353             track = 1;  
00354             caloEvent.SetXTelescope(xmax[0]);
00355             caloEvent.SetYTelescope(ymax[0]);
00356             //reloop over hits
00357             for (int j=0;j<nchannel;j++)
00358             {
00359               MTChannel* channel = (MTChannel*)evt.GetChannels()->UncheckedAt(j);
00360 
00361               //look in m2 prototype
00362               if (channel->GetChamberId() == 10)
00363               {
00364                 t3 = channel->GetBcId_Hit();
00365                 dt = t2 - t3;
00366 
00367                 if (dt>=dt_min && dt<=dt_max)
00368                 {
00369                   digit = channel->GetDigitalValue();
00370                   xpos = (int)channel->GetX();
00371                   ypos = (int)channel->GetY();
00372                   zpos = (int)channel->GetZ();
00373 
00374                   xpos /= 10000;
00375                   ypos /= 10000;
00376 
00377                   t = (int)dt;
00378 
00379                   CaloHit hit;
00380                   hit.SetX(xpos);
00381                   hit.SetY(ypos);
00382                         hResidualX.Fill(xmax[0]-xpos);
00383                         hResidualY.Fill(ymax[0]-ypos);
00384                   hit.SetZ(0);
00385                   hit.SetDeltaT(t);
00386                   hit.SetThreshold(digit);
00387                   caloEvent.AddHit(hit);        
00388                 }
00389               }
00390             }
00391           }
00392           else { track = 0; }
00393         }
00394       } //end nhit[0]>0 && nhit[1]>0 && nhit[2]>0
00395     } // end time <= 3.35 && i != 0
00396   } // enf if nChannel != 0
00397     
00398   for (int i=0;i<3;i++)
00399   {
00400    delete hxy_tel_adc[i];
00401   }
00402 
00403   return bcIdAbsPrev;
00404 }

void usage ( void   ) 

Definition at line 27 of file buildCaloHits.cpp.

Referenced by main().

00028 {
00029     cout  << "usage: buildCaloHits -t timestampMin timestampMax outputfileName" << endl;
00030     cout  << "     : buildCaloHits -f filename outputfileName" << endl;
00031     cout << "1312577280 1312590660" << endl;
00032 }

int main ( int  argc,
char **  argv 
)

Definition at line 34 of file buildCaloHits.cpp.

00034                                 {
00035 
00036   if ( argc < 4 || argc > 5 )
00037   {
00038     usage();
00039     return -1;
00040   }
00041 
00042 
00043   // Get inputFile name in database by timestamp limit
00044   if ( strcmp (argv[1] ,"-t" )== 0 )
00045   {
00046     if ( argc != 5 ) { usage(); return -1; } 
00047     string outputFileName = argv[4];
00048     Mysql sql;
00049     string start = argv[2];
00050     string end = argv[3];
00051     
00052     // RECREATE Root file to clear its contains if it alread exist
00053 //    TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00054 
00055     try 
00056     {
00057       // Get list of TFile to filter from Database
00058       const vector<string>& ids = sql.getMergedFilesByTimestamp(start,end);
00059       vector<string>::const_iterator cii;
00060 
00061       // Loop over input TFile
00062       for(cii=ids.begin(); cii!=ids.end(); cii++)
00063       {
00064 
00065         string inputFileName = *cii;
00066         selectAndCopyEvent(inputFileName,outputFileName);
00067       }
00068       delete &ids;
00069     }
00070     catch (MicroException e) { }
00071   }
00072 
00073   // Get input file name in a input file
00074   else if ( strcmp (argv[1] ,"-f") == 0 )
00075   {
00076      if ( argc != 4 ) { usage(); return -1; } 
00077      string outputFileName = argv[3];
00078 
00079      // RECREATE Root file to clear its contains if it alread exist
00080      TFile caloFile(outputFileName.c_str(), "RECREATE","Micromegas calorimeter data");
00081      caloFile.Write();
00082      caloFile.Close();
00083 
00084      ifstream myReadFile;
00085      myReadFile.open(argv[2]);
00086      string inputRootFile;
00087      if (myReadFile.is_open()) {
00088      while (!myReadFile.eof()) {
00089         getline(myReadFile,inputRootFile);
00090         if ( inputRootFile.size() > 0 ) 
00091         {
00092           selectAndCopyEvent(inputRootFile,outputFileName);
00093         }
00094      }
00095     }
00096     myReadFile.close();
00097     return 0;
00098   }
00099   else 
00100   {
00101     usage();
00102     return -1;
00103   }
00104   return 0;
00105 }


Generated on Mon Jan 7 13:16:58 2013 for MicromegasFramework by  doxygen 1.4.7