]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONTRKOCCda.cxx
Fixing a serious memory leak
[u/mrichter/AliRoot.git] / MUON / MUONTRKOCCda.cxx
1 /*
2  MCH DA for online occupancy
3  
4  Contact: Laurent Aphecetche <laurent.aphecetche@subatech.in2p3.fr>, Jean-Luc Charvet <jean-luc.charvet@cea.fr>, Alberto Baldisseri <alberto.baldisseri@cea.fr>
5  Link: 
6  Run Type: PHYSICS STANDALONE
7  DA Type: MON
8  Number of events needed: all (or at least as much as possible...)
9  Input Files: 09000094301009.10.raw
10  Output Files: mch.occupancy, to be exported to the DAQ FXS
11  Trigger types used: PHYSICS_EVENT
12 */
13
14 /**************************************************************************
15  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
16  *                                                                        *
17  * Author: The ALICE Off-line Project.                                    *
18  * Contributors are mentioned in the code where appropriate.              *
19  *                                                                        *
20  * Permission to use, copy, modify and distribute this software and its   *
21  * documentation strictly for non-commercial purposes is hereby granted   *
22  * without fee, provided that the above copyright notice appears in all   *
23  * copies and that both the copyright notice and this permission notice   *
24  * appear in the supporting documentation. The authors make no claims     *
25  * about the suitability of this software for any purpose. It is          *
26  * provided "as is" without express or implied warranty.                  *
27  **************************************************************************/
28
29 ///
30 /// MUON TRACKER DA to compute the hit count at manu level.
31 ///
32 /// In the end, this DA produces an ASCII file containing
33 /// the hit count of all the manus (that were seen in the data flow)
34 /// of the MUON Tracker (and the number of seen events, so we can
35 /// later on compute the occupancy)
36 ///
37 /// $Id$
38
39 #include "AliMUON2DMap.h"
40 #include "AliMUONCalibParamNI.h"
41 #include "AliMUONRawStreamTrackerHP.h"
42 #include "AliMpConstants.h"
43 #include "AliRawEventHeaderBase.h"
44 #include "AliRawReaderDate.h"
45 #include "Riostream.h"
46 #include "TPluginManager.h"
47 #include "TROOT.h"
48 #include "TString.h"
49 #include "TTimeStamp.h"
50 #include "TStopwatch.h"
51 #include "daqDA.h"
52 #include "event.h"
53 #include "monitor.h"
54
55 #ifdef ALI_AMORE
56 #include <AmoreDA.h>
57 #include "TObjString.h"
58 #include "TSystem.h"
59 #include <sstream>
60 #endif
61
62 #include "AliSysInfo.h"
63
64 const char* OUTPUT_FILE = "mch.occupancy";
65 const char* DAVERSION = "MUONTRKOCCda v1.2 ($Id$)";
66
67 //______________________________________________________________________________
68 void Add(AliMUONVStore& destStore, const AliMUONVStore& srcStore)
69 {
70   /// Add all elements from srcStore to destStore
71   /// Each element of srcStore is supposed to be an AliMUONCalibParamNI,
72   /// with ID0=busPatchId and ID1=manuId
73   
74   TIter next(srcStore.CreateIterator());
75   AliMUONVCalibParam* source;
76   
77   while ( ( source = static_cast<AliMUONVCalibParam*>(next()) ) )
78   {
79     AliMUONCalibParamNI* dest = static_cast<AliMUONCalibParamNI*>(destStore.FindObject(source->ID0(),source->ID1()));
80     if (!dest)
81     {
82       dest = static_cast<AliMUONCalibParamNI*>(source->Clone());
83       destStore.Add(dest);
84     }
85     else
86     {
87       for ( Int_t i = 0; i < source->Size(); ++i ) 
88       {
89         for ( Int_t j = 0; j  < source->Dimension(); ++j ) 
90         {
91           dest->SetValueAsIntFast(i,j,dest->ValueAsIntFast(i,j)+source->ValueAsIntFast(i,j));
92         }
93       }
94     }
95   }
96 }
97
98 //______________________________________________________________________________
99 void GenerateOutputFile(const AliMUONVStore& store, ostream& out, 
100                         Int_t runNumber, Int_t nevents)
101 {
102   /// Write the channel hit count (grouped by manu) in the output file.
103   
104   TIter next(store.CreateIterator());
105   AliMUONVCalibParam* manu;
106   
107   out << "//===========================================================================" << endl;
108   out << "//  Hit counter file calculated by " << __FILE__ << endl;
109   out << "//===========================================================================" << endl;
110   out << "//" << endl;
111   out << "//       * Run Number          : " << runNumber << endl;
112   out << "//       * File Creation Date  : " << TTimeStamp().AsString("l") << endl;
113   out << "//---------------------------------------------------------------------------" << endl;
114   out << "//  BP   MANU  SUM_N  NEVENTS" << endl;
115   out << "//---------------------------------------------------------------------------" << endl;
116   
117   while ( ( manu = static_cast<AliMUONVCalibParam*>(next()) ) )
118   {
119     Int_t sum(0);
120 //    Int_t nevents(0);
121     
122     for ( Int_t i = 0; i < manu->Size(); ++i ) 
123     {
124       sum += manu->ValueAsInt(i);
125       //      nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
126       // nevents = TMath::Max(nevents,manu->ValueAsInt(i,1));
127     }
128     
129     out << Form("%5d %5d %10d %10d",manu->ID0(),manu->ID1(),sum,nevents) << endl;
130   }
131 }
132   
133 //______________________________________________________________________________
134 int main(int argc, char **argv) 
135 {
136   /// Main method.
137   ///
138   /// We loop over all physics events.
139   /// For each event we store the channels that were hit for that event.
140   /// If the event is good, we then increment the list of channels hit for
141   /// the whole run.
142   /// We delete the store for a single event and move to next event.
143   ///
144   /// In the end we output an ASCII file with the necessary information 
145   /// to compute the occupancy later on, i.e. the number of times channels
146   /// were seen per manu, and the number of events.
147   ///
148   
149   TStopwatch timers;
150   timers.Start(kTRUE); 
151   
152   ios::sync_with_stdio();
153
154   cout << "Running " << DAVERSION << endl;
155   
156   if ( argc < 2 ) 
157   {
158     cout << "Wrong number of arguments" << endl;
159     cout << "Usage : " << argv[0] << " datasource1 [datasource2] ..." << endl;
160     return -1;
161   }
162   
163   // needed for streamer application
164   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo",
165                                         "*",
166                                         "TStreamerInfo",
167                                         "RIO",
168                                         "TStreamerInfo()");
169   
170   Int_t numberOfEvents(0);
171   Int_t numberOfPhysicsEvent(0);
172   Int_t numberOfBadEvents(0);
173   Int_t numberOfUsedEvents(0);
174   
175   AliMUON2DMap oneEventData(kTRUE);
176   AliMUON2DMap accumulatedData(kTRUE);
177   
178   UInt_t runNumber(0);
179
180   for ( Int_t i = 1; i < argc; ++i ) 
181   {    
182     int status;
183     AliRawReaderDate* rawReader(0x0);
184     
185 // define data source : 
186    status=monitorSetDataSource(argv[i]);
187     if (status!=0) 
188     {
189       printf("MCH Occupancy DA ERROR: monitorSetDataSource() failed: %s\n", monitorDecodeError(status));
190       return -1;
191     }
192     
193 // Declare monitoring program 
194     status=monitorDeclareMp("MUON_TRK_OCC");
195     if (status!=0) 
196     {
197       printf("MCH Occupancy DA ERROR: monitorDeclareMp() failed: %s\n", monitorDecodeError(status));
198       return -1;
199     }
200 // Define wait event timeout - 1s max
201     monitorSetNowait();
202     monitorSetNoWaitNetworkTimeout(1000);
203     
204     for(;;)
205     {
206       struct eventHeaderStruct *event;
207       eventTypeType eventT;
208       
209       status=monitorGetEventDynamic((void **)&event);
210       if (status!=0)
211       {
212         printf("MCH Occupancy DA ERROR: %s\n", monitorDecodeError(status));
213         delete event;
214         break;
215       }
216
217       /* check shutdown condition */
218       if (daqDA_checkShutdown())
219       {
220         delete event;
221         break;
222       }
223       
224       /* retry if got no event */
225       if (event==NULL) continue;
226
227       ++numberOfEvents;
228
229       eventT=event->eventType;
230       if ((eventT == END_OF_RUN)||(eventT == END_OF_RUN_FILES)) 
231       {
232         delete event;
233         break;
234       }
235       if (eventT != PHYSICS_EVENT) 
236       {
237         delete event;
238         continue;
239       }
240                  
241       ++numberOfPhysicsEvent;
242       
243       rawReader = new AliRawReaderDate((void*)event);
244       
245       if ( rawReader->GetRunNumber() != runNumber )
246       {
247         if ( runNumber != 0 ) 
248         {
249           cout << "Uh oh. That's bad... Changing of run number ???" << endl;
250           delete event;
251           delete rawReader;
252           return -9999;
253         }
254         runNumber = rawReader->GetRunNumber();
255       }
256       
257       AliMUONRawStreamTrackerHP stream(rawReader);
258       
259       stream.DisableWarnings();
260       
261       oneEventData.Clear();
262       
263       Int_t buspatchId;
264       UShort_t  manuId;
265       UChar_t manuChannel;
266       UShort_t adc;
267       
268       stream.First();
269             
270       while ( stream.Next(buspatchId,manuId,manuChannel,adc,kTRUE) )
271       {    
272         AliMUONVCalibParam* one = static_cast<AliMUONVCalibParam*>(oneEventData.FindObject(buspatchId,manuId));
273         
274         if (!one)
275         {
276           one = new AliMUONCalibParamNI(1,AliMpConstants::ManuNofChannels(),buspatchId,manuId);
277           oneEventData.Add(one);
278         }
279         
280         one->SetValueAsInt(manuChannel,0,one->ValueAsInt(manuChannel,0)+1);
281       }
282       
283       Bool_t badEvent = stream.HasPaddingError() || stream.HasGlitchError();
284       
285       if ( !badEvent )
286       {
287         ++numberOfUsedEvents;
288         Add(accumulatedData,oneEventData);
289       }
290       else
291       {
292         ++numberOfBadEvents;
293       }
294
295       if ( numberOfEvents % 10 == 0 ) 
296       {
297         AliSysInfo::AddStamp(Form("Event%d",numberOfEvents),1,numberOfEvents,-1);
298       }
299       
300       delete event;
301       
302       delete rawReader;
303     }    
304   }
305   
306   
307   cout << Form("%12d events processed : %12d physics %d used ones %d bad ones",
308                numberOfEvents,numberOfPhysicsEvent,numberOfUsedEvents,numberOfBadEvents) << endl;
309     
310   ofstream fout(OUTPUT_FILE);
311   
312   GenerateOutputFile(accumulatedData,fout,runNumber,numberOfUsedEvents);
313   
314   fout.close();
315   
316 #ifdef ALI_AMORE
317   
318   // Send occupancy store (as a big string) to the AMORE DB
319   amore::da::AmoreDA amoreDA(amore::da::AmoreDA::kSender);
320   
321   ostringstream str;
322   
323   GenerateOutputFile(accumulatedData,str,runNumber,numberOfUsedEvents);
324     
325   TObjString occupancyAsString(str.str().c_str());
326   
327   Int_t status = amoreDA.Send("Occupancy",&occupancyAsString);
328   if ( status )
329   {
330     cerr << "ERROR : Failed to write occupancies in the AMORE database : " << status << endl;
331   } 
332
333 #endif
334   
335   /* store the result file on FXS */  
336   if (daqDA_FES_storeFile(OUTPUT_FILE,"OCCUPANCY")) return -9;
337
338   timers.Stop();
339   printf("\nExecution time : R:%7.2fs C:%7.2fs\n", timers.RealTime(), timers.CpuTime());
340
341   return 0;
342 }